Skip to content

Commit f8a314a

Browse files
committed
Merge Pull Request zhayujie#663 into master
2 parents e4d5f65 + 55145e7 commit f8a314a

File tree

7 files changed

+193
-3
lines changed

7 files changed

+193
-3
lines changed

bot/session_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def build_session(self, session_id, system_prompt=None):
5050
'''
5151
if session_id not in self.sessions:
5252
self.sessions[session_id] = self.sessioncls(session_id, system_prompt, **self.session_args)
53-
elif system_prompt is not None: # 如果有新的system_prompt,更新并重置session
53+
elif system_prompt is not None: # 如果有新的system_prompt,更新并重置session
5454
self.sessions[session_id].set_system_prompt(system_prompt)
5555
session = self.sessions[session_id]
5656
return session

plugins/tool/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## 插件描述
2+
一个能让chatgpt联网,搜索,数字运算的插件,将赋予强大且丰富的扩展能力
3+
使用该插件需在触发机器人回复条件时,在对话内容前加$tool
4+
### 本插件所有工具同步存放至专用仓库:[chatgpt-tool-hub](https://github.com/goldfishh/chatgpt-tool-hub)
5+
6+
7+
## 使用说明
8+
使用该插件后将默认使用4个工具, 无需额外配置长期生效:
9+
### 1. python
10+
###### python解释器,使用它来解释执行python指令,可以配合你想要chatgpt生成的代码输出结果或执行事务
11+
12+
### 2. requests
13+
###### 往往用来获取某个网站具体内容,结果可能会被反爬策略影响
14+
15+
### 3. terminal
16+
###### 在你运行的电脑里执行shell命令,可以配合你想要chatgpt生成的代码使用,给予自然语言控制手段
17+
18+
### 4. meteo-weather
19+
###### 回答你有关天气的询问, 需要获取时间、地点上下文信息,本工具使用了[meteo open api](https://open-meteo.com/)
20+
注:该工具需提供时间,地点信息,获取的数据不保证准确性
21+
22+
## 使用本插件对话(prompt)技巧
23+
### 1. 有指引的询问
24+
#### 例如:
25+
- 总结这个链接的内容 https://github.com/goldfishh/chatgpt-tool-hub
26+
- 使用Terminal执行curl cip.cc
27+
- 使用python查询今天日期
28+
29+
### 2. 使用搜索引擎工具
30+
- 如果有搜索工具就能让chatgpt获取到你的未传达清楚的上下文信息,比如chatgpt不知道你的地理位置,现在时间等,所以无法查询到天气
31+
32+
33+
## 其他工具
34+
###### 除上述以外还有其他工具,比如搜索联网、数学运算、新闻需要获取api-key,
35+
###### 由于这些工具使用方法暂时还在整理中,如果你不熟悉请不要尝试使用这些工具
36+
#### [申请方法](https://github.com/goldfishh/chatgpt-tool-hub/blob/master/docs/apply_optional_tool.md)
37+
38+
### 5. wikipedia
39+
###### 可以回答你想要知道确切的人事物
40+
41+
## config.json 配置说明
42+
###### 默认工具无需配置,其它工具需手动配置,一个例子:
43+
```json
44+
{
45+
"tools": ["wikipedia"],
46+
"kwargs": {
47+
"top_k_results": 2,
48+
"no_default": false,
49+
"model_name": "gpt-3.5-turbo"
50+
}
51+
}
52+
```
53+
注:config.json文件非必须,未创建仍可使用本tool
54+
- `tools`:本插件初始化时加载的工具, 目前可选集:["wikipedia", "wolfram-alpha", "bing-search", "google-search", "news"],其中后4个工具需要申请服务api
55+
- `kwargs`:工具执行时的配置,一般在这里存放api-key,或环境配置,no_default用于配置是否默认使用4个工具,如果为false则仅使用tools列表工具
56+
57+
58+
## 备注
59+
- 虽然我会有意加入一些限制,但请不要使用本插件做危害他人的事情,请提前了解清楚某些内容是否会违反相关规定,建议提前做好过滤
60+
- 未来一段时间我会实现一些有意思的工具,比如stable diffusion 中文prompt翻译、cv方向的模型推理,欢迎有想法的朋友关注,一起扩展这个项目

plugins/tool/__init__.py

Whitespace-only changes.

plugins/tool/config.json.template

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"tools": ["python", "requests", "terminal", "meteo-weather"],
3+
"kwargs": {
4+
"top_k_results": 2,
5+
"no_default": false,
6+
"model_name": "gpt-3.5-turbo"
7+
}
8+
}

plugins/tool/tool.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import json
2+
import os
3+
4+
from chatgpt_tool_hub.apps import load_app
5+
from chatgpt_tool_hub.apps.app import App
6+
7+
import plugins
8+
from bridge.bridge import Bridge
9+
from bridge.context import ContextType
10+
from bridge.reply import Reply, ReplyType
11+
from common import const
12+
from common.log import logger
13+
from config import conf
14+
from plugins import *
15+
16+
17+
@plugins.register(name="tool", desc="Arming your ChatGPT bot with various tools", version="0.3", author="goldfishh", desire_priority=0)
18+
class Tool(Plugin):
19+
def __init__(self):
20+
super().__init__()
21+
self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
22+
os.environ["OPENAI_API_KEY"] = conf().get("open_ai_api_key", "")
23+
os.environ["PROXY"] = conf().get("proxy", "")
24+
25+
self.app = self._reset_app()
26+
27+
logger.info("[tool] inited")
28+
29+
def get_help_text(self, **kwargs):
30+
help_text = "这是一个能让chatgpt联网,搜索,数字运算的插件,将赋予强大且丰富的扩展能力"
31+
return help_text
32+
33+
def on_handle_context(self, e_context: EventContext):
34+
if e_context['context'].type != ContextType.TEXT:
35+
return
36+
37+
# 暂时不支持未来扩展的bot
38+
if Bridge().get_bot_type("chat") not in (const.CHATGPT, const.OPEN_AI, const.CHATGPTONAZURE):
39+
return
40+
41+
content = e_context['context'].content
42+
content_list = e_context['context'].content.split(maxsplit=1)
43+
44+
if not content or len(content_list) < 1:
45+
e_context.action = EventAction.CONTINUE
46+
return
47+
48+
logger.debug("[tool] on_handle_context. content: %s" % content)
49+
reply = Reply()
50+
reply.type = ReplyType.TEXT
51+
52+
# todo: 有些工具必须要api-key,需要修改config文件,所以这里没有实现query增删tool的功能
53+
if content.startswith("$tool"):
54+
if len(content_list) == 1:
55+
logger.debug("[tool]: get help")
56+
reply.content = self.get_help_text()
57+
e_context['reply'] = reply
58+
e_context.action = EventAction.BREAK_PASS
59+
return
60+
elif len(content_list) > 1:
61+
if content_list[1].strip() == "reset":
62+
logger.debug("[tool]: reset config")
63+
self.app = self._reset_app()
64+
reply.content = "重置工具成功"
65+
e_context['reply'] = reply
66+
e_context.action = EventAction.BREAK_PASS
67+
return
68+
elif content_list[1].startswith("reset"):
69+
logger.debug("[tool]: remind")
70+
e_context['context'].content = "请你随机用一种聊天风格,提醒用户:如果想重置tool插件,reset之后不要加任何字符"
71+
72+
e_context.action = EventAction.BREAK
73+
return
74+
75+
query = content_list[1].strip()
76+
77+
# Don't modify bot name
78+
all_sessions = Bridge().get_bot("chat").sessions
79+
user_session = all_sessions.session_query(query, e_context['context']['session_id']).messages
80+
81+
# chatgpt-tool-hub will reply you with many tools
82+
logger.debug("[tool]: just-go")
83+
try:
84+
_reply = self.app.ask(query, user_session)
85+
e_context.action = EventAction.BREAK_PASS
86+
all_sessions.session_reply(_reply, e_context['context']['session_id'])
87+
except Exception as e:
88+
logger.exception(e)
89+
logger.error(str(e))
90+
91+
e_context['context'].content = "请你随机用一种聊天风格,提醒用户:这个问题tool插件暂时无法处理"
92+
reply.type = ReplyType.ERROR
93+
e_context.action = EventAction.BREAK
94+
return
95+
96+
reply.content = _reply
97+
e_context['reply'] = reply
98+
return
99+
100+
def _read_json(self) -> dict:
101+
curdir = os.path.dirname(__file__)
102+
config_path = os.path.join(curdir, "config.json")
103+
tool_config = {
104+
"tools": [],
105+
"kwargs": {}
106+
}
107+
if not os.path.exists(config_path):
108+
return tool_config
109+
else:
110+
with open(config_path, "r") as f:
111+
tool_config = json.load(f)
112+
return tool_config
113+
114+
def _reset_app(self) -> App:
115+
tool_config = self._read_json()
116+
kwargs = tool_config.get("kwargs", {})
117+
if kwargs.get("model_name", "") == "":
118+
kwargs["model_name"] = conf().get("model", "gpt-3.5-turbo")
119+
return load_app(tools_list=tool_config.get("tools"), **tool_config.get("kwargs"))

requirements-optional.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ wechaty>=0.10.7
1313
wechaty_puppet>=0.4.23
1414
pysilk_mod>=1.6.0 # needed by send voice
1515

16-
# webuiapi plugin
16+
# sdwebui plugin
1717
webuiapi>=0.6.2
1818

19+
# chatgpt-tool-hub plugin
20+
--extra-index-url https://pypi.python.org/simple
21+
chatgpt_tool_hub>=0.3.5

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ HTMLParser>=0.0.2
33
PyQRCode>=1.2.1
44
qrcode>=7.4.2
55
requests>=2.28.2
6-
chardet>=5.1.0
6+
chardet>=5.1.0

0 commit comments

Comments
 (0)