Reformat codes
This commit is contained in:
@@ -1,21 +1,22 @@
|
|||||||
from zhipuai import ZhipuAI
|
from zhipuai import ZhipuAI
|
||||||
|
|
||||||
|
|
||||||
class ZhiPu():
|
class ZhiPu():
|
||||||
def __init__(self, conf: dict) -> None:
|
def __init__(self, conf: dict) -> None:
|
||||||
self.api_key = conf.get("api_key")
|
self.api_key = conf.get("api_key")
|
||||||
self.model = conf.get("model", "glm-4") # 默认使用 glm-4 模型
|
self.model = conf.get("model", "glm-4") # 默认使用 glm-4 模型
|
||||||
self.client = ZhipuAI(api_key=self.api_key)
|
self.client = ZhipuAI(api_key=self.api_key)
|
||||||
self.converstion_list = {}
|
self.converstion_list = {}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def value_check(conf: dict) -> bool:
|
def value_check(conf: dict) -> bool:
|
||||||
if conf and conf.get("api_key") :
|
if conf and conf.get("api_key"):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return 'ZhiPu'
|
return 'ZhiPu'
|
||||||
|
|
||||||
def get_answer(self, msg: str, wxid: str, **args) -> str:
|
def get_answer(self, msg: str, wxid: str, **args) -> str:
|
||||||
self._update_message(wxid, str(msg), "user")
|
self._update_message(wxid, str(msg), "user")
|
||||||
response = self.client.chat.completions.create(
|
response = self.client.chat.completions.create(
|
||||||
@@ -26,13 +27,14 @@ class ZhiPu():
|
|||||||
answer = resp_msg.content
|
answer = resp_msg.content
|
||||||
self._update_message(wxid, answer, "assistant")
|
self._update_message(wxid, answer, "assistant")
|
||||||
return answer
|
return answer
|
||||||
|
|
||||||
def _update_message(self, wxid: str, msg: str, role: str) -> None:
|
def _update_message(self, wxid: str, msg: str, role: str) -> None:
|
||||||
if wxid not in self.converstion_list.keys():
|
if wxid not in self.converstion_list.keys():
|
||||||
self.converstion_list[wxid] = []
|
self.converstion_list[wxid] = []
|
||||||
content = {"role": role, "content": str(msg)}
|
content = {"role": role, "content": str(msg)}
|
||||||
self.converstion_list[wxid].append(content)
|
self.converstion_list[wxid].append(content)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
from configuration import Config
|
from configuration import Config
|
||||||
config = Config().ZHIPU
|
config = Config().ZHIPU
|
||||||
@@ -41,4 +43,4 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
zhipu = ZhiPu(config)
|
zhipu = ZhiPu(config)
|
||||||
rsp = zhipu.get_answer("你好")
|
rsp = zhipu.get_answer("你好")
|
||||||
print(rsp)
|
print(rsp)
|
||||||
|
|||||||
Reference in New Issue
Block a user