Skip to content

Commit bc70ad0

Browse files
modify adding-model in openai and azure-openai
1 parent 16472eb commit bc70ad0

File tree

10 files changed

+137
-94
lines changed

10 files changed

+137
-94
lines changed

api/apps/llm_app.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,40 +58,49 @@ def set_api_key():
5858
chat_passed, embd_passed, rerank_passed = False, False, False
5959
factory = req["llm_factory"]
6060
msg = ""
61-
for llm in LLMService.query(fid=factory)[:3]:
61+
default_model=req.get("default_model") if req.get("default_model")!="" else None
62+
api_version=req.get("api_version")
63+
default_model=default_model+'#'+api_version if factory=='Azure-OpenAI'and default_model else default_model
64+
for llm in LLMService.query(fid=factory, llm_name=default_model)[:3]:
65+
llm_name = llm.llm_name.split('#')[0] if llm.fid == 'Azure-OpenAI' else llm.llm_name
66+
api_version = llm.llm_name.split('#')[1] if llm.fid == 'Azure-OpenAI' else None
67+
6268
if not embd_passed and llm.model_type == LLMType.EMBEDDING.value:
6369
mdl = EmbeddingModel[factory](
64-
req["api_key"], llm.llm_name, base_url=req.get("base_url"))
70+
req["api_key"], llm_name, base_url=req.get("base_url"), api_version=api_version)
6571
try:
6672
arr, tc = mdl.encode(["Test if the api key is available"])
6773
if len(arr[0]) == 0:
6874
raise Exception("Fail")
6975
embd_passed = True
76+
break
7077
except Exception as e:
71-
msg += f"\nFail to access embedding model({llm.llm_name}) using this api key." + str(e)
78+
msg += f"\nFail to access embedding model({llm_name}) using this api key." + str(e)
79+
7280
elif not chat_passed and llm.model_type == LLMType.CHAT.value:
7381
mdl = ChatModel[factory](
74-
req["api_key"], llm.llm_name, base_url=req.get("base_url"))
82+
req["api_key"], llm_name, base_url=req.get("base_url"), api_version=api_version)
7583
try:
76-
m, tc = mdl.chat(None, [{"role": "user", "content": "Hello! How are you doing!"}],
77-
{"temperature": 0.9,'max_tokens':50})
78-
if m.find("**ERROR**") >=0:
84+
m, tc = mdl.chat(None, [{"role": "user", "content": "Hello! How are you doing!"}],
85+
{"temperature": 0.9, 'max_tokens': 50})
86+
if m.find("**ERROR**") >= 0:
7987
raise Exception(m)
88+
chat_passed = True
89+
break
8090
except Exception as e:
81-
msg += f"\nFail to access model({llm.llm_name}) using this api key." + str(
82-
e)
83-
chat_passed = True
91+
msg += f"\nFail to access model({llm_name}) using this api key." + str(e)
92+
8493
elif not rerank_passed and llm.model_type == LLMType.RERANK:
8594
mdl = RerankModel[factory](
86-
req["api_key"], llm.llm_name, base_url=req.get("base_url"))
95+
req["api_key"], llm_name, base_url=req.get("base_url"))
8796
try:
8897
arr, tc = mdl.similarity("What's the weather?", ["Is it sunny today?"])
8998
if len(arr) == 0 or tc == 0:
9099
raise Exception("Fail")
100+
rerank_passed = True
101+
break
91102
except Exception as e:
92-
msg += f"\nFail to access model({llm.llm_name}) using this api key." + str(
93-
e)
94-
rerank_passed = True
103+
msg += f"\nFail to access model({llm_name}) using this api key." + str(e)
95104

96105
if msg:
97106
return get_data_error_result(retmsg=msg)
@@ -105,6 +114,7 @@ def set_api_key():
105114
llm_config[n] = req[n]
106115

107116
for llm in LLMService.query(fid=factory):
117+
llm.llm_name = llm.llm_name.split('#')[0]+'#'+api_version if llm.fid == 'Azure-OpenAI' else llm.llm_name
108118
if not TenantLLMService.filter_update(
109119
[TenantLLM.tenant_id == current_user.id,
110120
TenantLLM.llm_factory == factory,
@@ -215,7 +225,7 @@ def apikey_json(keys):
215225
base_url=llm["api_base"]
216226
)
217227
try:
218-
m, tc = mdl.chat(None, [{"role": "user", "content": "Hello! How are you doing!"}], {
228+
m, tc = mdl.chat("", [{"role": "user", "content": "Hello! How are you doing!"}], {
219229
"temperature": 0.9})
220230
if not tc:
221231
raise Exception(m)

api/db/services/llm_service.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,46 +102,48 @@ def model_instance(cls, tenant_id, llm_type,
102102
if not mdlnm:
103103
raise LookupError(f"Type of {llm_type} model is not set.")
104104
raise LookupError("Model({}) not authorized".format(mdlnm))
105-
105+
model_with_version = model_config["llm_name"].split('#')
106+
model_name = model_with_version[0]
107+
api_version = model_with_version[1] if len(model_with_version) > 1 else None
106108
if llm_type == LLMType.EMBEDDING.value:
107109
if model_config["llm_factory"] not in EmbeddingModel:
108110
return
109111
return EmbeddingModel[model_config["llm_factory"]](
110-
model_config["api_key"], model_config["llm_name"], base_url=model_config["api_base"])
112+
model_config["api_key"], model_name, base_url=model_config["api_base"], api_version=api_version)
111113

112114
if llm_type == LLMType.RERANK:
113115
if model_config["llm_factory"] not in RerankModel:
114116
return
115117
return RerankModel[model_config["llm_factory"]](
116-
model_config["api_key"], model_config["llm_name"], base_url=model_config["api_base"])
118+
model_config["api_key"], model_name, base_url=model_config["api_base"])
117119

118120
if llm_type == LLMType.IMAGE2TEXT.value:
119121
if model_config["llm_factory"] not in CvModel:
120122
return
121123
return CvModel[model_config["llm_factory"]](
122-
model_config["api_key"], model_config["llm_name"], lang,
124+
model_config["api_key"], model_name, lang,
123125
base_url=model_config["api_base"]
124126
)
125127

126128
if llm_type == LLMType.CHAT.value:
127129
if model_config["llm_factory"] not in ChatModel:
128130
return
129131
return ChatModel[model_config["llm_factory"]](
130-
model_config["api_key"], model_config["llm_name"], base_url=model_config["api_base"])
132+
model_config["api_key"], model_name, base_url=model_config["api_base"], api_version=api_version)
131133

132134
if llm_type == LLMType.SPEECH2TEXT:
133135
if model_config["llm_factory"] not in Seq2txtModel:
134136
return
135137
return Seq2txtModel[model_config["llm_factory"]](
136-
model_config["api_key"], model_config["llm_name"], lang,
138+
model_config["api_key"],model_name, lang,
137139
base_url=model_config["api_base"]
138140
)
139141
if llm_type == LLMType.TTS:
140142
if model_config["llm_factory"] not in TTSModel:
141143
return
142144
return TTSModel[model_config["llm_factory"]](
143145
model_config["api_key"],
144-
model_config["llm_name"],
146+
model_name,
145147
base_url=model_config["api_base"],
146148
)
147149

conf/llm_factories.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -607,73 +607,73 @@
607607
"status": "1",
608608
"llm": [
609609
{
610-
"llm_name": "gpt-4o-mini",
610+
"llm_name": "gpt-4o-mini#2024-02-01",
611611
"tags": "LLM,CHAT,128K",
612612
"max_tokens": 128000,
613613
"model_type": "image2text"
614614
},
615615
{
616-
"llm_name": "gpt-4o",
616+
"llm_name": "gpt-4o#2024-02-01",
617617
"tags": "LLM,CHAT,128K",
618618
"max_tokens": 128000,
619619
"model_type": "chat,image2text"
620620
},
621621
{
622-
"llm_name": "gpt-35-turbo",
622+
"llm_name": "gpt-3.5-turbo#2024-02-01",
623623
"tags": "LLM,CHAT,4K",
624624
"max_tokens": 4096,
625625
"model_type": "chat"
626626
},
627627
{
628-
"llm_name": "gpt-35-turbo-16k",
628+
"llm_name": "gpt-3.5-turbo-16k#2024-02-01",
629629
"tags": "LLM,CHAT,16k",
630630
"max_tokens": 16385,
631631
"model_type": "chat"
632632
},
633633
{
634-
"llm_name": "text-embedding-ada-002",
634+
"llm_name": "text-embedding-ada-002#2024-02-01",
635635
"tags": "TEXT EMBEDDING,8K",
636636
"max_tokens": 8191,
637637
"model_type": "embedding"
638638
},
639639
{
640-
"llm_name": "text-embedding-3-small",
640+
"llm_name": "text-embedding-3-small#2024-02-01",
641641
"tags": "TEXT EMBEDDING,8K",
642642
"max_tokens": 8191,
643643
"model_type": "embedding"
644644
},
645645
{
646-
"llm_name": "text-embedding-3-large",
646+
"llm_name": "text-embedding-3-large#2024-02-01",
647647
"tags": "TEXT EMBEDDING,8K",
648648
"max_tokens": 8191,
649649
"model_type": "embedding"
650650
},
651651
{
652-
"llm_name": "whisper-1",
652+
"llm_name": "whisper-1#2024-02-01",
653653
"tags": "SPEECH2TEXT",
654654
"max_tokens": 26214400,
655655
"model_type": "speech2text"
656656
},
657657
{
658-
"llm_name": "gpt-4",
658+
"llm_name": "gpt-4#2024-02-01",
659659
"tags": "LLM,CHAT,8K",
660660
"max_tokens": 8191,
661661
"model_type": "chat"
662662
},
663663
{
664-
"llm_name": "gpt-4-turbo",
664+
"llm_name": "gpt-4-turbo#2024-02-01",
665665
"tags": "LLM,CHAT,8K",
666666
"max_tokens": 8191,
667667
"model_type": "chat"
668668
},
669669
{
670-
"llm_name": "gpt-4-32k",
670+
"llm_name": "gpt-4-32k#2024-02-01",
671671
"tags": "LLM,CHAT,32K",
672672
"max_tokens": 32768,
673673
"model_type": "chat"
674674
},
675675
{
676-
"llm_name": "gpt-4-vision-preview",
676+
"llm_name": "gpt-4-vision-preview#2024-02-01",
677677
"tags": "LLM,CHAT,IMAGE2TEXT",
678678
"max_tokens": 765,
679679
"model_type": "image2text"

0 commit comments

Comments
 (0)