Skip to content

Commit 23ca654

Browse files
committed
feat(Application): Application can configure specified model
1 parent a6ca7af commit 23ca654

5 files changed

Lines changed: 75 additions & 52 deletions

File tree

backend/apps/chat/task/llm.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
from apps.datasource.embedding.ds_embedding import get_ds_embedding
4242
from apps.datasource.models.datasource import CoreDatasource
4343
from apps.db.db import exec_sql, get_version, check_connection
44+
from apps.system.crud.aimodel_manage import get_ai_model_list_by_workspace
4445
from apps.system.crud.assistant import AssistantOutDs, AssistantOutDsFactory, get_assistant_ds
4546
from apps.system.crud.parameter_manage import get_groups
4647
from apps.system.schemas.system_schema import AssistantOutDsSchema
@@ -176,11 +177,16 @@ def __init__(self, session: Session, current_user: CurrentUser, chat_question: C
176177
@classmethod
177178
async def create(cls, *args, **kwargs):
178179
specialized_model_id = None
180+
_ai_model_list = []
179181
if args[3]:
182+
if args[1]:
183+
ws_id = args[1].oid
184+
_ai_model_list = get_ai_model_list_by_workspace(args[0], ws_id)
180185
if args[3].enable_custom_model:
181186
if args[3].custom_model:
182-
specialized_model_id = args[3].custom_model
183-
print("use custom model: id[" + args[3].custom_model + "]")
187+
if any(str(model.id) == str(args[3].custom_model) for model in _ai_model_list):
188+
specialized_model_id = args[3].custom_model
189+
print("use custom model: id[" + specialized_model_id + "]")
184190
config: LLMConfig = await get_default_config(specialized_model_id)
185191
instance = cls(*args, **kwargs, config=config)
186192

backend/apps/system/api/aimodel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,11 @@ async def update_model_ws_mapping_by_id(
249249
return [str(ws_id) for ws_id in ws_ids]
250250

251251

252-
@router.get("/list_by_ws", response_model=AiModelBrief, summary=f"{PLACEHOLDER_PREFIX}system_model_query",
252+
@router.get("/list/by_ws", response_model=List[AiModelBrief], summary=f"{PLACEHOLDER_PREFIX}system_model_query",
253253
description=f"{PLACEHOLDER_PREFIX}system_model_query")
254254
@require_permissions(permission=SqlbotPermission(role=['ws_admin']))
255255
async def get_model_by_ws(
256256
session: SessionDep,
257257
current_user: CurrentUser
258258
):
259-
return get_ai_model_list_by_workspace(session, current_user.workspace_id)
259+
return get_ai_model_list_by_workspace(session, current_user.oid)
Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
21
from typing import Optional
2+
3+
from pydantic import field_serializer
34
from sqlmodel import BigInteger, Field, Text, SQLModel
5+
46
from common.core.models import SnowflakeBase
57
from common.core.schemas import BaseCreatorDTO
68

@@ -9,84 +11,98 @@ class AiModelBase:
911
supplier: int = Field(nullable=False)
1012
name: str = Field(max_length=255, nullable=False)
1113
model_type: int = Field(nullable=False)
12-
base_model: str = Field(max_length = 255, nullable=False)
14+
base_model: str = Field(max_length=255, nullable=False)
1315
default_model: bool = Field(default=False, nullable=False)
1416

17+
1518
class AiModelDetail(SnowflakeBase, AiModelBase, table=True):
16-
__tablename__ = "ai_model"
17-
api_key: str | None = Field(default=None, nullable=True, sa_type=Text())
18-
api_domain: str = Field(nullable=False, sa_type=Text())
19-
protocol: int = Field(nullable=False, default = 1)
20-
config: str = Field(sa_type = Text())
21-
status: int = Field(nullable=False, default = 1)
22-
create_time: int = Field(default=0, sa_type=BigInteger())
23-
19+
__tablename__ = "ai_model"
20+
api_key: str | None = Field(default=None, nullable=True, sa_type=Text())
21+
api_domain: str = Field(nullable=False, sa_type=Text())
22+
protocol: int = Field(nullable=False, default=1)
23+
config: str = Field(sa_type=Text())
24+
status: int = Field(nullable=False, default=1)
25+
create_time: int = Field(default=0, sa_type=BigInteger())
26+
27+
2428
class AiModelWorkspaceMapping(SnowflakeBase, table=True):
2529
__tablename__ = "ai_model_workspace_mapping"
2630
ai_model_id: int = Field(default=None, nullable=True, sa_type=BigInteger())
2731
workspace_id: int = Field(default=None, nullable=True, sa_type=BigInteger())
2832

33+
2934
class AiModelBrief(SQLModel):
3035
id: int
3136
name: str
3237
default_model: bool
3338
supplier: int
3439

40+
@field_serializer("id")
41+
def id_to_str(self, v: int) -> str:
42+
return str(v)
43+
44+
3545
class WorkspaceBase(SQLModel):
3646
name: str = Field(max_length=255, nullable=False)
3747

48+
3849
class WorkspaceEditor(WorkspaceBase, BaseCreatorDTO):
3950
pass
40-
51+
52+
4153
class WorkspaceModel(SnowflakeBase, WorkspaceBase, table=True):
4254
__tablename__ = "sys_workspace"
4355
create_time: int = Field(default=0, sa_type=BigInteger())
44-
56+
57+
4558
class UserWsBaseModel(SQLModel):
4659
uid: int = Field(nullable=False, sa_type=BigInteger())
4760
oid: int = Field(nullable=False, sa_type=BigInteger())
48-
weight: int = Field(default=0, nullable=False)
49-
61+
weight: int = Field(default=0, nullable=False)
62+
63+
5064
class UserWsModel(SnowflakeBase, UserWsBaseModel, table=True):
5165
__tablename__ = "sys_user_ws"
52-
66+
5367

5468
class AssistantBaseModel(SQLModel):
5569
name: str = Field(max_length=255, nullable=False)
5670
type: int = Field(nullable=False, default=0)
5771
domain: str = Field(max_length=255, nullable=False)
58-
description: Optional[str] = Field(sa_type = Text(), nullable=True)
59-
configuration: Optional[str] = Field(sa_type = Text(), nullable=True)
72+
description: Optional[str] = Field(sa_type=Text(), nullable=True)
73+
configuration: Optional[str] = Field(sa_type=Text(), nullable=True)
6074
create_time: int = Field(default=0, sa_type=BigInteger())
61-
app_id: Optional[str] = Field(default=None, max_length=255, nullable=True)
75+
app_id: Optional[str] = Field(default=None, max_length=255, nullable=True)
6276
app_secret: Optional[str] = Field(default=None, max_length=255, nullable=True)
6377
oid: Optional[int] = Field(nullable=True, sa_type=BigInteger(), default=1)
6478
enable_custom_model: Optional[bool] = Field(default=False, nullable=True)
6579
custom_model: Optional[str] = Field(default=None, max_length=255, nullable=True)
6680

81+
6782
class AssistantModel(SnowflakeBase, AssistantBaseModel, table=True):
6883
__tablename__ = "sys_assistant"
69-
84+
7085

7186
class AuthenticationBaseModel(SQLModel):
7287
name: str = Field(max_length=255, nullable=False)
7388
type: int = Field(nullable=False, default=0)
74-
config: Optional[str] = Field(sa_type = Text(), nullable=True)
75-
76-
89+
config: Optional[str] = Field(sa_type=Text(), nullable=True)
90+
91+
7792
class AuthenticationModel(SnowflakeBase, AuthenticationBaseModel, table=True):
7893
__tablename__ = "sys_authentication"
7994
create_time: Optional[int] = Field(default=0, sa_type=BigInteger())
8095
enable: bool = Field(default=False, nullable=False)
8196
valid: bool = Field(default=False, nullable=False)
82-
97+
8398

8499
class ApiKeyBaseModel(SQLModel):
85100
access_key: str = Field(max_length=255, nullable=False)
86101
secret_key: str = Field(max_length=255, nullable=False)
87102
create_time: int = Field(default=0, sa_type=BigInteger())
88-
uid: int = Field(default=0,nullable=False, sa_type=BigInteger())
103+
uid: int = Field(default=0, nullable=False, sa_type=BigInteger())
89104
status: bool = Field(default=True, nullable=False)
90-
105+
106+
91107
class ApiKeyModel(SnowflakeBase, ApiKeyBaseModel, table=True):
92-
__tablename__ = "sys_apikey"
108+
__tablename__ = "sys_apikey"

frontend/src/api/system.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ export const modelApi = {
3030
platform: (id: number, lazy?: number, pid?: string) =>
3131
request.post(`/system/platform/org/${id}`, { lazy, pid }),
3232
userSync: (data: any) => request.post(`/system/platform/user/sync`, data),
33+
list_by_ws: () => request.get(`/system/aimodel/list/by_ws`),
3334
}

frontend/src/views/system/embedded/iframe.vue

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -101,25 +101,23 @@ const dsListOptions = ref<any[]>([])
101101
const embeddedListWithSearch = computed(() => {
102102
if (!keywords.value) return embeddedList.value
103103
return embeddedList.value.filter((ele: any) =>
104-
ele.name.toLowerCase().includes(keywords.value.toLowerCase()),
104+
ele.name.toLowerCase().includes(keywords.value.toLowerCase())
105105
)
106106
})
107107
108108
interface Model {
109+
id: number
109110
name: string
110-
model_type: string
111-
base_model: string
112-
id: string
113111
default_model: boolean
114112
supplier: number
115113
}
116114
117-
const modelList =ref<Array<Model>>([])
115+
const modelList = ref<Array<Model>>([])
118116
119117
const searchModels = () => {
120118
searchLoading.value = true
121119
modelApi
122-
.queryAll()
120+
.list_by_ws()
123121
.then((res: any) => {
124122
modelList.value = res
125123
})
@@ -307,8 +305,8 @@ const validateUrl = (_: any, value: any, callback: any) => {
307305
if (value === '') {
308306
callback(
309307
new Error(
310-
t('datasource.please_enter') + t('common.empty') + t('embedded.cross_domain_settings'),
311-
),
308+
t('datasource.please_enter') + t('common.empty') + t('embedded.cross_domain_settings')
309+
)
312310
)
313311
} else {
314312
// var Expression = /(https?:\/\/)?([\da-z\.-]+)\.([a-z]{2,6})(:\d{1,5})?([\/\w\.-]*)*\/?(#[\S]+)?/ // eslint-disable-line
@@ -352,7 +350,7 @@ const dsRules = {
352350
const validatePass = (_: any, value: any, callback: any) => {
353351
if (value === '') {
354352
callback(
355-
new Error(t('datasource.please_enter') + t('common.empty') + t('embedded.interface_url')),
353+
new Error(t('datasource.please_enter') + t('common.empty') + t('embedded.interface_url'))
356354
)
357355
} else {
358356
// var Expression = /(https?:\/\/)?([\da-z\.-]+)\.([a-z]{2,6})(:\d{1,5})?([\/\w\.-]*)*\/?(#[\S]+)?/ // eslint-disable-line
@@ -470,7 +468,7 @@ const saveEmbedded = () => {
470468
if (!currentEmbedded.id) {
471469
delete obj.id
472470
}
473-
if (obj.custom_model == undefined){
471+
if (obj.custom_model == undefined) {
474472
obj.custom_model = ''
475473
}
476474
req(obj).then(() => {
@@ -526,29 +524,29 @@ const handleEmbedded = (row: any) => {
526524
}
527525
const copyJsCode = () => {
528526
copy(jsCodeElement.value)
529-
.then(function() {
527+
.then(function () {
530528
ElMessage.success(t('embedded.copy_successful'))
531529
})
532-
.catch(function() {
530+
.catch(function () {
533531
ElMessage.error(t('embedded.copy_failed'))
534532
})
535533
}
536534
537535
const copyJsCodeFull = () => {
538536
copy(jsCodeElementFull.value)
539-
.then(function() {
537+
.then(function () {
540538
ElMessage.success(t('embedded.copy_successful'))
541539
})
542-
.catch(function() {
540+
.catch(function () {
543541
ElMessage.error(t('embedded.copy_failed'))
544542
})
545543
}
546544
const copyCode = () => {
547545
copy(scriptElement.value)
548-
.then(function() {
546+
.then(function () {
549547
ElMessage.success(t('embedded.copy_successful'))
550548
})
551-
.catch(function() {
549+
.catch(function () {
552550
ElMessage.error(t('embedded.copy_failed'))
553551
})
554552
}
@@ -821,13 +819,16 @@ const saveHandler = () => {
821819
</el-form-item>
822820

823821
<el-form-item prop="enable_custom_model">
824-
<el-checkbox v-model="currentEmbedded.enable_custom_model" >
825-
{{t('embedded.enableCustomModel')}}
822+
<el-checkbox v-model="currentEmbedded.enable_custom_model">
823+
{{ t('embedded.enableCustomModel') }}
826824
</el-checkbox>
827825
</el-form-item>
828826

829-
<el-form-item prop="custom_model" :label="t('modelType.llm')"
830-
v-if="currentEmbedded.enable_custom_model">
827+
<el-form-item
828+
v-if="currentEmbedded.enable_custom_model"
829+
prop="custom_model"
830+
:label="t('modelType.llm')"
831+
>
831832
<el-select v-model="currentEmbedded.custom_model" clearable filterable>
832833
<el-option
833834
v-for="item in modelList"
@@ -837,7 +838,6 @@ const saveHandler = () => {
837838
/>
838839
</el-select>
839840
</el-form-item>
840-
841841
</el-form>
842842
</div>
843843
</el-scrollbar>
@@ -1013,7 +1013,7 @@ const saveHandler = () => {
10131013
<div class="private-list">
10141014
{{ t('embedded.set_data_source') }}
10151015
<span :title="$t('embedded.open_the_query')" class="open-the_query ellipsis"
1016-
>{{ $t('embedded.open_the_query') }}
1016+
>{{ $t('embedded.open_the_query') }}
10171017
</span>
10181018
</div>
10191019
</template>

0 commit comments

Comments
 (0)