diff --git a/README.md b/README.md index 702d996d..c21117ff 100644 --- a/README.md +++ b/README.md @@ -5,21 +5,18 @@ **模型:** - [x] ChatGPT - - ... **应用:** - [ ] 终端 - [ ] Web - [x] 个人微信 - - [x] 公众号 + - [x] 公众号 (个人/企业) - [ ] 企业微信 - [ ] Telegram - [ ] QQ - [ ] 钉钉 - - ... - - + - [ ] 飞书 # 快速开始 @@ -39,24 +36,159 @@ cd bot-on-anything/ ### 2.配置说明 -核心配置文件为 `config.json`, +核心配置文件为 `config.json`,项目中提供了模板文件 `config-template.json` ,可以从模板复制生成最终生效的 `config.json` 文件: +```bash +cp config-template.json config.json +``` + +配置文件结构如下: + +```bash +{ + "model": { + "type" : "openai", # 选用的算法模型 + "openai": { + # openAI配置 + } + }, + "channel": { + "type": "wechat_mp", # 需要接入的应用 + "wechat": { + # 个人微信配置 + }, + "wechat_mp": { + # 公众号配置 + } + } +} +``` +配置文件在最外层分成 `model` 和 `channel` 两部分,model 部分为模型配置,其中的 `type` 指定了选用哪个模型;`channel` 部分包含了应用渠道的配置,`type` 字段指定了接入哪个应用,同时下方对应的配置块也会生效。 + +在使用时只需要更改 `model` 和 `channel` 配置块下的 `type` 字段,即可在任意模型和应用间完成切换,连接不同的通路。下面将依次介绍各个 模型 及 应用 的配置和运行过程。 ## 二、选择模型 ### 1.ChatGPT +#### 1.1 注册 OpenAI 账号 + +前往 [OpenAI注册页面](https://beta.openai.com/signup) 创建账号,参考这篇 [教程](https://www.cnblogs.com/damugua/p/16969508.html) 可以通过虚拟手机号来接收验证码。创建完账号则前往 [API管理页面](https://beta.openai.com/account/api-keys) 创建一个 API Key 并保存下来,后面需要在项目中配置这个key。 + +> 项目中使用的对话模型是 davinci,计费方式是约每 750 字 (包含请求和回复) 消耗 $0.02,图片生成是每张消耗 $0.016,账号创建有免费的 $18 额度,使用完可以更换邮箱重新注册。 + +#### 1.2 配置项说明 + +```bash +{ + "model": { + "type" : "openai", + + "openai": { + "api_key": "YOUR API KEY", + "conversation_max_tokens": 1000, + "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。" + } +} +``` ++ `api_key`:填入上面注册账号时创建的 `OpenAI API KEY` ++ `conversation_max_tokens`:表示能够记忆的上下文最大字数(一问一答为一组对话,如果累积的对话字数超出限制,就会优先移除最早的一组对话) ++ `character_desc` 配置中保存着你对机器人说的一段话,他会记住这段话并作为他的设定,你可以为他定制任何人格 + + +## 三、运行应用 + +### 1.个人微信 + +与项目 [chatgpt-on-wechat](https://github.com/zhayujie/chatgpt-on-wechat) 的使用方式相同,目前接入个人微信可能导致账号被限制,暂时不建议使用。 + +配置项说明: + +```bash +"channel": { + "type": "wechat", + + "single_chat_prefix": ["bot", "@bot"], + "single_chat_reply_prefix": "[bot] ", + "group_chat_prefix": ["@bot"], + "group_name_white_list": ["ChatGPT测试群"], + "image_create_prefix": ["画", "看", "找一张"], + + "wechat": { + } +} +``` +个人微信的配置项放在和 `type` 同级的层次,表示这些为公共配置,会复用于其他应用。配置加载时会优先使用模块内的配置,如果未找到便使用公共配置。 + +在项目根目录下执行 `python3 app.py` 即可启动程序,用手机扫码后完成登录,使用详情参考 [chatgpt-on-wechat](https://github.com/zhayujie/chatgpt-on-wechat)。 + +### 2.个人订阅号 + +#### 2.1 依赖安装 + +安装 [werobot](https://github.com/offu/WeRoBot) 依赖: + +```bash +pip3 install werobot +``` + +#### 2.2 配置 + +```bash +"channel": { + "type": "wechat_mp", + + "wechat_mp": { + "token": "YOUR TOKEN", # token值 + "port": "8088" # 程序启动监听的端口 + } +} +``` + +#### 2.1 运行程序 + +在项目目录下运行 `python3 app.py`,终端显示如下则表示已成功运行: -## 三、选择应用 +``` +[INFO][2023-02-16 01:39:53][app.py:12] - [INIT] load config: ... +[INFO][2023-02-16 01:39:53][wechat_mp_channel.py:25] - [WX_Public] Wechat Public account service start! +Bottle v0.12.23 server starting up (using AutoServer())... +Listening on http://127.0.0.1:8088/ +Hit Ctrl-C to quit. +``` -### 1.微信 +#### 2.2 设置公众号回调地址 +在 [微信公众平台](https://mp.weixin.qq.com/) 中进入个人订阅号,启用服务器配置: -### 2.公众号 +![wx_mp_config.png](docs/images/wx_mp_config.png) +- 服务器地址 (URL):在浏览器访问该URL需要能访问到服务器上运行的python程序 (默认为8088端口) +- 令牌 (Token):需和配置中的token一致 +#### 2.3 使用 +用户关注订阅号后,发送消息即可。 -## 四、运行 +> 注:用户发送消息后,微信后台会向配置的URL地址推送,但如果5s内未回复就会断开连接,同时重试3次,但往往请求openai接口不止5s。本项目中通过异步和缓存将5s超时限制优化至15s,但超出该时间仍无法正常回复。 同时每次5s连接断开时web框架会报错,待后续优化。 + +### 3.企业服务号 + +在企业服务号中,通过先异步访问openai接口,再通过客服接口主动推送用户的方式,解决了个人订阅号的15s超时问题。 + +企业服务号配置只需修改type为`wechat_mp_service`,配置块仍复用 `wechat_mp`,在基础上增加了 `app_id` 和 `app_secret` 两个配置项。 + +```bash +"channel": { + "type": "wechat_mp_service", + + "wechat_mp": { + "token": "YOUR TOKEN", # token值 + "port": "8088", # 程序启动监听的端口 + "app_id": "YOUR APP ID", # appID + "app_secret": "YOUR APP SECRET" # app secret + } +} +``` diff --git a/app.py b/app.py index fa41023b..d5e03280 100644 --- a/app.py +++ b/app.py @@ -12,7 +12,7 @@ logger.info("[INIT] load config: {}".format(config.conf())) # create channel - channel = channel_factory.create_channel(config.conf().get("channel")) + channel = channel_factory.create_channel(config.conf().get("channel").get("type")) # startup channel channel.startup() diff --git a/bridge/bridge.py b/bridge/bridge.py index bc5b2f3b..9e19251c 100644 --- a/bridge/bridge.py +++ b/bridge/bridge.py @@ -6,4 +6,4 @@ def __init__(self): pass def fetch_reply_content(self, query, context): - return model_factory.create_bot(config.conf().get("model")).reply(query, context) + return model_factory.create_bot(config.conf().get("model").get("type")).reply(query, context) diff --git a/channel/channel_factory.py b/channel/channel_factory.py index ad60c4be..0758a7a2 100644 --- a/channel/channel_factory.py +++ b/channel/channel_factory.py @@ -14,8 +14,12 @@ def create_channel(channel_type): return WechatChannel() elif channel_type == const.WECHAT_MP: - from channel.wechat.wechat_mp_channel import WechatPublicAccount - return WechatPublicAccount() + from channel.wechat.wechat_mp_channel import WechatSubsribeAccount + return WechatSubsribeAccount() + + elif channel_type == const.WECHAT_MP_SERVICE: + from channel.wechat.wechat_mp_service_channel import WechatServiceAccount + return WechatServiceAccount() else: raise RuntimeError diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index 75c3c687..ea3f3ff9 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -9,7 +9,8 @@ from channel.channel import Channel from concurrent.futures import ThreadPoolExecutor from common.log import logger -from config import conf +from common import const +from config import channel_conf_val, channel_conf import requests import io @@ -45,7 +46,7 @@ def handle(self, msg): to_user_id = msg['ToUserName'] # 接收人id other_user_id = msg['User']['UserName'] # 对手方id content = msg['Text'] - match_prefix = self.check_prefix(content, conf().get('single_chat_prefix')) + match_prefix = self.check_prefix(content, channel_conf_val(const.WECHAT, 'single_chat_prefix')) if from_user_id == other_user_id and match_prefix is not None: # 好友向自己发送消息 if match_prefix != '': @@ -53,7 +54,7 @@ def handle(self, msg): if len(str_list) == 2: content = str_list[1].strip() - img_match_prefix = self.check_prefix(content, conf().get('image_create_prefix')) + img_match_prefix = self.check_prefix(content, channel_conf_val(const.WECHAT, 'image_create_prefix')) if img_match_prefix: content = content.split(img_match_prefix, 1)[1].strip() thread_pool.submit(self._do_send_img, content, from_user_id) @@ -65,7 +66,7 @@ def handle(self, msg): str_list = content.split(match_prefix, 1) if len(str_list) == 2: content = str_list[1].strip() - img_match_prefix = self.check_prefix(content, conf().get('image_create_prefix')) + img_match_prefix = self.check_prefix(content, channel_conf_val(const.WECHAT, 'image_create_prefix')) if img_match_prefix: content = content.split(img_match_prefix, 1)[1].strip() thread_pool.submit(self._do_send_img, content, to_user_id) @@ -88,11 +89,11 @@ def handle_group(self, msg): elif len(content_list) == 2: content = content_list[1] - config = conf() - match_prefix = (msg['IsAt'] and not config.get("group_at_off", False)) or self.check_prefix(origin_content, config.get('group_chat_prefix')) \ - or self.check_contain(origin_content, config.get('group_chat_keyword')) - if ('ALL_GROUP' in config.get('group_name_white_list') or group_name in config.get('group_name_white_list') or self.check_contain(group_name, config.get('group_name_keyword_white_list'))) and match_prefix: - img_match_prefix = self.check_prefix(content, conf().get('image_create_prefix')) + match_prefix = (msg['IsAt'] and not channel_conf_val(const.WECHAT, "group_at_off", False)) or self.check_prefix(origin_content, channel_conf_val(const.WECHAT, 'group_chat_prefix')) \ + or self.check_contain(origin_content, channel_conf_val(const.WECHAT, 'group_chat_keyword')) + group_white_list = channel_conf_val(const.WECHAT, 'group_name_white_list') + if ('ALL_GROUP' in group_white_list or group_name in group_white_list or self.check_contain(group_name, channel_conf_val(const.WECHAT, 'group_name_keyword_white_list'))) and match_prefix: + img_match_prefix = self.check_prefix(content, channel_conf_val(const.WECHAT, 'image_create_prefix')) if img_match_prefix: content = content.split(img_match_prefix, 1)[1].strip() thread_pool.submit(self._do_send_img, content, group_id) @@ -111,7 +112,7 @@ def _do_send(self, query, reply_user_id): context['from_user_id'] = reply_user_id reply_text = super().build_reply_content(query, context) if reply_text: - self.send(conf().get("single_chat_reply_prefix") + reply_text, reply_user_id) + self.send(channel_conf_val(const.WECHAT, "single_chat_reply_prefix") + reply_text, reply_user_id) except Exception as e: logger.exception(e) @@ -146,7 +147,7 @@ def _do_send_group(self, query, msg): reply_text = super().build_reply_content(query, context) if reply_text: reply_text = '@' + msg['ActualNickName'] + ' ' + reply_text.strip() - self.send(conf().get("group_chat_reply_prefix", "") + reply_text, msg['User']['UserName']) + self.send(channel_conf_val(const.WECHAT, "group_chat_reply_prefix", "") + reply_text, msg['User']['UserName']) def check_prefix(self, content, prefix_list): diff --git a/channel/wechat/wechat_mp_channel.py b/channel/wechat/wechat_mp_channel.py index 6a4fac63..79a4efdb 100644 --- a/channel/wechat/wechat_mp_channel.py +++ b/channel/wechat/wechat_mp_channel.py @@ -1,12 +1,12 @@ import werobot import time -import config +from config import channel_conf from common import const from common.log import logger from channel.channel import Channel from concurrent.futures import ThreadPoolExecutor -robot = werobot.WeRoBot(token=config.fetch(const.WECHAT_MP).get('token')) +robot = werobot.WeRoBot(token=channel_conf(const.WECHAT_MP).get('token')) thread_pool = ThreadPoolExecutor(max_workers=8) cache = {} @@ -15,14 +15,15 @@ def hello_world(msg): logger.info('[WX_Public] receive public msg: {}, userId: {}'.format(msg.content, msg.source)) key = msg.content + '|' + msg.source if cache.get(key): + # request time cache.get(key)['req_times'] += 1 - return WechatPublicAccount().handle(msg) + return WechatSubsribeAccount().handle(msg) -class WechatPublicAccount(Channel): +class WechatSubsribeAccount(Channel): def startup(self): logger.info('[WX_Public] Wechat Public account service start!') - robot.config['PORT'] = config.fetch(const.WECHAT_MP).get('port') + robot.config['PORT'] = channel_conf(const.WECHAT_MP).get('port') robot.run() def handle(self, msg, count=0): diff --git a/channel/wechat/wechat_mp_service_channel.py b/channel/wechat/wechat_mp_service_channel.py new file mode 100644 index 00000000..14677246 --- /dev/null +++ b/channel/wechat/wechat_mp_service_channel.py @@ -0,0 +1,36 @@ +import werobot +from config import channel_conf +from common import const +from common.log import logger +from channel.channel import Channel +from concurrent.futures import ThreadPoolExecutor + +robot = werobot.WeRoBot(token=channel_conf(const.WECHAT_MP).get('token')) +thread_pool = ThreadPoolExecutor(max_workers=8) + +@robot.text +def hello_world(msg): + logger.info('[WX_Public] receive public msg: {}, userId: {}'.format(msg.content, msg.source)) + return WechatServiceAccount().handle(msg) + + +class WechatServiceAccount(Channel): + def startup(self): + logger.info('[WX_Public] Wechat Public account service start!') + robot.config['PORT'] = channel_conf(const.WECHAT_MP).get('port') + robot.config["APP_ID"] = "YOUR APP ID" + robot.config["APP_SECRET"] = "YOUR APP SECRET" + robot.run() + + def handle(self, msg, count=0): + context = {} + context['from_user_id'] = msg.source + thread_pool.submit(self._do_send, msg.content, context) + return "正在思考中..." + + + def _do_send(self, query, context): + reply_text = super().build_reply_content(query, context) + logger.info('[WX_Public] reply content: {}'.format(reply_text)) + client = robot.client + client.send_text_message(context['from_user_id'], reply_text) diff --git a/common/const.py b/common/const.py index 1f525c40..1ae175fc 100644 --- a/common/const.py +++ b/common/const.py @@ -1,6 +1,7 @@ # channel WECHAT = "wechat" WECHAT_MP = "wechat_mp" +WECHAT_MP_SERVICE = "wechat_mp_service" # model OPEN_AI = "openai" diff --git a/config-template.json b/config-template.json index d524472d..f6c2991e 100644 --- a/config-template.json +++ b/config-template.json @@ -1,23 +1,26 @@ { - "channel": "wechat", - "bot": "openai", - - "openai": { - "api_key": "YOUR API KEY", - "conversation_max_tokens": 1000, - "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。" + "model": { + "type" : "openai", + "openai": { + "api_key": "YOUR API KEY", + "conversation_max_tokens": 1000, + "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。" + } }, - - "wechat": { + "channel": { + "type": "wechat_mp", "single_chat_prefix": ["bot", "@bot"], "single_chat_reply_prefix": "[bot] ", "group_chat_prefix": ["@bot"], - "group_name_white_list": ["ALL_GROUP"], - "image_create_prefix": ["画", "看", "找一张"] - }, + "group_name_white_list": ["ChatGPT测试群"], + "image_create_prefix": ["画", "看", "找一张"], + + "wechat": { + }, - "wechat_mp": { - "token": "YOUR TOKEN", - "port": "8088" + "wechat_mp": { + "token": "YOUR TOKEN", + "port": "8088" + } } } diff --git a/config.py b/config.py index e203b8da..755421a5 100644 --- a/config.py +++ b/config.py @@ -28,5 +28,25 @@ def read_file(path): def conf(): return config -def fetch(model): - return config.get(model) + +def model_conf(model_type): + return config.get('model').get(model_type) + +def model_conf_val(model_type, key): + val = config.get('model').get(model_type).get(key) + if not val: + # common default config + return config.get('model').get(key) + return val + + +def channel_conf(channel_type): + return config.get('channel').get(channel_type) + + +def channel_conf_val(channel_type, key, default=None): + val = config.get('channel').get(channel_type).get(key) + if not val: + # common default config + return config.get('channel').get(key, default) + return val diff --git a/docs/images/wx_mp_config.png b/docs/images/wx_mp_config.png new file mode 100644 index 00000000..a84beadd Binary files /dev/null and b/docs/images/wx_mp_config.png differ diff --git a/model/openai/open_ai_model.py b/model/openai/open_ai_model.py index 8fbd4bf3..dd75ca2b 100644 --- a/model/openai/open_ai_model.py +++ b/model/openai/open_ai_model.py @@ -1,7 +1,7 @@ # encoding:utf-8 from model.model import Model -from config import fetch +from config import model_conf from common import const from common.log import logger import openai @@ -12,7 +12,7 @@ # OpenAI对话模型API (可用) class OpenAIModel(Model): def __init__(self): - openai.api_key = fetch(const.OPEN_AI).get('api_key') + openai.api_key = model_conf(const.OPEN_AI).get('api_key') def reply(self, query, context=None): @@ -103,7 +103,7 @@ def build_session_query(query, user_id): :param user_id: from user id :return: query content with conversaction ''' - prompt = fetch(const.OPEN_AI).get("character_desc", "") + prompt = model_conf(const.OPEN_AI).get("character_desc", "") if prompt: prompt += "<|endoftext|>\n\n\n" session = user_session.get(user_id, None) @@ -117,7 +117,7 @@ def build_session_query(query, user_id): @staticmethod def save_session(query, answer, user_id): - max_tokens = fetch(const.OPEN_AI).get("conversation_max_tokens") + max_tokens = model_conf(const.OPEN_AI).get("conversation_max_tokens") if not max_tokens: # default 3000 max_tokens = 1000