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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
.append_default_model_info(model_info_list[3])
.append_default_model_info(model_info_list[4])
.append_default_model_info(model_info_list[0])
.append_default_model_info(model_info_list[2])
.append_model_info_list(model_info_ttv_list)
.append_default_model_info(model_info_ttv_list[0])
.append_model_info_list(module_info_itv_list)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,5 @@ def encryption_dict(self, model: Dict[str, Any]) -> Dict[str, Any]:
def get_model_params_setting_form(self, model_name):
return BaiLianEmbeddingModelParams()

api_base = forms.TextInputField(_('API URL'), required=False, default_value='https://dashscope.aliyuncs.com/compatible-mode/v1')
dashscope_api_key = forms.PasswordInputField('API Key', required=True)
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def is_valid(
def encryption_dict(self, model: Dict[str, object]) -> Dict[str, object]:
return {**model, 'api_key': super().encryption(model.get('api_key', ''))}

api_base = forms.TextInputField(_('API URL'), required=False, default_value='https://dashscope.aliyuncs.com/compatible-mode/v1')
api_key = forms.PasswordInputField('API Key', required=True)

def get_model_params_setting_form(self, model_name):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.utils.translation import gettext_lazy as _, gettext

from common.exception.app_exception import AppApiException
from common import forms
from common.forms import BaseForm, PasswordInputField, SingleSelect, SliderField, TooltipLabel
from common.forms.switch_field import SwitchField
from models_provider.base_model_provider import BaseModelCredential, ValidCode
Expand Down Expand Up @@ -41,6 +42,7 @@ class ImageToVideoModelCredential(BaseForm, BaseModelCredential):
Provides validation and encryption for the model credentials.
"""

api_base = forms.TextInputField(_('API URL'), required=False, default_value='https://dashscope.aliyuncs.com/compatible-mode/v1')
api_key = PasswordInputField('API Key', required=True)

def is_valid(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class BaiLianLLMModelParams(BaseForm):


class BaiLianLLMModelCredential(BaseForm, BaseModelCredential):
api_base = forms.TextInputField(_('API URL'), required=True)
api_base = forms.TextInputField(_('API URL'), required=True, default_value='https://dashscope.aliyuncs.com/compatible-mode/v1')
api_key = forms.PasswordInputField(_('API Key'), required=True)

def is_valid(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from langchain_core.documents import Document

from common.exception.app_exception import AppApiException
from common import forms
from common.forms import BaseForm, PasswordInputField
from models_provider.base_model_provider import BaseModelCredential, ValidCode
from models_provider.impl.aliyun_bai_lian_model_provider.model.reranker import AliyunBaiLianReranker
Expand All @@ -17,6 +18,7 @@ class AliyunBaiLianRerankerCredential(BaseForm, BaseModelCredential):
Provides validation and encryption for the model credentials.
"""

api_base = forms.TextInputField(_('API URL'), required=False, default_value='https://dashscope.aliyuncs.com/compatible-mode/v1')
dashscope_api_key = PasswordInputField('API Key', required=True)

def is_valid(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class AliyunBaiLianAsrSTTModelCredential(BaseForm, BaseModelCredential):
api_url = forms.TextInputField(_('API URL'), required=True)
api_base = forms.TextInputField(_('API URL'), required=True, default_value='https://dashscope.aliyuncs.com/compatible-mode/v1')
api_key = forms.PasswordInputField(_('API Key'), required=True)

def is_valid(self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class AliyunBaiLianDefaultSTTModelCredential(BaseForm, BaseModelCredential):
{'label': _('Real-time speech recognition - Fun-ASR/Paraformer'),
'value': 'other'}
])
api_url = forms.TextInputField(_('API URL'), required=True, relation_show_field_dict={'type': ['qwen', 'omni']})
api_base = forms.TextInputField(_('API URL'), required=True, default_value='https://dashscope.aliyuncs.com/compatible-mode/v1', relation_show_field_dict={'type': ['qwen', 'omni']})
api_key = forms.PasswordInputField(_('API Key'), required=True)

def is_valid(self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class AliyunBaiLianOmiSTTModelParams(BaseForm):


class AliyunBaiLianOmiSTTModelCredential(BaseForm, BaseModelCredential):
api_url = forms.TextInputField(_('API URL'), required=True)
api_base = forms.TextInputField(_('API URL'), required=True, default_value='https://dashscope.aliyuncs.com/compatible-mode/v1')
api_key = forms.PasswordInputField(_('API Key'), required=True)

def is_valid(self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class AliyunBaiLianSTTModelCredential(BaseForm, BaseModelCredential):
Provides validation and encryption for the model credentials.
"""

api_base = forms.TextInputField(_('API URL'), required=False, default_value='https://dashscope.aliyuncs.com/compatible-mode/v1')
api_key = PasswordInputField("API Key", required=True)

def is_valid(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.utils.translation import gettext_lazy as _, gettext

from common.exception.app_exception import AppApiException
from common import forms
from common.forms import BaseForm, PasswordInputField, SingleSelect, SliderField, TooltipLabel
from models_provider.base_model_provider import BaseModelCredential, ValidCode
from common.utils.logger import maxkb_logger
Expand Down Expand Up @@ -68,6 +69,7 @@ class QwenTextToImageModelCredential(BaseForm, BaseModelCredential):
Provides validation and encryption for the model credentials.
"""

api_base = forms.TextInputField(_('API URL'), required=False, default_value='https://dashscope.aliyuncs.com/compatible-mode/v1')
api_key = PasswordInputField('API Key', required=True)

def is_valid(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.utils.translation import gettext_lazy as _, gettext

from common.exception.app_exception import AppApiException
from common import forms
from common.forms import BaseForm, PasswordInputField, SingleSelect, SliderField, TooltipLabel
from models_provider.base_model_provider import BaseModelCredential, ValidCode
from common.utils.logger import maxkb_logger
Expand Down Expand Up @@ -53,12 +54,55 @@ class AliyunBaiLianTTSModelGeneralParams(BaseForm):
)


class AliyunBaiLianTTSQwenFlashParams(BaseForm):
"""
Parameters class for the qwen TTS models.
"""
voice = SingleSelect(
TooltipLabel(_('Voice'), _('Voice options for qwen TTS models')),
required=True,
default_value='Cherry',
text_field='value',
value_field='value',
option_list=[
{'text': '芊悦', 'value': 'Cherry'},
{'text': '苏瑶', 'value': 'Serena'},
{'text': '晨煦', 'value': 'Ethan'},
{'text': '千雪', 'value': 'Chelsie'},
{'text': '月白', 'value': 'Moon'},
{'text': '四月', 'value': 'Maia'},
{'text': '凯', 'value': 'Kai'},
]
)

language_type = SingleSelect(
TooltipLabel(_('Language Type'), _('Language type for the speech synthesis')),
required=True,
default_value='Chinese',
text_field='value',
value_field='value',
option_list=[
{'text': _('Chinese'), 'value': 'Chinese'},
{'text': _('English'), 'value': 'English'},
{'text': _('German'), 'value': 'German'},
{'text': _('Italian'), 'value': 'Italian'},
{'text': _('Portuguese'), 'value': 'Portuguese'},
{'text': _('Spanish'), 'value': 'Spanish'},
{'text': _('Japanese'), 'value': 'Japanese'},
{'text': _('Korean'), 'value': 'Korean'},
{'text': _('French'), 'value': 'French'},
{'text': _('Russian'), 'value': 'Russian'},
]
)


class AliyunBaiLianTTSModelCredential(BaseForm, BaseModelCredential):
"""
Credential class for the Aliyun BaiLian TTS (Text-to-Speech) model.
Provides validation and encryption for the model credentials.
"""

api_base = forms.TextInputField(_('API URL'), required=False, default_value='https://dashscope.aliyuncs.com')
api_key = PasswordInputField("API Key", required=True)

def is_valid(
Expand Down Expand Up @@ -135,4 +179,9 @@ def get_model_params_setting_form(self, model_name: str):
:param model_name: Name of the model.
:return: Parameter setting form.
"""
return AliyunBaiLianTTSModelGeneralParams()
# 根据模型名称返回不同的参数设置表单
if model_name in ['cosyvoice-v1']:
return AliyunBaiLianTTSModelGeneralParams()
else:
# 其他模型(包括qwen-tts系列)使用新的参数表单
return AliyunBaiLianTTSQwenFlashParams()
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.utils.translation import gettext_lazy as _, gettext

from common.exception.app_exception import AppApiException
from common import forms
from common.forms import BaseForm, PasswordInputField, SingleSelect, SliderField, TooltipLabel
from common.forms.switch_field import SwitchField
from models_provider.base_model_provider import BaseModelCredential, ValidCode
Expand Down Expand Up @@ -43,6 +44,7 @@ class TextToVideoModelCredential(BaseForm, BaseModelCredential):
Provides validation and encryption for the model credentials.
"""

api_base = forms.TextInputField(_('API URL'), required=False, default_value='https://dashscope.aliyuncs.com/compatible-mode/v1')
api_key = PasswordInputField('API Key', required=True)

def is_valid(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class AliyunBaiLianEmbedding(MaxKBBaseModel):
model_name: str
optional_params: dict

def __init__(self, api_key, model_name: str, optional_params: dict):
self.client = OpenAI(api_key=api_key, base_url='https://dashscope.aliyuncs.com/compatible-mode/v1').embeddings
def __init__(self, api_key, api_base, model_name: str, optional_params: dict):
api_base = api_base or 'https://dashscope.aliyuncs.com/compatible-mode/v1'
self.client = OpenAI(api_key=api_key, base_url=api_base).embeddings
self.model_name = model_name
self.optional_params = optional_params

Expand All @@ -30,6 +31,7 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], **
optional_params = MaxKBBaseModel.filter_optional_params(model_kwargs)
return AliyunBaiLianEmbedding(
api_key=model_credential.get('dashscope_api_key'),
api_base=model_credential.get('api_base'),
model_name=model_name,
optional_params=optional_params
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ def is_cache_model():
@staticmethod
def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs):
optional_params = MaxKBBaseModel.filter_optional_params(model_kwargs)
api_base = model_credential.get('api_base') or 'https://dashscope.aliyuncs.com/compatible-mode/v1'
chat_tong_yi = QwenVLChatModel(
model_name=model_name,
openai_api_key=model_credential.get('api_key'),
openai_api_base='https://dashscope.aliyuncs.com/compatible-mode/v1',
openai_api_base=api_base,
# stream_options={"include_usage": True},
streaming=True,
stream_usage=True,
Expand All @@ -41,7 +42,15 @@ def check_auth(self, api_key):

def get_upload_policy(self, api_key, model_name):
"""获取文件上传凭证"""
url = "https://dashscope.aliyuncs.com/api/v1/uploads"
# 如果有自定义api_base,提取host部分,否则使用默认URL
if hasattr(self, 'openai_api_base') and self.openai_api_base:
# 从api_base中提取host,替换默认URL
from urllib.parse import urlparse
parsed_url = urlparse(self.openai_api_base)
base_url = f"{parsed_url.scheme}://{parsed_url.netloc}"
url = f"{base_url}/api/v1/uploads"
else:
url = "https://dashscope.aliyuncs.com/api/v1/uploads"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
Expand Down Expand Up @@ -109,7 +118,11 @@ def stream(
stop: Optional[list[str]] = None,
**kwargs: Any,
) -> Iterator[BaseMessageChunk]:
url = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions"
# 如果有自定义api_base,使用它,否则使用默认URL
if hasattr(self, 'openai_api_base') and self.openai_api_base:
url = f"{self.openai_api_base}/chat/completions"
else:
url = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions"

headers = {
"Authorization": f"Bearer {self.openai_api_key.get_secret_value()}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
class AliyunBaiLianReranker(MaxKBBaseModel, BaseDocumentCompressor):
model: Optional[str]
api_key: Optional[str]
api_base: Optional[str]

top_n: Optional[int] = 3 # 取前 N 个最相关的结果

Expand All @@ -31,6 +32,7 @@ def is_cache_model():
def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs):
return AliyunBaiLianReranker(model=model_name,
api_key=model_credential.get('dashscope_api_key'),
api_base=model_credential.get('api_base'),
top_n=model_kwargs.get('top_n', 3))

def compress_documents(self, documents: Sequence[Document], query: str, callbacks: Optional[Callbacks] = None) -> \
Expand All @@ -39,6 +41,9 @@ def compress_documents(self, documents: Sequence[Document], query: str, callback
return []

texts = [doc.page_content for doc in documents]
# 如果提供了api_base,则配置dashscope使用自定义endpoint
if self.api_base:
dashscope.base_http_url = self.api_base
resp = dashscope.TextReRank.call(
model=self.model,
query=query,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], **
return AliyunBaiLianAsrSpeechToText(
model=model_name,
api_key=model_credential.get('api_key'),
api_url=model_credential.get('api_url'),
api_url=model_credential.get('api_base'),
params=model_kwargs,
**model_kwargs
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,23 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], **
return AliyunBaiLianAsrSpeechToText(
model=model_name,
api_key=model_credential.get('api_key'),
api_url=model_credential.get('api_url'),
api_url=model_credential.get('api_base'),
params=model_kwargs,
**model_kwargs
)
elif stt_type == 'omni':
return AliyunBaiLianOmiSpeechToText(
model=model_name,
api_key=model_credential.get('api_key'),
api_url=model_credential.get('api_url'),
api_url=model_credential.get('api_base'),
params=model_kwargs,
**model_kwargs
)
else:
return AliyunBaiLianSpeechToText(
model=model_name,
api_key=model_credential.get('api_key'),
api_base=model_credential.get('api_base'),
params=model_kwargs,
**model_kwargs,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], **
return AliyunBaiLianOmiSpeechToText(
model=model_name,
api_key=model_credential.get('api_key'),
api_url=model_credential.get('api_url') ,
api_url=model_credential.get('api_base') ,
params= model_kwargs,
**model_kwargs
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@

class AliyunBaiLianSpeechToText(MaxKBBaseModel, BaseSpeechToText):
api_key: str
api_base: str
model: str
params: dict

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.api_key = kwargs.get('api_key')
self.api_base = kwargs.get('api_base')
self.model = kwargs.get('model')
self.params = kwargs.get('params')

Expand All @@ -36,6 +38,7 @@ def new_instance(model_type, model_name, model_credential: Dict[str, object], **
return AliyunBaiLianSpeechToText(
model=model_name,
api_key=model_credential.get('api_key'),
api_base=model_credential.get('api_base'),
params=model_kwargs,
**optional_params,
)
Expand All @@ -47,6 +50,9 @@ def check_auth(self):

def speech_to_text(self, audio_file):
dashscope.api_key = self.api_key
# 如果提供了api_base,则配置dashscope使用自定义endpoint
if self.api_base:
dashscope.base_http_url = self.api_base
recognition_params = {
'model': self.model,
'format': 'mp3',
Expand Down
Loading