mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-03-19 21:38:18 +08:00
Merge Pull Request #663 into master
This commit is contained in:
@@ -50,7 +50,7 @@ class SessionManager(object):
|
|||||||
'''
|
'''
|
||||||
if session_id not in self.sessions:
|
if session_id not in self.sessions:
|
||||||
self.sessions[session_id] = self.sessioncls(session_id, system_prompt, **self.session_args)
|
self.sessions[session_id] = self.sessioncls(session_id, system_prompt, **self.session_args)
|
||||||
elif system_prompt is not None: # 如果有新的system_prompt,更新并重置session
|
elif system_prompt is not None: # 如果有新的system_prompt,更新并重置session
|
||||||
self.sessions[session_id].set_system_prompt(system_prompt)
|
self.sessions[session_id].set_system_prompt(system_prompt)
|
||||||
session = self.sessions[session_id]
|
session = self.sessions[session_id]
|
||||||
return session
|
return session
|
||||||
|
|||||||
60
plugins/tool/README.md
Normal file
60
plugins/tool/README.md
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
## 插件描述
|
||||||
|
一个能让chatgpt联网,搜索,数字运算的插件,将赋予强大且丰富的扩展能力
|
||||||
|
使用该插件需在触发机器人回复条件时,在对话内容前加$tool
|
||||||
|
### 本插件所有工具同步存放至专用仓库:[chatgpt-tool-hub](https://github.com/goldfishh/chatgpt-tool-hub)
|
||||||
|
|
||||||
|
|
||||||
|
## 使用说明
|
||||||
|
使用该插件后将默认使用4个工具, 无需额外配置长期生效:
|
||||||
|
### 1. python
|
||||||
|
###### python解释器,使用它来解释执行python指令,可以配合你想要chatgpt生成的代码输出结果或执行事务
|
||||||
|
|
||||||
|
### 2. requests
|
||||||
|
###### 往往用来获取某个网站具体内容,结果可能会被反爬策略影响
|
||||||
|
|
||||||
|
### 3. terminal
|
||||||
|
###### 在你运行的电脑里执行shell命令,可以配合你想要chatgpt生成的代码使用,给予自然语言控制手段
|
||||||
|
|
||||||
|
### 4. meteo-weather
|
||||||
|
###### 回答你有关天气的询问, 需要获取时间、地点上下文信息,本工具使用了[meteo open api](https://open-meteo.com/)
|
||||||
|
注:该工具需提供时间,地点信息,获取的数据不保证准确性
|
||||||
|
|
||||||
|
## 使用本插件对话(prompt)技巧
|
||||||
|
### 1. 有指引的询问
|
||||||
|
#### 例如:
|
||||||
|
- 总结这个链接的内容 https://github.com/goldfishh/chatgpt-tool-hub
|
||||||
|
- 使用Terminal执行curl cip.cc
|
||||||
|
- 使用python查询今天日期
|
||||||
|
|
||||||
|
### 2. 使用搜索引擎工具
|
||||||
|
- 如果有搜索工具就能让chatgpt获取到你的未传达清楚的上下文信息,比如chatgpt不知道你的地理位置,现在时间等,所以无法查询到天气
|
||||||
|
|
||||||
|
|
||||||
|
## 其他工具
|
||||||
|
###### 除上述以外还有其他工具,比如搜索联网、数学运算、新闻需要获取api-key,
|
||||||
|
###### 由于这些工具使用方法暂时还在整理中,如果你不熟悉请不要尝试使用这些工具
|
||||||
|
#### [申请方法](https://github.com/goldfishh/chatgpt-tool-hub/blob/master/docs/apply_optional_tool.md)
|
||||||
|
|
||||||
|
### 5. wikipedia
|
||||||
|
###### 可以回答你想要知道确切的人事物
|
||||||
|
|
||||||
|
## config.json 配置说明
|
||||||
|
###### 默认工具无需配置,其它工具需手动配置,一个例子:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"tools": ["wikipedia"],
|
||||||
|
"kwargs": {
|
||||||
|
"top_k_results": 2,
|
||||||
|
"no_default": false,
|
||||||
|
"model_name": "gpt-3.5-turbo"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
注:config.json文件非必须,未创建仍可使用本tool
|
||||||
|
- `tools`:本插件初始化时加载的工具, 目前可选集:["wikipedia", "wolfram-alpha", "bing-search", "google-search", "news"],其中后4个工具需要申请服务api
|
||||||
|
- `kwargs`:工具执行时的配置,一般在这里存放api-key,或环境配置,no_default用于配置是否默认使用4个工具,如果为false则仅使用tools列表工具
|
||||||
|
|
||||||
|
|
||||||
|
## 备注
|
||||||
|
- 虽然我会有意加入一些限制,但请不要使用本插件做危害他人的事情,请提前了解清楚某些内容是否会违反相关规定,建议提前做好过滤
|
||||||
|
- 未来一段时间我会实现一些有意思的工具,比如stable diffusion 中文prompt翻译、cv方向的模型推理,欢迎有想法的朋友关注,一起扩展这个项目
|
||||||
0
plugins/tool/__init__.py
Normal file
0
plugins/tool/__init__.py
Normal file
8
plugins/tool/config.json.template
Normal file
8
plugins/tool/config.json.template
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"tools": ["python", "requests", "terminal", "meteo-weather"],
|
||||||
|
"kwargs": {
|
||||||
|
"top_k_results": 2,
|
||||||
|
"no_default": false,
|
||||||
|
"model_name": "gpt-3.5-turbo"
|
||||||
|
}
|
||||||
|
}
|
||||||
119
plugins/tool/tool.py
Normal file
119
plugins/tool/tool.py
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
from chatgpt_tool_hub.apps import load_app
|
||||||
|
from chatgpt_tool_hub.apps.app import App
|
||||||
|
|
||||||
|
import plugins
|
||||||
|
from bridge.bridge import Bridge
|
||||||
|
from bridge.context import ContextType
|
||||||
|
from bridge.reply import Reply, ReplyType
|
||||||
|
from common import const
|
||||||
|
from common.log import logger
|
||||||
|
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)
|
||||||
|
class Tool(Plugin):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
|
||||||
|
os.environ["OPENAI_API_KEY"] = conf().get("open_ai_api_key", "")
|
||||||
|
os.environ["PROXY"] = conf().get("proxy", "")
|
||||||
|
|
||||||
|
self.app = self._reset_app()
|
||||||
|
|
||||||
|
logger.info("[tool] inited")
|
||||||
|
|
||||||
|
def get_help_text(self, **kwargs):
|
||||||
|
help_text = "这是一个能让chatgpt联网,搜索,数字运算的插件,将赋予强大且丰富的扩展能力"
|
||||||
|
return help_text
|
||||||
|
|
||||||
|
def on_handle_context(self, e_context: EventContext):
|
||||||
|
if e_context['context'].type != ContextType.TEXT:
|
||||||
|
return
|
||||||
|
|
||||||
|
# 暂时不支持未来扩展的bot
|
||||||
|
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)
|
||||||
|
|
||||||
|
if not content or len(content_list) < 1:
|
||||||
|
e_context.action = EventAction.CONTINUE
|
||||||
|
return
|
||||||
|
|
||||||
|
logger.debug("[tool] on_handle_context. content: %s" % content)
|
||||||
|
reply = Reply()
|
||||||
|
reply.type = ReplyType.TEXT
|
||||||
|
|
||||||
|
# todo: 有些工具必须要api-key,需要修改config文件,所以这里没有实现query增删tool的功能
|
||||||
|
if content.startswith("$tool"):
|
||||||
|
if len(content_list) == 1:
|
||||||
|
logger.debug("[tool]: get help")
|
||||||
|
reply.content = self.get_help_text()
|
||||||
|
e_context['reply'] = reply
|
||||||
|
e_context.action = EventAction.BREAK_PASS
|
||||||
|
return
|
||||||
|
elif len(content_list) > 1:
|
||||||
|
if content_list[1].strip() == "reset":
|
||||||
|
logger.debug("[tool]: reset config")
|
||||||
|
self.app = self._reset_app()
|
||||||
|
reply.content = "重置工具成功"
|
||||||
|
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.action = EventAction.BREAK
|
||||||
|
return
|
||||||
|
|
||||||
|
query = content_list[1].strip()
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# 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'])
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception(e)
|
||||||
|
logger.error(str(e))
|
||||||
|
|
||||||
|
e_context['context'].content = "请你随机用一种聊天风格,提醒用户:这个问题tool插件暂时无法处理"
|
||||||
|
reply.type = ReplyType.ERROR
|
||||||
|
e_context.action = EventAction.BREAK
|
||||||
|
return
|
||||||
|
|
||||||
|
reply.content = _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": {}
|
||||||
|
}
|
||||||
|
if not os.path.exists(config_path):
|
||||||
|
return tool_config
|
||||||
|
else:
|
||||||
|
with open(config_path, "r") as f:
|
||||||
|
tool_config = json.load(f)
|
||||||
|
return tool_config
|
||||||
|
|
||||||
|
def _reset_app(self) -> App:
|
||||||
|
tool_config = self._read_json()
|
||||||
|
kwargs = tool_config.get("kwargs", {})
|
||||||
|
if kwargs.get("model_name", "") == "":
|
||||||
|
kwargs["model_name"] = conf().get("model", "gpt-3.5-turbo")
|
||||||
|
return load_app(tools_list=tool_config.get("tools"), **tool_config.get("kwargs"))
|
||||||
@@ -13,6 +13,9 @@ wechaty>=0.10.7
|
|||||||
wechaty_puppet>=0.4.23
|
wechaty_puppet>=0.4.23
|
||||||
pysilk_mod>=1.6.0 # needed by send voice
|
pysilk_mod>=1.6.0 # needed by send voice
|
||||||
|
|
||||||
# webuiapi plugin
|
# sdwebui plugin
|
||||||
webuiapi>=0.6.2
|
webuiapi>=0.6.2
|
||||||
|
|
||||||
|
# chatgpt-tool-hub plugin
|
||||||
|
--extra-index-url https://pypi.python.org/simple
|
||||||
|
chatgpt_tool_hub>=0.3.5
|
||||||
@@ -3,4 +3,4 @@ HTMLParser>=0.0.2
|
|||||||
PyQRCode>=1.2.1
|
PyQRCode>=1.2.1
|
||||||
qrcode>=7.4.2
|
qrcode>=7.4.2
|
||||||
requests>=2.28.2
|
requests>=2.28.2
|
||||||
chardet>=5.1.0
|
chardet>=5.1.0
|
||||||
|
|||||||
Reference in New Issue
Block a user