From de339114600a558ecfc6cd0977ba53b060c95a6f Mon Sep 17 00:00:00 2001 From: lanvent Date: Tue, 18 Apr 2023 23:34:08 +0800 Subject: [PATCH 1/4] feat: add support for PATPAT context --- bridge/context.py | 1 + channel/wechat/wechat_channel.py | 16 +++++++++++----- channel/wechat/wechat_message.py | 10 ++++++++-- plugins/hello/hello.py | 13 ++++++++++++- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/bridge/context.py b/bridge/context.py index 7eac9c196..c1eb10c1f 100644 --- a/bridge/context.py +++ b/bridge/context.py @@ -9,6 +9,7 @@ class ContextType(Enum): IMAGE = 3 # 图片消息 IMAGE_CREATE = 10 # 创建图片命令 JOIN_GROUP = 20 # 加入群聊 + PATPAT = 21 # 拍了拍 def __str__(self): return self.name diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index b3099f62d..cf200b17e 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -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) @@ -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 ) @@ -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 ) diff --git a/channel/wechat/wechat_message.py b/channel/wechat/wechat_message.py index 6bfd9f2ef..18884259c 100644 --- a/channel/wechat/wechat_message.py +++ b/channel/wechat/wechat_message.py @@ -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"]: @@ -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"] @@ -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"] diff --git a/plugins/hello/hello.py b/plugins/hello/hello.py index 00732ecbb..254b17254 100644 --- a/plugins/hello/hello.py +++ b/plugins/hello/hello.py @@ -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: @@ -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": From a83e5a9b6589bd00e5c352b695852e491e038787 Mon Sep 17 00:00:00 2001 From: lanvent Date: Wed, 19 Apr 2023 00:51:52 +0800 Subject: [PATCH 2/4] feat(azure_voice.py): improve error logging in textToVoice method --- voice/azure/azure_voice.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/voice/azure/azure_voice.py b/voice/azure/azure_voice.py index 9317b8352..8b45a8790 100644 --- a/voice/azure/azure_voice.py +++ b/voice/azure/azure_voice.py @@ -83,6 +83,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 From a0cbe9c3e2eedad6ce286061eb0ebadb748f9111 Mon Sep 17 00:00:00 2001 From: lanvent Date: Wed, 19 Apr 2023 00:55:22 +0800 Subject: [PATCH 3/4] feat(azure_voice.py): improve error logging in voiceToText method --- voice/azure/azure_voice.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/voice/azure/azure_voice.py b/voice/azure/azure_voice.py index 8b45a8790..3ee95043e 100644 --- a/voice/azure/azure_voice.py +++ b/voice/azure/azure_voice.py @@ -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 From 0a7d6e4577de362f3a29272aa536b8ff34d5a067 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?goldfish=E8=8F=8C?= Date: Wed, 19 Apr 2023 10:05:28 +0800 Subject: [PATCH 4/4] plugin(tool) ver0.4.1 (#891) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * plugin(tool) fix bugs * plugin(tool) tool插件更新至0.4.1 版本 --- .gitignore | 1 + plugins/tool/README.md | 70 +++++++++++++++++++++++++++++++++------ plugins/tool/tool.py | 25 +++++++------- requirements-optional.txt | 3 +- 4 files changed, 74 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index e1daced52..4eb71e59f 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ plugins.json itchat.pkl *.log user_datas.pkl +chatgpt_tool_hub/ plugins/**/ !plugins/bdunit !plugins/dungeon diff --git a/plugins/tool/README.md b/plugins/tool/README.md index 0a78193fa..3ce92da47 100644 --- a/plugins/tool/README.md +++ b/plugins/tool/README.md @@ -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生成的代码使用,给予自然语言控制手段 @@ -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 diff --git a/plugins/tool/tool.py b/plugins/tool/tool.py index 5457acee3..5a9c81a6c 100644 --- a/plugins/tool/tool.py +++ b/plugins/tool/tool.py @@ -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 @@ -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, ) @@ -131,17 +131,17 @@ def _read_json(self) -> dict: def _build_tool_kwargs(self, kwargs: dict): tool_model_name = kwargs.get("model_name") + 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", ""), # for bing-search tool @@ -157,8 +157,6 @@ def _build_tool_kwargs(self, kwargs: dict): "zaobao_api_key": kwargs.get("zaobao_api_key", ""), # for visual_dl tool "cuda_device": kwargs.get("cuda_device", "cpu"), - # for browser tool - "phantomjs_exec_path": kwargs.get("phantomjs_exec_path", ""), } def _filter_tool_list(self, tool_list: list): @@ -172,11 +170,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) \ No newline at end of file diff --git a/requirements-optional.txt b/requirements-optional.txt index ce297779a..cfb52c953 100644 --- a/requirements-optional.txt +++ b/requirements-optional.txt @@ -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 \ No newline at end of file +chatgpt_tool_hub>=0.4.1 \ No newline at end of file