Skip to content

Commit 14799a3

Browse files
perf: Set default model
1 parent 176f78f commit 14799a3

File tree

6 files changed

+37
-553
lines changed

6 files changed

+37
-553
lines changed

backend/apps/system/api/aimodel.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import List, Union
33
from apps.system.schemas.ai_model_schema import AiModelConfigItem, AiModelCreator, AiModelEditor, AiModelGridItem
44
from fastapi import APIRouter, Query
5-
from sqlmodel import func, select
5+
from sqlmodel import func, select, update
66

77
from apps.system.models.system_model import AiModelDetail
88
from common.core.deps import SessionDep
@@ -23,7 +23,7 @@ async def query(
2323
AiModelDetail.default_model)
2424
if keyword is not None:
2525
statement = statement.where(AiModelDetail.name.like(f"%{keyword}%"))
26-
26+
statement = statement.order_by(AiModelDetail.create_time.asc())
2727
items = session.exec(statement).all()
2828
return items
2929

@@ -85,6 +85,27 @@ async def delete_model(
8585
id: int
8686
):
8787
item = session.get(AiModelDetail, id)
88+
if item.default_model:
89+
raise RuntimeError(f"Can not delete [${item.name}], because it is default model!")
8890
session.delete(item)
8991
session.commit()
92+
93+
@router.put("/default/{id}")
94+
async def set_default(session: SessionDep, id: int):
95+
db_model = session.get(AiModelDetail, id)
96+
if not db_model:
97+
raise ValueError(f"AiModelDetail with id {id} not found")
98+
if db_model.default_model:
99+
return
100+
101+
try:
102+
session.exec(
103+
update(AiModelDetail).values(default_model=False)
104+
)
105+
db_model.default_model = True
106+
session.add(db_model)
107+
session.commit()
108+
except Exception as e:
109+
session.rollback()
110+
raise e
90111

frontend/src/api/system.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ export const modelApi = {
77
edit: (data: any) => request.put('/system/aimodel', data),
88
delete: (id: number) => request.delete(`/system/aimodel/${id}`),
99
query: (id: number) => request.get(`/system/aimodel/${id}`),
10-
status: (data: any) => request.patch('/system/aimodel/status', data),
10+
setDefault: (id: number) => request.put(`/system/aimodel/default/${id}`),
1111
}

frontend/src/router/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import Datasource from '@/views/ds/Datasource.vue'
99
import DashboardEditor from '@/views/dashboard/editor/index.vue'
1010
import DashboardPreview from '@//views/dashboard/preview/SQPreviewSingle.vue'
1111
import Dashboard from '@/views/dashboard/index.vue'
12-
// import Model from '@/views/system/model/index.vue'
1312
import Model from '@/views/system/model/Model.vue'
1413
// import User from "@/views/system/user/index.vue";
1514
import { watchRouter } from './watch'

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,12 @@ const handleDel = () => {
5959
<edit></edit>
6060
</el-icon>
6161
</el-tooltip>
62-
<template v-if="!isDefault">
63-
<span class="divide"></span>
64-
<el-tooltip :offset="14" effect="dark" :content="$t('dashboard.delete')" placement="top">
65-
<el-icon size="16" @click="handleDel">
66-
<delIcon></delIcon>
67-
</el-icon>
68-
</el-tooltip>
69-
</template>
62+
<span class="divide"></span>
63+
<el-tooltip :offset="14" effect="dark" :content="$t('dashboard.delete')" placement="top">
64+
<el-icon size="16" @click="handleDel">
65+
<delIcon></delIcon>
66+
</el-icon>
67+
</el-tooltip>
7068
</div>
7169
</div>
7270
</template>

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ const defaultModelListWithSearch = computed(() => {
6969
})
7070
7171
const handleDefaultModelChange = (item: any) => {
72+
const current_default_node = modelList.value.find((ele: Model) => ele.default_model)
73+
if (current_default_node?.id === item.id) {
74+
return
75+
}
7276
ElMessageBox.confirm(`是否设置 ${item.name} 为系统默认模型?`, {
7377
confirmButtonType: 'primary',
7478
tip: '系统默认模型被替换后,智能问数的结果将会受到影响,请谨慎操作。',
@@ -78,10 +82,10 @@ const handleDefaultModelChange = (item: any) => {
7882
autofocus: false,
7983
callback: (val: string) => {
8084
if (val === 'confirm') {
81-
modelList.value.forEach((ele: any) => {
82-
ele.default_model = ele.id === item.id
85+
modelApi.setDefault(item.id).then(() => {
86+
ElMessage.success('设置成功')
87+
search()
8388
})
84-
ElMessage.success('设置成功')
8589
}
8690
},
8791
})

0 commit comments

Comments
 (0)