Skip to content

Commit

Permalink
Merge pull request #3 from tudou2/master
Browse files Browse the repository at this point in the history
update
  • Loading branch information
congxuma committed Apr 13, 2023
2 parents 62324b0 + aaee15e commit df1cbdf
Show file tree
Hide file tree
Showing 19 changed files with 116 additions and 38 deletions.
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@

- [x] **文本对话:** 接收私聊及群组中的微信消息,使用ChatGPT生成回复内容,完成自动回复
- [x] **规则定制化:** 支持私聊中按指定规则触发自动回复,支持对群组设置自动回复白名单
- [x] **多账号:** 支持多微信账号同时运行
- [x] **图片生成:** 支持根据描述生成图片,并自动发送至个人聊天或群聊
- [x] **图片生成:** 支持根据描述生成图片,支持图片修复
- [x] **上下文记忆**:支持多轮对话记忆,且为每个好友维护独立的上下会话
- [x] **语音识别:** 支持接收和处理语音消息,通过文字或语音回复
- [x] **插件化:** 支持个性化功能插件,提供角色扮演、文字冒险游戏等预设插件
- [x] **插件化:** 支持个性化插件,提供角色扮演、文字冒险、与操作系统交互、访问网络数据等能力

> 目前支持微信和微信个人号部署,欢迎接入更多应用,参考[`Terminal`代码](https://github.com/zhayujie/chatgpt-on-wechat/blob/master/channel/terminal/terminal_channel.py)实现接收和发送消息逻辑即可接入。
> 目前支持微信和微信公众号部署,欢迎接入更多应用,参考 [Terminal代码](https://github.com/zhayujie/chatgpt-on-wechat/blob/master/channel/terminal/terminal_channel.py)实现接收和发送消息逻辑即可接入。 同时欢迎增加新的插件,参考 [插件说明文档](https://github.com/zhayujie/chatgpt-on-wechat/tree/master/plugins)

快速部署:
>
>[![Deploy on Railway](https://railway.app/button.svg)](https://railway.app/template/qApznZ?referralCode=RC3znh)
**一键部署:**

[![Deploy on Railway](https://railway.app/button.svg)](https://railway.app/template/qApznZ?referralCode=RC3znh)


# 更新日志
Expand Down Expand Up @@ -217,6 +216,6 @@ FAQs: <https://github.com/zhayujie/chatgpt-on-wechat/wiki/FAQs>

## 联系

欢迎提交PR、Issues,以及Star支持一下。程序运行遇到问题优先查看 [常见问题列表](https://github.com/zhayujie/chatgpt-on-wechat/wiki/FAQs) ,其次前往 [Issues](https://github.com/zhayujie/chatgpt-on-wechat/issues) 中搜索,若无相似问题可创建Issue,或加微信 eijuyahz 交流。
欢迎提交PR、Issues,以及Star支持一下。程序运行遇到问题优先查看 [常见问题列表](https://github.com/zhayujie/chatgpt-on-wechat/wiki/FAQs) ,其次前往 [Issues](https://github.com/zhayujie/chatgpt-on-wechat/issues) 中搜索。如果你想了解更多项目细节,并与开发者们交流更多关于AI技术的实践,欢迎加入星球:

<a href="https://public.zsxq.com/groups/88885848842852.html"><img width="360" src="./docs/images/planet.jpg"></a>
1 change: 1 addition & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def func(_signo, _stack_frame):
conf().save_user_datas()
if callable(old_handler): # check old_handler
return old_handler(_signo, _stack_frame)
sys.exit(0)
signal.signal(_signo, func)

def run():
Expand Down
10 changes: 7 additions & 3 deletions channel/chat_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ def _compose_context(self, ctype: ContextType, content, **kwargs):
if first_in: # context首次传入时,receiver是None,根据类型设置receiver
config = conf()
cmsg = context['msg']
if cmsg.from_user_id == self.user_id and not config.get('trigger_by_self', True):
logger.debug("[WX]self message skipped")
return None
if context.get("isgroup", False):
group_name = cmsg.other_user_nickname
group_id = cmsg.other_user_id
Expand All @@ -69,6 +66,13 @@ def _compose_context(self, ctype: ContextType, content, **kwargs):
else:
context['session_id'] = cmsg.other_user_id
context['receiver'] = cmsg.other_user_id
e_context = PluginManager().emit_event(EventContext(Event.ON_RECEIVE_MESSAGE, {'channel': self, 'context': context}))
context = e_context['context']
if e_context.is_pass() or context is None:
return context
if cmsg.from_user_id == self.user_id and not config.get('trigger_by_self', True):
logger.debug("[WX]self message skipped")
return None

# 消息内容匹配过程,并处理content
if ctype == ContextType.TEXT:
Expand Down
5 changes: 4 additions & 1 deletion channel/wechatmp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ sudo iptables-save > /etc/iptables/rules.v4
## 私有api_key
公共api有访问频率限制(免费账号每分钟最多20次ChatGPT的API调用),这在服务多人的时候会遇到问题。因此这里多加了一个设置私有api_key的功能。目前通过godcmd插件的命令来设置私有api_key。

## 语音输入
利用微信自带的语音识别功能,提供语音输入能力。需要在公众号管理页面的“设置与开发”->“接口权限”页面开启“接收语音识别结果”。

## 测试范围
目前在`RoboStyle`这个公众号上进行了测试(基于[wechatmp-stable分支](https://github.com/JS00000/chatgpt-on-wechat/tree/wechatmp-stable),而[master分支](https://github.com/zhayujie/chatgpt-on-wechat)含有最新功能,但是稳定性有待测试),感兴趣的可以关注并体验。开启了godcmd, Banwords, role, dungeon, finish这五个插件,其他的插件还没有测试。百度的接口暂未测试。语音对话没有测试。图片直接以链接形式回复(没有临时素材上传接口的权限)。
目前在`RoboStyle`这个公众号上进行了测试基于[wechatmp分支](https://github.com/JS00000/chatgpt-on-wechat/tree/wechatmp),感兴趣的可以关注并体验。开启了godcmd, Banwords, role, dungeon, finish这五个插件,其他的插件还没有测试。百度的接口暂未测试。语音对话没有测试。图片直接以链接形式回复(没有临时素材上传接口的权限)。

## TODO
* 服务号交互完善
Expand Down
2 changes: 1 addition & 1 deletion channel/wechatmp/ServiceAccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def POST(self):
webData = web.data()
# logger.debug("[wechatmp] Receive request:\n" + webData.decode("utf-8"))
wechatmp_msg = receive.parse_xml(webData)
if wechatmp_msg.msg_type == 'text':
if wechatmp_msg.msg_type == 'text' or wechatmp_msg.msg_type == 'voice':
from_user = wechatmp_msg.from_user_id
message = wechatmp_msg.content.decode("utf-8")
message_id = wechatmp_msg.msg_id
Expand Down
2 changes: 1 addition & 1 deletion channel/wechatmp/SubscribeAccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def POST(self):
webData = web.data()
logger.debug("[wechatmp] Receive request:\n" + webData.decode("utf-8"))
wechatmp_msg = receive.parse_xml(webData)
if wechatmp_msg.msg_type == 'text':
if wechatmp_msg.msg_type == 'text' or wechatmp_msg.msg_type == 'voice':
from_user = wechatmp_msg.from_user_id
to_user = wechatmp_msg.to_user_id
message = wechatmp_msg.content.decode("utf-8")
Expand Down
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"group_speech_recognition": False, # 是否开启群组语音识别
"voice_reply_voice": False, # 是否使用语音回复语音,需要设置对应语音合成引擎的api key
"always_reply_voice": False, # 是否一直使用语音回复
"voice_to_text": "openai", # 语音识别引擎,支持openai,google,azure
"voice_to_text": "openai", # 语音识别引擎,支持openai,baidu,google,azure
"text_to_voice": "baidu", # 语音合成引擎,支持baidu,google,pytts(offline),azure

# baidu 语音api配置, 使用百度语音识别和语音合成时需要
Expand Down
Binary file added docs/images/planet.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 19 additions & 1 deletion plugins/banwords/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@

## 插件描述

简易的敏感词插件,暂不支持分词,请自行导入词库到插件文件夹中的`banwords.txt`,每行一个词,一个参考词库是[1](https://github.com/cjh0613/tencent-sensitive-words/blob/main/sensitive_words_lines.txt)

`config.json`中能够填写默认的处理行为,目前行为有:
使用前将`config.json.template`复制为`config.json`,并自行配置。

目前插件对消息的默认处理行为有如下两种:

- `ignore` : 无视这条消息。
- `replace` : 将消息中的敏感词替换成"*",并回复违规。

```json
"action": "replace",
"reply_filter": true,
"reply_action": "ignore"
```

在以上配置项中:

- `action`: 对用户消息的默认处理行为
- `reply_filter`: 是否对ChatGPT的回复也进行敏感词过滤
- `reply_action`: 如果开启了回复过滤,对回复的默认处理行为

## 致谢

搜索功能实现来自https://github.com/toolgood/ToolGood.Words
26 changes: 25 additions & 1 deletion plugins/banwords/banwords.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def __init__(self):
words.append(word)
self.searchr.SetKeywords(words)
self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
if conf.get("reply_filter",True):
self.handlers[Event.ON_DECORATE_REPLY] = self.on_decorate_reply
self.reply_action = conf.get("reply_action","ignore")
logger.info("[Banwords] inited")
except Exception as e:
logger.warn("[Banwords] init failed, ignore or see https://github.com/zhayujie/chatgpt-on-wechat/tree/master/plugins/banwords .")
Expand All @@ -53,7 +56,7 @@ def on_handle_context(self, e_context: EventContext):
if self.action == "ignore":
f = self.searchr.FindFirst(content)
if f:
logger.info("Banwords: %s" % f["Keyword"])
logger.info("[Banwords] %s in message" % f["Keyword"])
e_context.action = EventAction.BREAK_PASS
return
elif self.action == "replace":
Expand All @@ -63,5 +66,26 @@ def on_handle_context(self, e_context: EventContext):
e_context.action = EventAction.BREAK_PASS
return

def on_decorate_reply(self, e_context: EventContext):

if e_context['reply'].type not in [ReplyType.TEXT]:
return

reply = e_context['reply']
content = reply.content
if self.reply_action == "ignore":
f = self.searchr.FindFirst(content)
if f:
logger.info("[Banwords] %s in reply" % f["Keyword"])
e_context['reply'] = None
e_context.action = EventAction.BREAK_PASS
return
elif self.reply_action == "replace":
if self.searchr.ContainsAny(content):
reply = Reply(ReplyType.INFO, "已替换回复中的敏感词: \n"+self.searchr.Replace(content))
e_context['reply'] = reply
e_context.action = EventAction.CONTINUE
return

def get_help_text(self, **kwargs):
return Banwords.desc
4 changes: 3 additions & 1 deletion plugins/banwords/config.json.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"action": "ignore"
"action": "replace",
"reply_filter": true,
"reply_action": "ignore"
}
5 changes: 4 additions & 1 deletion plugins/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@


class Event(Enum):
# ON_RECEIVE_MESSAGE = 1 # 收到消息
ON_RECEIVE_MESSAGE = 1 # 收到消息
"""
e_context = { "channel": 消息channel, "context" : 本次消息的context}
"""

ON_HANDLE_CONTEXT = 2 # 处理消息前
"""
Expand Down
2 changes: 1 addition & 1 deletion plugins/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
,
"getnews": {
"url": "https://github.com/congxuma/plugin_getnews.get",
"url": "https://github.com/congxuma/plugin_getnews.git",
"desc": "测试获取新闻"
}
}
Expand Down
42 changes: 29 additions & 13 deletions plugins/tool/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 插件描述
一个能让chatgpt联网,搜索,数字运算的插件,将赋予强大且丰富的扩展能力
使用该插件需在触发机器人回复条件时,在对话内容前加$tool
使用该插件需在机器人回复你的前提下,在对话内容前加$tool;仅输入$tool将返回tool插件帮助信息,用于测试插件是否加载成功
### 本插件所有工具同步存放至专用仓库:[chatgpt-tool-hub](https://github.com/goldfishh/chatgpt-tool-hub)


Expand All @@ -9,15 +9,19 @@
### 1. python
###### python解释器,使用它来解释执行python指令,可以配合你想要chatgpt生成的代码输出结果或执行事务

### 2. requests
### 2. url-get
###### 往往用来获取某个网站具体内容,结果可能会被反爬策略影响

### 3. terminal
###### 在你运行的电脑里执行shell命令,可以配合你想要chatgpt生成的代码使用,给予自然语言控制手段

> terminal调优记录:https://github.com/zhayujie/chatgpt-on-wechat/issues/776#issue-1659347640
### 4. meteo-weather
###### 回答你有关天气的询问, 需要获取时间、地点上下文信息,本工具使用了[meteo open api](https://open-meteo.com/)
注:该工具需提供时间,地点信息,获取的数据不保证准确性
注:该工具需要较高的对话技巧,不保证你问的任何问题均能得到满意的回复

> meteo调优记录:https://github.com/zhayujie/chatgpt-on-wechat/issues/776#issuecomment-1500771334
## 使用本插件对话(prompt)技巧
### 1. 有指引的询问
Expand All @@ -37,30 +41,40 @@
### 6. news *
###### 从全球 80,000 多个信息源中获取当前和历史新闻文章

### 7. bing-search *
### 7. morning-news *
###### 每日60秒早报,每天凌晨一点更新,本工具使用了[alapi-每日60秒早报](https://alapi.cn/api/view/93)

> 该tool每天返回内容相同
### 8. bing-search *
###### bing搜索引擎,从此你不用再烦恼搜索要用哪些关键词

### 8. wolfram-alpha *
### 9. wolfram-alpha *
###### 知识搜索引擎、科学问答系统,常用于专业学科计算

### 10. google-search *
###### google搜索引擎,申请流程较bing-search繁琐

###### 注1:带*工具需要获取api-key才能使用,部分工具需要外网支持
#### [申请方法](https://github.com/goldfishh/chatgpt-tool-hub/blob/master/docs/apply_optional_tool.md)

## config.json 配置说明
###### 默认工具无需配置,其它工具需手动配置,一个例子:
```json
{
"tools": ["wikipedia"],
"tools": ["wikipedia"], // 填入你想用到的额外工具名
"kwargs": {
"top_k_results": 2,
"no_default": false,
"model_name": "gpt-3.5-turbo"
"request_timeout": 60, // openai接口超时时间
"no_default": false, // 是否不使用默认的4个工具
"OPTIONAL_API_NAME": "OPTIONAL_API_KEY" // 带*工具需要申请api-key,在这里填入,api_name参考前述`申请方法`
}
}

```
注:config.json文件非必须,未创建仍可使用本tool
- `tools`:本插件初始化时加载的工具, 目前可选集:["wikipedia", "wolfram-alpha", "bing-search", "google-search", "news"],其中后4个工具需要申请服务api
- `kwargs`:工具执行时的配置,一般在这里存放api-key,或环境配置
注:config.json文件非必须,未创建仍可使用本tool;带*工具需在kwargs填入对应api-key键值对
- `tools`:本插件初始化时加载的工具, 目前可选集:["wikipedia", "wolfram-alpha", "bing-search", "google-search", "news", "morning-news"] & 默认工具,除wikipedia工具之外均需要申请api-key
- `kwargs`:工具执行时的配置,一般在这里存放**api-key**,或环境配置
- `request_timeout`: 访问openai接口的超时时间,默认与wechat-on-chatgpt配置一致,可单独配置
- `no_default`: 用于配置默认加载4个工具的行为,如果为true则仅使用tools列表工具,不加载默认工具
- `top_k_results`: 控制所有有关搜索的工具返回条目数,数字越高则参考信息越多,但无用信息可能干扰判断,该值一般为2
- `model_name`: 用于控制tool插件底层使用的llm模型,目前暂未测试3.5以外的模型,一般保持默认
Expand All @@ -69,4 +83,6 @@
## 备注
- 强烈建议申请搜索工具搭配使用,推荐bing-search
- 虽然我会有意加入一些限制,但请不要使用本插件做危害他人的事情,请提前了解清楚某些内容是否会违反相关规定,建议提前做好过滤
- 未来一段时间我会实现一些有意思的工具,比如stable diffusion 中文prompt翻译、cv方向的模型推理,欢迎有想法的朋友关注,一起扩展这个项目
- 如有本插件问题,请将debug设置为true无上下文重新问一遍,如仍有问题请访问[chatgpt-tool-hub](https://github.com/goldfishh/chatgpt-tool-hub)建个issue,将日志贴进去,我无法处理不能复现的问题
- 欢迎 star & 宣传,有能力请提pr

2 changes: 1 addition & 1 deletion plugins/tool/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"tools": ["morning-news", "wolfram-alpha", "bing-search"],
"tools": [ "wolfram-alpha", "bing-search"],
"kwargs": {
"top_k_results": 2,
"no_default": false,
Expand Down
2 changes: 1 addition & 1 deletion plugins/tool/config.json.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"tools": ["python", "requests", "terminal", "meteo-weather"],
"tools": ["python", "url-get", "terminal", "meteo-weather"],
"kwargs": {
"top_k_results": 2,
"no_default": false,
Expand Down
8 changes: 8 additions & 0 deletions plugins/tool/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def _build_tool_kwargs(self, kwargs: dict):
return {
"openai_api_key": conf().get("open_ai_api_key", ""),
"proxy": conf().get("proxy", ""),
"request_timeout": conf().get("request_timeout", 60),
# note: 目前tool暂未对其他模型测试,但这里仍对配置来源做了优先级区分,一般插件配置可覆盖全局配置
"model_name": tool_model_name if tool_model_name else conf().get("model", "gpt-3.5-turbo"),
"no_default": kwargs.get("no_default", False),
Expand All @@ -164,8 +165,15 @@ def _build_tool_kwargs(self, kwargs: dict):
"google_cse_id": kwargs.get("google_cse_id", google_cse_id),
# for searxng-search tool
"searx_host": kwargs.get("searx_host", searx_host),

# for visual_dl tool
"cuda_device": kwargs.get("cuda_device", "cpu"),
# for browser tool
"phantomjs_exec_path": kwargs.get("phantomjs_exec_path", ""),

# for zaobao_api_key tool
"zaobao_api_key": kwargs.get("zaobao_api_key", zaobao_api_key),

# for wolfram-alpha tool
"wolfram_alpha_appid": kwargs.get("wolfram_alpha_appid", wolfram_alpha_appid),
}
Expand Down
2 changes: 1 addition & 1 deletion requirements-optional.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ web.py

# chatgpt-tool-hub plugin
--extra-index-url https://pypi.python.org/simple
chatgpt_tool_hub>=0.3.7
chatgpt_tool_hub>=0.3.9
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ chardet>=5.1.0

# chatgpt-tool-hub plugin
--extra-index-url https://pypi.python.org/simple
chatgpt_tool_hub>=0.3.8
chatgpt_tool_hub>=0.3.9


timeout_decorator
Expand Down

0 comments on commit df1cbdf

Please sign in to comment.