Refa: refine the error message. (#6151)

### What problem does this PR solve?

#6138

### Type of change

- [x] Refactoring
This commit is contained in:
Kevin Hu 2025-03-17 13:07:22 +08:00 committed by GitHub
parent bfa8d342b3
commit 5748d58c74
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -61,6 +61,7 @@ def set_api_key():
msg = "" msg = ""
for llm in LLMService.query(fid=factory): for llm in LLMService.query(fid=factory):
if not embd_passed and llm.model_type == LLMType.EMBEDDING.value: if not embd_passed and llm.model_type == LLMType.EMBEDDING.value:
assert factory in EmbeddingModel, f"Embedding model from {factory} is not supported yet."
mdl = EmbeddingModel[factory]( 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"))
try: try:
@ -71,6 +72,7 @@ def set_api_key():
except Exception as e: except Exception as e:
msg += f"\nFail to access embedding model({llm.llm_name}) using this api key." + str(e) 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: elif not chat_passed and llm.model_type == LLMType.CHAT.value:
assert factory in ChatModel, f"Chat model from {factory} is not supported yet."
mdl = ChatModel[factory]( 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"))
try: try:
@ -83,6 +85,7 @@ def set_api_key():
msg += f"\nFail to access model({llm.llm_name}) using this api key." + str( msg += f"\nFail to access model({llm.llm_name}) using this api key." + str(
e) e)
elif not rerank_passed and llm.model_type == LLMType.RERANK: elif not rerank_passed and llm.model_type == LLMType.RERANK:
assert factory in RerankModel, f"Re-rank model from {factory} is not supported yet."
mdl = RerankModel[factory]( mdl = RerankModel[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"))
try: try:
@ -203,6 +206,7 @@ def add_llm():
msg = "" msg = ""
mdl_nm = llm["llm_name"].split("___")[0] mdl_nm = llm["llm_name"].split("___")[0]
if llm["model_type"] == LLMType.EMBEDDING.value: if llm["model_type"] == LLMType.EMBEDDING.value:
assert factory in EmbeddingModel, f"Embedding model from {factory} is not supported yet."
mdl = EmbeddingModel[factory]( mdl = EmbeddingModel[factory](
key=llm['api_key'], key=llm['api_key'],
model_name=mdl_nm, model_name=mdl_nm,
@ -214,6 +218,7 @@ def add_llm():
except Exception as e: except Exception as e:
msg += f"\nFail to access embedding model({mdl_nm})." + str(e) msg += f"\nFail to access embedding model({mdl_nm})." + str(e)
elif llm["model_type"] == LLMType.CHAT.value: elif llm["model_type"] == LLMType.CHAT.value:
assert factory in ChatModel, f"Chat model from {factory} is not supported yet."
mdl = ChatModel[factory]( mdl = ChatModel[factory](
key=llm['api_key'], key=llm['api_key'],
model_name=mdl_nm, model_name=mdl_nm,
@ -228,6 +233,7 @@ def add_llm():
msg += f"\nFail to access model({mdl_nm})." + str( msg += f"\nFail to access model({mdl_nm})." + str(
e) e)
elif llm["model_type"] == LLMType.RERANK: elif llm["model_type"] == LLMType.RERANK:
assert factory in RerankModel, f"RE-rank model from {factory} is not supported yet."
try: try:
mdl = RerankModel[factory]( mdl = RerankModel[factory](
key=llm["api_key"], key=llm["api_key"],
@ -243,6 +249,7 @@ def add_llm():
msg += f"\nFail to access model({mdl_nm})." + str( msg += f"\nFail to access model({mdl_nm})." + str(
e) e)
elif llm["model_type"] == LLMType.IMAGE2TEXT.value: elif llm["model_type"] == LLMType.IMAGE2TEXT.value:
assert factory in CvModel, f"Image to text model from {factory} is not supported yet."
mdl = CvModel[factory]( mdl = CvModel[factory](
key=llm["api_key"], key=llm["api_key"],
model_name=mdl_nm, model_name=mdl_nm,
@ -256,6 +263,7 @@ def add_llm():
except Exception as e: except Exception as e:
msg += f"\nFail to access model({mdl_nm})." + str(e) msg += f"\nFail to access model({mdl_nm})." + str(e)
elif llm["model_type"] == LLMType.TTS: elif llm["model_type"] == LLMType.TTS:
assert factory in TTSModel, f"TTS model from {factory} is not supported yet."
mdl = TTSModel[factory]( mdl = TTSModel[factory](
key=llm["api_key"], model_name=mdl_nm, base_url=llm["api_base"] key=llm["api_key"], model_name=mdl_nm, base_url=llm["api_base"]
) )