From d9c6b6bdb636f61ebc0cf3b6514b13243733de65 Mon Sep 17 00:00:00 2001 From: shaohuzhang1 <80892890+shaohuzhang1@users.noreply.github.com> Date: Tue, 21 Jan 2025 15:52:59 +0800 Subject: [PATCH] fix: model i18n error (#2055) --- apps/common/util/common.py | 9 ++++--- .../credential/embedding.py | 13 ++++++---- .../credential/image.py | 10 +++---- .../credential/llm.py | 10 +++---- .../credential/reranker.py | 10 +++---- .../credential/stt.py | 9 ++++--- .../credential/tti.py | 9 ++++--- .../credential/tts.py | 8 +++--- .../model/tti.py | 5 ++-- .../model/tts.py | 5 ++-- .../credential/image.py | 10 ++++--- .../credential/llm.py | 10 +++---- .../credential/embedding.py | 18 ++++++++----- .../credential/llm.py | 8 +++--- .../credential/embedding.py | 11 ++++---- .../azure_model_provider/credential/image.py | 8 +++--- .../azure_model_provider/credential/llm.py | 9 ++++--- .../azure_model_provider/credential/stt.py | 12 ++++++--- .../azure_model_provider/credential/tti.py | 12 ++++++--- .../azure_model_provider/credential/tts.py | 10 ++++--- .../impl/azure_model_provider/model/tti.py | 2 +- .../impl/azure_model_provider/model/tts.py | 4 +-- .../deepseek_model_provider/credential/llm.py | 10 +++---- .../credential/embedding.py | 15 +++++++---- .../gemini_model_provider/credential/image.py | 22 ++++++++++------ .../gemini_model_provider/credential/llm.py | 20 +++++++++----- .../gemini_model_provider/credential/stt.py | 13 +++++++--- .../impl/gemini_model_provider/model/stt.py | 7 +++-- .../kimi_model_provider/credential/llm.py | 10 +++---- .../credential/embedding.py | 14 ++++++---- .../credential/reranker.py | 13 ++++++---- .../credential/embedding.py | 15 ++++++----- .../ollama_model_provider/credential/image.py | 18 ++++++++----- .../ollama_model_provider/credential/llm.py | 19 +++++++++----- .../credential/embedding.py | 14 ++++++---- .../openai_model_provider/credential/image.py | 19 +++++++++----- .../openai_model_provider/credential/llm.py | 8 +++--- .../openai_model_provider/credential/stt.py | 13 +++++++--- .../openai_model_provider/credential/tti.py | 21 ++++++++------- .../openai_model_provider/credential/tts.py | 16 ++++++++---- .../qwen_model_provider/credential/image.py | 22 +++++++++------- .../qwen_model_provider/credential/llm.py | 20 +++++++++----- .../qwen_model_provider/credential/tti.py | 16 +++++++----- .../credential/embedding.py | 13 +++++++--- .../credential/image.py | 22 +++++++++------- .../tencent_model_provider/credential/llm.py | 20 +++++++++----- .../tencent_model_provider/credential/tti.py | 15 +++++++---- .../credential/embedding.py | 15 ++++++----- .../vllm_model_provider/credential/image.py | 15 ++++++----- .../vllm_model_provider/credential/llm.py | 20 ++++++++------ .../credential/embedding.py | 14 ++++++---- .../credential/image.py | 23 +++++++++------- .../credential/llm.py | 19 +++++++++----- .../credential/stt.py | 15 +++++++---- .../credential/tti.py | 9 ++++--- .../credential/tts.py | 8 +++--- .../model/tts.py | 7 ++--- .../credential/embedding.py | 12 ++++++--- .../wenxin_model_provider/credential/llm.py | 20 ++++++++------ .../xf_model_provider/credential/embedding.py | 13 +++++++--- .../xf_model_provider/credential/image.py | 19 +++++++++----- .../impl/xf_model_provider/credential/llm.py | 26 ++++++++++++------- .../impl/xf_model_provider/credential/stt.py | 13 ++++++---- .../impl/xf_model_provider/credential/tts.py | 15 +++++++---- .../credential/embedding.py | 17 +++++++----- .../credential/image.py | 12 ++++----- .../credential/llm.py | 13 +++++----- .../credential/reranker.py | 13 ++++++---- .../credential/stt.py | 12 ++++++--- .../credential/tti.py | 23 +++++++++------- .../credential/tts.py | 12 ++++++--- .../zhipu_model_provider/credential/image.py | 23 +++++++++------- .../zhipu_model_provider/credential/llm.py | 19 +++++++++----- .../zhipu_model_provider/credential/tti.py | 12 ++++++--- 74 files changed, 612 insertions(+), 404 deletions(-) diff --git a/apps/common/util/common.py b/apps/common/util/common.py index 757246340..00a5f7302 100644 --- a/apps/common/util/common.py +++ b/apps/common/util/common.py @@ -9,19 +9,20 @@ import hashlib import importlib import io +import mimetypes import re import shutil -import mimetypes from functools import reduce from typing import Dict, List from django.core.files.uploadedfile import InMemoryUploadedFile from django.db.models import QuerySet +from django.utils.translation import gettext as __ from pydub import AudioSegment from ..exception.app_exception import AppApiException from ..models.db_model_manage import DBModelManage -from django.utils.translation import gettext_lazy as _ + def sub_array(array: List, item_num=10): result = [] @@ -215,9 +216,9 @@ def split_and_transcribe(file_path, model, max_segment_length_ms=59000, audio_fo def _remove_empty_lines(text): if not isinstance(text, str): - raise AppApiException(500, _('Text-to-speech node, the text content must be of string type')) + raise AppApiException(500, __('Text-to-speech node, the text content must be of string type')) if not text: - raise AppApiException(500, _('Text-to-speech node, the text content cannot be empty')) + raise AppApiException(500, __('Text-to-speech node, the text content cannot be empty')) result = '\n'.join(line for line in text.split('\n') if line.strip()) return markdown_to_plain_text(result) diff --git a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/embedding.py b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/embedding.py index 7ae87a754..530bf7300 100644 --- a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/embedding.py +++ b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/embedding.py @@ -8,12 +8,13 @@ """ from typing import Dict +from django.utils.translation import gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import ValidCode, BaseModelCredential from setting.models_provider.impl.aliyun_bai_lian_model_provider.model.embedding import AliyunBaiLianEmbedding -from django.utils.translation import gettext_lazy as _ class AliyunBaiLianEmbeddingCredential(BaseForm, BaseModelCredential): @@ -23,21 +24,23 @@ class AliyunBaiLianEmbeddingCredential(BaseForm, BaseModelCredential): 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, - _('{model_type} Model type is not supported').format(model_type=model_type)) + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['dashscope_api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model: AliyunBaiLianEmbedding = provider.get_model(model_type, model_name, model_credential) - model.embed_query(_('Hello')) + model.embed_query(__('Hello')) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/image.py b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/image.py index 9149d4f61..f29190c87 100644 --- a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/image.py +++ b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/image.py @@ -6,17 +6,15 @@ @date:2024/7/11 18:41 @desc: """ -import base64 -import os from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class QwenModelParams(BaseForm): @@ -45,11 +43,11 @@ class QwenVLModelCredential(BaseForm, BaseModelCredential): 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, - _('{model_type} Model type is not supported').format(model_type=model_type)) + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -62,7 +60,7 @@ class QwenVLModelCredential(BaseForm, BaseModelCredential): raise e if raise_exception: raise AppApiException(ValidCode.valid_error.value, - _('Verification failed, please check whether the parameters are correct: {error}').format( + __('Verification failed, please check whether the parameters are correct: {error}').format( error=str(e))) else: return False diff --git a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/llm.py b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/llm.py index 43c778a78..fff6a972d 100644 --- a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/llm.py @@ -7,7 +7,7 @@ from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ +from django.utils.translation import gettext_lazy as _, gettext as __ class BaiLianLLMModelParams(BaseForm): @@ -36,23 +36,23 @@ class BaiLianLLMModelCredential(BaseForm, BaseModelCredential): 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, - _('{model_type} Model type is not supported').format(model_type=model_type)) + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential, **model_params) - model.invoke([HumanMessage(content=_('Hello'))]) + model.invoke([HumanMessage(content=__('Hello'))]) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: raise AppApiException(ValidCode.valid_error.value, - _('Verification failed, please check whether the parameters are correct: {error}').format( + __('Verification failed, please check whether the parameters are correct: {error}').format( error=str(e))) else: return False diff --git a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/reranker.py b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/reranker.py index e38a85c1f..53c6d36fd 100644 --- a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/reranker.py +++ b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/reranker.py @@ -8,6 +8,7 @@ """ from typing import Dict +from django.utils.translation import gettext as __ from langchain_core.documents import Document from common import forms @@ -15,7 +16,6 @@ from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode from setting.models_provider.impl.aliyun_bai_lian_model_provider.model.reranker import AliyunBaiLianReranker -from django.utils.translation import gettext_lazy as _ class AliyunBaiLianRerankerCredential(BaseForm, BaseModelCredential): @@ -24,22 +24,22 @@ class AliyunBaiLianRerankerCredential(BaseForm, BaseModelCredential): raise_exception=False): if not model_type == 'RERANKER': raise AppApiException(ValidCode.valid_error.value, - _('{model_type} Model type is not supported').format(model_type=model_type)) + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['dashscope_api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model: AliyunBaiLianReranker = provider.get_model(model_type, model_name, model_credential) - model.compress_documents([Document(page_content=_('Hello'))], _('Hello')) + model.compress_documents([Document(page_content=__('Hello'))], __('Hello')) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: raise AppApiException(ValidCode.valid_error.value, - _('Verification failed, please check whether the parameters are correct: {error}').format( + __('Verification failed, please check whether the parameters are correct: {error}').format( error=str(e))) else: return False diff --git a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/stt.py b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/stt.py index f83ce9059..2b05db3e7 100644 --- a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/stt.py +++ b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/stt.py @@ -2,11 +2,12 @@ from typing import Dict +from django.utils.translation import gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class AliyunBaiLianSTTModelCredential(BaseForm, BaseModelCredential): @@ -17,12 +18,12 @@ class AliyunBaiLianSTTModelCredential(BaseForm, BaseModelCredential): 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, - _('{model_type} Model type is not supported').format(model_type=model_type)) + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -33,7 +34,7 @@ class AliyunBaiLianSTTModelCredential(BaseForm, BaseModelCredential): raise e if raise_exception: raise AppApiException(ValidCode.valid_error.value, - _('Verification failed, please check whether the parameters are correct: {error}').format( + __('Verification failed, please check whether the parameters are correct: {error}').format( error=str(e))) else: return False diff --git a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/tti.py b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/tti.py index ed6832be7..7571623c3 100644 --- a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/tti.py +++ b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/tti.py @@ -8,11 +8,12 @@ """ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class QwenModelParams(BaseForm): @@ -63,11 +64,11 @@ class QwenTextToImageModelCredential(BaseForm, BaseModelCredential): 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, - _('{model_type} Model type is not supported').format(model_type=model_type)) + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -79,7 +80,7 @@ class QwenTextToImageModelCredential(BaseForm, BaseModelCredential): raise e if raise_exception: raise AppApiException(ValidCode.valid_error.value, - _('Verification failed, please check whether the parameters are correct: {error}').format( + __('Verification failed, please check whether the parameters are correct: {error}').format( error=str(e))) else: return False diff --git a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/tts.py b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/tts.py index c5d39892c..81b9b8fa0 100644 --- a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/tts.py +++ b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/credential/tts.py @@ -6,7 +6,7 @@ from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ +from django.utils.translation import gettext_lazy as _, gettext as __ class AliyunBaiLianTTSModelGeneralParams(BaseForm): @@ -51,12 +51,12 @@ class AliyunBaiLianTTSModelCredential(BaseForm, BaseModelCredential): 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, - _('{model_type} Model type is not supported').format(model_type=model_type)) + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -67,7 +67,7 @@ class AliyunBaiLianTTSModelCredential(BaseForm, BaseModelCredential): raise e if raise_exception: raise AppApiException(ValidCode.valid_error.value, - _('Verification failed, please check whether the parameters are correct: {error}').format( + __('Verification failed, please check whether the parameters are correct: {error}').format( error=str(e))) else: return False diff --git a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/tti.py b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/tti.py index c070363b9..e4677f13b 100644 --- a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/tti.py +++ b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/tti.py @@ -3,12 +3,13 @@ from http import HTTPStatus from typing import Dict from dashscope import ImageSynthesis +from django.utils.translation import gettext as __ from langchain_community.chat_models import ChatTongyi from langchain_core.messages import HumanMessage from setting.models_provider.base_model_provider import MaxKBBaseModel from setting.models_provider.impl.base_tti import BaseTextToImage -from django.utils.translation import gettext_lazy as _ + class QwenTextToImageModel(MaxKBBaseModel, BaseTextToImage): api_key: str @@ -39,7 +40,7 @@ class QwenTextToImageModel(MaxKBBaseModel, BaseTextToImage): def check_auth(self): chat = ChatTongyi(api_key=self.api_key, model_name='qwen-max') - chat.invoke([HumanMessage([{"type": "text", "text": _('Hello')}])]) + chat.invoke([HumanMessage([{"type": "text", "text": __('Hello')}])]) def generate_image(self, prompt: str, negative_prompt: str = None): # api_base='https://dashscope.aliyuncs.com/compatible-mode/v1', diff --git a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/tts.py b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/tts.py index c9d29b6b5..c9fc71f9f 100644 --- a/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/tts.py +++ b/apps/setting/models_provider/impl/aliyun_bai_lian_model_provider/model/tts.py @@ -2,11 +2,12 @@ from typing import Dict import dashscope from dashscope.audio.tts_v2 import * +from django.utils.translation import gettext as __ from common.util.common import _remove_empty_lines from setting.models_provider.base_model_provider import MaxKBBaseModel from setting.models_provider.impl.base_tts import BaseTextToSpeech -from django.utils.translation import gettext_lazy as _ + class AliyunBaiLianTextToSpeech(MaxKBBaseModel, BaseTextToSpeech): api_key: str @@ -33,7 +34,7 @@ class AliyunBaiLianTextToSpeech(MaxKBBaseModel, BaseTextToSpeech): ) def check_auth(self): - self.text_to_speech(_('Hello')) + self.text_to_speech(__('Hello')) def text_to_speech(self, text): dashscope.api_key = self.api_key diff --git a/apps/setting/models_provider/impl/anthropic_model_provider/credential/image.py b/apps/setting/models_provider/impl/anthropic_model_provider/credential/image.py index a8342d861..88fa44984 100644 --- a/apps/setting/models_provider/impl/anthropic_model_provider/credential/image.py +++ b/apps/setting/models_provider/impl/anthropic_model_provider/credential/image.py @@ -9,7 +9,7 @@ from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ +from django.utils.translation import gettext_lazy as _, gettext as __ class AnthropicImageModelParams(BaseForm): @@ -39,12 +39,13 @@ class AnthropicImageModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -56,7 +57,8 @@ class AnthropicImageModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format( + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( error=str(e))) else: return False diff --git a/apps/setting/models_provider/impl/anthropic_model_provider/credential/llm.py b/apps/setting/models_provider/impl/anthropic_model_provider/credential/llm.py index 46f21931e..c52be356b 100644 --- a/apps/setting/models_provider/impl/anthropic_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/anthropic_model_provider/credential/llm.py @@ -14,7 +14,7 @@ from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ +from django.utils.translation import gettext_lazy as _, gettext as __ class AnthropicLLMModelParams(BaseForm): @@ -43,23 +43,23 @@ class AnthropicLLMModelCredential(BaseForm, BaseModelCredential): 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, - _('{model_type} Model type is not supported').format(model_type=model_type)) + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential) - model.invoke([HumanMessage(content=_('Hello'))]) + model.invoke([HumanMessage(content=__('Hello'))]) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: raise AppApiException(ValidCode.valid_error.value, - _('Verification failed, please check whether the parameters are correct: {error}').format( + __('Verification failed, please check whether the parameters are correct: {error}').format( error=str(e))) else: return False diff --git a/apps/setting/models_provider/impl/aws_bedrock_model_provider/credential/embedding.py b/apps/setting/models_provider/impl/aws_bedrock_model_provider/credential/embedding.py index 9acd2a0b1..cc2f9c917 100644 --- a/apps/setting/models_provider/impl/aws_bedrock_model_provider/credential/embedding.py +++ b/apps/setting/models_provider/impl/aws_bedrock_model_provider/credential/embedding.py @@ -1,13 +1,12 @@ -import os -import re from typing import Dict +from django.utils.translation import gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode from setting.models_provider.impl.aws_bedrock_model_provider.model.embedding import BedrockEmbeddingModel -from django.utils.translation import gettext_lazy as _ class BedrockEmbeddingCredential(BaseForm, BaseModelCredential): @@ -17,24 +16,29 @@ class BedrockEmbeddingCredential(BaseForm, BaseModelCredential): model_type_list = provider.get_model_type_list() if not any(mt.get('value') == model_type for mt in model_type_list): if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) return False required_keys = ['region_name', 'access_key_id', 'secret_access_key'] if not all(key in model_credential for key in required_keys): if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('The following fields are required: {keys}').format(keys=", ".join(required_keys))) + raise AppApiException(ValidCode.valid_error.value, + __('The following fields are required: {keys}').format( + keys=", ".join(required_keys))) return False try: model: BedrockEmbeddingModel = provider.get_model(model_type, model_name, model_credential) - aa = model.embed_query(_('Hello')) + aa = model.embed_query(__('Hello')) print(aa) except AppApiException: raise except Exception as e: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) return False return True diff --git a/apps/setting/models_provider/impl/aws_bedrock_model_provider/credential/llm.py b/apps/setting/models_provider/impl/aws_bedrock_model_provider/credential/llm.py index de324966e..8ff0f0b2e 100644 --- a/apps/setting/models_provider/impl/aws_bedrock_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/aws_bedrock_model_provider/credential/llm.py @@ -1,12 +1,12 @@ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import ValidCode, BaseModelCredential -from django.utils.translation import gettext_lazy as _ class BedrockLLMModelParams(BaseForm): @@ -36,14 +36,14 @@ class BedrockLLMModelCredential(BaseForm, BaseModelCredential): if not any(mt.get('value') == model_type for mt in model_type_list): if raise_exception: raise AppApiException(ValidCode.valid_error.value, - _('{model_type} Model type is not supported').format(model_type=model_type)) + __('{model_type} Model type is not supported').format(model_type=model_type)) return False required_keys = ['region_name', 'access_key_id', 'secret_access_key'] if not all(key in model_credential for key in required_keys): if raise_exception: raise AppApiException(ValidCode.valid_error.value, - _('The following fields are required: {keys}').format( + __('The following fields are required: {keys}').format( keys=", ".join(required_keys))) return False @@ -55,7 +55,7 @@ class BedrockLLMModelCredential(BaseForm, BaseModelCredential): except Exception as e: if raise_exception: raise AppApiException(ValidCode.valid_error.value, - _('Verification failed, please check whether the parameters are correct: {error}').format( + __('Verification failed, please check whether the parameters are correct: {error}').format( error=str(e))) return False diff --git a/apps/setting/models_provider/impl/azure_model_provider/credential/embedding.py b/apps/setting/models_provider/impl/azure_model_provider/credential/embedding.py index 4deda9f67..fcbe2fdf4 100644 --- a/apps/setting/models_provider/impl/azure_model_provider/credential/embedding.py +++ b/apps/setting/models_provider/impl/azure_model_provider/credential/embedding.py @@ -8,11 +8,12 @@ """ from typing import Dict +from django.utils.translation import gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class AzureOpenAIEmbeddingCredential(BaseForm, BaseModelCredential): @@ -22,23 +23,23 @@ class AzureOpenAIEmbeddingCredential(BaseForm, BaseModelCredential): 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, - _('{model_type} Model type is not supported').format(model_type=model_type)) + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key', 'api_version']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential) - model.embed_query(_('Hello')) + model.embed_query(__('Hello')) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: raise AppApiException(ValidCode.valid_error.value, - _('Verification failed, please check whether the parameters are correct')) + __('Verification failed, please check whether the parameters are correct')) else: return False diff --git a/apps/setting/models_provider/impl/azure_model_provider/credential/image.py b/apps/setting/models_provider/impl/azure_model_provider/credential/image.py index 98b64e03f..f326a5130 100644 --- a/apps/setting/models_provider/impl/azure_model_provider/credential/image.py +++ b/apps/setting/models_provider/impl/azure_model_provider/credential/image.py @@ -9,7 +9,7 @@ from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ +from django.utils.translation import gettext_lazy as _, gettext as __ class AzureOpenAIImageModelParams(BaseForm): @@ -41,12 +41,12 @@ class AzureOpenAIImageModelCredential(BaseForm, BaseModelCredential): 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, - _('{model_type} Model type is not supported').format(model_type=model_type)) + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key', 'api_version']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -59,7 +59,7 @@ class AzureOpenAIImageModelCredential(BaseForm, BaseModelCredential): raise e if raise_exception: raise AppApiException(ValidCode.valid_error.value, - _('Verification failed, please check whether the parameters are correct: {error}').format( + __('Verification failed, please check whether the parameters are correct: {error}').format( error=str(e))) else: return False diff --git a/apps/setting/models_provider/impl/azure_model_provider/credential/llm.py b/apps/setting/models_provider/impl/azure_model_provider/credential/llm.py index 04d8ea498..a53b66f51 100644 --- a/apps/setting/models_provider/impl/azure_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/azure_model_provider/credential/llm.py @@ -14,7 +14,7 @@ from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ +from django.utils.translation import gettext_lazy as _, gettext as __ class AzureLLMModelParams(BaseForm): @@ -43,12 +43,12 @@ class AzureLLMModelCredential(BaseForm, BaseModelCredential): 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, - _('{model_type} Model type is not supported').format(model_type=model_type)) + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key', 'deployment_name', 'api_version']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -58,7 +58,8 @@ class AzureLLMModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct')) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct')) else: return False diff --git a/apps/setting/models_provider/impl/azure_model_provider/credential/stt.py b/apps/setting/models_provider/impl/azure_model_provider/credential/stt.py index f7127a0be..5687caee8 100644 --- a/apps/setting/models_provider/impl/azure_model_provider/credential/stt.py +++ b/apps/setting/models_provider/impl/azure_model_provider/credential/stt.py @@ -1,11 +1,12 @@ # coding=utf-8 from typing import Dict +from django.utils.translation import gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class AzureOpenAISTTModelCredential(BaseForm, BaseModelCredential): @@ -17,12 +18,13 @@ class AzureOpenAISTTModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key', 'api_version']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -32,7 +34,9 @@ class AzureOpenAISTTModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/azure_model_provider/credential/tti.py b/apps/setting/models_provider/impl/azure_model_provider/credential/tti.py index 76796057b..2a218d01a 100644 --- a/apps/setting/models_provider/impl/azure_model_provider/credential/tti.py +++ b/apps/setting/models_provider/impl/azure_model_provider/credential/tti.py @@ -1,11 +1,12 @@ # coding=utf-8 from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class AzureOpenAITTIModelParams(BaseForm): @@ -52,12 +53,13 @@ class AzureOpenAITextToImageModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key', 'api_version']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -68,7 +70,9 @@ class AzureOpenAITextToImageModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/azure_model_provider/credential/tts.py b/apps/setting/models_provider/impl/azure_model_provider/credential/tts.py index d662f37ef..3ad33788d 100644 --- a/apps/setting/models_provider/impl/azure_model_provider/credential/tts.py +++ b/apps/setting/models_provider/impl/azure_model_provider/credential/tts.py @@ -1,11 +1,13 @@ # coding=utf-8 from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + class AzureOpenAITTSModelGeneralParams(BaseForm): # alloy, echo, fable, onyx, nova, shimmer @@ -33,12 +35,12 @@ class AzureOpenAITTSModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key', 'api_version']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -48,7 +50,7 @@ class AzureOpenAITTSModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, __('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/azure_model_provider/model/tti.py b/apps/setting/models_provider/impl/azure_model_provider/model/tti.py index 5424ec581..fd5c6ffab 100644 --- a/apps/setting/models_provider/impl/azure_model_provider/model/tti.py +++ b/apps/setting/models_provider/impl/azure_model_provider/model/tti.py @@ -1,6 +1,6 @@ from typing import Dict -from openai import OpenAI, AzureOpenAI +from openai import AzureOpenAI from common.config.tokenizer_manage_config import TokenizerManage from setting.models_provider.base_model_provider import MaxKBBaseModel diff --git a/apps/setting/models_provider/impl/azure_model_provider/model/tts.py b/apps/setting/models_provider/impl/azure_model_provider/model/tts.py index b029f1fca..fa0676534 100644 --- a/apps/setting/models_provider/impl/azure_model_provider/model/tts.py +++ b/apps/setting/models_provider/impl/azure_model_provider/model/tts.py @@ -1,8 +1,6 @@ from typing import Dict -from dashscope import api_key -from dashscope.common.env import api_version -from openai import OpenAI, AzureOpenAI +from openai import AzureOpenAI from common.config.tokenizer_manage_config import TokenizerManage from common.util.common import _remove_empty_lines diff --git a/apps/setting/models_provider/impl/deepseek_model_provider/credential/llm.py b/apps/setting/models_provider/impl/deepseek_model_provider/credential/llm.py index 7e7d0fa41..cacc2675a 100644 --- a/apps/setting/models_provider/impl/deepseek_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/deepseek_model_provider/credential/llm.py @@ -8,13 +8,13 @@ """ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class DeepSeekLLMModelParams(BaseForm): @@ -43,23 +43,23 @@ class DeepSeekLLMModelCredential(BaseForm, BaseModelCredential): 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, - _('{model_type} Model type is not supported').format(model_type=model_type)) + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential, **model_params) - model.invoke([HumanMessage(content=_('Hello'))]) + model.invoke([HumanMessage(content=__('Hello'))]) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: raise AppApiException(ValidCode.valid_error.value, - _('Verification failed, please check whether the parameters are correct: {error}').format( + __('Verification failed, please check whether the parameters are correct: {error}').format( error=str(e))) else: return False diff --git a/apps/setting/models_provider/impl/gemini_model_provider/credential/embedding.py b/apps/setting/models_provider/impl/gemini_model_provider/credential/embedding.py index 41f7bb0fe..02fa43aeb 100644 --- a/apps/setting/models_provider/impl/gemini_model_provider/credential/embedding.py +++ b/apps/setting/models_provider/impl/gemini_model_provider/credential/embedding.py @@ -8,33 +8,38 @@ """ from typing import Dict +from django.utils.translation import gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + class GeminiEmbeddingCredential(BaseForm, BaseModelCredential): def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider, raise_exception=True): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential) - model.embed_query(_('Hello')) + model.embed_query(__('Hello')) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/gemini_model_provider/credential/image.py b/apps/setting/models_provider/impl/gemini_model_provider/credential/image.py index b45e7e4ab..b556b0c9d 100644 --- a/apps/setting/models_provider/impl/gemini_model_provider/credential/image.py +++ b/apps/setting/models_provider/impl/gemini_model_provider/credential/image.py @@ -2,15 +2,18 @@ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + + class GeminiImageModelParams(BaseForm): - temperature = forms.SliderField(TooltipLabel(_('Temperature'), _('Higher values make the output more random, while lower values make it more focused and deterministic')), + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), required=True, default_value=0.7, _min=0.1, _max=1.0, @@ -18,7 +21,8 @@ class GeminiImageModelParams(BaseForm): precision=2) max_tokens = forms.SliderField( - TooltipLabel(_('Output the maximum Tokens'), _('Specify the maximum number of tokens that the model can generate')), + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), required=True, default_value=800, _min=1, _max=100000, @@ -26,7 +30,6 @@ class GeminiImageModelParams(BaseForm): precision=0) - class GeminiImageModelCredential(BaseForm, BaseModelCredential): api_key = forms.PasswordInputField('API Key', required=True) @@ -34,24 +37,27 @@ class GeminiImageModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential, **model_params) - res = model.stream([HumanMessage(content=[{"type": "text", "text": _('Hello')}])]) + res = model.stream([HumanMessage(content=[{"type": "text", "text": __('Hello')}])]) for chunk in res: print(chunk) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/gemini_model_provider/credential/llm.py b/apps/setting/models_provider/impl/gemini_model_provider/credential/llm.py index be86da0d1..2041bfc3d 100644 --- a/apps/setting/models_provider/impl/gemini_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/gemini_model_provider/credential/llm.py @@ -8,16 +8,18 @@ """ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + class GeminiLLMModelParams(BaseForm): - temperature = forms.SliderField(TooltipLabel(_('Temperature'), _('Higher values make the output more random, while lower values make it more focused and deterministic')), + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), required=True, default_value=0.7, _min=0.1, _max=1.0, @@ -25,7 +27,8 @@ class GeminiLLMModelParams(BaseForm): precision=2) max_tokens = forms.SliderField( - TooltipLabel(_('Output the maximum Tokens'), _('Specify the maximum number of tokens that the model can generate')), + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), required=True, default_value=800, _min=1, _max=100000, @@ -39,23 +42,26 @@ class GeminiLLMModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential, **model_params) - res = model.invoke([HumanMessage(content=_('Hello'))]) + res = model.invoke([HumanMessage(content=__('Hello'))]) print(res) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/gemini_model_provider/credential/stt.py b/apps/setting/models_provider/impl/gemini_model_provider/credential/stt.py index e4a1c5208..95294721a 100644 --- a/apps/setting/models_provider/impl/gemini_model_provider/credential/stt.py +++ b/apps/setting/models_provider/impl/gemini_model_provider/credential/stt.py @@ -1,11 +1,13 @@ # coding=utf-8 from typing import Dict +from django.utils.translation import gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + class GeminiSTTModelCredential(BaseForm, BaseModelCredential): api_key = forms.PasswordInputField('API Key', required=True) @@ -14,12 +16,13 @@ class GeminiSTTModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -29,7 +32,9 @@ class GeminiSTTModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/gemini_model_provider/model/stt.py b/apps/setting/models_provider/impl/gemini_model_provider/model/stt.py index 3724b5086..5401a90e3 100644 --- a/apps/setting/models_provider/impl/gemini_model_provider/model/stt.py +++ b/apps/setting/models_provider/impl/gemini_model_provider/model/stt.py @@ -1,13 +1,12 @@ - from typing import Dict +from django.utils.translation import gettext as __ from langchain_core.messages import HumanMessage from langchain_google_genai import ChatGoogleGenerativeAI from common.config.tokenizer_manage_config import TokenizerManage from setting.models_provider.base_model_provider import MaxKBBaseModel from setting.models_provider.impl.base_stt import BaseSpeechToText -from django.utils.translation import gettext_lazy as _ def custom_get_token_ids(text: str): @@ -41,7 +40,7 @@ class GeminiSpeechToText(MaxKBBaseModel, BaseSpeechToText): model=self.model, google_api_key=self.api_key ) - response_list = client.invoke(_('Hello')) + response_list = client.invoke(__('Hello')) # print(response_list) def speech_to_text(self, audio_file): @@ -51,7 +50,7 @@ class GeminiSpeechToText(MaxKBBaseModel, BaseSpeechToText): ) audio_data = audio_file.read() msg = HumanMessage(content=[ - {'type': 'text', 'text': _('convert audio to text')}, + {'type': 'text', 'text': __('convert audio to text')}, {"type": "media", 'mime_type': 'audio/mp3', "data": audio_data} ]) res = client.invoke([msg]) diff --git a/apps/setting/models_provider/impl/kimi_model_provider/credential/llm.py b/apps/setting/models_provider/impl/kimi_model_provider/credential/llm.py index 4de385bb7..c5ea71c60 100644 --- a/apps/setting/models_provider/impl/kimi_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/kimi_model_provider/credential/llm.py @@ -8,13 +8,13 @@ """ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class KimiLLMModelParams(BaseForm): @@ -40,22 +40,22 @@ class KimiLLMModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential, **model_params) - model.invoke([HumanMessage(content=_('Hello'))]) + model.invoke([HumanMessage(content=__('Hello'))]) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, __('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/local_model_provider/credential/embedding.py b/apps/setting/models_provider/impl/local_model_provider/credential/embedding.py index edb08610f..afec693b9 100644 --- a/apps/setting/models_provider/impl/local_model_provider/credential/embedding.py +++ b/apps/setting/models_provider/impl/local_model_provider/credential/embedding.py @@ -8,12 +8,13 @@ """ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode from setting.models_provider.impl.local_model_provider.model.embedding import LocalEmbedding -from django.utils.translation import gettext_lazy as _ class LocalEmbeddingCredential(BaseForm, BaseModelCredential): @@ -21,21 +22,24 @@ class LocalEmbeddingCredential(BaseForm, BaseModelCredential): def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider, raise_exception=False): if not model_type == 'EMBEDDING': - raise AppApiException(ValidCode.valid_error.value, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['cache_folder']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model: LocalEmbedding = provider.get_model(model_type, model_name, model_credential) - model.embed_query(_('Hello')) + model.embed_query(__('Hello')) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/local_model_provider/credential/reranker.py b/apps/setting/models_provider/impl/local_model_provider/credential/reranker.py index 1df993fdd..1a32b6493 100644 --- a/apps/setting/models_provider/impl/local_model_provider/credential/reranker.py +++ b/apps/setting/models_provider/impl/local_model_provider/credential/reranker.py @@ -15,7 +15,7 @@ from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode from setting.models_provider.impl.local_model_provider.model.reranker import LocalBaseReranker -from django.utils.translation import gettext_lazy as _ +from django.utils.translation import gettext_lazy as _, gettext as __ class LocalRerankerCredential(BaseForm, BaseModelCredential): @@ -23,21 +23,24 @@ class LocalRerankerCredential(BaseForm, BaseModelCredential): def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider, raise_exception=False): if not model_type == 'RERANKER': - raise AppApiException(ValidCode.valid_error.value, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['cache_dir']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model: LocalBaseReranker = provider.get_model(model_type, model_name, model_credential) - model.compress_documents([Document(page_content=_('Hello'))], _('Hello')) + model.compress_documents([Document(page_content=__('Hello'))], __('Hello')) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/ollama_model_provider/credential/embedding.py b/apps/setting/models_provider/impl/ollama_model_provider/credential/embedding.py index 328408169..c4366a55f 100644 --- a/apps/setting/models_provider/impl/ollama_model_provider/credential/embedding.py +++ b/apps/setting/models_provider/impl/ollama_model_provider/credential/embedding.py @@ -8,12 +8,13 @@ """ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode from setting.models_provider.impl.local_model_provider.model.embedding import LocalEmbedding -from django.utils.translation import gettext_lazy as _ class OllamaEmbeddingModelCredential(BaseForm, BaseModelCredential): @@ -21,17 +22,19 @@ class OllamaEmbeddingModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) try: model_list = provider.get_base_model_list(model_credential.get('api_base')) except Exception as e: - raise AppApiException(ValidCode.valid_error.value, _('API domain name is invalid')) + raise AppApiException(ValidCode.valid_error.value, __('API domain name is invalid')) 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, _('The model does not exist, please download the model first')) + raise AppApiException(ValidCode.model_not_fount, + __('The model does not exist, please download the model first')) model: LocalEmbedding = provider.get_model(model_type, model_name, model_credential) - model.embed_query(_('Hello')) + model.embed_query(__('Hello')) return True def encryption_dict(self, model_info: Dict[str, object]): @@ -40,7 +43,7 @@ class OllamaEmbeddingModelCredential(BaseForm, BaseModelCredential): def build_model(self, model_info: Dict[str, object]): for key in ['model']: if key not in model_info: - raise AppApiException(500, _('{key} is required').format(key=key)) + raise AppApiException(500, __('{key} is required').format(key=key)) return self api_base = forms.TextInputField('API Url', required=True) 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 63be15d22..4e07be88c 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 @@ -5,10 +5,12 @@ from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ +from django.utils.translation import gettext_lazy as _, gettext as __ + class OllamaImageModelParams(BaseForm): - temperature = forms.SliderField(TooltipLabel(_('Temperature'), _('Higher values make the output more random, while lower values make it more focused and deterministic')), + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), required=True, default_value=0.7, _min=0.1, _max=1.0, @@ -16,7 +18,8 @@ class OllamaImageModelParams(BaseForm): precision=2) max_tokens = forms.SliderField( - TooltipLabel(_('Output the maximum Tokens'), _('Specify the maximum number of tokens that the model can generate')), + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), required=True, default_value=800, _min=1, _max=100000, @@ -24,7 +27,6 @@ class OllamaImageModelParams(BaseForm): precision=0) - class OllamaImageModelCredential(BaseForm, BaseModelCredential): api_base = forms.TextInputField('API Url', required=True) api_key = forms.PasswordInputField('API Key', required=True) @@ -33,15 +35,17 @@ class OllamaImageModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) try: model_list = provider.get_base_model_list(model_credential.get('api_base')) except Exception as e: - raise AppApiException(ValidCode.valid_error.value, _('API domain name is invalid')) + raise AppApiException(ValidCode.valid_error.value, __('API domain name is invalid')) 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, _('The model does not exist, please download the model first')) + raise AppApiException(ValidCode.model_not_fount, + __('The model does not exist, please download the model first')) return True diff --git a/apps/setting/models_provider/impl/ollama_model_provider/credential/llm.py b/apps/setting/models_provider/impl/ollama_model_provider/credential/llm.py index d951aefa3..715471f00 100644 --- a/apps/setting/models_provider/impl/ollama_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/ollama_model_provider/credential/llm.py @@ -8,15 +8,17 @@ """ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class OllamaLLMModelParams(BaseForm): - temperature = forms.SliderField(TooltipLabel(_('Temperature'), _('Higher values make the output more random, while lower values make it more focused and deterministic')), + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), required=True, default_value=0.3, _min=0.1, _max=1.0, @@ -24,7 +26,8 @@ class OllamaLLMModelParams(BaseForm): precision=2) max_tokens = forms.SliderField( - TooltipLabel(_('Output the maximum Tokens'), _('Specify the maximum number of tokens that the model can generate')), + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), required=True, default_value=1024, _min=1, _max=100000, @@ -37,15 +40,17 @@ class OllamaLLMModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) try: model_list = provider.get_base_model_list(model_credential.get('api_base')) except Exception as e: - raise AppApiException(ValidCode.valid_error.value, _('API domain name is invalid')) + raise AppApiException(ValidCode.valid_error.value, __('API domain name is invalid')) 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, _('The model does not exist, please download the model first')) + raise AppApiException(ValidCode.model_not_fount, + __('The model does not exist, please download the model first')) return True def encryption_dict(self, model_info: Dict[str, object]): @@ -54,7 +59,7 @@ class OllamaLLMModelCredential(BaseForm, BaseModelCredential): def build_model(self, model_info: Dict[str, object]): for key in ['api_key', 'model']: if key not in model_info: - raise AppApiException(500, _('{key} is required').format(key=key)) + raise AppApiException(500, __('{key} is required').format(key=key)) self.api_key = model_info.get('api_key') return self diff --git a/apps/setting/models_provider/impl/openai_model_provider/credential/embedding.py b/apps/setting/models_provider/impl/openai_model_provider/credential/embedding.py index c3e1f22d4..0d378b5cc 100644 --- a/apps/setting/models_provider/impl/openai_model_provider/credential/embedding.py +++ b/apps/setting/models_provider/impl/openai_model_provider/credential/embedding.py @@ -8,11 +8,12 @@ """ from typing import Dict +from django.utils.translation import gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class OpenAIEmbeddingCredential(BaseForm, BaseModelCredential): @@ -20,22 +21,25 @@ class OpenAIEmbeddingCredential(BaseForm, BaseModelCredential): raise_exception=True): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential) - model.embed_query(_('Hello')) + model.embed_query(__('Hello')) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/openai_model_provider/credential/image.py b/apps/setting/models_provider/impl/openai_model_provider/credential/image.py index 0149848e4..6dd10a83e 100644 --- a/apps/setting/models_provider/impl/openai_model_provider/credential/image.py +++ b/apps/setting/models_provider/impl/openai_model_provider/credential/image.py @@ -9,10 +9,12 @@ from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ +from django.utils.translation import gettext_lazy as _, gettext as __ + class OpenAIImageModelParams(BaseForm): - temperature = forms.SliderField(TooltipLabel(_('Temperature'), _('Higher values make the output more random, while lower values make it more focused and deterministic')), + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), required=True, default_value=0.7, _min=0.1, _max=1.0, @@ -20,7 +22,8 @@ class OpenAIImageModelParams(BaseForm): precision=2) max_tokens = forms.SliderField( - TooltipLabel(_('Output the maximum Tokens'), _('Specify the maximum number of tokens that the model can generate')), + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), required=True, default_value=800, _min=1, _max=100000, @@ -28,7 +31,6 @@ class OpenAIImageModelParams(BaseForm): precision=0) - class OpenAIImageModelCredential(BaseForm, BaseModelCredential): api_base = forms.TextInputField('API Url', required=True) api_key = forms.PasswordInputField('API Key', required=True) @@ -37,12 +39,13 @@ class OpenAIImageModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -54,7 +57,9 @@ class OpenAIImageModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/openai_model_provider/credential/llm.py b/apps/setting/models_provider/impl/openai_model_provider/credential/llm.py index 03eecca44..68f12c77d 100644 --- a/apps/setting/models_provider/impl/openai_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/openai_model_provider/credential/llm.py @@ -8,13 +8,13 @@ """ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class OpenAILLMModelParams(BaseForm): @@ -40,12 +40,12 @@ class OpenAILLMModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -56,7 +56,7 @@ class OpenAILLMModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, __('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/openai_model_provider/credential/stt.py b/apps/setting/models_provider/impl/openai_model_provider/credential/stt.py index cfb8a71ad..c81407c43 100644 --- a/apps/setting/models_provider/impl/openai_model_provider/credential/stt.py +++ b/apps/setting/models_provider/impl/openai_model_provider/credential/stt.py @@ -1,11 +1,13 @@ # coding=utf-8 from typing import Dict +from django.utils.translation import gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + class OpenAISTTModelCredential(BaseForm, BaseModelCredential): api_base = forms.TextInputField('API Url', required=True) @@ -15,12 +17,13 @@ class OpenAISTTModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -30,7 +33,9 @@ class OpenAISTTModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/openai_model_provider/credential/tti.py b/apps/setting/models_provider/impl/openai_model_provider/credential/tti.py index 78fc438d7..e4fbc628b 100644 --- a/apps/setting/models_provider/impl/openai_model_provider/credential/tti.py +++ b/apps/setting/models_provider/impl/openai_model_provider/credential/tti.py @@ -1,19 +1,18 @@ # coding=utf-8 -import base64 -import os from typing import Dict -from langchain_core.messages import HumanMessage +from django.utils.translation import gettext_lazy as _, gettext as __ from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + class OpenAITTIModelParams(BaseForm): size = forms.SingleSelect( - TooltipLabel(_('Image size'), _('The image generation endpoint allows you to create raw images based on text prompts. When using the DALL·E 3, the image size can be 1024x1024, 1024x1792 or 1792x1024 pixels.')), + TooltipLabel(_('Image size'), + _('The image generation endpoint allows you to create raw images based on text prompts. When using the DALL·E 3, the image size can be 1024x1024, 1024x1792 or 1792x1024 pixels.')), required=True, default_value='1024x1024', option_list=[ @@ -40,7 +39,8 @@ By default, images are produced in standard quality, but with DALL·E 3 you can ) n = forms.SliderField( - TooltipLabel(_('Number of pictures'), _('You can use DALL·E 3 to request 1 image at a time (requesting more images by issuing parallel requests), or use DALL·E 2 with the n parameter to request up to 10 images at a time.')), + TooltipLabel(_('Number of pictures'), + _('You can use DALL·E 3 to request 1 image at a time (requesting more images by issuing parallel requests), or use DALL·E 2 with the n parameter to request up to 10 images at a time.')), required=True, default_value=1, _min=1, _max=10, @@ -56,12 +56,13 @@ class OpenAITextToImageModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -72,7 +73,9 @@ class OpenAITextToImageModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/openai_model_provider/credential/tts.py b/apps/setting/models_provider/impl/openai_model_provider/credential/tts.py index e8409df5e..f92dec2a8 100644 --- a/apps/setting/models_provider/impl/openai_model_provider/credential/tts.py +++ b/apps/setting/models_provider/impl/openai_model_provider/credential/tts.py @@ -1,16 +1,19 @@ # coding=utf-8 from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + class OpenAITTSModelGeneralParams(BaseForm): # alloy, echo, fable, onyx, nova, shimmer voice = forms.SingleSelect( - TooltipLabel('Voice', _('Try out the different sounds (Alloy, Echo, Fable, Onyx, Nova, and Sparkle) to find one that suits your desired tone and audience. The current voiceover is optimized for English.')), + TooltipLabel('Voice', + _('Try out the different sounds (Alloy, Echo, Fable, Onyx, Nova, and Sparkle) to find one that suits your desired tone and audience. The current voiceover is optimized for English.')), required=True, default_value='alloy', text_field='value', value_field='value', @@ -32,12 +35,13 @@ class OpenAITTSModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -47,7 +51,9 @@ class OpenAITTSModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/qwen_model_provider/credential/image.py b/apps/setting/models_provider/impl/qwen_model_provider/credential/image.py index 399f66845..da8f2e897 100644 --- a/apps/setting/models_provider/impl/qwen_model_provider/credential/image.py +++ b/apps/setting/models_provider/impl/qwen_model_provider/credential/image.py @@ -6,20 +6,20 @@ @date:2024/7/11 18:41 @desc: """ -import base64 -import os from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + class QwenModelParams(BaseForm): - temperature = forms.SliderField(TooltipLabel(_('Temperature'), _('Higher values make the output more random, while lower values make it more focused and deterministic')), + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), required=True, default_value=1.0, _min=0.1, _max=1.9, @@ -27,7 +27,8 @@ class QwenModelParams(BaseForm): precision=2) max_tokens = forms.SliderField( - TooltipLabel(_('Output the maximum Tokens'), _('Specify the maximum number of tokens that the model can generate')), + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), required=True, default_value=800, _min=1, _max=100000, @@ -41,23 +42,26 @@ class QwenVLModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential, **model_params) - res = model.stream([HumanMessage(content=[{"type": "text", "text": _('Hello')}])]) + res = model.stream([HumanMessage(content=[{"type": "text", "text": __('Hello')}])]) for chunk in res: print(chunk) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/qwen_model_provider/credential/llm.py b/apps/setting/models_provider/impl/qwen_model_provider/credential/llm.py index 0cc026089..f7de75c34 100644 --- a/apps/setting/models_provider/impl/qwen_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/qwen_model_provider/credential/llm.py @@ -8,16 +8,18 @@ """ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + class QwenModelParams(BaseForm): - temperature = forms.SliderField(TooltipLabel(_('Temperature'), _('Higher values make the output more random, while lower values make it more focused and deterministic')), + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), required=True, default_value=1.0, _min=0.1, _max=1.9, @@ -25,7 +27,8 @@ class QwenModelParams(BaseForm): precision=2) max_tokens = forms.SliderField( - TooltipLabel(_('Output the maximum Tokens'), _('Specify the maximum number of tokens that the model can generate')), + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), required=True, default_value=800, _min=1, _max=100000, @@ -39,21 +42,24 @@ class OpenAILLMModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential, **model_params) - model.invoke([HumanMessage(content=_('Hello'))]) + model.invoke([HumanMessage(content=__('Hello'))]) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/qwen_model_provider/credential/tti.py b/apps/setting/models_provider/impl/qwen_model_provider/credential/tti.py index 6a7f6fcf1..7571623c3 100644 --- a/apps/setting/models_provider/impl/qwen_model_provider/credential/tti.py +++ b/apps/setting/models_provider/impl/qwen_model_provider/credential/tti.py @@ -6,17 +6,16 @@ @date:2024/7/11 18:41 @desc: """ -import base64 -import os from typing import Dict -from langchain_core.messages import HumanMessage +from django.utils.translation import gettext_lazy as _, gettext as __ from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + + class QwenModelParams(BaseForm): size = forms.SingleSelect( TooltipLabel(_('Image size'), _('Specify the size of the generated image, such as: 1024x1024')), @@ -64,11 +63,12 @@ class QwenTextToImageModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -79,7 +79,9 @@ class QwenTextToImageModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/tencent_model_provider/credential/embedding.py b/apps/setting/models_provider/impl/tencent_model_provider/credential/embedding.py index 2d3a8e1d0..0eba48a4a 100644 --- a/apps/setting/models_provider/impl/tencent_model_provider/credential/embedding.py +++ b/apps/setting/models_provider/impl/tencent_model_provider/credential/embedding.py @@ -1,10 +1,12 @@ from typing import Dict +from django.utils.translation import gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + class TencentEmbeddingCredential(BaseForm, BaseModelCredential): @@ -12,16 +14,19 @@ class TencentEmbeddingCredential(BaseForm, BaseModelCredential): raise_exception=True) -> bool: 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) self.valid_form(model_credential) try: model = provider.get_model(model_type, model_name, model_credential) - model.embed_query(_('Hello')) + model.embed_query(__('Hello')) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/tencent_model_provider/credential/image.py b/apps/setting/models_provider/impl/tencent_model_provider/credential/image.py index 6d2b74f37..f2706dcb8 100644 --- a/apps/setting/models_provider/impl/tencent_model_provider/credential/image.py +++ b/apps/setting/models_provider/impl/tencent_model_provider/credential/image.py @@ -6,20 +6,20 @@ @date:2024/7/11 18:41 @desc: """ -import base64 -import os from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + class QwenModelParams(BaseForm): - temperature = forms.SliderField(TooltipLabel(_('Temperature'), _('Higher values make the output more random, while lower values make it more focused and deterministic')), + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), required=True, default_value=1.0, _min=0.1, _max=1.9, @@ -27,7 +27,8 @@ class QwenModelParams(BaseForm): precision=2) max_tokens = forms.SliderField( - TooltipLabel(_('Output the maximum Tokens'), _('Specify the maximum number of tokens that the model can generate')), + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), required=True, default_value=800, _min=1, _max=100000, @@ -41,23 +42,26 @@ class TencentVisionModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential, **model_params) - res = model.stream([HumanMessage(content=[{"type": "text", "text": _('Hello')}])]) + res = model.stream([HumanMessage(content=[{"type": "text", "text": __('Hello')}])]) for chunk in res: print(chunk) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/tencent_model_provider/credential/llm.py b/apps/setting/models_provider/impl/tencent_model_provider/credential/llm.py index 531bfff46..9f8c005d9 100644 --- a/apps/setting/models_provider/impl/tencent_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/tencent_model_provider/credential/llm.py @@ -1,14 +1,16 @@ # coding=utf-8 +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + class TencentLLMModelParams(BaseForm): - temperature = forms.SliderField(TooltipLabel(_('Temperature'), _('Higher values make the output more random, while lower values make it more focused and deterministic')), + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), required=True, default_value=0.5, _min=0.1, _max=2.0, @@ -23,7 +25,8 @@ class TencentLLMModelCredential(BaseForm, BaseModelCredential): def _validate_model_type(cls, model_type, provider, raise_exception=False): if not any(mt['value'] == model_type for mt in provider.get_model_type_list()): if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) return False return True @@ -32,20 +35,23 @@ class TencentLLMModelCredential(BaseForm, BaseModelCredential): missing_keys = [key for key in cls.REQUIRED_FIELDS if key not in model_credential] if missing_keys: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{keys} is required').format(keys=", ".join(missing_keys))) + raise AppApiException(ValidCode.valid_error.value, + __('{keys} is required').format(keys=", ".join(missing_keys))) return False return True - def is_valid(self, model_type, model_name, model_credential, model_params, provider, raise_exception=False): + def is_valid(self, model_type, model_name, model_credential, model_params, provider, raise_exception=False): if not (self._validate_model_type(model_type, provider, raise_exception) and self._validate_credential_fields(model_credential, raise_exception)): return False try: model = provider.get_model(model_type, model_name, model_credential, **model_params) - model.invoke([HumanMessage(content=_('Hello'))]) + model.invoke([HumanMessage(content=__('Hello'))]) except Exception as e: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) return False return True diff --git a/apps/setting/models_provider/impl/tencent_model_provider/credential/tti.py b/apps/setting/models_provider/impl/tencent_model_provider/credential/tti.py index 77410c182..dd8a0312a 100644 --- a/apps/setting/models_provider/impl/tencent_model_provider/credential/tti.py +++ b/apps/setting/models_provider/impl/tencent_model_provider/credential/tti.py @@ -1,11 +1,12 @@ # coding=utf-8 -from langchain_core.messages import HumanMessage + +from django.utils.translation import gettext_lazy as _, gettext as __ from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + class TencentTTIModelParams(BaseForm): Style = forms.SingleSelect( @@ -72,7 +73,8 @@ class TencentTTIModelCredential(BaseForm, BaseModelCredential): def _validate_model_type(cls, model_type, provider, raise_exception=False): if not any(mt['value'] == model_type for mt in provider.get_model_type_list()): if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) return False return True @@ -81,7 +83,8 @@ class TencentTTIModelCredential(BaseForm, BaseModelCredential): missing_keys = [key for key in cls.REQUIRED_FIELDS if key not in model_credential] if missing_keys: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{keys} is required').format(keys=", ".join(missing_keys))) + raise AppApiException(ValidCode.valid_error.value, + __('{keys} is required').format(keys=", ".join(missing_keys))) return False return True @@ -94,7 +97,9 @@ class TencentTTIModelCredential(BaseForm, BaseModelCredential): model.check_auth() except Exception as e: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) return False return True diff --git a/apps/setting/models_provider/impl/vllm_model_provider/credential/embedding.py b/apps/setting/models_provider/impl/vllm_model_provider/credential/embedding.py index ab8628431..9cbe24d58 100644 --- a/apps/setting/models_provider/impl/vllm_model_provider/credential/embedding.py +++ b/apps/setting/models_provider/impl/vllm_model_provider/credential/embedding.py @@ -8,12 +8,12 @@ """ from typing import Dict +from django.utils.translation import gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ - class VllmEmbeddingCredential(BaseForm, BaseModelCredential): @@ -21,22 +21,25 @@ class VllmEmbeddingCredential(BaseForm, BaseModelCredential): raise_exception=True): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential) - model.embed_query('你儽') + model.embed_query(__('Hello')) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/vllm_model_provider/credential/image.py b/apps/setting/models_provider/impl/vllm_model_provider/credential/image.py index 9cc8215ed..6ee900d1a 100644 --- a/apps/setting/models_provider/impl/vllm_model_provider/credential/image.py +++ b/apps/setting/models_provider/impl/vllm_model_provider/credential/image.py @@ -1,15 +1,14 @@ # coding=utf-8 -import base64 -import os from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + class VllmImageModelParams(BaseForm): temperature = forms.SliderField(TooltipLabel(_('Temperature'), @@ -30,7 +29,6 @@ class VllmImageModelParams(BaseForm): precision=0) - class VllmImageModelCredential(BaseForm, BaseModelCredential): api_base = forms.TextInputField('API Url', required=True) api_key = forms.PasswordInputField('API Key', required=True) @@ -39,12 +37,13 @@ class VllmImageModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -56,7 +55,9 @@ class VllmImageModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/vllm_model_provider/credential/llm.py b/apps/setting/models_provider/impl/vllm_model_provider/credential/llm.py index ba6e2235a..d43775f3e 100644 --- a/apps/setting/models_provider/impl/vllm_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/vllm_model_provider/credential/llm.py @@ -2,17 +2,18 @@ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class VLLMModelParams(BaseForm): - temperature = forms.SliderField(TooltipLabel(_('Temperature'), _('Higher values make the output more random, while lower values make it more focused and deterministic')), + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), required=True, default_value=0.7, _min=0.1, _max=1.0, @@ -20,7 +21,8 @@ class VLLMModelParams(BaseForm): precision=2) max_tokens = forms.SliderField( - TooltipLabel(_('Output the maximum Tokens'), _('Specify the maximum number of tokens that the model can generate')), + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), required=True, default_value=800, _min=1, _max=100000, @@ -33,17 +35,19 @@ class VLLMModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) try: model_list = provider.get_base_model_list(model_credential.get('api_base'), model_credential.get('api_key')) except Exception as e: - raise AppApiException(ValidCode.valid_error.value, _('API domain name is invalid')) + raise AppApiException(ValidCode.valid_error.value, __('API domain name is invalid')) exist = provider.get_model_info_by_name(model_list, model_name) if len(exist) == 0: - raise AppApiException(ValidCode.valid_error.value, _('The model does not exist, please download the model first')) + raise AppApiException(ValidCode.valid_error.value, + __('The model does not exist, please download the model first')) model = provider.get_model(model_type, model_name, model_credential, **model_params) try: - res = model.invoke([HumanMessage(content=_('Hello'))]) + res = model.invoke([HumanMessage(content=__('Hello'))]) print(res) except Exception as e: print(e) @@ -55,7 +59,7 @@ class VLLMModelCredential(BaseForm, BaseModelCredential): def build_model(self, model_info: Dict[str, object]): for key in ['api_key', 'model']: if key not in model_info: - raise AppApiException(500, _('{key} is required').format(key=key)) + raise AppApiException(500, __('{key} is required').format(key=key)) self.api_key = model_info.get('api_key') return self diff --git a/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/embedding.py b/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/embedding.py index d3dbea285..5e245d3dd 100644 --- a/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/embedding.py +++ b/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/embedding.py @@ -8,11 +8,12 @@ """ from typing import Dict +from django.utils.translation import gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class VolcanicEmbeddingCredential(BaseForm, BaseModelCredential): @@ -20,22 +21,25 @@ class VolcanicEmbeddingCredential(BaseForm, BaseModelCredential): raise_exception=True): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential) - model.embed_query(_('Hello')) + model.embed_query(__('Hello')) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/image.py b/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/image.py index 328f7b3ab..16e35ac68 100644 --- a/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/image.py +++ b/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/image.py @@ -1,18 +1,18 @@ # coding=utf-8 -import base64 -import os from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + class VolcanicEngineImageModelParams(BaseForm): - temperature = forms.SliderField(TooltipLabel(_('Temperature'), _('Higher values make the output more random, while lower values make it more focused and deterministic')), + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), required=True, default_value=0.95, _min=0.1, _max=1.0, @@ -20,13 +20,15 @@ class VolcanicEngineImageModelParams(BaseForm): precision=2) max_tokens = forms.SliderField( - TooltipLabel(_('Output the maximum Tokens'), _('Specify the maximum number of tokens that the model can generate')), + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), required=True, default_value=1024, _min=1, _max=100000, _step=1, precision=0) + class VolcanicEngineImageModelCredential(BaseForm, BaseModelCredential): api_key = forms.PasswordInputField('API Key', required=True) api_base = forms.TextInputField('API Url', required=True) @@ -35,24 +37,27 @@ class VolcanicEngineImageModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_key', 'api_base']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential, **model_params) - res = model.stream([HumanMessage(content=[{"type": "text", "text": _('Hello')}])]) + res = model.stream([HumanMessage(content=[{"type": "text", "text": __('Hello')}])]) for chunk in res: print(chunk) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/llm.py b/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/llm.py index 5dcd60685..993fa8d7c 100644 --- a/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/llm.py @@ -8,17 +8,18 @@ """ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class VolcanicEngineLLMModelParams(BaseForm): - temperature = forms.SliderField(TooltipLabel(_('Temperature'), _('Higher values make the output more random, while lower values make it more focused and deterministic')), + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), required=True, default_value=0.3, _min=0.1, _max=1.0, @@ -26,7 +27,8 @@ class VolcanicEngineLLMModelParams(BaseForm): precision=2) max_tokens = forms.SliderField( - TooltipLabel(_('Output the maximum Tokens'), _('Specify the maximum number of tokens that the model can generate')), + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), required=True, default_value=1024, _min=1, _max=100000, @@ -40,23 +42,26 @@ class VolcanicEngineLLMModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['access_key_id', 'secret_access_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential, **model_params) - res = model.invoke([HumanMessage(content=_('Hello'))]) + res = model.invoke([HumanMessage(content=__('Hello'))]) print(res) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/stt.py b/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/stt.py index bcf2954ec..b2c007a45 100644 --- a/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/stt.py +++ b/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/stt.py @@ -2,15 +2,17 @@ from typing import Dict +from django.utils.translation import gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class VolcanicEngineSTTModelCredential(BaseForm, BaseModelCredential): - volcanic_api_url = forms.TextInputField('API Url', required=True, default_value='wss://openspeech.bytedance.com/api/v2/asr') + volcanic_api_url = forms.TextInputField('API Url', required=True, + default_value='wss://openspeech.bytedance.com/api/v2/asr') volcanic_app_id = forms.TextInputField('App ID', required=True) volcanic_token = forms.PasswordInputField('Access Token', required=True) volcanic_cluster = forms.TextInputField('Cluster ID', required=True) @@ -19,12 +21,13 @@ class VolcanicEngineSTTModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['volcanic_api_url', 'volcanic_app_id', 'volcanic_token', 'volcanic_cluster']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -34,7 +37,9 @@ class VolcanicEngineSTTModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/tti.py b/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/tti.py index 5a68c238d..728dff887 100644 --- a/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/tti.py +++ b/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/tti.py @@ -2,11 +2,12 @@ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class VolcanicEngineTTIModelGeneralParams(BaseForm): @@ -36,12 +37,12 @@ class VolcanicEngineTTIModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['access_key', 'secret_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -51,7 +52,7 @@ class VolcanicEngineTTIModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, __('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/tts.py b/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/tts.py index 95adc4ffe..96ce13404 100644 --- a/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/tts.py +++ b/apps/setting/models_provider/impl/volcanic_engine_model_provider/credential/tts.py @@ -6,7 +6,7 @@ from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ +from django.utils.translation import gettext_lazy as _, gettext as __ class VolcanicEngineTTSModelGeneralParams(BaseForm): @@ -47,12 +47,12 @@ class VolcanicEngineTTSModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['volcanic_api_url', 'volcanic_app_id', 'volcanic_token', 'volcanic_cluster']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -62,7 +62,7 @@ class VolcanicEngineTTSModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, __('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/tts.py b/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/tts.py index c462d69a3..c97ddeb3b 100644 --- a/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/tts.py +++ b/apps/setting/models_provider/impl/volcanic_engine_model_provider/model/tts.py @@ -13,15 +13,16 @@ import copy import gzip import json import re +import ssl import uuid from typing import Dict -import ssl + import websockets +from django.utils.translation import gettext as __ from common.util.common import _remove_empty_lines from setting.models_provider.base_model_provider import MaxKBBaseModel from setting.models_provider.impl.base_tts import BaseTextToSpeech -from django.utils.translation import gettext_lazy as _ MESSAGE_TYPES = {11: "audio-only server response", 12: "frontend server response", 15: "error message from server"} MESSAGE_TYPE_SPECIFIC_FLAGS = {0: "no sequence number", 1: "sequence number > 0", @@ -73,7 +74,7 @@ class VolcanicEngineTextToSpeech(MaxKBBaseModel, BaseTextToSpeech): ) def check_auth(self): - self.text_to_speech(_('Hello')) + self.text_to_speech(__('Hello')) def text_to_speech(self, text): request_json = { diff --git a/apps/setting/models_provider/impl/wenxin_model_provider/credential/embedding.py b/apps/setting/models_provider/impl/wenxin_model_provider/credential/embedding.py index 2715663f4..6f3d78f8b 100644 --- a/apps/setting/models_provider/impl/wenxin_model_provider/credential/embedding.py +++ b/apps/setting/models_provider/impl/wenxin_model_provider/credential/embedding.py @@ -8,11 +8,12 @@ """ from typing import Dict +from django.utils.translation import gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class QianfanEmbeddingCredential(BaseForm, BaseModelCredential): @@ -21,16 +22,19 @@ class QianfanEmbeddingCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) self.valid_form(model_credential) try: model = provider.get_model(model_type, model_name, model_credential) - model.embed_query(_('Hello')) + model.embed_query(__('Hello')) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/wenxin_model_provider/credential/llm.py b/apps/setting/models_provider/impl/wenxin_model_provider/credential/llm.py index fa687ed9f..b72b1457b 100644 --- a/apps/setting/models_provider/impl/wenxin_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/wenxin_model_provider/credential/llm.py @@ -8,17 +8,18 @@ """ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class WenxinLLMModelParams(BaseForm): - temperature = forms.SliderField(TooltipLabel(_('Temperature'), _('Higher values make the output more random, while lower values make it more focused and deterministic')), + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), required=True, default_value=0.95, _min=0.1, _max=1.0, @@ -26,7 +27,8 @@ class WenxinLLMModelParams(BaseForm): precision=2) max_tokens = forms.SliderField( - TooltipLabel(_('Output the maximum Tokens'), _('Specify the maximum number of tokens that the model can generate')), + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), required=True, default_value=1024, _min=2, _max=100000, @@ -39,20 +41,22 @@ class WenxinLLMModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) model = provider.get_model(model_type, model_name, model_credential, **model_params) model_info = [model.lower() for model in model.client.models()] if not model_info.__contains__(model_name.lower()): - raise AppApiException(ValidCode.valid_error.value, _('{model_name} The model does not support').format(model_name=model_name)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_name} The model does not support').format(model_name=model_name)) for key in ['api_key', 'secret_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model.invoke( - [HumanMessage(content=_('Hello'))]) + [HumanMessage(content=__('Hello'))]) except Exception as e: raise e return True @@ -63,7 +67,7 @@ class WenxinLLMModelCredential(BaseForm, BaseModelCredential): def build_model(self, model_info: Dict[str, object]): for key in ['api_key', 'secret_key', 'model']: if key not in model_info: - raise AppApiException(500, _('{key} is required').format(key=key)) + raise AppApiException(500, __('{key} is required').format(key=key)) self.api_key = model_info.get('api_key') self.secret_key = model_info.get('secret_key') return self diff --git a/apps/setting/models_provider/impl/xf_model_provider/credential/embedding.py b/apps/setting/models_provider/impl/xf_model_provider/credential/embedding.py index 89a8e91d9..e782182df 100644 --- a/apps/setting/models_provider/impl/xf_model_provider/credential/embedding.py +++ b/apps/setting/models_provider/impl/xf_model_provider/credential/embedding.py @@ -8,11 +8,13 @@ """ from typing import Dict +from django.utils.translation import gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + class XFEmbeddingCredential(BaseForm, BaseModelCredential): @@ -20,16 +22,19 @@ class XFEmbeddingCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) self.valid_form(model_credential) try: model = provider.get_model(model_type, model_name, model_credential) - model.embed_query(_('Hello')) + model.embed_query(__('Hello')) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/xf_model_provider/credential/image.py b/apps/setting/models_provider/impl/xf_model_provider/credential/image.py index 9e021db65..56fb5ffd5 100644 --- a/apps/setting/models_provider/impl/xf_model_provider/credential/image.py +++ b/apps/setting/models_provider/impl/xf_model_provider/credential/image.py @@ -3,6 +3,7 @@ import base64 import os from typing import Dict +from django.utils.translation import gettext as __ from langchain_core.messages import HumanMessage from common import forms @@ -10,10 +11,11 @@ from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode from setting.models_provider.impl.xf_model_provider.model.image import ImageMessage -from django.utils.translation import gettext_lazy as _ + class XunFeiImageModelCredential(BaseForm, BaseModelCredential): - spark_api_url = forms.TextInputField('API Url', required=True, default_value='wss://spark-api.cn-huabei-1.xf-yun.com/v2.1/image') + spark_api_url = forms.TextInputField('API Url', required=True, + default_value='wss://spark-api.cn-huabei-1.xf-yun.com/v2.1/image') spark_app_id = forms.TextInputField('APP ID', required=True) spark_api_key = forms.PasswordInputField("API Key", required=True) spark_api_secret = forms.PasswordInputField('API Secret', required=True) @@ -22,25 +24,29 @@ class XunFeiImageModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['spark_api_url', 'spark_app_id', 'spark_api_key', 'spark_api_secret']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential, **model_params) cwd = os.path.dirname(os.path.abspath(__file__)) with open(f'{cwd}/img_1.png', 'rb') as f: - message_list = [ImageMessage(str(base64.b64encode(f.read()), 'utf-8')), HumanMessage(_('Please outline this picture'))] + message_list = [ImageMessage(str(base64.b64encode(f.read()), 'utf-8')), + HumanMessage(__('Please outline this picture'))] model.stream(message_list) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True @@ -48,6 +54,5 @@ class XunFeiImageModelCredential(BaseForm, BaseModelCredential): def encryption_dict(self, model: Dict[str, object]): return {**model, 'spark_api_secret': super().encryption(model.get('spark_api_secret', ''))} - def get_model_params_setting_form(self, model_name): pass diff --git a/apps/setting/models_provider/impl/xf_model_provider/credential/llm.py b/apps/setting/models_provider/impl/xf_model_provider/credential/llm.py index 8bb78ba2c..113124628 100644 --- a/apps/setting/models_provider/impl/xf_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/xf_model_provider/credential/llm.py @@ -8,16 +8,18 @@ """ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + class XunFeiLLMModelGeneralParams(BaseForm): - temperature = forms.SliderField(TooltipLabel(_('Temperature'), _('Higher values make the output more random, while lower values make it more focused and deterministic')), + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), required=True, default_value=0.5, _min=0.1, _max=1.0, @@ -25,7 +27,8 @@ class XunFeiLLMModelGeneralParams(BaseForm): precision=2) max_tokens = forms.SliderField( - TooltipLabel(_('Output the maximum Tokens'), _('Specify the maximum number of tokens that the model can generate')), + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), required=True, default_value=4096, _min=1, _max=100000, @@ -34,7 +37,8 @@ class XunFeiLLMModelGeneralParams(BaseForm): class XunFeiLLMModelProParams(BaseForm): - temperature = forms.SliderField(TooltipLabel(_('Temperature'), _('Higher values make the output more random, while lower values make it more focused and deterministic')), + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), required=True, default_value=0.5, _min=0.1, _max=1.0, @@ -42,7 +46,8 @@ class XunFeiLLMModelProParams(BaseForm): precision=2) max_tokens = forms.SliderField( - TooltipLabel(_('Output the maximum Tokens'), _('Specify the maximum number of tokens that the model can generate')), + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), required=True, default_value=4096, _min=1, _max=100000, @@ -56,22 +61,25 @@ class XunFeiLLMModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['spark_api_url', 'spark_app_id', 'spark_api_key', 'spark_api_secret']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential, **model_params) - model.invoke([HumanMessage(content=_('Hello'))]) + model.invoke([HumanMessage(content=__('Hello'))]) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/xf_model_provider/credential/stt.py b/apps/setting/models_provider/impl/xf_model_provider/credential/stt.py index cf8f74e1e..a40668a12 100644 --- a/apps/setting/models_provider/impl/xf_model_provider/credential/stt.py +++ b/apps/setting/models_provider/impl/xf_model_provider/credential/stt.py @@ -2,11 +2,12 @@ from typing import Dict +from django.utils.translation import gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class XunFeiSTTModelCredential(BaseForm, BaseModelCredential): @@ -19,12 +20,13 @@ class XunFeiSTTModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['spark_api_url', 'spark_app_id', 'spark_api_key', 'spark_api_secret']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -34,7 +36,9 @@ class XunFeiSTTModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True @@ -42,6 +46,5 @@ class XunFeiSTTModelCredential(BaseForm, BaseModelCredential): def encryption_dict(self, model: Dict[str, object]): return {**model, 'spark_api_secret': super().encryption(model.get('spark_api_secret', ''))} - def get_model_params_setting_form(self, model_name): pass diff --git a/apps/setting/models_provider/impl/xf_model_provider/credential/tts.py b/apps/setting/models_provider/impl/xf_model_provider/credential/tts.py index a88856b3d..c58f5f86e 100644 --- a/apps/setting/models_provider/impl/xf_model_provider/credential/tts.py +++ b/apps/setting/models_provider/impl/xf_model_provider/credential/tts.py @@ -2,16 +2,18 @@ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class XunFeiTTSModelGeneralParams(BaseForm): vcn = forms.SingleSelect( - TooltipLabel(_('Speaker'), _('Speaker, optional value: Please go to the console to add a trial or purchase speaker. After adding, the speaker parameter value will be displayed.')), + TooltipLabel(_('Speaker'), + _('Speaker, optional value: Please go to the console to add a trial or purchase speaker. After adding, the speaker parameter value will be displayed.')), required=True, default_value='xiaoyan', text_field='value', value_field='value', @@ -41,12 +43,13 @@ class XunFeiTTSModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['spark_api_url', 'spark_app_id', 'spark_api_key', 'spark_api_secret']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -56,7 +59,9 @@ class XunFeiTTSModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/xinference_model_provider/credential/embedding.py b/apps/setting/models_provider/impl/xinference_model_provider/credential/embedding.py index 3437938be..0773e041d 100644 --- a/apps/setting/models_provider/impl/xinference_model_provider/credential/embedding.py +++ b/apps/setting/models_provider/impl/xinference_model_provider/credential/embedding.py @@ -1,31 +1,34 @@ # coding=utf-8 from typing import Dict +from django.utils.translation import gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode from setting.models_provider.impl.local_model_provider.model.embedding import LocalEmbedding -from django.utils.translation import gettext_lazy as _ class XinferenceEmbeddingModelCredential(BaseForm, BaseModelCredential): - def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object],model_params, provider, + def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider, raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) try: model_list = provider.get_base_model_list(model_credential.get('api_base'), model_credential.get('api_key'), 'embedding') except Exception as e: - raise AppApiException(ValidCode.valid_error.value, _('API domain name is invalid')) + raise AppApiException(ValidCode.valid_error.value, __('API domain name is invalid')) exist = provider.get_model_info_by_name(model_list, model_name) model: LocalEmbedding = provider.get_model(model_type, model_name, model_credential) if len(exist) == 0: model.start_down_model_thread() - raise AppApiException(ValidCode.model_not_fount, _('The model does not exist, please download the model first')) - model.embed_query(_('Hello')) + raise AppApiException(ValidCode.model_not_fount, + __('The model does not exist, please download the model first')) + model.embed_query(__('Hello')) return True def encryption_dict(self, model_info: Dict[str, object]): @@ -34,7 +37,7 @@ class XinferenceEmbeddingModelCredential(BaseForm, BaseModelCredential): def build_model(self, model_info: Dict[str, object]): for key in ['model']: if key not in model_info: - raise AppApiException(500, _('{key} is required').format(key=key)) + raise AppApiException(500, __('{key} is required').format(key=key)) return self api_base = forms.TextInputField('API Url', required=True) diff --git a/apps/setting/models_provider/impl/xinference_model_provider/credential/image.py b/apps/setting/models_provider/impl/xinference_model_provider/credential/image.py index d8b187e6d..326960127 100644 --- a/apps/setting/models_provider/impl/xinference_model_provider/credential/image.py +++ b/apps/setting/models_provider/impl/xinference_model_provider/credential/image.py @@ -1,15 +1,13 @@ # coding=utf-8 -import base64 -import os from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class XinferenceImageModelParams(BaseForm): @@ -40,17 +38,17 @@ class XinferenceImageModelCredential(BaseForm, BaseModelCredential): 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, - _('{model_type} Model type is not supported').format(model_type=model_type)) + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential, **model_params) - res = model.stream([HumanMessage(content=[{"type": "text", "text": _('Hello')}])]) + res = model.stream([HumanMessage(content=[{"type": "text", "text": __('Hello')}])]) for chunk in res: print(chunk) except Exception as e: @@ -58,7 +56,7 @@ class XinferenceImageModelCredential(BaseForm, BaseModelCredential): raise e if raise_exception: raise AppApiException(ValidCode.valid_error.value, - _('Verification failed, please check whether the parameters are correct: {error}').format( + __('Verification failed, please check whether the parameters are correct: {error}').format( error=str(e))) else: return False diff --git a/apps/setting/models_provider/impl/xinference_model_provider/credential/llm.py b/apps/setting/models_provider/impl/xinference_model_provider/credential/llm.py index cd3628a23..3fecebf45 100644 --- a/apps/setting/models_provider/impl/xinference_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/xinference_model_provider/credential/llm.py @@ -2,6 +2,7 @@ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms @@ -9,8 +10,6 @@ from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ - class XinferenceLLMModelParams(BaseForm): temperature = forms.SliderField(TooltipLabel(_('Temperature'), @@ -37,18 +36,18 @@ class XinferenceLLMModelCredential(BaseForm, BaseModelCredential): 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, - _('{model_type} Model type is not supported').format(model_type=model_type)) + __('{model_type} Model type is not supported').format(model_type=model_type)) try: model_list = provider.get_base_model_list(model_credential.get('api_base'), model_credential.get('api_key'), model_type) except Exception as e: - raise AppApiException(ValidCode.valid_error.value, _('API domain name is invalid')) + raise AppApiException(ValidCode.valid_error.value, __('API domain name is invalid')) exist = provider.get_model_info_by_name(model_list, model_name) if len(exist) == 0: raise AppApiException(ValidCode.valid_error.value, - _('The model does not exist, please download the model first')) + __('The model does not exist, please download the model first')) model = provider.get_model(model_type, model_name, model_credential, **model_params) - model.invoke([HumanMessage(content=_('Hello'))]) + model.invoke([HumanMessage(content=__('Hello'))]) return True def encryption_dict(self, model_info: Dict[str, object]): @@ -57,7 +56,7 @@ class XinferenceLLMModelCredential(BaseForm, BaseModelCredential): def build_model(self, model_info: Dict[str, object]): for key in ['api_key', 'model']: if key not in model_info: - raise AppApiException(500, _('{key} is required').format(key=key)) + raise AppApiException(500, __('{key} is required').format(key=key)) self.api_key = model_info.get('api_key') return self diff --git a/apps/setting/models_provider/impl/xinference_model_provider/credential/reranker.py b/apps/setting/models_provider/impl/xinference_model_provider/credential/reranker.py index 3033c4091..88ea8df47 100644 --- a/apps/setting/models_provider/impl/xinference_model_provider/credential/reranker.py +++ b/apps/setting/models_provider/impl/xinference_model_provider/credential/reranker.py @@ -8,34 +8,37 @@ """ from typing import Dict +from django.utils.translation import gettext as __ from langchain_core.documents import Document from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class XInferenceRerankerModelCredential(BaseForm, BaseModelCredential): def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], model_params, provider, raise_exception=True): if not model_type == 'RERANKER': - raise AppApiException(ValidCode.valid_error.value, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['server_url']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential) - model.compress_documents([Document(page_content=_('Hello'))], _('Hello')) + model.compress_documents([Document(page_content=__('Hello'))], __('Hello')) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/xinference_model_provider/credential/stt.py b/apps/setting/models_provider/impl/xinference_model_provider/credential/stt.py index 046b60fa4..a192eea52 100644 --- a/apps/setting/models_provider/impl/xinference_model_provider/credential/stt.py +++ b/apps/setting/models_provider/impl/xinference_model_provider/credential/stt.py @@ -1,12 +1,13 @@ # coding=utf-8 from typing import Dict +from django.utils.translation import gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class XInferenceSTTModelCredential(BaseForm, BaseModelCredential): api_base = forms.TextInputField('API Url', required=True) @@ -16,12 +17,13 @@ class XInferenceSTTModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -31,7 +33,9 @@ class XInferenceSTTModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/xinference_model_provider/credential/tti.py b/apps/setting/models_provider/impl/xinference_model_provider/credential/tti.py index 497af8991..1c1ad5db4 100644 --- a/apps/setting/models_provider/impl/xinference_model_provider/credential/tti.py +++ b/apps/setting/models_provider/impl/xinference_model_provider/credential/tti.py @@ -1,20 +1,18 @@ # coding=utf-8 -import base64 -import os from typing import Dict -from langchain_core.messages import HumanMessage +from django.utils.translation import gettext_lazy as _, gettext as __ from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class XinferenceTTIModelParams(BaseForm): size = forms.SingleSelect( - TooltipLabel(_('Image size'), _('The image generation endpoint allows you to create raw images based on text prompts. The dimensions of the image can be 1024x1024, 1024x1792, or 1792x1024 pixels.')), + TooltipLabel(_('Image size'), + _('The image generation endpoint allows you to create raw images based on text prompts. The dimensions of the image can be 1024x1024, 1024x1792, or 1792x1024 pixels.')), required=True, default_value='1024x1024', option_list=[ @@ -27,7 +25,8 @@ class XinferenceTTIModelParams(BaseForm): ) quality = forms.SingleSelect( - TooltipLabel(_('Picture quality'), _('By default, images are generated in standard quality, you can set quality: "hd" to enhance detail. Square, standard quality images are generated fastest.')), + TooltipLabel(_('Picture quality'), + _('By default, images are generated in standard quality, you can set quality: "hd" to enhance detail. Square, standard quality images are generated fastest.')), required=True, default_value='standard', option_list=[ @@ -39,7 +38,8 @@ class XinferenceTTIModelParams(BaseForm): ) n = forms.SliderField( - TooltipLabel(_('Number of pictures'), _('You can request 1 image at a time (requesting more images by making parallel requests), or up to 10 images at a time using the n parameter.')), + TooltipLabel(_('Number of pictures'), + _('You can request 1 image at a time (requesting more images by making parallel requests), or up to 10 images at a time using the n parameter.')), required=True, default_value=1, _min=1, _max=10, @@ -55,12 +55,13 @@ class XinferenceTextToImageModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -71,7 +72,9 @@ class XinferenceTextToImageModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/xinference_model_provider/credential/tts.py b/apps/setting/models_provider/impl/xinference_model_provider/credential/tts.py index 7cbeed3ec..50cb27ee5 100644 --- a/apps/setting/models_provider/impl/xinference_model_provider/credential/tts.py +++ b/apps/setting/models_provider/impl/xinference_model_provider/credential/tts.py @@ -1,11 +1,12 @@ # coding=utf-8 from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class XInferenceTTSModelGeneralParams(BaseForm): @@ -34,12 +35,13 @@ class XInferenceTTSModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_base', 'api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -49,7 +51,9 @@ class XInferenceTTSModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/zhipu_model_provider/credential/image.py b/apps/setting/models_provider/impl/zhipu_model_provider/credential/image.py index 24fcdc97a..532726ce8 100644 --- a/apps/setting/models_provider/impl/zhipu_model_provider/credential/image.py +++ b/apps/setting/models_provider/impl/zhipu_model_provider/credential/image.py @@ -1,18 +1,18 @@ # coding=utf-8 -import base64 -import os from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ + class ZhiPuImageModelParams(BaseForm): - temperature = forms.SliderField(TooltipLabel(_('Temperature'), _('Higher values make the output more random, while lower values make it more focused and deterministic')), + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), required=True, default_value=0.95, _min=0.1, _max=1.0, @@ -20,13 +20,15 @@ class ZhiPuImageModelParams(BaseForm): precision=2) max_tokens = forms.SliderField( - TooltipLabel(_('Output the maximum Tokens'), _('Specify the maximum number of tokens that the model can generate')), + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), required=True, default_value=1024, _min=1, _max=100000, _step=1, precision=0) + class ZhiPuImageModelCredential(BaseForm, BaseModelCredential): api_key = forms.PasswordInputField('API Key', required=True) @@ -34,24 +36,27 @@ class ZhiPuImageModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential, **model_params) - res = model.stream([HumanMessage(content=[{"type": "text", "text": _('Hello')}])]) + res = model.stream([HumanMessage(content=[{"type": "text", "text": __('Hello')}])]) for chunk in res: print(chunk) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/zhipu_model_provider/credential/llm.py b/apps/setting/models_provider/impl/zhipu_model_provider/credential/llm.py index 237ad3711..db133b15f 100644 --- a/apps/setting/models_provider/impl/zhipu_model_provider/credential/llm.py +++ b/apps/setting/models_provider/impl/zhipu_model_provider/credential/llm.py @@ -8,17 +8,18 @@ """ from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ from langchain_core.messages import HumanMessage from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class ZhiPuLLMModelParams(BaseForm): - temperature = forms.SliderField(TooltipLabel(_('Temperature'), _('Higher values make the output more random, while lower values make it more focused and deterministic')), + temperature = forms.SliderField(TooltipLabel(_('Temperature'), + _('Higher values make the output more random, while lower values make it more focused and deterministic')), required=True, default_value=0.95, _min=0.1, _max=1.0, @@ -26,7 +27,8 @@ class ZhiPuLLMModelParams(BaseForm): precision=2) max_tokens = forms.SliderField( - TooltipLabel(_('Output the maximum Tokens'), _('Specify the maximum number of tokens that the model can generate')), + TooltipLabel(_('Output the maximum Tokens'), + _('Specify the maximum number of tokens that the model can generate')), required=True, default_value=1024, _min=1, _max=100000, @@ -40,21 +42,24 @@ class ZhiPuLLMModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: model = provider.get_model(model_type, model_name, model_credential, **model_params) - model.invoke([HumanMessage(content=_('Hello'))]) + model.invoke([HumanMessage(content=__('Hello'))]) except Exception as e: if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True diff --git a/apps/setting/models_provider/impl/zhipu_model_provider/credential/tti.py b/apps/setting/models_provider/impl/zhipu_model_provider/credential/tti.py index 772003a27..a863c6ee8 100644 --- a/apps/setting/models_provider/impl/zhipu_model_provider/credential/tti.py +++ b/apps/setting/models_provider/impl/zhipu_model_provider/credential/tti.py @@ -1,12 +1,13 @@ # coding=utf-8 from typing import Dict +from django.utils.translation import gettext_lazy as _, gettext as __ + from common import forms from common.exception.app_exception import AppApiException from common.forms import BaseForm, TooltipLabel from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode -from django.utils.translation import gettext_lazy as _ class ZhiPuTTIModelParams(BaseForm): size = forms.SingleSelect( @@ -34,12 +35,13 @@ class ZhiPuTextToImageModelCredential(BaseForm, BaseModelCredential): raise_exception=False): 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, _('{model_type} Model type is not supported').format(model_type=model_type)) + raise AppApiException(ValidCode.valid_error.value, + __('{model_type} Model type is not supported').format(model_type=model_type)) for key in ['api_key']: if key not in model_credential: if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('{key} is required').format(key=key)) + raise AppApiException(ValidCode.valid_error.value, __('{key} is required').format(key=key)) else: return False try: @@ -50,7 +52,9 @@ class ZhiPuTextToImageModelCredential(BaseForm, BaseModelCredential): if isinstance(e, AppApiException): raise e if raise_exception: - raise AppApiException(ValidCode.valid_error.value, _('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))) + raise AppApiException(ValidCode.valid_error.value, + __('Verification failed, please check whether the parameters are correct: {error}').format( + error=str(e))) else: return False return True