From ae7c446e811d0085c0fca757e64c28f786e321ef Mon Sep 17 00:00:00 2001 From: CaptainB Date: Wed, 25 Dec 2024 14:27:51 +0800 Subject: [PATCH] fix: show tips for non exist ollama model MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1050792 --user=刘瑞斌 【模型管理】ollama图片理解模型,选择服务器上不存在的模型时没有自动下载 https://www.tapd.cn/57709429/s/1636732 --- .../ollama_model_provider/credential/image.py | 24 ++++++------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/apps/setting/models_provider/impl/ollama_model_provider/credential/image.py b/apps/setting/models_provider/impl/ollama_model_provider/credential/image.py index c7077138fd3..35b36626641 100644 --- a/apps/setting/models_provider/impl/ollama_model_provider/credential/image.py +++ b/apps/setting/models_provider/impl/ollama_model_provider/credential/image.py @@ -37,25 +37,15 @@ def is_valid(self, model_type: str, model_name, model_credential: Dict[str, obje model_type_list = provider.get_model_type_list() if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))): raise AppApiException(ValidCode.valid_error.value, f'{model_type} 模型类型不支持') - - for key in ['api_base', 'api_key']: - if key not in model_credential: - if raise_exception: - raise AppApiException(ValidCode.valid_error.value, f'{key} 字段为必填字段') - else: - return False try: - model = provider.get_model(model_type, model_name, model_credential) - res = model.stream([HumanMessage(content=[{"type": "text", "text": "你好"}])]) - for chunk in res: - print(chunk) + model_list = provider.get_base_model_list(model_credential.get('api_base')) except Exception as e: - if isinstance(e, AppApiException): - raise e - if raise_exception: - raise AppApiException(ValidCode.valid_error.value, f'校验失败,请检查参数是否正确: {str(e)}') - else: - return False + raise AppApiException(ValidCode.valid_error.value, "API 域名无效") + exist = [model for model in (model_list.get('models') if model_list.get('models') is not None else []) if + model.get('model') == model_name or model.get('model').replace(":latest", "") == model_name] + if len(exist) == 0: + raise AppApiException(ValidCode.model_not_fount, "模型不存在,请先下载模型") + return True def encryption_dict(self, model: Dict[str, object]):