Skip to content

Commit 1e18095

Browse files
perf: Model check
1 parent f3457fb commit 1e18095

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

backend/apps/system/api/aimodel.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
11
import json
22
from typing import List, Union
3+
from apps.ai_model.model_factory import LLMConfig, LLMFactory
34
from apps.system.schemas.ai_model_schema import AiModelConfigItem, AiModelCreator, AiModelEditor, AiModelGridItem
45
from fastapi import APIRouter, Query
56
from sqlmodel import func, select, update
67

78
from apps.system.models.system_model import AiModelDetail
89
from common.core.deps import SessionDep
910
from common.utils.time import get_timestamp
11+
from common.utils.utils import SQLBotLogUtil
1012

1113
router = APIRouter(tags=["system/aimodel"], prefix="/system/aimodel")
1214

15+
@router.post("/status")
16+
async def check_llm(info: AiModelCreator):
17+
try:
18+
additional_params = {item.key: item.val for item in info.config_list}
19+
config = LLMConfig(
20+
model_type="openai",
21+
model_name=info.base_model,
22+
api_key=info.api_key,
23+
api_base_url=info.api_domain,
24+
additional_params=additional_params,
25+
)
26+
llm_instance = LLMFactory.create_llm(config)
27+
result = llm_instance.llm.invoke("who are you?")
28+
SQLBotLogUtil.info(f"check_llm result: {result}")
29+
except Exception as e:
30+
SQLBotLogUtil.error(f"Error checking LLM: {e}")
31+
raise e
32+
1333
@router.get("", response_model=list[AiModelGridItem])
1434
async def query(
1535
session: SessionDep,
@@ -74,8 +94,8 @@ async def update_model(
7494
data["config"] = json.dumps([item.model_dump(exclude_unset=True) for item in editor.config_list])
7595
data.pop("config_list", None)
7696
db_model = session.get(AiModelDetail, id)
77-
update_data = AiModelDetail.model_validate(data)
78-
db_model.sqlmodel_update(update_data)
97+
#update_data = AiModelDetail.model_validate(data)
98+
db_model.sqlmodel_update(data)
7999
session.add(db_model)
80100
session.commit()
81101

frontend/src/api/system.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ export const modelApi = {
88
delete: (id: number) => request.delete(`/system/aimodel/${id}`),
99
query: (id: number) => request.get(`/system/aimodel/${id}`),
1010
setDefault: (id: number) => request.put(`/system/aimodel/default/${id}`),
11+
check: (data: any) =>
12+
request.post('/system/aimodel/status', data, { requestOptions: { silent: true } }),
1113
}

frontend/src/i18n/zh-CN.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@
279279
"verification_successful": "校验成功",
280280
"del_default_tip": "无法删除模型: {msg}",
281281
"del_default_warn": "该模型为系统默认模型,请先设置其他模型为系统默认模型,再删除此模型。",
282-
"del_warn_tip": "是否删除模型: {msg}?"
282+
"del_warn_tip": "是否删除模型: {msg}?",
283+
"check_failed": "模型无效【{msg}】"
283284
},
284285
"user": {
285286
"workspace": "工作空间",

frontend/src/views/system/model/Model.vue

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ const defaultModelListWithSearch = computed(() => {
6969
})
7070
})
7171
72+
const modelCheckHandler = (item: any) => {
73+
setTimeout(() => {
74+
modelApi.check(item).catch((err: any) => {
75+
if (err.response?.data?.msg) {
76+
ElMessage.error(t('model.check_failed', { msg: err.response.data.msg || '' }))
77+
} else {
78+
ElMessage.error(t('model.check_failed', { msg: err.message || '' }))
79+
}
80+
})
81+
}, 1000)
82+
}
7283
const duplicateName = async (item: any) => {
7384
const res = await modelApi.queryAll()
7485
const names = res.filter((ele: any) => ele.id !== item.id).map((ele: any) => ele.name)
@@ -85,6 +96,7 @@ const duplicateName = async (item: any) => {
8596
type: 'success',
8697
message: t('workspace.add_successfully'),
8798
})
99+
modelCheckHandler(item)
88100
})
89101
return
90102
}
@@ -95,6 +107,7 @@ const duplicateName = async (item: any) => {
95107
type: 'success',
96108
message: t('common.save_success'),
97109
})
110+
modelCheckHandler(item)
98111
})
99112
}
100113

0 commit comments

Comments
 (0)