Skip to content

Commit 525058e

Browse files
committed
fix: add API base URL field to AliyunBaiLianReranker and update instance creation
1 parent 1c63df0 commit 525058e

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

apps/models_provider/impl/aliyun_bai_lian_model_provider/credential/reranker.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,31 @@
55
from django.utils.translation import gettext as _
66
from langchain_core.documents import Document
77

8+
from common import forms
89
from common.exception.app_exception import AppApiException
910
from common.forms import BaseForm, PasswordInputField
1011
from models_provider.base_model_provider import BaseModelCredential, ValidCode
1112
from models_provider.impl.aliyun_bai_lian_model_provider.model.reranker import AliyunBaiLianReranker
1213
from common.utils.logger import maxkb_logger
1314

15+
1416
class AliyunBaiLianRerankerCredential(BaseForm, BaseModelCredential):
1517
"""
1618
Credential class for the Aliyun BaiLian Reranker model.
1719
Provides validation and encryption for the model credentials.
1820
"""
19-
21+
api_base = forms.TextInputField(_('API URL'), required=True,
22+
default_value='https://dashscope.aliyuncs.com/compatible-mode/v1')
2023
dashscope_api_key = PasswordInputField('API Key', required=True)
2124

2225
def is_valid(
23-
self,
24-
model_type: str,
25-
model_name: str,
26-
model_credential: Dict[str, Any],
27-
model_params: Dict[str, Any],
28-
provider,
29-
raise_exception: bool = False
26+
self,
27+
model_type: str,
28+
model_name: str,
29+
model_credential: Dict[str, Any],
30+
model_params: Dict[str, Any],
31+
provider,
32+
raise_exception: bool = False
3033
) -> bool:
3134
"""
3235
Validate the model credentials.
@@ -65,7 +68,8 @@ def is_valid(
6568
if raise_exception:
6669
raise AppApiException(
6770
ValidCode.valid_error.value,
68-
_('Verification failed, please check whether the parameters are correct: {error}').format(error=str(e))
71+
_('Verification failed, please check whether the parameters are correct: {error}').format(
72+
error=str(e))
6973
)
7074
return False
7175

apps/models_provider/impl/aliyun_bai_lian_model_provider/model/reranker.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
class AliyunBaiLianReranker(MaxKBBaseModel, BaseDocumentCompressor):
2121
model: Optional[str]
2222
api_key: Optional[str]
23+
base_url: str = ''
2324

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

@@ -31,13 +32,14 @@ def is_cache_model():
3132
def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs):
3233
return AliyunBaiLianReranker(model=model_name,
3334
api_key=model_credential.get('dashscope_api_key'),
35+
base_url=model_credential.get('api_base') or 'https://dashscope.aliyuncs.com/compatible-mode/v1',
3436
top_n=model_kwargs.get('top_n', 3))
3537

3638
def compress_documents(self, documents: Sequence[Document], query: str, callbacks: Optional[Callbacks] = None) -> \
3739
Sequence[Document]:
3840
if not documents:
3941
return []
40-
42+
dashscope.base_http_api_url = self.base_url
4143
texts = [doc.page_content for doc in documents]
4244
resp = dashscope.TextReRank.call(
4345
model=self.model,

0 commit comments

Comments
 (0)