Skip to content

Commit

Permalink
feat: 高级编排支持文件上传(WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuruibin committed Nov 11, 2024
1 parent 0dab48a commit a245a82
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# coding=utf-8
"""
@project: MaxKB
@Author:虎
@file: llm.py
@date:2024/7/11 18:41
@desc:
"""
import base64
import os
from typing import Dict

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


class QwenModelParams(BaseForm):
temperature = forms.SliderField(TooltipLabel('温度', '较高的数值会使输出更加随机,而较低的数值会使其更加集中和确定'),
required=True, default_value=1.0,
_min=0.1,
_max=1.9,
_step=0.01,
precision=2)

max_tokens = forms.SliderField(
TooltipLabel('输出最大Tokens', '指定模型可生成的最大token个数'),
required=True, default_value=800,
_min=1,
_max=100000,
_step=1,
precision=0)


class TencentVisionModelCredential(BaseForm, BaseModelCredential):

def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], 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, f'{model_type} 模型类型不支持')
for key in ['api_key']:
if key not in model_credential:
if raise_exception:
raise AppApiException(ValidCode.valid_error.value, f'{key} 字段为必填字段')
else:
return False
try:
model = provider.get_model(model_type, model_name, model_credential)
model.stream([HumanMessage(content=[{"type": "text", "text": "你好"}])])
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: Dict[str, object]):
return {**model, 'api_key': super().encryption(model.get('api_key', ''))}

api_key = forms.PasswordInputField('API Key', required=True)

def get_model_params_setting_form(self, model_name):
return QwenModelParams()
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from typing import Dict

from langchain_openai.chat_models import ChatOpenAI

from common.config.tokenizer_manage_config import TokenizerManage
from setting.models_provider.base_model_provider import MaxKBBaseModel


def custom_get_token_ids(text: str):
tokenizer = TokenizerManage.get_tokenizer()
return tokenizer.encode(text)


class TencentVision(MaxKBBaseModel, ChatOpenAI):

@staticmethod
def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs):
optional_params = MaxKBBaseModel.filter_optional_params(model_kwargs)
return TencentVision(
model=model_name,
openai_api_base='https://api.hunyuan.cloud.tencent.com/v1',
openai_api_key=model_credential.get('api_key'),
stream_options={"include_usage": True},
**optional_params,
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
IModelProvider, ModelProvideInfo, ModelInfo, ModelTypeConst, ModelInfoManage
)
from setting.models_provider.impl.tencent_model_provider.credential.embedding import TencentEmbeddingCredential
from setting.models_provider.impl.tencent_model_provider.credential.image import TencentVisionModelCredential
from setting.models_provider.impl.tencent_model_provider.credential.llm import TencentLLMModelCredential
from setting.models_provider.impl.tencent_model_provider.model.embedding import TencentEmbeddingModel
from setting.models_provider.impl.tencent_model_provider.model.image import TencentVision
from setting.models_provider.impl.tencent_model_provider.model.llm import TencentModel
from smartdoc.conf import PROJECT_DIR

Expand Down Expand Up @@ -78,9 +80,18 @@ def _initialize_model_info():

model_info_embedding_list = [tencent_embedding_model_info]

model_info_vision_list = [_create_model_info(
'hunyuan-vision',
'混元视觉模型',
ModelTypeConst.IMAGE,
TencentVisionModelCredential,
TencentVision)]


model_info_manage = ModelInfoManage.builder() \
.append_model_info_list(model_info_list) \
.append_model_info_list(model_info_embedding_list) \
.append_model_info_list(model_info_vision_list) \
.append_default_model_info(model_info_list[0]) \
.build()

Expand Down

0 comments on commit a245a82

Please sign in to comment.