Skip to content

Commit 904cded

Browse files
committed
feat: 支持智谱图片理解模型
1 parent 88b565f commit 904cded

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# coding=utf-8
2+
import base64
3+
import os
4+
from typing import Dict
5+
6+
from langchain_core.messages import HumanMessage
7+
8+
from common import forms
9+
from common.exception.app_exception import AppApiException
10+
from common.forms import BaseForm
11+
from setting.models_provider.base_model_provider import BaseModelCredential, ValidCode
12+
13+
14+
class ZhiPuImageModelCredential(BaseForm, BaseModelCredential):
15+
api_key = forms.PasswordInputField('API Key', required=True)
16+
17+
def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], provider,
18+
raise_exception=False):
19+
model_type_list = provider.get_model_type_list()
20+
if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
21+
raise AppApiException(ValidCode.valid_error.value, f'{model_type} 模型类型不支持')
22+
23+
for key in ['api_key']:
24+
if key not in model_credential:
25+
if raise_exception:
26+
raise AppApiException(ValidCode.valid_error.value, f'{key} 字段为必填字段')
27+
else:
28+
return False
29+
try:
30+
model = provider.get_model(model_type, model_name, model_credential)
31+
res = model.stream([HumanMessage(content=[{"type": "text", "text": "你好"}])])
32+
for chunk in res:
33+
print(chunk)
34+
except Exception as e:
35+
if isinstance(e, AppApiException):
36+
raise e
37+
if raise_exception:
38+
raise AppApiException(ValidCode.valid_error.value, f'校验失败,请检查参数是否正确: {str(e)}')
39+
else:
40+
return False
41+
return True
42+
43+
def encryption_dict(self, model: Dict[str, object]):
44+
return {**model, 'api_key': super().encryption(model.get('api_key', ''))}
45+
46+
def get_model_params_setting_form(self, model_name):
47+
pass
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import Dict
2+
3+
from langchain_community.chat_models import ChatZhipuAI
4+
from langchain_openai.chat_models import ChatOpenAI
5+
6+
from common.config.tokenizer_manage_config import TokenizerManage
7+
from setting.models_provider.base_model_provider import MaxKBBaseModel
8+
9+
10+
def custom_get_token_ids(text: str):
11+
tokenizer = TokenizerManage.get_tokenizer()
12+
return tokenizer.encode(text)
13+
14+
15+
class ZhiPuImage(MaxKBBaseModel, ChatZhipuAI):
16+
17+
@staticmethod
18+
def new_instance(model_type, model_name, model_credential: Dict[str, object], **model_kwargs):
19+
optional_params = MaxKBBaseModel.filter_optional_params(model_kwargs)
20+
return ZhiPuImage(
21+
model_name=model_name,
22+
api_key=model_credential.get('api_key'),
23+
# stream_options={"include_usage": True},
24+
streaming=True,
25+
**optional_params,
26+
)

apps/setting/models_provider/impl/zhipu_model_provider/zhipu_model_provider.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,40 @@
1111
from common.util.file_util import get_file_content
1212
from setting.models_provider.base_model_provider import ModelProvideInfo, ModelTypeConst, ModelInfo, IModelProvider, \
1313
ModelInfoManage
14+
from setting.models_provider.impl.zhipu_model_provider.credential.image import ZhiPuImageModelCredential
1415
from setting.models_provider.impl.zhipu_model_provider.credential.llm import ZhiPuLLMModelCredential
16+
from setting.models_provider.impl.zhipu_model_provider.model.image import ZhiPuImage
1517
from setting.models_provider.impl.zhipu_model_provider.model.llm import ZhipuChatModel
1618
from smartdoc.conf import PROJECT_DIR
1719

1820
qwen_model_credential = ZhiPuLLMModelCredential()
21+
zhipu_image_model_credential = ZhiPuImageModelCredential()
22+
1923
model_info_list = [
2024
ModelInfo('glm-4', '', ModelTypeConst.LLM, qwen_model_credential, ZhipuChatModel),
2125
ModelInfo('glm-4v', '', ModelTypeConst.LLM, qwen_model_credential, ZhipuChatModel),
2226
ModelInfo('glm-3-turbo', '', ModelTypeConst.LLM, qwen_model_credential, ZhipuChatModel)
2327
]
24-
model_info_manage = ModelInfoManage.builder().append_model_info_list(model_info_list).append_default_model_info(
25-
ModelInfo('glm-4', '', ModelTypeConst.LLM, qwen_model_credential, ZhipuChatModel)).build()
28+
29+
model_info_image_list = [
30+
ModelInfo('glm-4v-plus', '具有强大的多模态理解能力。能够同时理解多达五张图像,并支持视频内容理解',
31+
ModelTypeConst.IMAGE, zhipu_image_model_credential,
32+
ZhiPuImage),
33+
ModelInfo('glm-4v', '专注于单图理解。适用于需要高效图像解析的场景',
34+
ModelTypeConst.IMAGE, zhipu_image_model_credential,
35+
ZhiPuImage),
36+
ModelInfo('glm-4v-flash', '专注于单图理解。适用于需要高效图像解析的场景(免费)',
37+
ModelTypeConst.IMAGE, zhipu_image_model_credential,
38+
ZhiPuImage),
39+
]
40+
41+
model_info_manage = (
42+
ModelInfoManage.builder()
43+
.append_model_info_list(model_info_list)
44+
.append_default_model_info(ModelInfo('glm-4', '', ModelTypeConst.LLM, qwen_model_credential, ZhipuChatModel))
45+
.append_model_info_list(model_info_image_list)
46+
.build()
47+
)
2648

2749

2850
class ZhiPuModelProvider(IModelProvider):

0 commit comments

Comments
 (0)