From 6404332adce895b9797ade54404f44432334fdee Mon Sep 17 00:00:00 2001 From: lanvent Date: Tue, 18 Apr 2023 02:21:41 +0800 Subject: [PATCH 1/8] feat: itchat support joingroup message --- bridge/context.py | 1 + channel/wechat/wechat_channel.py | 19 +++++++++++++------ channel/wechat/wechat_message.py | 29 +++++++++++++++++++++++++++-- plugins/hello/hello.py | 11 ++++++++++- 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/bridge/context.py b/bridge/context.py index 7a857b3..7eac9c1 100644 --- a/bridge/context.py +++ b/bridge/context.py @@ -8,6 +8,7 @@ class ContextType(Enum): VOICE = 2 # 音频消息 IMAGE = 3 # 图片消息 IMAGE_CREATE = 10 # 创建图片命令 + JOIN_GROUP = 20 # 加入群聊 def __str__(self): return self.name diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index 52c8ee3..b3099f6 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -28,18 +28,23 @@ from plugins import * @itchat.msg_register([TEXT, VOICE, PICTURE]) def handler_single_msg(msg): - # logger.debug("handler_single_msg: {}".format(msg)) - if msg["Type"] == PICTURE and msg["MsgType"] == 47: + try: + cmsg = WeChatMessage(msg, False) + except NotImplementedError as e: + logger.debug("[WX]single message {} skipped: {}".format(msg["MsgId"], e)) return None - WechatChannel().handle_single(WeChatMessage(msg)) + WechatChannel().handle_single(cmsg) return None -@itchat.msg_register([TEXT, VOICE, PICTURE], isGroupChat=True) +@itchat.msg_register([TEXT, VOICE, PICTURE, NOTE], isGroupChat=True) def handler_group_msg(msg): - if msg["Type"] == PICTURE and msg["MsgType"] == 47: + try: + cmsg = WeChatMessage(msg, True) + except NotImplementedError as e: + logger.debug("[WX]group message {} skipped: {}".format(msg["MsgId"], e)) return None - WechatChannel().handle_group(WeChatMessage(msg, True)) + WechatChannel().handle_group(cmsg) return None @@ -186,6 +191,8 @@ class WechatChannel(ChatChannel): 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: # logger.debug("[WX]receive group msg: {}, cmsg={}".format(json.dumps(cmsg._rawmsg, ensure_ascii=False), cmsg)) pass diff --git a/channel/wechat/wechat_message.py b/channel/wechat/wechat_message.py index 92182bd..6bfd9f2 100644 --- a/channel/wechat/wechat_message.py +++ b/channel/wechat/wechat_message.py @@ -1,3 +1,5 @@ +import re + from bridge.context import ContextType from channel.chat_message import ChatMessage from common.log import logger @@ -24,9 +26,31 @@ class WeChatMessage(ChatMessage): self.ctype = ContextType.IMAGE self.content = TmpDir().path() + itchat_msg["FileName"] # content直接存临时目录路径 self._prepare_fn = lambda: itchat_msg.download(self.content) + elif itchat_msg["Type"] == NOTE and itchat_msg["MsgType"] == 10000: + if is_group and ( + "加入群聊" 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"]: + self.actual_user_nickname = re.findall( + r"\"(.*?)\"", itchat_msg["Content"] + )[-1] + elif "加入群聊" in itchat_msg["Content"]: + self.actual_user_nickname = re.findall( + r"\"(.*?)\"", itchat_msg["Content"] + )[0] + else: + raise NotImplementedError( + "Unsupported note message: " + itchat_msg["Content"] + ) else: raise NotImplementedError( - "Unsupported message type: {}".format(itchat_msg["Type"]) + "Unsupported message type: Type:{} MsgType:{}".format( + itchat_msg["Type"], itchat_msg["MsgType"] + ) ) self.from_user_id = itchat_msg["FromUserName"] @@ -58,4 +82,5 @@ class WeChatMessage(ChatMessage): if self.is_group: self.is_at = itchat_msg["IsAt"] self.actual_user_id = itchat_msg["ActualUserName"] - self.actual_user_nickname = itchat_msg["ActualNickName"] + if self.ctype != ContextType.JOIN_GROUP: + self.actual_user_nickname = itchat_msg["ActualNickName"] diff --git a/plugins/hello/hello.py b/plugins/hello/hello.py index 4067c2a..00732ec 100644 --- a/plugins/hello/hello.py +++ b/plugins/hello/hello.py @@ -23,7 +23,16 @@ class Hello(Plugin): logger.info("[Hello] inited") def on_handle_context(self, e_context: EventContext): - if e_context["context"].type != ContextType.TEXT: + if e_context["context"].type not in [ContextType.TEXT, ContextType.JOIN_GROUP]: + return + + if e_context["context"].type == ContextType.JOIN_GROUP: + e_context["context"].type = ContextType.TEXT + msg: ChatMessage = e_context["context"]["msg"] + e_context[ + "context" + ].content = f'请你随机使用一种风格说一句问候语来欢迎新用户"{msg.actual_user_nickname}"加入群聊。' + e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑 return content = e_context["context"].content From f2e29f3f2e73b95105dd56ee476dd7d949fc0128 Mon Sep 17 00:00:00 2001 From: lanvent Date: Tue, 18 Apr 2023 11:43:34 +0800 Subject: [PATCH 2/8] fix: banwords help --- plugins/banwords/banwords.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/banwords/banwords.py b/plugins/banwords/banwords.py index 138b4c2..4f7f75c 100644 --- a/plugins/banwords/banwords.py +++ b/plugins/banwords/banwords.py @@ -102,4 +102,4 @@ class Banwords(Plugin): return def get_help_text(self, **kwargs): - return Banwords.desc + return "过滤消息中的敏感词。" From 6a13dd04a3221c92324bbb963923bd22e0f835e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E7=9D=A3=E7=BF=94?= Date: Tue, 18 Apr 2023 13:57:20 +0800 Subject: [PATCH 3/8] =?UTF-8?q?feat(=E6=8F=92=E4=BB=B6=E5=BC=80=E5=8F=91?= =?UTF-8?q?=EF=BC=89=EF=BC=9A=E6=96=B0=E5=A2=9E=E5=85=B3=E9=94=AE=E5=AD=97?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/keyword/README.md | 13 ++++++ plugins/keyword/__init__.py | 1 + plugins/keyword/config.json.template | 5 ++ plugins/keyword/keyword.py | 67 +++++++++++++++++++++++++++ plugins/keyword/test-keyword.png | Bin 0 -> 12350 bytes 5 files changed, 86 insertions(+) create mode 100644 plugins/keyword/README.md create mode 100644 plugins/keyword/__init__.py create mode 100644 plugins/keyword/config.json.template create mode 100644 plugins/keyword/keyword.py create mode 100644 plugins/keyword/test-keyword.png diff --git a/plugins/keyword/README.md b/plugins/keyword/README.md new file mode 100644 index 0000000..4678f68 --- /dev/null +++ b/plugins/keyword/README.md @@ -0,0 +1,13 @@ +# 目的 +关键字匹配并回复 + +# 试用场景 +目前是在微信公众号下面使用过。 + +# 使用步骤 +1. 复制 `config.json.template` 为 `config.json` +2. 在关键字 `keyword` 新增需要关键字匹配的内容 +3. 重启程序做验证 + +# 验证结果 +![结果](test-keyword.png) \ No newline at end of file diff --git a/plugins/keyword/__init__.py b/plugins/keyword/__init__.py new file mode 100644 index 0000000..b860b69 --- /dev/null +++ b/plugins/keyword/__init__.py @@ -0,0 +1 @@ +from .keyword import * diff --git a/plugins/keyword/config.json.template b/plugins/keyword/config.json.template new file mode 100644 index 0000000..9a8332f --- /dev/null +++ b/plugins/keyword/config.json.template @@ -0,0 +1,5 @@ +{ + "keyword": { + "关键字匹配": "测试成功" + } +} \ No newline at end of file diff --git a/plugins/keyword/keyword.py b/plugins/keyword/keyword.py new file mode 100644 index 0000000..cc286bd --- /dev/null +++ b/plugins/keyword/keyword.py @@ -0,0 +1,67 @@ +# encoding:utf-8 + +import json +import os +import plugins +from bridge.context import ContextType +from bridge.reply import Reply, ReplyType +from common.log import logger +from plugins import * + +@plugins.register( + name="Keyword", + desire_priority=900, + hidden=True, + desc="关键词匹配过滤", + version="0.1", + author="fengyege.top", +) +class Keyword(Plugin): + def __init__(self): + super().__init__() + try: + curdir = os.path.dirname(__file__) + config_path = os.path.join(curdir, "config.json") + conf = None + if not os.path.exists(config_path): + logger.debug(f"[keyword]不存在配置文件{config_path}") + conf = {"keyword": {}} + with open(config_path, "w", encoding="utf-8") as f: + json.dump(conf, f, indent=4) + else: + logger.debug(f"[keyword]加载配置文件{config_path}") + with open(config_path, "r", encoding="utf-8") as f: + conf = json.load(f) + # 加载关键词 + self.keyword = conf["keyword"] + + logger.info(self.keyword) + self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context + logger.info("[keyword] inited") + except Exception as e: + logger.warn( + "[keyword] init failed, ignore or see https://github.com/zhayujie/chatgpt-on-wechat/tree/master/plugins/keyword ." + ) + raise e + + def on_handle_context(self, e_context: EventContext): + if e_context["context"].type != ContextType.TEXT: + return + + content = e_context["context"].content.strip() + logger.info("[keyword] on_handle_context. content: %s" % content) + if content in self.keyword: + logger.debug(f"[keyword] 匹配到关键字【{content}】") + reply_text = self.keyword[content] + + reply = Reply() + reply.type = ReplyType.TEXT + reply.content = reply_text + e_context['reply'] = reply + e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑 + + def get_help_text(self, **kwargs): + help_text = "关键词过滤" + return help_text + + diff --git a/plugins/keyword/test-keyword.png b/plugins/keyword/test-keyword.png new file mode 100644 index 0000000000000000000000000000000000000000..0f17ae80b8c584095ef3eed8770ae8638b2cd3da GIT binary patch literal 12350 zcmbt)Wn5g%vnC;d1P>70-DiNHgG+D=?u6hF+#P~L7~ExWg1fs8?(Xgm!DY$&-uvIX z-}blrVa`mSGhN+PUDfA#syakLP8-Jlt%b8yfgP`!?1hou#3gk!$jr4pEV=jZ)0k{Kr}LVeroEuU!Yq7985YYPFVcGx zPNj&;=qoXlS$Ztfu_lqi1`3zT9iQ`f6I2TpsONABr;ZKga-e?~P}z`|9Nrs)OGum` znLUxnOAPs<(M4Dz&xk#^d&!Q54#U;=Vk!p%Q~lF=57$pGnnuiFq+>orCt1tHfv;qmCQfh!DjtSQ4`4%G68YxR*kiqK`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+8! zt9MDkE#)0EypH^D1xP^oHL&ENRAt6zDx_CQYYi)v;;cS+W&d zC=6A*tp#UB(2kvJz%&m0Q`&jw@K^)DRmeiZA^tWltQe>#+z(tz&}9D-2P$Gjo=w(Y z({oW|a->|HIT!fs0=L$N3-^emOFle^24f0GlVL!s74_`Wnt~}AF^>7}Ob^AuGqb4J zb^Q&D<@!4Z4=JB$>b)vNBYB>FPQfOh(qd8aHw7$$VdKO^))cU&z+@Z(m@z=L?BB!4 iXL^YL@5S7Xm-p0$YpRcdJb&xKFp^*7L`#Lg{rq3XCgoQE literal 0 HcmV?d00001 From abcbb34b1c5eb27253c8a2d9625cfc028a2b6dbc Mon Sep 17 00:00:00 2001 From: lanvent Date: Tue, 18 Apr 2023 14:18:22 +0800 Subject: [PATCH 4/8] fix(chat_gpt_bot.py, open_ai_bot.py): increase retry time to 20 seconds when encountering RateLimitError --- bot/chatgpt/chat_gpt_bot.py | 2 +- bot/openai/open_ai_bot.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bot/chatgpt/chat_gpt_bot.py b/bot/chatgpt/chat_gpt_bot.py index 59acaa6..d8e4b0e 100644 --- a/bot/chatgpt/chat_gpt_bot.py +++ b/bot/chatgpt/chat_gpt_bot.py @@ -142,7 +142,7 @@ class ChatGPTBot(Bot, OpenAIImage): logger.warn("[CHATGPT] RateLimitError: {}".format(e)) result["content"] = "提问太快啦,请休息一下再问我吧" if need_retry: - time.sleep(5) + time.sleep(20) elif isinstance(e, openai.error.Timeout): logger.warn("[CHATGPT] Timeout: {}".format(e)) result["content"] = "我没有收到你的消息" diff --git a/bot/openai/open_ai_bot.py b/bot/openai/open_ai_bot.py index 8a56cf1..1cfbf10 100644 --- a/bot/openai/open_ai_bot.py +++ b/bot/openai/open_ai_bot.py @@ -114,7 +114,7 @@ class OpenAIBot(Bot, OpenAIImage): logger.warn("[OPEN_AI] RateLimitError: {}".format(e)) result["content"] = "提问太快啦,请休息一下再问我吧" if need_retry: - time.sleep(5) + time.sleep(20) elif isinstance(e, openai.error.Timeout): logger.warn("[OPEN_AI] Timeout: {}".format(e)) result["content"] = "我没有收到你的消息" From de339114600a558ecfc6cd0977ba53b060c95a6f Mon Sep 17 00:00:00 2001 From: lanvent Date: Tue, 18 Apr 2023 23:34:08 +0800 Subject: [PATCH 5/8] 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 7eac9c1..c1eb10c 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 b3099f6..cf200b1 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -26,7 +26,7 @@ from lib.itchat.content import * 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 @@ class WechatChannel(ChatChannel): 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 @@ class WechatChannel(ChatChannel): 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 6bfd9f2..1888425 100644 --- a/channel/wechat/wechat_message.py +++ b/channel/wechat/wechat_message.py @@ -31,7 +31,6 @@ class WeChatMessage(ChatMessage): "加入群聊" 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 @@ class WeChatMessage(ChatMessage): 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 @@ class WeChatMessage(ChatMessage): 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 00732ec..254b172 100644 --- a/plugins/hello/hello.py +++ b/plugins/hello/hello.py @@ -23,7 +23,11 @@ class Hello(Plugin): 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 @@ class Hello(Plugin): 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 6/8] 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 9317b83..8b45a87 100644 --- a/voice/azure/azure_voice.py +++ b/voice/azure/azure_voice.py @@ -83,6 +83,10 @@ class AzureVoice(Voice): ) 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 7/8] 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 8b45a87..3ee9504 100644 --- a/voice/azure/azure_voice.py +++ b/voice/azure/azure_voice.py @@ -66,7 +66,11 @@ class AzureVoice(Voice): ) 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 8/8] 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 e1daced..4eb71e5 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 0a78193..3ce92da 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 5457ace..5a9c81a 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 @@ from plugins import * @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 @@ class Tool(Plugin): 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 @@ class Tool(Plugin): "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 @@ class Tool(Plugin): 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 ce29777..cfb52c9 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