From e247e7832d2b2ae038deda2905021af963275da0 Mon Sep 17 00:00:00 2001 From: weiensong Date: Fri, 17 Nov 2023 11:13:25 +0800 Subject: [PATCH 1/2] feat: model name log, args help hint and the proxy of key does not participate in the judgment. --- constants.py | 12 ++++++++++++ func_chatglm.py | 5 ++++- func_chatgpt.py | 5 ++++- func_tigerbot.py | 5 ++++- func_xinghuo_web.py | 5 ++++- main.py | 3 ++- robot.py | 38 ++++++++++++++++++++------------------ 7 files changed, 50 insertions(+), 23 deletions(-) diff --git a/constants.py b/constants.py index 1e2fb6c..c373ca3 100644 --- a/constants.py +++ b/constants.py @@ -8,3 +8,15 @@ class ChatType(IntEnum): CHATGPT = 2 # ChatGPT XINGHUO_WEB = 3 # 讯飞星火 ChatGLM = 4 # ChatGLM + + @staticmethod + def is_in_chat_types(chat_type: int) -> bool: + if chat_type in [ChatType.TIGER_BOT.value, ChatType.CHATGPT.value, + ChatType.XINGHUO_WEB.value, ChatType.ChatGLM.value]: + return True + return False + + @staticmethod + def help_hint(): + return str({member.value: member.name for member in ChatType}).replace('{', '').replace('}', '') + diff --git a/func_chatglm.py b/func_chatglm.py index 9fe57f3..5d4001e 100644 --- a/func_chatglm.py +++ b/func_chatglm.py @@ -16,7 +16,7 @@ from chatglm.tool_registry import dispatch_tool, extract_code, get_tools functions = get_tools() -class ChatGLM(): +class ChatGLM: def __init__(self, config={}, wcf: Optional[Wcf] = None, max_retry=5) -> None: openai.api_key = config.get('key', 'XXX') @@ -35,6 +35,9 @@ class ChatGLM(): "tool": [{"role": "system", "content": "Answer the following questions as best as you can. You have access to the following tools:"}], "code": [{"role": "system", "content": "你是一位智能AI助手,你叫ChatGLM,你连接着一台电脑,但请注意不能联网。在使用Python解决任务时,你可以运行代码并得到结果,如果运行结果有错误,你需要尽可能对代码进行改进。你可以处理用户上传到电脑上的文件,文件默认存储路径是{}。".format(self.filePath)}]} + def __repr__(self): + return 'ChatGLM' + def get_answer(self, question: str, wxid: str) -> str: # wxid或者roomid,个人时为微信id,群消息时为群id if '#帮助' == question: diff --git a/func_chatgpt.py b/func_chatgpt.py index e7eb8bb..9fb6b55 100644 --- a/func_chatgpt.py +++ b/func_chatgpt.py @@ -6,7 +6,7 @@ from datetime import datetime import openai -class ChatGPT(): +class ChatGPT: def __init__(self, key: str, api: str, proxy: str, prompt: str) -> None: openai.api_key = key @@ -17,6 +17,9 @@ class ChatGPT(): self.conversation_list = {} self.system_content_msg = {"role": "system", "content": prompt} + def __repr__(self): + return 'ChatGPT' + def get_answer(self, question: str, wxid: str) -> str: # wxid或者roomid,个人时为微信id,群消息时为群id self.updateMessage(wxid, question, "user") diff --git a/func_tigerbot.py b/func_tigerbot.py index 3e82665..320bfd5 100644 --- a/func_tigerbot.py +++ b/func_tigerbot.py @@ -7,7 +7,7 @@ import requests from random import randint -class TigerBot(): +class TigerBot: def __init__(self, tbconf=None) -> None: self.LOG = logging.getLogger(__file__) self.tburl = "https://api.tigerbot.com/bot-service/ai_service/gpt" @@ -15,6 +15,9 @@ class TigerBot(): self.tbmodel = tbconf["model"] self.fallback = ["滚", "快滚", "赶紧滚"] + def __repr__(self): + return 'TigerBot' + def get_answer(self, msg: str, sender: str = None) -> str: payload = { "text": msg, diff --git a/func_xinghuo_web.py b/func_xinghuo_web.py index 266eadf..d2a2b1f 100644 --- a/func_xinghuo_web.py +++ b/func_xinghuo_web.py @@ -3,7 +3,7 @@ from sparkdesk_web.core import SparkWeb -class XinghuoWeb(): +class XinghuoWeb: def __init__(self, xhconf=None) -> None: self._sparkWeb = SparkWeb( @@ -16,6 +16,9 @@ class XinghuoWeb(): if xhconf["prompt"]: self._chat.chat(xhconf["prompt"]) + def __repr__(self): + return 'XinghuoWeb' + def get_answer(self, msg: str, sender: str = None) -> str: answer = self._chat.chat(msg) return answer diff --git a/main.py b/main.py index 0a21679..946842c 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,7 @@ from wcferry import Wcf from configuration import Config from func_report_reminder import ReportReminder from robot import Robot +from constants import ChatType def weather_report(robot: Robot) -> None: @@ -61,6 +62,6 @@ def main(chat_type: int): if __name__ == "__main__": parser = ArgumentParser() - parser.add_argument('-c', type=int, default=0, help='选择模型参数: 1, 2, 3, 4') + parser.add_argument('-c', type=int, default=0, help=f'选择模型参数序号: {ChatType.help_hint()}') args = parser.parse_args().c main(args) diff --git a/robot.py b/robot.py index 8142f7d..9e0b29a 100644 --- a/robot.py +++ b/robot.py @@ -31,37 +31,39 @@ class Robot(Job): self.wxid = self.wcf.get_self_wxid() self.allContacts = self.getAllContacts() - if chat_type == ChatType.UnKnown.value: - if all(value is not None for value in self.config.TIGERBOT.values()): + if ChatType.is_in_chat_types(chat_type): + if chat_type == ChatType.TIGER_BOT.value and self.value_check(self.config.TIGERBOT.values()): self.chat = TigerBot(self.config.TIGERBOT) - elif all(value is not None for value in self.config.CHATGPT.values()): + elif chat_type == ChatType.CHATGPT.value and self.value_check(self.config.CHATGPT.values()): cgpt = self.config.CHATGPT self.chat = ChatGPT(cgpt.get("key"), cgpt.get("api"), cgpt.get("proxy"), cgpt.get("prompt")) - elif all(value is not None for value in self.config.XINGHUO_WEB.values()): + elif chat_type == ChatType.XINGHUO_WEB.value and self.value_check(self.config.XINGHUO_WEB.values()): self.chat = XinghuoWeb(self.config.XINGHUO_WEB) - elif all(value is not None for value in self.config.CHATGLM.values()): + elif chat_type == ChatType.CHATGLM.value and self.value_check(self.config.CHATGLM.values()): self.chat = ChatGLM(self.config.CHATGLM) else: self.LOG.warning('未配置模型') self.chat = None else: - if chat_type == ChatType.TIGER_BOT.value and all( - value is not None for value in self.config.TIGERBOT.values()): + if self.value_check(self.config.TIGERBOT.values()): self.chat = TigerBot(self.config.TIGERBOT) - elif chat_type == ChatType.CHATGPT.value and all( - value is not None for value in self.config.CHATGPT.values()): + elif self.value_check(self.config.CHATGPT.values()): cgpt = self.config.CHATGPT self.chat = ChatGPT(cgpt.get("key"), cgpt.get("api"), cgpt.get("proxy"), cgpt.get("prompt")) - elif chat_type == ChatType.XINGHUO_WEB.value and all( - value is not None for value in self.config.XINGHUO_WEB.values()): + elif self.value_check(self.config.XINGHUO_WEB.values()): self.chat = XinghuoWeb(self.config.XINGHUO_WEB) - elif chat_type == ChatType.CHATGLM.value and all( - value is not None for value in self.config.CHATGLM.values()): + elif self.value_check(self.config.CHATGLM.values()): self.chat = ChatGLM(self.config.CHATGLM) else: self.LOG.warning('未配置模型') self.chat = None + self.LOG.info(f'已选择: {self.chat}') + + @staticmethod + def value_check(args: dict) -> bool: + return all(value is not None for key, value in args.items() if key != 'proxy') + def toAt(self, msg: WxMsg) -> bool: """处理被 @ 消息 :param msg: 微信消息结构 @@ -131,22 +133,22 @@ class Robot(Job): if msg.roomid not in self.config.GROUPS: # 不在配置的响应的群列表里,忽略 return - if msg.is_at(self.wxid): # 被@ + if msg.is_at(self.wxid): # 被@ self.toAt(msg) - else: # 其他消息 + else: # 其他消息 self.toChengyu(msg) return # 处理完群聊信息,后面就不需要处理了 # 非群聊信息,按消息类型进行处理 - if msg.type == 37: # 好友请求 + if msg.type == 37: # 好友请求 self.autoAcceptFriendRequest(msg) elif msg.type == 10000: # 系统信息 self.sayHiToNewFriend(msg) - elif msg.type == 0x01: # 文本消息 + elif msg.type == 0x01: # 文本消息 # 让配置加载更灵活,自己可以更新配置。也可以利用定时任务更新。 if msg.from_self(): if msg.content == "^更新$": @@ -213,7 +215,7 @@ class Robot(Job): 格式: {"wxid": "NickName"} """ contacts = self.wcf.query_sql("MicroMsg.db", "SELECT UserName, NickName FROM Contact;") - return {contact["UserName"]: contact["NickName"]for contact in contacts} + return {contact["UserName"]: contact["NickName"] for contact in contacts} def keepRunningAndBlockProcess(self) -> None: """ From d3f715271047e0adc27368bb44e970ffbc00ceb1 Mon Sep 17 00:00:00 2001 From: weiensong Date: Fri, 17 Nov 2023 11:47:49 +0800 Subject: [PATCH 2/2] fix: constants name --- constants.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/constants.py b/constants.py index c373ca3..2c76914 100644 --- a/constants.py +++ b/constants.py @@ -3,20 +3,20 @@ from enum import IntEnum, unique @unique class ChatType(IntEnum): - UnKnown = 0 # 未知, 即未设置 + # UnKnown = 0 # 未知, 即未设置 TIGER_BOT = 1 # TigerBot CHATGPT = 2 # ChatGPT XINGHUO_WEB = 3 # 讯飞星火 - ChatGLM = 4 # ChatGLM + CHATGLM = 4 # ChatGLM @staticmethod def is_in_chat_types(chat_type: int) -> bool: if chat_type in [ChatType.TIGER_BOT.value, ChatType.CHATGPT.value, - ChatType.XINGHUO_WEB.value, ChatType.ChatGLM.value]: + ChatType.XINGHUO_WEB.value, ChatType.CHATGLM.value]: return True return False @staticmethod - def help_hint(): + def help_hint() -> str: return str({member.value: member.name for member in ChatType}).replace('{', '').replace('}', '')