From 4477267744aa11eb335a2209a4dfadd764223530 Mon Sep 17 00:00:00 2001 From: touer angler Date: Thu, 16 Nov 2023 16:25:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E5=85=BC=E5=AE=B9=E6=97=A7?= =?UTF-8?q?=E7=89=88=E7=9A=84=E5=8A=A8=E6=80=81=E9=80=89=E6=8B=A9=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.MD | 22 ++++++++++++++++++++-- config.yaml.template | 42 ++++++++++++++++++++---------------------- constants.py | 10 ++++++++++ main.py | 10 +++++++--- robot.py | 42 +++++++++++++++++++++++++++++++----------- 5 files changed, 88 insertions(+), 38 deletions(-) create mode 100644 constants.py diff --git a/README.MD b/README.MD index e1681ad..dd75fa0 100644 --- a/README.MD +++ b/README.MD @@ -31,12 +31,30 @@ pip install -r requirements.txt 4. 运行 -直接运行程序会自动拉起微信: +我们需要运行两次main.py第一次是生成配置文件config.yaml, 第二次是真正跑你的机器人。 +直接运行程序会自动拉起微信,如果微信未打开,会自动打开微信;如果版本不对,也会有提示;其他报错,请进群交流。 + +下面代码为第一次运行: 第一次运行main.py会在WeChatRobot目录下生成一个config.yaml文件,参照修改配置进行修改。 +其中chatgpt、tigerbot和xinghuo_web是三种模型的配置信息, 你需要配置它们的参数, 不知道的可以加群交流。 ```sh python main.py -# 如果微信未打开,会自动打开微信;如果版本不对,也会有提示;其他报错,请进群交流。 ``` + +下面代码为第二次运行: 你可以通过命令行参数选择模型,默认是不选择, 这样你配置了什么参数就跑什么模型。正因如此你需要配置前面所说三种模型中的至少一种(当然也可以都配置, 想跑那个模型就选什么参数), 然后就可以开始使用你的机器人了。 +```sh +python main.py +``` +如果你配置了多个模型,下面的内容才对你有帮助否则略过, 通过参数可以选择要跑的模型。 +```sh +# 例: 我想运行选择chatgpt的机器人 +python main.py -c 2 +``` +> python main.py -c C 其中参数C可选择如下所示 +>> 1. tigerbot模型 +>> 2. chatgpt模型 +>> 3. 讯飞星火模型 +>> 4. chatglm模型 ### 修改配置 配置文件 `config.yaml` 是运行程序后自动从模板复制过来的,以下功能默认关闭。 diff --git a/config.yaml.template b/config.yaml.template index 4520880..1e94a5c 100644 --- a/config.yaml.template +++ b/config.yaml.template @@ -47,28 +47,26 @@ news: report_reminder: receivers: [] # 定时日报周报月报提醒(roomid 或者 wxid) -# 如果要使用 ChatGPT,取消下面的注释并填写相关内容 -# chatgpt: -# key: 填写你 ChatGPT 的 key -# api: https://api.openai.com/v1 # 如果你不知道这是干嘛的,就不要改 -# proxy: # 如果你在国内,你可能需要魔法,大概长这样:http://域名或者IP地址:端口号 -# prompt: 你是智能聊天机器人,你叫wcferry # 根据需要对角色进行设定 -#chatglm: -# key: key #暂时没有 -# api: http://localhost:8000/v1 # 根据自己的chatglm地址修改 -# proxy: # 如果你在国内,你可能需要魔法,大概长这样:http://域名或者IP地址:端口号 -# prompt: 你是智能聊天机器人,你叫小薇 # 根据需要对角色进行设定 -# file_path: F:/Pictures/temp #设定生成图片和代码使用的文件夹路径 +chatgpt: # -----chatgpt配置这行不填----- + key: # 填写你 ChatGPT 的 key + api: https://api.openai.com/v1 # 如果你不知道这是干嘛的,就不要改 + proxy: # 如果你在国内,你可能需要魔法,大概长这样:http://域名或者IP地址:端口号 + prompt: # 根据需要对角色进行设定 你是智能聊天机器人,你叫wcferry -tigerbot: - key: key - model: tigerbot-7b-sft +chatglm: # -----chatglm配置这行不填----- + key: # key 暂时没有 + api: http://localhost:8000/v1 # 根据自己的chatglm地址修改 + proxy: # 如果你在国内,你可能需要魔法,大概长这样:http://域名或者IP地址:端口号 + prompt: # 根据需要对角色进行设定 你是智能聊天机器人,你叫小薇 + file_path: F:/Pictures/temp #设定生成图片和代码使用的文件夹路径 -# 使用讯飞星火web模式api需要抓取以下参数 -# 抓取方式详见文档:https://www.bilibili.com/read/cv27066577 -# xinghuo_web: -# cookie: cookie -# fd: fd -# GtToken: GtToken -# prompt: 你是智能聊天机器人,你叫wcferry。请用这个角色回答我的问题 # 根据需要对角色进行设定 +tigerbot: # -----tigerbot配置这行不填----- + key: # key + model: # tigerbot-7b-sft + +xinghuo_web: # -----讯飞星火web模式api配置这行不填 抓取方式详见文档:https://www.bilibili.com/read/cv27066577----- + cookie: # cookie + fd: # fd + GtToken: # GtToken + prompt: # 根据需要对角色进行设定你是智能聊天机器人,你叫wcferry。请用这个角色回答我的问题 diff --git a/constants.py b/constants.py new file mode 100644 index 0000000..1e2fb6c --- /dev/null +++ b/constants.py @@ -0,0 +1,10 @@ +from enum import IntEnum, unique + + +@unique +class ChatType(IntEnum): + UnKnown = 0 # 未知, 即未设置 + TIGER_BOT = 1 # TigerBot + CHATGPT = 2 # ChatGPT + XINGHUO_WEB = 3 # 讯飞星火 + ChatGLM = 4 # ChatGLM diff --git a/main.py b/main.py index 5e3288d..944a39d 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- import signal +from argparse import ArgumentParser from wcferry import Wcf @@ -25,7 +26,7 @@ def weather_report(robot: Robot) -> None: # robot.sendTextMsg(report, r, "nofity@all") # 发送消息并@所有人 -def main(): +def main(chat_type: int): config = Config() wcf = Wcf(debug=True) @@ -35,7 +36,7 @@ def main(): signal.signal(signal.SIGINT, handler) - robot = Robot(config, wcf) + robot = Robot(config, wcf, chat_type) robot.LOG.info("正在启动机器人···") # 机器人启动发送测试消息 @@ -58,5 +59,8 @@ def main(): robot.keepRunningAndBlockProcess() +parser = ArgumentParser() +parser.add_argument('-c', type=int, default=0, help='选择模型参数: 1, 2, 3, 4') if __name__ == "__main__": - main() + args = parser.parse_args().c + main(args) diff --git a/robot.py b/robot.py index 2447807..8142f7d 100644 --- a/robot.py +++ b/robot.py @@ -9,6 +9,7 @@ from threading import Thread from wcferry import Wcf, WxMsg +from constants import ChatType from configuration import Config from func_chatgpt import ChatGPT from func_chatglm import ChatGLM @@ -23,24 +24,43 @@ class Robot(Job): """个性化自己的机器人 """ - def __init__(self, config: Config, wcf: Wcf) -> None: + def __init__(self, config: Config, wcf: Wcf, chat_type: int) -> None: self.wcf = wcf self.config = config self.LOG = logging.getLogger("Robot") self.wxid = self.wcf.get_self_wxid() self.allContacts = self.getAllContacts() - if self.config.TIGERBOT: - self.chat = TigerBot(self.config.TIGERBOT) - elif self.config.CHATGPT: - cgpt = self.config.CHATGPT - self.chat = ChatGPT(cgpt.get("key"), cgpt.get("api"), cgpt.get("proxy"), cgpt.get("prompt")) - elif self.config.XINGHUO_WEB: - self.chat = XinghuoWeb(self.config.XINGHUO_WEB) - elif self.config.CHATGLM: - self.chat = ChatGLM(self.config.CHATGLM,wcf) + if chat_type == ChatType.UnKnown.value: + if all(value is not None for value in self.config.TIGERBOT.values()): + self.chat = TigerBot(self.config.TIGERBOT) + elif all(value is not None for value in 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()): + self.chat = XinghuoWeb(self.config.XINGHUO_WEB) + elif all(value is not None for value in self.config.CHATGLM.values()): + self.chat = ChatGLM(self.config.CHATGLM) + else: + self.LOG.warning('未配置模型') + self.chat = None else: - self.chat = None + if chat_type == ChatType.TIGER_BOT.value and all( + value is not None for value in 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()): + 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()): + 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()): + self.chat = ChatGLM(self.config.CHATGLM) + else: + self.LOG.warning('未配置模型') + self.chat = None def toAt(self, msg: WxMsg) -> bool: """处理被 @ 消息 From dfdc3f83a248c83deafe3f0a89a8e7c04a744115 Mon Sep 17 00:00:00 2001 From: touer angler <61647484+weiensong@users.noreply.github.com> Date: Thu, 16 Nov 2023 21:57:12 +0800 Subject: [PATCH 2/2] fix: default role and yaml formatted with chatgpt --- config.yaml.template | 25 ++++++++++++------------- main.py | 4 ++-- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/config.yaml.template b/config.yaml.template index 1e94a5c..364cf3d 100644 --- a/config.yaml.template +++ b/config.yaml.template @@ -21,7 +21,7 @@ logging: level: INFO formatter: simple filename: wx_info.log - maxBytes: 10485760 # 10MB + maxBytes: 10485760 # 10MB backupCount: 20 encoding: utf8 @@ -30,7 +30,7 @@ logging: level: ERROR formatter: error filename: wx_error.log - maxBytes: 10485760 # 10MB + maxBytes: 10485760 # 10MB backupCount: 20 encoding: utf8 @@ -39,34 +39,33 @@ logging: handlers: [console, info_file_handler, error_file_handler] groups: - enable: [] # 允许响应的群 roomId,大概长这样:2xxxxxxxxx3@chatroom + enable: [] # 允许响应的群 roomId,大概长这样:2xxxxxxxxx3@chatroom news: - receivers: [] # 定时新闻接收人(roomid 或者 wxid) + receivers: [] # 定时新闻接收人(roomid 或者 wxid) report_reminder: - receivers: [] # 定时日报周报月报提醒(roomid 或者 wxid) - + receivers: [] # 定时日报周报月报提醒(roomid 或者 wxid) chatgpt: # -----chatgpt配置这行不填----- key: # 填写你 ChatGPT 的 key - api: https://api.openai.com/v1 # 如果你不知道这是干嘛的,就不要改 - proxy: # 如果你在国内,你可能需要魔法,大概长这样:http://域名或者IP地址:端口号 - prompt: # 根据需要对角色进行设定 你是智能聊天机器人,你叫wcferry + api: https://api.openai.com/v1 # 如果你不知道这是干嘛的,就不要改 + proxy: # 如果你在国内,你可能需要魔法,大概长这样:http://域名或者IP地址:端口号 + prompt: 你是智能聊天机器人,你叫wcferry # 根据需要对角色进行设定 chatglm: # -----chatglm配置这行不填----- key: # key 暂时没有 - api: http://localhost:8000/v1 # 根据自己的chatglm地址修改 + api: http://localhost:8000/v1 # 根据自己的chatglm地址修改 proxy: # 如果你在国内,你可能需要魔法,大概长这样:http://域名或者IP地址:端口号 - prompt: # 根据需要对角色进行设定 你是智能聊天机器人,你叫小薇 + prompt: 你是智能聊天机器人,你叫小薇 # 根据需要对角色进行设定 file_path: F:/Pictures/temp #设定生成图片和代码使用的文件夹路径 tigerbot: # -----tigerbot配置这行不填----- key: # key model: # tigerbot-7b-sft -xinghuo_web: # -----讯飞星火web模式api配置这行不填 抓取方式详见文档:https://www.bilibili.com/read/cv27066577----- +xinghuo_web: # -----讯飞星火web模式api配置这行不填 抓取方式详见文档:https://www.bilibili.com/read/cv27066577----- cookie: # cookie fd: # fd GtToken: # GtToken - prompt: # 根据需要对角色进行设定你是智能聊天机器人,你叫wcferry。请用这个角色回答我的问题 + prompt: 你是智能聊天机器人,你叫wcferry。请用这个角色回答我的问题 # 根据需要对角色进行设定 diff --git a/main.py b/main.py index 944a39d..0a21679 100644 --- a/main.py +++ b/main.py @@ -59,8 +59,8 @@ def main(chat_type: int): robot.keepRunningAndBlockProcess() -parser = ArgumentParser() -parser.add_argument('-c', type=int, default=0, help='选择模型参数: 1, 2, 3, 4') if __name__ == "__main__": + parser = ArgumentParser() + parser.add_argument('-c', type=int, default=0, help='选择模型参数: 1, 2, 3, 4') args = parser.parse_args().c main(args)