Skip to content

Commit

Permalink
Merge pull request #11 from tudou2/master
Browse files Browse the repository at this point in the history
update
  • Loading branch information
congxuma committed Apr 19, 2023
2 parents 520fc04 + 96a4ee8 commit 20c29ab
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ plugins.json
itchat.pkl
*.log
user_datas.pkl
chatgpt_tool_hub/
plugins/**/
!plugins/bdunit
!plugins/dungeon
Expand Down
1 change: 1 addition & 0 deletions bridge/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ContextType(Enum):
IMAGE = 3 # 图片消息
IMAGE_CREATE = 10 # 创建图片命令
JOIN_GROUP = 20 # 加入群聊
PATPAT = 21 # 拍了拍

def __str__(self):
return self.name
Expand Down
16 changes: 11 additions & 5 deletions channel/wechat/wechat_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from plugins import *


@itchat.msg_register([TEXT, VOICE, PICTURE])
@itchat.msg_register([TEXT, VOICE, PICTURE, NOTE])
def handler_single_msg(msg):
try:
cmsg = WeChatMessage(msg, False)
Expand Down Expand Up @@ -170,12 +170,16 @@ def handle_single(self, cmsg: ChatMessage):
logger.debug("[WX]receive voice msg: {}".format(cmsg.content))
elif cmsg.ctype == ContextType.IMAGE:
logger.debug("[WX]receive image msg: {}".format(cmsg.content))
else:
elif cmsg.ctype == ContextType.PATPAT:
logger.debug("[WX]receive patpat msg: {}".format(cmsg.content))
elif cmsg.ctype == ContextType.TEXT:
logger.debug(
"[WX]receive text msg: {}, cmsg={}".format(
json.dumps(cmsg._rawmsg, ensure_ascii=False), cmsg
)
)
else:
logger.debug("[WX]receive msg: {}, cmsg={}".format(cmsg.content, cmsg))
context = self._compose_context(
cmsg.ctype, cmsg.content, isgroup=False, msg=cmsg
)
Expand All @@ -191,11 +195,13 @@ def handle_group(self, cmsg: ChatMessage):
logger.debug("[WX]receive voice for group msg: {}".format(cmsg.content))
elif cmsg.ctype == ContextType.IMAGE:
logger.debug("[WX]receive image for group msg: {}".format(cmsg.content))
elif cmsg.ctype == ContextType.JOIN_GROUP:
logger.debug("[WX]receive join group msg: {}".format(cmsg.content))
else:
elif cmsg.ctype in [ContextType.JOIN_GROUP, ContextType.PATPAT]:
logger.debug("[WX]receive note msg: {}".format(cmsg.content))
elif cmsg.ctype == ContextType.TEXT:
# logger.debug("[WX]receive group msg: {}, cmsg={}".format(json.dumps(cmsg._rawmsg, ensure_ascii=False), cmsg))
pass
else:
logger.debug("[WX]receive group msg: {}".format(cmsg.content))
context = self._compose_context(
cmsg.ctype, cmsg.content, isgroup=True, msg=cmsg
)
Expand Down
10 changes: 8 additions & 2 deletions channel/wechat/wechat_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def __init__(self, itchat_msg, is_group=False):
"加入群聊" in itchat_msg["Content"] or "加入了群聊" in itchat_msg["Content"]
):
self.ctype = ContextType.JOIN_GROUP
logger.debug("[WX]join group message: " + itchat_msg["Content"])
self.content = itchat_msg["Content"]
# 这里只能得到nickname, actual_user_id还是机器人的id
if "加入了群聊" in itchat_msg["Content"]:
Expand All @@ -42,6 +41,13 @@ def __init__(self, itchat_msg, is_group=False):
self.actual_user_nickname = re.findall(
r"\"(.*?)\"", itchat_msg["Content"]
)[0]
elif "拍了拍我" in itchat_msg["Content"]:
self.ctype = ContextType.PATPAT
self.content = itchat_msg["Content"]
if is_group:
self.actual_user_nickname = re.findall(
r"\"(.*?)\"", itchat_msg["Content"]
)[0]
else:
raise NotImplementedError(
"Unsupported note message: " + itchat_msg["Content"]
Expand Down Expand Up @@ -82,5 +88,5 @@ def __init__(self, itchat_msg, is_group=False):
if self.is_group:
self.is_at = itchat_msg["IsAt"]
self.actual_user_id = itchat_msg["ActualUserName"]
if self.ctype != ContextType.JOIN_GROUP:
if self.ctype not in [ContextType.JOIN_GROUP, ContextType.PATPAT]:
self.actual_user_nickname = itchat_msg["ActualNickName"]
13 changes: 12 additions & 1 deletion plugins/hello/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ def __init__(self):
logger.info("[Hello] inited")

def on_handle_context(self, e_context: EventContext):
if e_context["context"].type not in [ContextType.TEXT, ContextType.JOIN_GROUP]:
if e_context["context"].type not in [
ContextType.TEXT,
ContextType.JOIN_GROUP,
ContextType.PATPAT,
]:
return

if e_context["context"].type == ContextType.JOIN_GROUP:
Expand All @@ -35,6 +39,13 @@ def on_handle_context(self, e_context: EventContext):
e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑
return

if e_context["context"].type == ContextType.PATPAT:
e_context["context"].type = ContextType.TEXT
msg: ChatMessage = e_context["context"]["msg"]
e_context["context"].content = f"请你随机使用一种风格介绍你自己,并告诉用户输入#help可以查看帮助信息。"
e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑
return

content = e_context["context"].content
logger.debug("[Hello] on_handle_context. content: %s" % content)
if content == "Hello":
Expand Down
1 change: 1 addition & 0 deletions plugins/plugin_getnews/getnews.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def on_handle_context(self, e_context: EventContext):
content = re.sub(r'^bot', '', content) #删除开头的bot
logger.info("[getnews] replace bot and new content:" + content)

e_context['context'].content = content
e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑

# if content == "Hi":
Expand Down
70 changes: 59 additions & 11 deletions plugins/tool/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,21 @@
### 1. python
###### python解释器,使用它来解释执行python指令,可以配合你想要chatgpt生成的代码输出结果或执行事务

### 2. url-get
### 2. 访问网页的工具汇总(默认url-get)

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

#### 2.2 browser
###### 浏览器,功能与2.1类似,但能更好模拟,不会被识别为爬虫影响获取网站内容

> 注1:url-get默认配置、browser需额外配置,browser依赖google-chrome,你需要提前安装好
> 注2:browser默认使用summary tool 分段总结长文本信息,tokens可能会大量消耗!
这是debian端安装google-chrome教程,其他系统请执行查找
> https://www.linuxjournal.com/content/how-can-you-install-google-browser-debian
### 3. terminal
###### 在你运行的电脑里执行shell命令,可以配合你想要chatgpt生成的代码使用,给予自然语言控制手段

Expand All @@ -38,47 +50,83 @@
### 5. wikipedia
###### 可以回答你想要知道确切的人事物

### 6. news *
### 6. 新闻类工具

#### 6.1. news-api *
###### 从全球 80,000 多个信息源中获取当前和历史新闻文章

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

> 该tool每天返回内容相同
### 8. bing-search *
#### 6.3. finance-news
###### 获取实时的金融财政新闻

> 该工具需要解决browser tool 的google-chrome依赖安装
### 7. bing-search *
###### bing搜索引擎,从此你不用再烦恼搜索要用哪些关键词

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

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

###### 注1:带*工具需要获取api-key才能使用,部分工具需要外网支持

### 10. arxiv(dev 开发中)
###### 用于查找论文


### 11. debug(dev 开发中,目前没有接入wechat)
###### 当bot遇到无法确定的信息时,将会向你寻求帮助的工具


### 12. summary
###### 总结工具,该工具必须输入一个本地文件的绝对路径

> 该工具目前是和其他工具配合使用,暂未测试单独使用效果

### 13. image2text
###### 将图片转换成文字,底层调用imageCaption模型,该工具必须输入一个本地文件的绝对路径


### 14. searxng-search *
###### 一个私有化的搜索引擎工具

> 安装教程:https://docs.searxng.org/admin/installation.html
---

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

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

```
注:config.json文件非必须,未创建仍可使用本tool;带*工具需在kwargs填入对应api-key键值对
- `tools`:本插件初始化时加载的工具, 目前可选集:["wikipedia", "wolfram-alpha", "bing-search", "google-search", "news", "morning-news"] & 默认工具,除wikipedia工具之外均需要申请api-key
- `tools`:本插件初始化时加载的工具, 目前可选集:["wikipedia", "wolfram-alpha", "bing-search", "google-search", "news"] & 默认工具,除wikipedia工具之外均需要申请api-key
- `kwargs`:工具执行时的配置,一般在这里存放**api-key**,或环境配置
- `debug`: 输出chatgpt-tool-hub额外信息用于调试
- `request_timeout`: 访问openai接口的超时时间,默认与wechat-on-chatgpt配置一致,可单独配置
- `no_default`: 用于配置默认加载4个工具的行为,如果为true则仅使用tools列表工具,不加载默认工具
- `top_k_results`: 控制所有有关搜索的工具返回条目数,数字越高则参考信息越多,但无用信息可能干扰判断,该值一般为2
- `model_name`: 用于控制tool插件底层使用的llm模型,目前暂未测试3.5以外的模型,一般保持默认

---

## 备注
- 强烈建议申请搜索工具搭配使用,推荐bing-search
Expand Down
25 changes: 13 additions & 12 deletions plugins/tool/tool.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import os

from chatgpt_tool_hub.apps import load_app
from chatgpt_tool_hub.apps import AppFactory
from chatgpt_tool_hub.apps.app import App
from chatgpt_tool_hub.tools.all_tool_list import get_all_tool_names

Expand All @@ -18,7 +18,7 @@
@plugins.register(
name="tool",
desc="Arming your ChatGPT bot with various tools",
version="0.3",
version="0.4",
author="goldfishh",
desire_priority=0,
)
Expand Down Expand Up @@ -160,18 +160,18 @@ def _build_tool_kwargs(self, kwargs: dict):
else:
logger.warn("[tool] getapi failed.")
raise e


request_timeout = kwargs.get("request_timeout")

return {
"debug": kwargs.get("debug", False),
"openai_api_key": conf().get("open_ai_api_key", ""),
"proxy": conf().get("proxy", ""),
"request_timeout": str(conf().get("request_timeout", 60)),
"request_timeout": request_timeout if request_timeout else conf().get("request_timeout", 120),
# note: 目前tool暂未对其他模型测试,但这里仍对配置来源做了优先级区分,一般插件配置可覆盖全局配置
"model_name": tool_model_name
if tool_model_name
else conf().get("model", "gpt-3.5-turbo"),
"model_name": tool_model_name if tool_model_name else conf().get("model", "gpt-3.5-turbo"),
"no_default": kwargs.get("no_default", False),
"top_k_results": kwargs.get("top_k_results", 2),
"top_k_results": kwargs.get("top_k_results", 3),
# for news tool
"news_api_key": kwargs.get("news_api_key", news_api_key),
# for bing-search tool
Expand Down Expand Up @@ -205,11 +205,12 @@ def _filter_tool_list(self, tool_list: list):

def _reset_app(self) -> App:
tool_config = self._read_json()
app_kwargs = self._build_tool_kwargs(tool_config.get("kwargs", {}))

app = AppFactory()
app.init_env(**app_kwargs)

# filter not support tool
tool_list = self._filter_tool_list(tool_config.get("tools", []))

return load_app(
tools_list=tool_list,
**self._build_tool_kwargs(tool_config.get("kwargs", {})),
)
return app.create_app(tools_list=tool_list, **app_kwargs)
3 changes: 2 additions & 1 deletion requirements-optional.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ pysilk_mod>=1.6.0 # needed by send voice
web.py

# chatgpt-tool-hub plugin

--extra-index-url https://pypi.python.org/simple
chatgpt_tool_hub>=0.3.9
chatgpt_tool_hub>=0.4.1
12 changes: 10 additions & 2 deletions voice/azure/azure_voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ def voiceToText(self, voice_file):
)
reply = Reply(ReplyType.TEXT, result.text)
else:
logger.error("[Azure] voiceToText error, result={}".format(result))
logger.error(
"[Azure] voiceToText error, result={}, canceldetails={}".format(
result, result.cancellation_details
)
)
reply = Reply(ReplyType.ERROR, "抱歉,语音识别失败")
return reply

Expand All @@ -83,6 +87,10 @@ def textToVoice(self, text):
)
reply = Reply(ReplyType.VOICE, fileName)
else:
logger.error("[Azure] textToVoice error, result={}".format(result))
logger.error(
"[Azure] textToVoice error, result={}, canceldetails={}".format(
result, result.cancellation_details
)
)
reply = Reply(ReplyType.ERROR, "抱歉,语音合成失败")
return reply

0 comments on commit 20c29ab

Please sign in to comment.