Skip to content

Commit 0f8e1c8

Browse files
feat: Check default llm before chat
1 parent 5ea91ec commit 0f8e1c8

File tree

11 files changed

+137
-29
lines changed

11 files changed

+137
-29
lines changed

backend/apps/ai_model/model_factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def get_default_config() -> LLMConfig:
110110
select(AiModelDetail).where(AiModelDetail.default_model == True)
111111
).first()
112112
if not db_model:
113-
raise ValueError("The system default model has not been set")
113+
raise Exception("The system default model has not been set")
114114

115115
additional_params = {}
116116
if db_model.config:

backend/apps/system/api/aimodel.py

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,33 @@ async def generate():
3838

3939
return StreamingResponse(generate(), media_type="application/x-ndjson")
4040

41+
@router.get("/default")
42+
async def check_default(session: SessionDep, trans: Trans):
43+
db_model = session.exec(
44+
select(AiModelDetail).where(AiModelDetail.default_model == True)
45+
).first()
46+
if not db_model:
47+
raise Exception(trans('i18n_llm.miss_default'))
48+
49+
@router.put("/default/{id}")
50+
async def set_default(session: SessionDep, id: int):
51+
db_model = session.get(AiModelDetail, id)
52+
if not db_model:
53+
raise ValueError(f"AiModelDetail with id {id} not found")
54+
if db_model.default_model:
55+
return
56+
57+
try:
58+
session.exec(
59+
update(AiModelDetail).values(default_model=False)
60+
)
61+
db_model.default_model = True
62+
session.add(db_model)
63+
session.commit()
64+
except Exception as e:
65+
session.rollback()
66+
raise e
67+
4168
@router.get("", response_model=list[AiModelGridItem])
4269
async def query(
4370
session: SessionDep,
@@ -119,22 +146,5 @@ async def delete_model(
119146
session.delete(item)
120147
session.commit()
121148

122-
@router.put("/default/{id}")
123-
async def set_default(session: SessionDep, id: int):
124-
db_model = session.get(AiModelDetail, id)
125-
if not db_model:
126-
raise ValueError(f"AiModelDetail with id {id} not found")
127-
if db_model.default_model:
128-
return
129149

130-
try:
131-
session.exec(
132-
update(AiModelDetail).values(default_model=False)
133-
)
134-
db_model.default_model = True
135-
session.add(db_model)
136-
session.commit()
137-
except Exception as e:
138-
session.rollback()
139-
raise e
140150

backend/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
},
3030
"i18n_llm": {
3131
"validate_error": "Validation failed [{msg}]",
32-
"delete_default_error": "Cannot delete default model [{key}]!"
32+
"delete_default_error": "Cannot delete default model [{key}]!",
33+
"miss_default": "The default large language model has not been configured"
3334
}
3435
}

backend/locales/zh-CN.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
},
3131
"i18n_llm": {
3232
"validate_error": "校验失败[{msg}]",
33-
"delete_default_error": "无法删除默认模型[{key}]!"
33+
"delete_default_error": "无法删除默认模型[{key}]!",
34+
"miss_default": "尚未配置默认大语言模型"
3435
}
3536
}

frontend/src/api/chat.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,4 +328,5 @@ export const chatApi = {
328328
recommendQuestions: (record_id: number | undefined, controller?: AbortController) => {
329329
return request.fetchStream(`/chat/recommend_questions/${record_id}`, {}, controller)
330330
},
331+
checkLLMModel: () => request.get('/system/aimodel/default', { requestOptions: { silent: true } }),
331332
}

frontend/src/entity/supplier.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const supplierList: Array<{
2222
},
2323
{
2424
id: 2,
25-
name: '千帆大模型I',
25+
name: '千帆大模型',
2626
icon: icon_bdyzn_colorful,
2727
model_config: {
2828
0: {

frontend/src/i18n/en.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@
133133
"no_time": "No Time",
134134
"rename_conversation_title": "Rename conversation title",
135135
"conversation_title": "Conversation title",
136-
"copied": "Copied"
136+
"copied": "Copied",
137+
"ask_failed": "Q&A failed"
137138
},
138139
"ds": {
139140
"title": "Data Sources",
@@ -291,7 +292,10 @@
291292
"del_default_tip": "Unable to delete the model: {msg}",
292293
"del_default_warn": "This model is the system default model. Please set another model as the system default before deleting this model.",
293294
"del_warn_tip": "Would you like to remove the model: {msg}?",
294-
"check_failed": "Model is invalid [{msg}]"
295+
"check_failed": "Model is invalid [{msg}]",
296+
"default_miss": "The default llm has not been configured yet, so the q&a feature cannot be enabled, Please contact the administrator to configure it.",
297+
"default_miss_admin": "The default llm has not been configured yet, so the q&a feature cannot be enabled.",
298+
"to_config": "Go to configuration"
295299
},
296300
"user": {
297301
"workspace": "Workspace",

frontend/src/i18n/zh-CN.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@
133133
"no_time": "没有时间",
134134
"rename_conversation_title": "重命名对话标题",
135135
"conversation_title": "对话标题",
136-
"copied": "已复制"
136+
"copied": "已复制",
137+
"ask_failed": "问数失败"
137138
},
138139
"ds": {
139140
"title": "数据源",
@@ -291,7 +292,10 @@
291292
"del_default_tip": "无法删除模型: {msg}",
292293
"del_default_warn": "该模型为系统默认模型,请先设置其他模型为系统默认模型,再删除此模型。",
293294
"del_warn_tip": "是否删除模型: {msg}?",
294-
"check_failed": "模型无效【{msg}】"
295+
"check_failed": "模型无效【{msg}】",
296+
"default_miss": "尚未配置默认大语言大模型,因此无法开启问数,请联系管理员配置。",
297+
"default_miss_admin": "尚未配置默认大语言大模型,因此无法开启问数。",
298+
"to_config": "去配置"
295299
},
296300
"user": {
297301
"workspace": "工作空间",

frontend/src/views/chat/ChatListContainer.vue

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import ChatCreator from '@/views/chat/ChatCreator.vue'
99
import { useAssistantStore } from '@/stores/assistant'
1010
import icon_sidebar_outlined from '@/assets/svg/icon_sidebar_outlined.svg'
1111
import icon_new_chat_outlined from '@/assets/svg/icon_new_chat_outlined.svg'
12-
12+
import { useUserStore } from '@/stores/user'
13+
import router from '@/router'
14+
const userStore = useUserStore()
1315
const props = withDefaults(
1416
defineProps<{
1517
inPopover?: boolean
@@ -112,6 +114,33 @@ function goEmpty(func?: (...p: any[]) => void, ...params: any[]) {
112114
}
113115
114116
const createNewChat = async () => {
117+
try {
118+
await chatApi.checkLLMModel()
119+
} catch (error: any) {
120+
console.error(error)
121+
let errorMsg = t('model.default_miss')
122+
let confirm_text = t('datasource.got_it')
123+
if (userStore.isAdmin) {
124+
errorMsg = t('model.default_miss_admin')
125+
confirm_text = t('model.to_config')
126+
}
127+
ElMessageBox.confirm(t('qa.ask_failed'), {
128+
confirmButtonType: 'primary',
129+
tip: errorMsg,
130+
showCancelButton: userStore.isAdmin,
131+
confirmButtonText: confirm_text,
132+
cancelButtonText: t('common.cancel'),
133+
customClass: 'confirm-no_icon',
134+
autofocus: false,
135+
showClose: false,
136+
callback: (val: string) => {
137+
if (userStore.isAdmin && val === 'confirm') {
138+
router.push('/system/model')
139+
}
140+
},
141+
})
142+
return
143+
}
115144
goEmpty(doCreateNewChat)
116145
}
117146

frontend/src/views/chat/index.vue

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,9 @@ import logo from '@/assets/LOGO.svg'
356356
import icon_send_filled from '@/assets/svg/icon_send_filled.svg'
357357
import { useAssistantStore } from '@/stores/assistant'
358358
import { onClickOutside } from '@vueuse/core'
359-
359+
import { useUserStore } from '@/stores/user'
360+
import router from '@/router'
361+
const userStore = useUserStore()
360362
const props = defineProps<{
361363
startChatDsId?: number
362364
}>()
@@ -437,6 +439,33 @@ const createNewChatSimple = async () => {
437439
}
438440
439441
const createNewChat = async () => {
442+
try {
443+
await chatApi.checkLLMModel()
444+
} catch (error: any) {
445+
console.error(error)
446+
let errorMsg = t('model.default_miss')
447+
let confirm_text = t('datasource.got_it')
448+
if (userStore.isAdmin) {
449+
errorMsg = t('model.default_miss_admin')
450+
confirm_text = t('model.to_config')
451+
}
452+
ElMessageBox.confirm(t('qa.ask_failed'), {
453+
confirmButtonType: 'primary',
454+
tip: errorMsg,
455+
showCancelButton: userStore.isAdmin,
456+
confirmButtonText: confirm_text,
457+
cancelButtonText: t('common.cancel'),
458+
customClass: 'confirm-no_icon',
459+
autofocus: false,
460+
showClose: false,
461+
callback: (val: string) => {
462+
if (userStore.isAdmin && val === 'confirm') {
463+
router.push('/system/model')
464+
}
465+
},
466+
})
467+
return
468+
}
440469
goEmpty()
441470
if (isAssistant.value) {
442471
currentChat.value = new ChatInfo()

0 commit comments

Comments
 (0)