Files
QQuiz/backend/services/config_service.py
handsomezhuzhu a01f3540c5 feat: 实现数据库驱动的API配置管理和项目结构重组
## 新功能
- 实现管理后台API配置管理(OpenAI/Anthropic/Qwen)
- API配置保存到数据库,实时生效无需重启
- API密钥遮罩显示(前10位+后4位)
- 完整endpoint URL自动显示

## 后端改进
- 新增 config_service.py 用于加载数据库配置
- LLMService 支持动态配置注入,回退到环境变量
- 更新 exam.py 和 question.py 使用数据库配置
- 扩展 schemas.py 支持所有API配置字段

## 前端改进
- 重写 AdminSettings.jsx 增强UI体验
- API密钥显示/隐藏切换
- 当前使用的提供商可视化标识
- 移除"需要重启"的误导性提示

## 项目结构重组
- 移动所有脚本到 scripts/ 目录
- 移动所有文档到 docs/ 目录
- 清理 Python 缓存文件

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-01 19:24:12 +08:00

44 lines
1.5 KiB
Python

"""
Configuration Service - Load system configuration from database
"""
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from typing import Dict
from models import SystemConfig
async def load_llm_config(db: AsyncSession) -> Dict[str, str]:
"""
Load LLM configuration from database.
Returns a dictionary with all LLM-related configuration:
{
'ai_provider': 'openai',
'openai_api_key': 'sk-...',
'openai_base_url': 'https://api.openai.com/v1',
'openai_model': 'gpt-4o-mini',
...
}
"""
# Fetch all config from database
result = await db.execute(select(SystemConfig))
db_configs = {config.key: config.value for config in result.scalars().all()}
# Build configuration dictionary
config = {
'ai_provider': db_configs.get('ai_provider', 'openai'),
# OpenAI
'openai_api_key': db_configs.get('openai_api_key'),
'openai_base_url': db_configs.get('openai_base_url', 'https://api.openai.com/v1'),
'openai_model': db_configs.get('openai_model', 'gpt-4o-mini'),
# Anthropic
'anthropic_api_key': db_configs.get('anthropic_api_key'),
'anthropic_model': db_configs.get('anthropic_model', 'claude-3-haiku-20240307'),
# Qwen
'qwen_api_key': db_configs.get('qwen_api_key'),
'qwen_base_url': db_configs.get('qwen_base_url', 'https://dashscope.aliyuncs.com/compatible-mode/v1'),
'qwen_model': db_configs.get('qwen_model', 'qwen-plus')
}
return config