mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-03-20 05:51:55 +08:00
formatting code
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
## 插件描述
|
||||
一个能让chatgpt联网,搜索,数字运算的插件,将赋予强大且丰富的扩展能力
|
||||
一个能让chatgpt联网,搜索,数字运算的插件,将赋予强大且丰富的扩展能力
|
||||
使用该插件需在机器人回复你的前提下,在对话内容前加$tool;仅输入$tool将返回tool插件帮助信息,用于测试插件是否加载成功
|
||||
### 本插件所有工具同步存放至专用仓库:[chatgpt-tool-hub](https://github.com/goldfishh/chatgpt-tool-hub)
|
||||
|
||||
|
||||
|
||||
|
||||
## 使用说明
|
||||
使用该插件后将默认使用4个工具, 无需额外配置长期生效:
|
||||
### 1. python
|
||||
使用该插件后将默认使用4个工具, 无需额外配置长期生效:
|
||||
### 1. python
|
||||
###### python解释器,使用它来解释执行python指令,可以配合你想要chatgpt生成的代码输出结果或执行事务
|
||||
|
||||
|
||||
### 2. url-get
|
||||
###### 往往用来获取某个网站具体内容,结果可能会被反爬策略影响
|
||||
|
||||
@@ -23,16 +23,16 @@
|
||||
|
||||
> meteo调优记录:https://github.com/zhayujie/chatgpt-on-wechat/issues/776#issuecomment-1500771334
|
||||
|
||||
## 使用本插件对话(prompt)技巧
|
||||
### 1. 有指引的询问
|
||||
## 使用本插件对话(prompt)技巧
|
||||
### 1. 有指引的询问
|
||||
#### 例如:
|
||||
- 总结这个链接的内容 https://github.com/goldfishh/chatgpt-tool-hub
|
||||
- 总结这个链接的内容 https://github.com/goldfishh/chatgpt-tool-hub
|
||||
- 使用Terminal执行curl cip.cc
|
||||
- 使用python查询今天日期
|
||||
|
||||
|
||||
### 2. 使用搜索引擎工具
|
||||
- 如果有搜索工具就能让chatgpt获取到你的未传达清楚的上下文信息,比如chatgpt不知道你的地理位置,现在时间等,所以无法查询到天气
|
||||
|
||||
|
||||
## 其他工具
|
||||
|
||||
### 5. wikipedia
|
||||
@@ -55,9 +55,9 @@
|
||||
### 10. google-search *
|
||||
###### google搜索引擎,申请流程较bing-search繁琐
|
||||
|
||||
###### 注1:带*工具需要获取api-key才能使用,部分工具需要外网支持
|
||||
###### 注1:带*工具需要获取api-key才能使用,部分工具需要外网支持
|
||||
#### [申请方法](https://github.com/goldfishh/chatgpt-tool-hub/blob/master/docs/apply_optional_tool.md)
|
||||
|
||||
|
||||
## config.json 配置说明
|
||||
###### 默认工具无需配置,其它工具需手动配置,一个例子:
|
||||
```json
|
||||
@@ -71,15 +71,15 @@
|
||||
}
|
||||
|
||||
```
|
||||
注:config.json文件非必须,未创建仍可使用本tool;带*工具需在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以外的模型,一般保持默认
|
||||
|
||||
|
||||
|
||||
|
||||
## 备注
|
||||
- 强烈建议申请搜索工具搭配使用,推荐bing-search
|
||||
- 虽然我会有意加入一些限制,但请不要使用本插件做危害他人的事情,请提前了解清楚某些内容是否会违反相关规定,建议提前做好过滤
|
||||
|
||||
@@ -1 +1 @@
|
||||
from .tool import *
|
||||
from .tool import *
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
{
|
||||
"tools": ["python", "url-get", "terminal", "meteo-weather"],
|
||||
"tools": [
|
||||
"python",
|
||||
"url-get",
|
||||
"terminal",
|
||||
"meteo-weather"
|
||||
],
|
||||
"kwargs": {
|
||||
"top_k_results": 2,
|
||||
"no_default": false,
|
||||
"model_name": "gpt-3.5-turbo"
|
||||
"top_k_results": 2,
|
||||
"no_default": false,
|
||||
"model_name": "gpt-3.5-turbo"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import os
|
||||
from chatgpt_tool_hub.apps import load_app
|
||||
from chatgpt_tool_hub.apps.app import App
|
||||
from chatgpt_tool_hub.tools.all_tool_list import get_all_tool_names
|
||||
|
||||
import plugins
|
||||
from bridge.bridge import Bridge
|
||||
from bridge.context import ContextType
|
||||
@@ -14,7 +15,13 @@ from config import conf
|
||||
from plugins import *
|
||||
|
||||
|
||||
@plugins.register(name="tool", desc="Arming your ChatGPT bot with various tools", version="0.3", author="goldfishh", desire_priority=0)
|
||||
@plugins.register(
|
||||
name="tool",
|
||||
desc="Arming your ChatGPT bot with various tools",
|
||||
version="0.3",
|
||||
author="goldfishh",
|
||||
desire_priority=0,
|
||||
)
|
||||
class Tool(Plugin):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
@@ -28,22 +35,26 @@ class Tool(Plugin):
|
||||
help_text = "这是一个能让chatgpt联网,搜索,数字运算的插件,将赋予强大且丰富的扩展能力。"
|
||||
if not verbose:
|
||||
return help_text
|
||||
trigger_prefix = conf().get('plugin_trigger_prefix', "$")
|
||||
trigger_prefix = conf().get("plugin_trigger_prefix", "$")
|
||||
help_text += "使用说明:\n"
|
||||
help_text += f"{trigger_prefix}tool "+"命令: 根据给出的{命令}使用一些可用工具尽力为你得到结果。\n"
|
||||
help_text += f"{trigger_prefix}tool " + "命令: 根据给出的{命令}使用一些可用工具尽力为你得到结果。\n"
|
||||
help_text += f"{trigger_prefix}tool reset: 重置工具。\n"
|
||||
return help_text
|
||||
|
||||
def on_handle_context(self, e_context: EventContext):
|
||||
if e_context['context'].type != ContextType.TEXT:
|
||||
if e_context["context"].type != ContextType.TEXT:
|
||||
return
|
||||
|
||||
# 暂时不支持未来扩展的bot
|
||||
if Bridge().get_bot_type("chat") not in (const.CHATGPT, const.OPEN_AI, const.CHATGPTONAZURE):
|
||||
if Bridge().get_bot_type("chat") not in (
|
||||
const.CHATGPT,
|
||||
const.OPEN_AI,
|
||||
const.CHATGPTONAZURE,
|
||||
):
|
||||
return
|
||||
|
||||
content = e_context['context'].content
|
||||
content_list = e_context['context'].content.split(maxsplit=1)
|
||||
content = e_context["context"].content
|
||||
content_list = e_context["context"].content.split(maxsplit=1)
|
||||
|
||||
if not content or len(content_list) < 1:
|
||||
e_context.action = EventAction.CONTINUE
|
||||
@@ -52,13 +63,13 @@ class Tool(Plugin):
|
||||
logger.debug("[tool] on_handle_context. content: %s" % content)
|
||||
reply = Reply()
|
||||
reply.type = ReplyType.TEXT
|
||||
trigger_prefix = conf().get('plugin_trigger_prefix', "$")
|
||||
trigger_prefix = conf().get("plugin_trigger_prefix", "$")
|
||||
# todo: 有些工具必须要api-key,需要修改config文件,所以这里没有实现query增删tool的功能
|
||||
if content.startswith(f"{trigger_prefix}tool"):
|
||||
if len(content_list) == 1:
|
||||
logger.debug("[tool]: get help")
|
||||
reply.content = self.get_help_text()
|
||||
e_context['reply'] = reply
|
||||
e_context["reply"] = reply
|
||||
e_context.action = EventAction.BREAK_PASS
|
||||
return
|
||||
elif len(content_list) > 1:
|
||||
@@ -66,12 +77,14 @@ class Tool(Plugin):
|
||||
logger.debug("[tool]: reset config")
|
||||
self.app = self._reset_app()
|
||||
reply.content = "重置工具成功"
|
||||
e_context['reply'] = reply
|
||||
e_context["reply"] = reply
|
||||
e_context.action = EventAction.BREAK_PASS
|
||||
return
|
||||
elif content_list[1].startswith("reset"):
|
||||
logger.debug("[tool]: remind")
|
||||
e_context['context'].content = "请你随机用一种聊天风格,提醒用户:如果想重置tool插件,reset之后不要加任何字符"
|
||||
e_context[
|
||||
"context"
|
||||
].content = "请你随机用一种聊天风格,提醒用户:如果想重置tool插件,reset之后不要加任何字符"
|
||||
|
||||
e_context.action = EventAction.BREAK
|
||||
return
|
||||
@@ -80,34 +93,35 @@ class Tool(Plugin):
|
||||
|
||||
# Don't modify bot name
|
||||
all_sessions = Bridge().get_bot("chat").sessions
|
||||
user_session = all_sessions.session_query(query, e_context['context']['session_id']).messages
|
||||
user_session = all_sessions.session_query(
|
||||
query, e_context["context"]["session_id"]
|
||||
).messages
|
||||
|
||||
# chatgpt-tool-hub will reply you with many tools
|
||||
logger.debug("[tool]: just-go")
|
||||
try:
|
||||
_reply = self.app.ask(query, user_session)
|
||||
e_context.action = EventAction.BREAK_PASS
|
||||
all_sessions.session_reply(_reply, e_context['context']['session_id'])
|
||||
all_sessions.session_reply(
|
||||
_reply, e_context["context"]["session_id"]
|
||||
)
|
||||
except Exception as e:
|
||||
logger.exception(e)
|
||||
logger.error(str(e))
|
||||
|
||||
e_context['context'].content = "请你随机用一种聊天风格,提醒用户:这个问题tool插件暂时无法处理"
|
||||
e_context["context"].content = "请你随机用一种聊天风格,提醒用户:这个问题tool插件暂时无法处理"
|
||||
reply.type = ReplyType.ERROR
|
||||
e_context.action = EventAction.BREAK
|
||||
return
|
||||
|
||||
reply.content = _reply
|
||||
e_context['reply'] = reply
|
||||
e_context["reply"] = reply
|
||||
return
|
||||
|
||||
def _read_json(self) -> dict:
|
||||
curdir = os.path.dirname(__file__)
|
||||
config_path = os.path.join(curdir, "config.json")
|
||||
tool_config = {
|
||||
"tools": [],
|
||||
"kwargs": {}
|
||||
}
|
||||
tool_config = {"tools": [], "kwargs": {}}
|
||||
if not os.path.exists(config_path):
|
||||
return tool_config
|
||||
else:
|
||||
@@ -123,7 +137,9 @@ class Tool(Plugin):
|
||||
"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"),
|
||||
"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),
|
||||
# for news tool
|
||||
@@ -160,4 +176,7 @@ class Tool(Plugin):
|
||||
# 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 load_app(
|
||||
tools_list=tool_list,
|
||||
**self._build_tool_kwargs(tool_config.get("kwargs", {})),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user