适配智谱

This commit is contained in:
yudong
2024-02-19 22:23:33 +08:00
parent 27277c97d2
commit 8c564fcb60
5 changed files with 59 additions and 0 deletions

48
base/func_zhipu.py Normal file
View File

@@ -0,0 +1,48 @@
from pyexpat import model
from zhipuai import ZhipuAI
class ZhiPu():
def __init__(self, conf: dict) -> None:
api_key = conf.get("api_key")
model = conf.get("model", "glm-4") # 默认使用 glm-4 模型
self.api_key = api_key
self.model = model
self.client = ZhipuAI(api_key=api_key)
self.converstion_list = {}
@staticmethod
def value_check(conf: dict) -> bool:
if conf:
if conf.get("api_key") :
return True
return False
def __repr__(self):
return 'ZhiPu'
def get_answer(self, msg: str, wxid: str, **args) -> str:
self._update_message(wxid, str(msg), "user")
response = self.client.chat.completions.create(
model=self.model,
messages=self.converstion_list[wxid]
)
resp_msg = response.choices[0].message
answer = resp_msg.content
self._update_message(wxid, answer, "assistant")
return answer
def _update_message(self, wxid: str, msg: str, role: str) -> None:
if wxid not in self.converstion_list.keys():
self.converstion_list[wxid] = []
content = {"role": role, "content": str(msg)}
self.converstion_list[wxid].append(content)
if __name__ == "__main__":
from configuration import Config
config = Config().ZHIPU
if not config:
exit(0)
zhipu = ZhiPu(config)
rsp = zhipu.get_answer("你好")
print(rsp)

View File

@@ -78,3 +78,7 @@ bard: # -----bard配置这行不填-----
# 提示词尽可能用英文bard对中文提示词的效果不是很理想下方提示词为英语老师的示例请按实际需要修改,默认设置的提示词为谷歌创造的AI大语言模型
# I want you to act as a spoken English teacher and improver. I will speak to you in English and you will reply to me in English to practice my spoken English. I want you to keep your reply neat, limiting the reply to 100 words. I want you to strictly correct my grammar mistakes, typos, and factual errors. I want you to ask me a question in your reply. Now let's start practicing, you could ask me a question first. Remember, I want you to strictly correct my grammar mistakes, typos, and factual errors.
prompt: You am a large language model, trained by Google.
zhipu: # -----zhipu配置这行不填-----
api_key: #api key
model: glm-4 # 模型类型

View File

@@ -36,3 +36,4 @@ class Config(object):
self.XINGHUO_WEB = yconfig.get("xinghuo_web", {})
self.CHATGLM = yconfig.get("chatglm", {})
self.BardAssistant = yconfig.get("bard", {})
self.ZhiPu = yconfig.get("zhipu", {})

View File

@@ -9,6 +9,7 @@ class ChatType(IntEnum):
XINGHUO_WEB = 3 # 讯飞星火
CHATGLM = 4 # ChatGLM
BardAssistant = 5 # Google Bard
ZhiPu = 6 # ZhiPu
@staticmethod
def is_in_chat_types(chat_type: int) -> bool:

View File

@@ -6,6 +6,7 @@ import time
import xml.etree.ElementTree as ET
from queue import Empty
from threading import Thread
from base.func_zhipu import ZhiPu
from wcferry import Wcf, WxMsg
@@ -45,6 +46,8 @@ class Robot(Job):
self.chat = ChatGLM(self.config.CHATGLM)
elif chat_type == ChatType.BardAssistant.value and BardAssistant.value_check(self.config.BardAssistant):
self.chat = BardAssistant(self.config.BardAssistant)
elif chat_type == ChatType.ZhiPu.value and ZhiPu.value_check(self.config.ZHIPU):
self.chat = ZhiPu(self.config.ZHIPU)
else:
self.LOG.warning("未配置模型")
self.chat = None
@@ -59,6 +62,8 @@ class Robot(Job):
self.chat = ChatGLM(self.config.CHATGLM)
elif BardAssistant.value_check(self.config.BardAssistant):
self.chat = BardAssistant(self.config.BardAssistant)
elif ZhiPu.value_check(self.config.ZhiPu):
self.chat = ZhiPu(self.config.ZhiPu)
else:
self.LOG.warning("未配置模型")
self.chat = None