mirror of
https://github.com/handsomezhuzhu/QQuiz.git
synced 2026-02-20 20:10:14 +00:00
主要功能: - 🎯 新增 Google Gemini AI 提供商支持 - 原生 PDF 理解能力(最多1000页) - 完整保留图片、表格、公式等内容 - 支持自定义 Base URL(用于代理/中转服务) - 🤖 实现 AI 参考答案自动生成 - 当题目缺少答案时自动调用 AI 生成参考答案 - 支持单选、多选、判断、简答等所有题型 - 答案标记为"AI参考答案:"便于识别 - 🔧 优化文档解析功能 - 改进中文 Prompt 提高识别准确度 - 自动修复 JSON 中的控制字符(换行符等) - 智能题目类型验证和自动转换(proof→short等) - 增加超时时间和重试机制 - 🎨 完善管理后台配置界面 - 新增 Gemini 配置区域 - 突出显示 PDF 原生支持特性 - 为其他提供商添加"仅文本"警告 - 支持 Gemini Base URL 自定义 技术改进: - 添加 google-genai 依赖 - 实现异步 API 调用适配 - 完善错误处理和日志输出 - 统一配置管理和数据库存储 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
82 lines
3.0 KiB
Python
82 lines
3.0 KiB
Python
"""
|
|
Admin Router
|
|
"""
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select
|
|
|
|
from database import get_db
|
|
from models import User, SystemConfig
|
|
from schemas import SystemConfigUpdate, SystemConfigResponse
|
|
from services.auth_service import get_current_admin_user
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/config", response_model=SystemConfigResponse)
|
|
async def get_system_config(
|
|
current_admin: User = Depends(get_current_admin_user),
|
|
db: AsyncSession = Depends(get_db)
|
|
):
|
|
"""Get system configuration (admin only)"""
|
|
|
|
# Fetch all config values
|
|
result = await db.execute(select(SystemConfig))
|
|
configs = {config.key: config.value for config in result.scalars().all()}
|
|
|
|
# Mask API keys (show only first 10 and last 4 characters)
|
|
def mask_api_key(key):
|
|
if not key or len(key) < 20:
|
|
return key
|
|
return f"{key[:10]}...{key[-4:]}"
|
|
|
|
return {
|
|
"allow_registration": configs.get("allow_registration", "true").lower() == "true",
|
|
"max_upload_size_mb": int(configs.get("max_upload_size_mb", "10")),
|
|
"max_daily_uploads": int(configs.get("max_daily_uploads", "20")),
|
|
"ai_provider": configs.get("ai_provider", "gemini"),
|
|
# API Configuration
|
|
"openai_api_key": mask_api_key(configs.get("openai_api_key")),
|
|
"openai_base_url": configs.get("openai_base_url", "https://api.openai.com/v1"),
|
|
"openai_model": configs.get("openai_model", "gpt-4o-mini"),
|
|
"anthropic_api_key": mask_api_key(configs.get("anthropic_api_key")),
|
|
"anthropic_model": configs.get("anthropic_model", "claude-3-haiku-20240307"),
|
|
"qwen_api_key": mask_api_key(configs.get("qwen_api_key")),
|
|
"qwen_base_url": configs.get("qwen_base_url", "https://dashscope.aliyuncs.com/compatible-mode/v1"),
|
|
"qwen_model": configs.get("qwen_model", "qwen-plus"),
|
|
"gemini_api_key": mask_api_key(configs.get("gemini_api_key")),
|
|
"gemini_base_url": configs.get("gemini_base_url", ""),
|
|
"gemini_model": configs.get("gemini_model", "gemini-2.0-flash-exp")
|
|
}
|
|
|
|
|
|
@router.put("/config", response_model=SystemConfigResponse)
|
|
async def update_system_config(
|
|
config_update: SystemConfigUpdate,
|
|
current_admin: User = Depends(get_current_admin_user),
|
|
db: AsyncSession = Depends(get_db)
|
|
):
|
|
"""Update system configuration (admin only)"""
|
|
|
|
update_data = config_update.dict(exclude_unset=True)
|
|
|
|
for key, value in update_data.items():
|
|
result = await db.execute(
|
|
select(SystemConfig).where(SystemConfig.key == key)
|
|
)
|
|
config = result.scalar_one_or_none()
|
|
|
|
if config:
|
|
config.value = str(value).lower() if isinstance(value, bool) else str(value)
|
|
else:
|
|
new_config = SystemConfig(
|
|
key=key,
|
|
value=str(value).lower() if isinstance(value, bool) else str(value)
|
|
)
|
|
db.add(new_config)
|
|
|
|
await db.commit()
|
|
|
|
# Return updated config
|
|
return await get_system_config(current_admin, db)
|