新增配置模型测试按钮
This commit is contained in:
@@ -741,6 +741,67 @@ async def model_list(current_user: dict = Depends(get_current_user)) -> dict[str
|
||||
return ok(get_platform_store().models())
|
||||
|
||||
|
||||
@router.post("/model-manage/test-online")
|
||||
async def test_online_model(payload: dict[str, Any] = Body(...)) -> dict[str, Any]:
|
||||
"""测试在线模型 API 是否可用:发送一个简单的 chat/completions 请求验证连通性。"""
|
||||
api_url = (payload.get("api_url") or "").rstrip("/")
|
||||
api_key = payload.get("api_key") or ""
|
||||
model_name = payload.get("online_model_name") or ""
|
||||
if not api_url:
|
||||
raise fail(400, "api_url is required")
|
||||
if not model_name:
|
||||
raise fail(400, "online_model_name is required")
|
||||
import httpx
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=15) as client:
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if api_key:
|
||||
headers["Authorization"] = f"Bearer {api_key}"
|
||||
# 尝试多种 OpenAI 兼容路径
|
||||
chat_paths = [
|
||||
f"{api_url}/chat/completions",
|
||||
f"{api_url}/v1/chat/completions",
|
||||
f"{api_url}/modelTF/v1/chat/completions",
|
||||
]
|
||||
resp = None
|
||||
for path in chat_paths:
|
||||
try:
|
||||
r = await client.post(
|
||||
path,
|
||||
json={
|
||||
"model": model_name,
|
||||
"messages": [{"role": "user", "content": "Hi"}],
|
||||
"max_tokens": 5,
|
||||
"temperature": 0,
|
||||
},
|
||||
headers=headers,
|
||||
)
|
||||
if r.status_code in (200, 201):
|
||||
resp = r
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
if resp is None:
|
||||
return ok({"success": False, "error": f"无法连接到 {api_url},请检查地址和端口"})
|
||||
body = resp.json()
|
||||
usage = body.get("usage", {})
|
||||
return ok({
|
||||
"success": True,
|
||||
"model": body.get("model", model_name),
|
||||
"provider": body.get("object", ""),
|
||||
"usage": {
|
||||
"prompt_tokens": usage.get("prompt_tokens", 0),
|
||||
"completion_tokens": usage.get("completion_tokens", 0),
|
||||
"total_tokens": usage.get("total_tokens", 0),
|
||||
},
|
||||
"latency_ms": None, # 由前端计算
|
||||
})
|
||||
except httpx.TimeoutException:
|
||||
return ok({"success": False, "error": "连接超时(15s),请检查网络或 API 地址是否正确"})
|
||||
except Exception as exc:
|
||||
return ok({"success": False, "error": str(exc)})
|
||||
|
||||
|
||||
@router.post("/model-manage")
|
||||
async def create_model(payload: dict[str, Any] = Body(...), current_user: dict = Depends(get_current_user)) -> dict[str, Any]:
|
||||
payload.setdefault("created_by", current_user.get("id"))
|
||||
|
||||
Reference in New Issue
Block a user