Skip to content

Commit

Permalink
Merge pull request lich0821#1 from yudong521/develop
Browse files Browse the repository at this point in the history
适配智谱
  • Loading branch information
yudong521 authored Feb 19, 2024
2 parents 27277c9 + 8c564fc commit 36f1f9e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
48 changes: 48 additions & 0 deletions base/func_zhipu.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions config.yaml.template
Original file line number Diff line number Diff line change
Expand Up @@ -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 # 模型类型
1 change: 1 addition & 0 deletions configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ def reload(self) -> None:
self.XINGHUO_WEB = yconfig.get("xinghuo_web", {})
self.CHATGLM = yconfig.get("chatglm", {})
self.BardAssistant = yconfig.get("bard", {})
self.ZhiPu = yconfig.get("zhipu", {})
1 change: 1 addition & 0 deletions constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions robot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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

Expand Down Expand Up @@ -45,6 +46,8 @@ def __init__(self, config: Config, wcf: Wcf, chat_type: int) -> None:
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
Expand All @@ -59,6 +62,8 @@ def __init__(self, config: Config, wcf: Wcf, chat_type: int) -> None:
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
Expand Down

0 comments on commit 36f1f9e

Please sign in to comment.