From 388edcd06256be5ecee993107f376b8bffcb974c Mon Sep 17 00:00:00 2001 From: Changhua Date: Sat, 24 Feb 2024 11:16:46 +0800 Subject: [PATCH] Reformat codes --- base/func_zhipu.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/base/func_zhipu.py b/base/func_zhipu.py index 75d1a3f..2eba7c0 100644 --- a/base/func_zhipu.py +++ b/base/func_zhipu.py @@ -1,21 +1,22 @@ from zhipuai import ZhipuAI + class ZhiPu(): def __init__(self, conf: dict) -> None: 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.converstion_list = {} - + @staticmethod def value_check(conf: dict) -> bool: - if conf and conf.get("api_key") : - return True + if conf and 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( @@ -26,13 +27,14 @@ class ZhiPu(): 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 @@ -41,4 +43,4 @@ if __name__ == "__main__": zhipu = ZhiPu(config) rsp = zhipu.get_answer("你好") - print(rsp) \ No newline at end of file + print(rsp)