完善模板编辑与模型管理体验
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
import httpx
|
||||
|
||||
from database import get_db
|
||||
from models.ai_model import AiModel
|
||||
@@ -11,6 +12,11 @@ from services.security import decrypt_text, encrypt_text, mask_secret
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
def _is_deepseek_model(model: AiModel) -> bool:
|
||||
provider = (model.provider or "").strip().lower()
|
||||
return provider == "deepseek"
|
||||
|
||||
|
||||
def _serialize_model(model: AiModel) -> dict:
|
||||
api_key = decrypt_text(model.api_key_encrypted)
|
||||
return {
|
||||
@@ -121,3 +127,36 @@ async def test_model(model_id: int, db: AsyncSession = Depends(get_db)):
|
||||
message=str(error),
|
||||
data={"id": model.id, "success": False},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{model_id}/balance")
|
||||
async def get_model_balance(model_id: int, db: AsyncSession = Depends(get_db)):
|
||||
model = await db.get(AiModel, model_id)
|
||||
if model is None:
|
||||
raise HTTPException(status_code=404, detail="模型不存在")
|
||||
if not _is_deepseek_model(model):
|
||||
raise HTTPException(status_code=400, detail="仅 DeepSeek 模型支持余额查询")
|
||||
|
||||
api_key = decrypt_text(model.api_key_encrypted)
|
||||
if not api_key:
|
||||
raise HTTPException(status_code=400, detail="模型 API Key 不可用")
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=20, trust_env=False) as client:
|
||||
response = await client.get(
|
||||
"https://api.deepseek.com/user/balance",
|
||||
headers={"Authorization": f"Bearer {api_key}", "Accept": "application/json"},
|
||||
)
|
||||
response.raise_for_status()
|
||||
payload = response.json()
|
||||
except Exception as error:
|
||||
raise HTTPException(status_code=400, detail=f"查询余额失败:{error}")
|
||||
|
||||
return Response(
|
||||
data={
|
||||
"id": model.id,
|
||||
"provider": model.provider,
|
||||
"is_available": payload.get("is_available", False),
|
||||
"balance_infos": payload.get("balance_infos", []),
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user