Skip to content

Commit 692abd9

Browse files
perf: AiModel validate
1 parent d5ba5dc commit 692abd9

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

backend/apps/ai_model/model_factory.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from functools import lru_cache
12
import json
23
from abc import ABC, abstractmethod
34
from typing import Optional, Dict, Any, Type
@@ -21,6 +22,18 @@ class LLMConfig(BaseModel):
2122
api_key: Optional[str] = None
2223
api_base_url: Optional[str] = None
2324
additional_params: Dict[str, Any] = {}
25+
class Config:
26+
frozen = True
27+
28+
def __hash__(self):
29+
return hash((
30+
self.model_id,
31+
self.model_type,
32+
self.model_name,
33+
self.api_key,
34+
self.api_base_url,
35+
frozenset(self.additional_params.items())
36+
))
2437

2538

2639
class BaseLLM(ABC):
@@ -66,6 +79,7 @@ class LLMFactory:
6679
}
6780

6881
@classmethod
82+
@lru_cache(maxsize=32)
6983
def create_llm(cls, config: LLMConfig) -> BaseLLM:
7084
llm_class = cls._llm_types.get(config.model_type)
7185
if not llm_class:

backend/apps/system/api/aimodel.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,8 @@ async def generate():
2727
additional_params=additional_params,
2828
)
2929
llm_instance = LLMFactory.create_llm(config)
30-
31-
res = llm_instance.llm.stream("who are you?")
32-
33-
for chunk in res:
30+
async for chunk in llm_instance.llm.astream("1+1=?"):
3431
if chunk and chunk.content:
35-
SQLBotLogUtil.info(chunk)
3632
yield json.dumps({"content": chunk.content}) + "\n"
3733

3834
except Exception as e:

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const activeName = ref('')
3535
const activeType = ref('')
3636
const modelFormRef = ref()
3737
const cardRefs = ref<any[]>([])
38-
// const showCardError = ref(true) // if you don`t want card mask error, just change this to false
38+
const showCardError = ref(false) // if you don`t want card mask error, just change this to false
3939
reactive({
4040
form: {
4141
id: '',
@@ -71,8 +71,6 @@ const defaultModelListWithSearch = computed(() => {
7171
})
7272
7373
const modelCheckHandler = async (item: any) => {
74-
console.log(item)
75-
/**
7674
const response = await modelApi.check(item)
7775
const reader = response.body.getReader()
7876
const decoder = new TextDecoder()
@@ -112,7 +110,6 @@ const modelCheckHandler = async (item: any) => {
112110
currentRef?.showErrorMask(checkMsg)
113111
}
114112
})
115-
*/
116113
}
117114
const duplicateName = async (item: any) => {
118115
const res = await modelApi.queryAll()

0 commit comments

Comments
 (0)