Merge pull request #21 from weiensong/master

feat: 兼容旧版的动态选择模型
This commit is contained in:
Changhua
2023-11-16 07:59:26 -06:00
committed by GitHub
5 changed files with 92 additions and 43 deletions

View File

@@ -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` 是运行程序后自动从模板复制过来的,以下功能默认关闭。

View File

@@ -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,36 +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 # 根据需要对角色进行设定
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 #设定生成图片和代码使用的文件夹路径
chatglm: # -----chatglm配置这行不填-----
key: # key 暂时没有
api: http://localhost:8000/v1 # 根据自己的chatglm地址修改
proxy: # 如果你在国内你可能需要魔法大概长这样http://域名或者IP地址:端口号
prompt: 你是智能聊天机器人,你叫小薇 # 根据需要对角色进行设定
file_path: F:/Pictures/temp #设定生成图片和代码使用的文件夹路径
tigerbot:
key: key
model: tigerbot-7b-sft
tigerbot: # -----tigerbot配置这行不填-----
key: # key
model: # tigerbot-7b-sft
# 使用讯飞星火web模式api需要抓取以下参数
# 抓取方式详见文档https://www.bilibili.com/read/cv27066577
# xinghuo_web:
# cookie: cookie
# fd: fd
# GtToken: GtToken
# prompt: 你是智能聊天机器人你叫wcferry。请用这个角色回答我的问题 # 根据需要对角色进行设定
xinghuo_web: # -----讯飞星火web模式api配置这行不填 抓取方式详见文档https://www.bilibili.com/read/cv27066577-----
cookie: # cookie
fd: # fd
GtToken: # GtToken
prompt: 你是智能聊天机器人你叫wcferry。请用这个角色回答我的问题 # 根据需要对角色进行设定

10
constants.py Normal file
View File

@@ -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

10
main.py
View File

@@ -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("正在启动机器人···")
# 机器人启动发送测试消息
@@ -59,4 +60,7 @@ def main():
if __name__ == "__main__":
main()
parser = ArgumentParser()
parser.add_argument('-c', type=int, default=0, help='选择模型参数: 1, 2, 3, 4')
args = parser.parse_args().c
main(args)

View File

@@ -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:
"""处理被 @ 消息