feat(model): 增加模型管理和测试功能

- 新增模型删除功能
- 实现模型测试连接功能
- 优化模型选择器组件
- 更新模型相关API和数据库操作
This commit is contained in:
JefferyHcool
2025-05-26 23:16:19 +08:00
parent ee9f6ed80c
commit 9b298d3094
10 changed files with 158 additions and 37 deletions
+42 -6
View File
@@ -1,5 +1,6 @@
from app.db.model_dao import insert_model, get_all_models
from app.db.model_dao import insert_model, get_all_models, get_model_by_provider_and_name, delete_model
from app.db.provider_dao import get_enabled_providers
from app.exceptions.provider import ConnectionTestError
from app.gpt.gpt_factory import GPTFactory
from app.gpt.provider.OpenAI_compatible_provider import OpenAICompatibleProvider
from app.models.model_config import ModelConfig
@@ -70,6 +71,13 @@ class ModelService:
})
return formatted
@staticmethod
def get_enabled_models_by_provider( provider_id: str|int,):
from app.db.model_dao import get_models_by_provider
all_models = get_models_by_provider(provider_id)
enabled_models = all_models
return enabled_models
@staticmethod
def get_all_models_by_id(provider_id: str, verbose: bool = False):
try:
provider = ProviderService.get_provider_by_id(provider_id)
@@ -86,13 +94,35 @@ class ModelService:
print(f"[{provider_id}] 获取模型失败: {e}")
return []
@staticmethod
def connect_test(api_key: str, base_url: str) -> bool:
def connect_test(id: str) -> bool:
try:
return OpenAICompatibleProvider.test_connection(api_key=api_key, base_url=base_url)
except Exception as e:
print(f"连接测试失败:{e}")
return False
provider = ProviderService.get_provider_by_id(id)
if provider:
if not provider.get('api_key'):
raise ConnectionTestError(f"供应商信息未找到,请先保存重试")
result = OpenAICompatibleProvider.test_connection(
api_key=provider.get('api_key'),
base_url=provider.get('base_url')
)
if result:
return True
else:
raise ConnectionTestError("请检查API Key 和 API 地址是否正确")
raise ConnectionTestError("供应商信息未找到,请先保存重试")
except Exception as e:
# 抛出业务异常,交由 Controller 处理
raise ConnectionTestError(f"{str(e)}") from e
@staticmethod
def delete_model_by_id( model_id: int) -> bool:
try:
delete_model(model_id)
return True
except Exception as e:
print(f"[{model_id}] <UNK>: {e}")
return False
@staticmethod
def add_new_model(provider_id: int, model_name: str) -> bool:
try:
@@ -102,6 +132,12 @@ class ModelService:
print(f"供应商ID {provider_id} 不存在,无法添加模型")
return False
# 查询是否已存在同名模型
existing = get_model_by_provider_and_name(provider_id, model_name)
if existing:
print(f"模型 {model_name} 已存在于供应商ID {provider_id} 下,跳过插入")
return False
# 插入模型
insert_model(provider_id=provider_id, model_name=model_name)
print(f"模型 {model_name} 已成功添加到供应商ID {provider_id}")