Skip to content

Commit 4aa1b58

Browse files
authored
feat: 支持智谱大模型 #178 (#180)
1 parent 73cf7d8 commit 4aa1b58

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

apps/setting/models_provider/constants/model_provider_constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from setting.models_provider.impl.qwen_model_provider.qwen_model_provider import QwenModelProvider
1515
from setting.models_provider.impl.wenxin_model_provider.wenxin_model_provider import WenxinModelProvider
1616
from setting.models_provider.impl.kimi_model_provider.kimi_model_provider import KimiModelProvider
17+
from setting.models_provider.impl.zhipu_model_provider.zhipu_model_provider import ZhiPuModelProvider
1718

1819

1920
class ModelProvideConstants(Enum):
@@ -23,3 +24,4 @@ class ModelProvideConstants(Enum):
2324
model_openai_provider = OpenAIModelProvider()
2425
model_kimi_provider = KimiModelProvider()
2526
model_qwen_provider = QwenModelProvider()
27+
model_zhipu_provider = ZhiPuModelProvider()

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

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" fill="none" viewBox="0 0 30 32" id="icon-pure-logo"><g clip-path="url(#icon-pure-logo_a)" fill="#1665FF"><path d="M15 32a.377.377 0 0 0 .377-.375.377.377 0 0 0-.378-.375.377.377 0 0 0-.378.375c0 .207.17.375.378.375Zm3.907-20.375a2.26 2.26 0 0 0 2.27-2.25 2.26 2.26 0 0 0-2.27-2.25 2.26 2.26 0 0 0-2.269 2.25 2.26 2.26 0 0 0 2.27 2.25Zm3.908 6.625a2.26 2.26 0 0 0 2.27-2.25 2.26 2.26 0 0 0-2.27-2.25A2.26 2.26 0 0 0 20.546 16a2.26 2.26 0 0 0 2.27 2.25Zm-15.63 0A2.26 2.26 0 0 0 9.454 16a2.26 2.26 0 0 0-2.27-2.25A2.26 2.26 0 0 0 4.917 16a2.26 2.26 0 0 0 2.269 2.25Zm17.647-6.501a1.38 1.38 0 0 0 1.386-1.375A1.38 1.38 0 0 0 24.832 9a1.38 1.38 0 0 0-1.386 1.374c0 .76.621 1.375 1.386 1.375ZM15 6.25a1.38 1.38 0 0 0 1.385-1.375c0-.76-.62-1.375-1.386-1.375a1.38 1.38 0 0 0-1.386 1.375c0 .759.62 1.374 1.386 1.374Zm-9.833 5.499a1.38 1.38 0 0 0 1.386-1.375A1.38 1.38 0 0 0 5.167 9a1.38 1.38 0 0 0-1.386 1.374c0 .76.621 1.375 1.386 1.375Zm0 11.251a1.38 1.38 0 0 0 1.386-1.374c0-.76-.62-1.375-1.386-1.375a1.38 1.38 0 0 0-1.386 1.375A1.38 1.38 0 0 0 5.167 23ZM15 28.625a1.38 1.38 0 0 0 1.385-1.374c0-.76-.62-1.375-1.386-1.375a1.38 1.38 0 0 0-1.386 1.375c0 .759.62 1.374 1.386 1.374ZM24.832 23a1.38 1.38 0 0 0 1.386-1.374c0-.76-.62-1.375-1.386-1.375a1.38 1.38 0 0 0-1.386 1.375A1.38 1.38 0 0 0 24.832 23ZM22.059 4.751a.88.88 0 0 0 .883-.875.88.88 0 0 0-.883-.876.88.88 0 0 0-.883.876.88.88 0 0 0 .883.875Zm-14.118 0a.88.88 0 0 0 .883-.875A.88.88 0 0 0 7.94 3a.88.88 0 0 0-.883.876.88.88 0 0 0 .883.875ZM.883 16.876a.88.88 0 0 0 .883-.875.88.88 0 0 0-.883-.876.88.88 0 0 0-.883.876.88.88 0 0 0 .883.875ZM7.941 29a.88.88 0 0 0 .883-.875.88.88 0 0 0-.883-.876.88.88 0 0 0-.883.876.88.88 0 0 0 .883.875Zm14.118 0a.88.88 0 0 0 .883-.875.88.88 0 0 0-.883-.876.88.88 0 0 0-.883.876.88.88 0 0 0 .883.875Zm7.058-12.124a.88.88 0 0 0 .883-.875.88.88 0 0 0-.883-.876.88.88 0 0 0-.883.876.88.88 0 0 0 .883.875Zm-.503-8.251a.377.377 0 0 0 .378-.375.377.377 0 0 0-.378-.375.377.377 0 0 0-.378.375c0 .207.17.375.378.375ZM15 .75a.377.377 0 0 0 .377-.375A.377.377 0 0 0 15 0a.377.377 0 0 0-.378.375c0 .207.17.375.378.375ZM1.386 8.625a.377.377 0 0 0 .378-.375.377.377 0 0 0-.378-.375.377.377 0 0 0-.378.375c0 .207.17.375.378.375Zm0 15.625a.377.377 0 0 0 .378-.374.377.377 0 0 0-.378-.375.377.377 0 0 0-.378.375c0 .207.17.375.378.375Zm27.228-.125a.377.377 0 0 0 .378-.375.377.377 0 0 0-.378-.375.377.377 0 0 0-.378.375c0 .207.17.375.378.375Z"></path><path d="M19.538 20.5c-1.007-.374-2.142.126-2.646 1-.505.876-1.513 1.375-2.647 1-.63-.126-1.008-.625-1.261-1 0-.125-.127-.25-.127-.5 0-1 .756-1.75 1.64-1.875h.25c1.765.125 3.277-1.375 3.404-3.126.127-1.75-1.386-3.25-3.152-3.375h-.378c-1.008 0-1.64-.75-1.64-1.75 0-.249 0-.5.127-.624v-.125c0-.126.127-.126.127-.25.378-1.25-.252-2.5-1.512-2.874-1.135-.375-2.52.25-2.9 1.5-.377 1.125.252 2.375 1.387 2.874.168.084.336.126.505.126h.126c.883.125 1.64.875 1.64 1.75 0 .374-.127.624-.252.875-.252.5-.505 1-.505 1.625 0 .626.127 1.375.505 1.875.126.25.251.625.251.876 0 .875-.63 1.625-1.512 1.75h-.378c-1.261.249-2.018 1.5-1.764 2.75.253 1.25 1.512 2 2.646 1.75.63-.126 1.135-.501 1.386-1 .505-.876 1.64-1.375 2.647-1 .505.126 1.008.5 1.262 1 .251.375.756.75 1.26 1 1.262.374 2.396-.25 2.9-1.5.504-1.126-.127-2.376-1.387-2.751h-.002Z"></path></g><defs><clipPath id="icon-pure-logo_a"><path fill="#fff" d="M0 0h30v32H0z"></path></clipPath></defs></svg>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# coding=utf-8
2+
"""
3+
@project: maxkb
4+
@Author:虎
5+
@file: zhipu_model_provider.py
6+
@date:2024/04/19 13:5
7+
@desc:
8+
"""
9+
import os
10+
from typing import Dict
11+
12+
from langchain.schema import HumanMessage
13+
from langchain_community.chat_models import ChatZhipuAI
14+
15+
from common import forms
16+
from common.exception.app_exception import AppApiException
17+
from common.forms import BaseForm
18+
from common.util.file_util import get_file_content
19+
from setting.models_provider.base_model_provider import ModelProvideInfo, ModelTypeConst, BaseModelCredential, \
20+
ModelInfo, IModelProvider, ValidCode
21+
from smartdoc.conf import PROJECT_DIR
22+
23+
24+
class ZhiPuLLMModelCredential(BaseForm, BaseModelCredential):
25+
26+
def is_valid(self, model_type: str, model_name, model_credential: Dict[str, object], raise_exception=False):
27+
model_type_list = ZhiPuModelProvider().get_model_type_list()
28+
if not any(list(filter(lambda mt: mt.get('value') == model_type, model_type_list))):
29+
raise AppApiException(ValidCode.valid_error.value, f'{model_type} 模型类型不支持')
30+
for key in ['api_key']:
31+
if key not in model_credential:
32+
if raise_exception:
33+
raise AppApiException(ValidCode.valid_error.value, f'{key} 字段为必填字段')
34+
else:
35+
return False
36+
try:
37+
model = ZhiPuModelProvider().get_model(model_type, model_name, model_credential)
38+
model.invoke([HumanMessage(content='你好')])
39+
except Exception as e:
40+
if isinstance(e, AppApiException):
41+
raise e
42+
if raise_exception:
43+
raise AppApiException(ValidCode.valid_error.value, f'校验失败,请检查参数是否正确: {str(e)}')
44+
else:
45+
return False
46+
return True
47+
48+
def encryption_dict(self, model: Dict[str, object]):
49+
return {**model, 'api_key': super().encryption(model.get('api_key', ''))}
50+
51+
api_key = forms.PasswordInputField('API Key', required=True)
52+
53+
54+
qwen_model_credential = ZhiPuLLMModelCredential()
55+
56+
model_dict = {
57+
'glm-4': ModelInfo('glm-4', '', ModelTypeConst.LLM, qwen_model_credential),
58+
'glm-4v': ModelInfo('glm-4v', '', ModelTypeConst.LLM, qwen_model_credential),
59+
'glm-3-turbo': ModelInfo('glm-3-turbo', '', ModelTypeConst.LLM, qwen_model_credential)
60+
}
61+
62+
63+
class ZhiPuModelProvider(IModelProvider):
64+
65+
def get_dialogue_number(self):
66+
return 3
67+
68+
def get_model(self, model_type, model_name, model_credential: Dict[str, object], **model_kwargs) -> ChatZhipuAI:
69+
zhipuai_chat = ChatZhipuAI(
70+
temperature=0.5,
71+
api_key=model_credential.get('api_key'),
72+
model=model_name
73+
)
74+
return zhipuai_chat
75+
76+
def get_model_credential(self, model_type, model_name):
77+
if model_name in model_dict:
78+
return model_dict.get(model_name).model_credential
79+
return qwen_model_credential
80+
81+
def get_model_provide_info(self):
82+
return ModelProvideInfo(provider='model_zhipu_provider', name='智谱AI', icon=get_file_content(
83+
os.path.join(PROJECT_DIR, "apps", "setting", 'models_provider', 'impl', 'zhipu_model_provider', 'icon',
84+
'zhipuai_icon_svg')))
85+
86+
def get_model_list(self, model_type: str):
87+
if model_type is None:
88+
raise AppApiException(500, '模型类型不能为空')
89+
return [model_dict.get(key).to_dict() for key in
90+
list(filter(lambda key: model_dict.get(key).model_type == model_type, model_dict.keys()))]
91+
92+
def get_model_type_list(self):
93+
return [{'key': "大语言模型", 'value': "LLM"}]

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ pymupdf = "1.24.1"
3333
python-docx = "^1.1.0"
3434
xlwt = "^1.3.0"
3535
dashscope = "^1.17.0"
36+
zhipuai = "^2.0.1"
37+
httpx = "^0.27.0"
38+
httpx-sse = "^0.4.0"
3639

3740
[build-system]
3841
requires = ["poetry-core"]

0 commit comments

Comments
 (0)