Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support different api version for azure openai #2712

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/apps/llm_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def set_api_key():
for llm in LLMService.query(fid=factory)[:3]:
if not embd_passed and llm.model_type == LLMType.EMBEDDING.value:
mdl = EmbeddingModel[factory](
req["api_key"], llm.llm_name, base_url=req.get("base_url"))
req["api_key"], llm.llm_name, base_url=req.get("base_url"), api_version=llm.api_version)
try:
arr, tc = mdl.encode(["Test if the api key is available"])
if len(arr[0]) == 0:
Expand All @@ -71,7 +71,7 @@ def set_api_key():
msg += f"\nFail to access embedding model({llm.llm_name}) using this api key." + str(e)
elif not chat_passed and llm.model_type == LLMType.CHAT.value:
mdl = ChatModel[factory](
req["api_key"], llm.llm_name, base_url=req.get("base_url"))
req["api_key"], llm.llm_name, base_url=req.get("base_url"), api_version=llm.api_version)
try:
m, tc = mdl.chat(None, [{"role": "user", "content": "Hello! How are you doing!"}],
{"temperature": 0.9,'max_tokens':50})
Expand Down
12 changes: 12 additions & 0 deletions api/db/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,11 @@ class LLM(DataBaseModel):
help_text="is it validate(0: wasted,1: validate)",
default="1",
index=True)
api_version = CharField(
max_length=255,
null=True,
help_text="2023-05-15, 2024-02-01...",
index=True)

def __str__(self):
return self.llm_name
Expand Down Expand Up @@ -1052,4 +1057,11 @@ def migrate_db():
)
except Exception as e:
pass
try:
migrate(
migrator.add_column('llm', 'api_version',
CharField(max_length=255, null=True, help_text="2023-05-15, 2024-02-01...", index=True))
)
except Exception as e:
pass

19 changes: 12 additions & 7 deletions api/db/services/llm_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ def model_instance(cls, tenant_id, llm_type,
fid = None if len(tmp) < 2 else tmp[1]
mdlnm = tmp[0]
if model_config: model_config = model_config.to_dict()
llm = LLMService.query(llm_name=mdlnm) if not fid else LLMService.query(llm_name=mdlnm, fid=fid)
if not model_config:
if llm_type in [LLMType.EMBEDDING, LLMType.RERANK]:
llm = LLMService.query(llm_name=mdlnm) if not fid else LLMService.query(llm_name=mdlnm, fid=fid)
if llm and llm[0].fid in ["Youdao", "FastEmbed", "BAAI"]:
model_config = {"llm_factory": llm[0].fid, "api_key":"", "llm_name": mdlnm, "api_base": ""}
if not model_config:
Expand All @@ -103,46 +103,51 @@ def model_instance(cls, tenant_id, llm_type,
raise LookupError(f"Type of {llm_type} model is not set.")
raise LookupError("Model({}) not authorized".format(mdlnm))

if llm and llm[0].api_version:
model_config["api_version"] = llm[0].api_version
else:
model_config["api_version"] = ''

if llm_type == LLMType.EMBEDDING.value:
if model_config["llm_factory"] not in EmbeddingModel:
return
return EmbeddingModel[model_config["llm_factory"]](
model_config["api_key"], model_config["llm_name"], base_url=model_config["api_base"])
model_config["api_key"], model_config["llm_name"], base_url=model_config["api_base"], api_version=model_config["api_version"])

if llm_type == LLMType.RERANK:
if model_config["llm_factory"] not in RerankModel:
return
return RerankModel[model_config["llm_factory"]](
model_config["api_key"], model_config["llm_name"], base_url=model_config["api_base"])
model_config["api_key"], model_config["llm_name"], base_url=model_config["api_base"], api_version=model_config["api_version"])

if llm_type == LLMType.IMAGE2TEXT.value:
if model_config["llm_factory"] not in CvModel:
return
return CvModel[model_config["llm_factory"]](
model_config["api_key"], model_config["llm_name"], lang,
base_url=model_config["api_base"]
base_url=model_config["api_base"], api_version=model_config["api_version"]
)

if llm_type == LLMType.CHAT.value:
if model_config["llm_factory"] not in ChatModel:
return
return ChatModel[model_config["llm_factory"]](
model_config["api_key"], model_config["llm_name"], base_url=model_config["api_base"])
model_config["api_key"], model_config["llm_name"], base_url=model_config["api_base"], api_version=model_config["api_version"])

if llm_type == LLMType.SPEECH2TEXT:
if model_config["llm_factory"] not in Seq2txtModel:
return
return Seq2txtModel[model_config["llm_factory"]](
model_config["api_key"], model_config["llm_name"], lang,
base_url=model_config["api_base"]
base_url=model_config["api_base"], api_version=model_config["api_version"]
)
if llm_type == LLMType.TTS:
if model_config["llm_factory"] not in TTSModel:
return
return TTSModel[model_config["llm_factory"]](
model_config["api_key"],
model_config["llm_name"],
base_url=model_config["api_base"],
base_url=model_config["api_base"], api_version=model_config["api_version"]
)

@classmethod
Expand Down
36 changes: 24 additions & 12 deletions conf/llm_factories.json
Original file line number Diff line number Diff line change
Expand Up @@ -610,73 +610,85 @@
"llm_name": "gpt-4o-mini",
"tags": "LLM,CHAT,128K",
"max_tokens": 128000,
"model_type": "image2text"
"model_type": "image2text",
"api_version": "2024-02-01"
},
{
"llm_name": "gpt-4o",
"tags": "LLM,CHAT,128K",
"max_tokens": 128000,
"model_type": "chat,image2text"
"model_type": "chat,image2text",
"api_version": "2024-02-01"
},
{
"llm_name": "gpt-35-turbo",
"tags": "LLM,CHAT,4K",
"max_tokens": 4096,
"model_type": "chat"
"model_type": "chat",
"api_version": "2024-02-01"
},
{
"llm_name": "gpt-35-turbo-16k",
"tags": "LLM,CHAT,16k",
"max_tokens": 16385,
"model_type": "chat"
"model_type": "chat",
"api_version": "2024-02-01"
},
{
"llm_name": "text-embedding-ada-002",
"tags": "TEXT EMBEDDING,8K",
"max_tokens": 8191,
"model_type": "embedding"
"model_type": "embedding",
"api_version": "2024-02-01"
},
{
"llm_name": "text-embedding-3-small",
"tags": "TEXT EMBEDDING,8K",
"max_tokens": 8191,
"model_type": "embedding"
"model_type": "embedding",
"api_version": "2024-02-01"
},
{
"llm_name": "text-embedding-3-large",
"tags": "TEXT EMBEDDING,8K",
"max_tokens": 8191,
"model_type": "embedding"
"model_type": "embedding",
"api_version": "2024-02-01"
},
{
"llm_name": "whisper-1",
"tags": "SPEECH2TEXT",
"max_tokens": 26214400,
"model_type": "speech2text"
"model_type": "speech2text",
"api_version": "2024-02-01"
},
{
"llm_name": "gpt-4",
"tags": "LLM,CHAT,8K",
"max_tokens": 8191,
"model_type": "chat"
"model_type": "chat",
"api_version": "2024-02-01"
},
{
"llm_name": "gpt-4-turbo",
"tags": "LLM,CHAT,8K",
"max_tokens": 8191,
"model_type": "chat"
"model_type": "chat",
"api_version": "2024-02-01"
},
{
"llm_name": "gpt-4-32k",
"tags": "LLM,CHAT,32K",
"max_tokens": 32768,
"model_type": "chat"
"model_type": "chat",
"api_version": "2024-02-01"
},
{
"llm_name": "gpt-4-vision-preview",
"tags": "LLM,CHAT,IMAGE2TEXT",
"max_tokens": 765,
"model_type": "image2text"
"model_type": "image2text",
"api_version": "2024-02-01"
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion rag/llm/chat_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def __init__(self, key, model_name="deepseek-chat", base_url="https://api.deepse

class AzureChat(Base):
def __init__(self, key, model_name, **kwargs):
self.client = AzureOpenAI(api_key=key, azure_endpoint=kwargs["base_url"], api_version="2024-02-01")
self.client = AzureOpenAI(api_key=key, azure_endpoint=kwargs["base_url"], api_version=kwargs["api_version"])
self.model_name = model_name


Expand Down
2 changes: 1 addition & 1 deletion rag/llm/embedding_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def encode_queries(self, text):

class AzureEmbed(OpenAIEmbed):
def __init__(self, key, model_name, **kwargs):
self.client = AzureOpenAI(api_key=key, azure_endpoint=kwargs["base_url"], api_version="2024-02-01")
self.client = AzureOpenAI(api_key=key, azure_endpoint=kwargs["base_url"], api_version=kwargs["api_version"])
self.model_name = model_name


Expand Down