-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
277ed17
commit 791c6b4
Showing
3 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
apps/setting/models_provider/impl/xinference_model_provider/credential/reranker.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# coding=utf-8 | ||
""" | ||
@project: MaxKB | ||
@Author:虎 | ||
@file: reranker.py | ||
@date:2024/9/10 9:46 | ||
@desc: | ||
""" | ||
from typing import Dict | ||
|
||
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 | ||
|
||
|
||
class XInferenceRerankerModelCredential(BaseForm, BaseModelCredential): | ||
def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], provider, | ||
raise_exception=True): | ||
if not model_type == 'RERANKER': | ||
raise AppApiException(ValidCode.valid_error.value, f'{model_type} 模型类型不支持') | ||
for key in ['server_url']: | ||
if key not in model_credential: | ||
if raise_exception: | ||
raise AppApiException(ValidCode.valid_error.value, f'{key} 字段为必填字段') | ||
else: | ||
return False | ||
try: | ||
model = provider.get_model(model_type, model_name, model_credential) | ||
model.compress_documents([Document(page_content='你好')], '你好') | ||
except Exception as e: | ||
if isinstance(e, AppApiException): | ||
raise e | ||
if raise_exception: | ||
raise AppApiException(ValidCode.valid_error.value, f'校验失败,请检查参数是否正确: {str(e)}') | ||
else: | ||
return False | ||
return True | ||
|
||
def encryption_dict(self, model_info: Dict[str, object]): | ||
return model_info | ||
|
||
server_url = forms.TextInputField('API 域名', required=True) | ||
|
||
api_key = forms.PasswordInputField('API Key', required=False) |
73 changes: 73 additions & 0 deletions
73
apps/setting/models_provider/impl/xinference_model_provider/model/reranker.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# coding=utf-8 | ||
""" | ||
@project: MaxKB | ||
@Author:虎 | ||
@file: reranker.py | ||
@date:2024/9/10 9:45 | ||
@desc: | ||
""" | ||
from typing import Sequence, Optional, Any, Dict | ||
|
||
from langchain_core.callbacks import Callbacks | ||
from langchain_core.documents import BaseDocumentCompressor, Document | ||
from xinference_client.client.restful.restful_client import RESTfulRerankModelHandle | ||
|
||
from setting.models_provider.base_model_provider import MaxKBBaseModel | ||
|
||
|
||
class XInferenceReranker(MaxKBBaseModel, BaseDocumentCompressor): | ||
client: Any | ||
server_url: Optional[str] | ||
"""URL of the xinference server""" | ||
model_uid: Optional[str] | ||
"""UID of the launched model""" | ||
api_key: Optional[str] | ||
|
||
@staticmethod | ||
def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs): | ||
return XInferenceReranker(server_url=model_credential.get('server_url'), model_uid=model_name, | ||
api_key=model_credential.get('api_key')) | ||
|
||
top_n: Optional[int] = 3 | ||
|
||
def __init__( | ||
self, server_url: Optional[str] = None, model_uid: Optional[str] = None, top_n=3, | ||
api_key: Optional[str] = None | ||
): | ||
try: | ||
from xinference.client import RESTfulClient | ||
except ImportError: | ||
try: | ||
from xinference_client import RESTfulClient | ||
except ImportError as e: | ||
raise ImportError( | ||
"Could not import RESTfulClient from xinference. Please install it" | ||
" with `pip install xinference` or `pip install xinference_client`." | ||
) from e | ||
|
||
super().__init__() | ||
|
||
if server_url is None: | ||
raise ValueError("Please provide server URL") | ||
|
||
if model_uid is None: | ||
raise ValueError("Please provide the model UID") | ||
|
||
self.server_url = server_url | ||
|
||
self.model_uid = model_uid | ||
|
||
self.api_key = api_key | ||
|
||
self.client = RESTfulClient(server_url, api_key) | ||
|
||
self.top_n = top_n | ||
|
||
def compress_documents(self, documents: Sequence[Document], query: str, callbacks: Optional[Callbacks] = None) -> \ | ||
Sequence[Document]: | ||
if documents is None or len(documents) == 0: | ||
return [] | ||
model: RESTfulRerankModelHandle = self.client.get_model(self.model_uid) | ||
res = model.rerank([document.page_content for document in documents], query, self.top_n, return_documents=True) | ||
return [Document(page_content=d.get('document', {}).get('text'), | ||
metadata={'relevance_score': d.get('relevance_score')}) for d in res.get('results', [])] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters