diff --git a/res/wiki/插件使用.md b/res/wiki/插件使用.md index f0db3f12..2469584d 100644 --- a/res/wiki/插件使用.md +++ b/res/wiki/插件使用.md @@ -33,6 +33,8 @@ QChatGPT 插件使用Wiki !plugin del <插件名> 删除插件(需要管理员权限) !plugin on <插件名> 启用插件(需要管理员权限) !plugin off <插件名> 禁用插件(需要管理员权限) + +!func 列出所有内容函数 ``` ### 控制插件执行顺序 diff --git a/res/wiki/插件开发.md b/res/wiki/插件开发.md index e3c6a7bb..6888f72c 100644 --- a/res/wiki/插件开发.md +++ b/res/wiki/插件开发.md @@ -113,6 +113,182 @@ class HelloPlugin(Plugin): - 一个目录内可以存放多个Python程序文件,以独立出插件的各个功能,便于开发者管理,但不建议在一个目录内注册多个插件 - 插件需要的依赖库请在插件目录下的`requirements.txt`中指定,程序从储存库获取此插件时将自动安装依赖 +## 🪝内容函数 + +通过[GPT的Function Calling能力](https://platform.openai.com/docs/guides/gpt/function-calling)实现的`内容函数`,这是一种嵌入对话中,由GPT自动调用的函数。 + +
+示例:联网插件 + +加载含有联网功能的内容函数的插件[WebwlkrPlugin](https://github.com/RockChinQ/WebwlkrPlugin),向机器人询问在线内容 + +``` +# 控制台输出 +[2023-07-29 17:37:18.698] message.py (26) - [INFO] : [person_1010553892]发送消息:介绍一下这个项目:https://git... +[2023-07-29 17:37:21.292] util.py (67) - [INFO] : message='OpenAI API response' path=https://api.openai.com/v1/chat/completions processing_ms=1902 request_id=941afc13b2e1bba1e7877b92a970cdea response_code=200 +[2023-07-29 17:37:21.293] chat_completion.py (159) - [INFO] : 执行函数调用: name=Webwlkr-access_the_web, arguments={'url': 'https://github.com/RockChinQ/QChatGPT', 'brief_len': 512} +[2023-07-29 17:37:21.848] chat_completion.py (164) - [INFO] : 函数执行完成。 +``` + +![Webwlkr插件](https://github.com/RockChinQ/QChatGPT/blob/master/res/screenshots/webwlkr_plugin.png?raw=true) + +
+ +### 内容函数编写步骤 + +1️⃣ 请先按照上方步骤编写您的插件基础结构,现在请删除(当然你也可以不删,只是为了简洁)上述插件内容的诸个由`@on`装饰的类函数 + +
+删除后的结构 + +```python +from pkg.plugin.models import * +from pkg.plugin.host import EventContext, PluginHost + +""" +在收到私聊或群聊消息"hello"时,回复"hello, <发送者id>!"或"hello, everyone!" +""" + + +# 注册插件 +@register(name="Hello", description="hello world", version="0.1", author="RockChinQ") +class HelloPlugin(Plugin): + + # 插件加载时触发 + # plugin_host (pkg.plugin.host.PluginHost) 提供了与主程序交互的一些方法,详细请查看其源码 + def __init__(self, plugin_host: PluginHost): + pass + + # 插件卸载时触发 + def __del__(self): + pass +``` + +
+ +2️⃣ 现在我们将以下函数添加到刚刚删除的函数的位置 + +```Python + +# 要添加的函数 + +@func(name="access_the_web") # 设置函数名称 +def _(url: str): + """Call this function to search about the question before you answer any questions. + - Do not search through baidu.com at any time. + - If you need to search somthing, visit https://www.google.com/search?q=xxx. + - If user ask you to open a url (start with http:// or https://), visit it directly. + - Summary the plain content result by yourself, DO NOT directly output anything in the result you got. + + Args: + url(str): url to visit + + Returns: + str: plain text content of the web page + """ + import requests + from bs4 import BeautifulSoup + # 你需要先使用 + # pip install beautifulsoup4 + # 安装依赖 + + r = requests.get( + url, + timeout=10, + headers={ + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.183" + } + ) + soup = BeautifulSoup(r.text, 'html.parser') + + s = soup.get_text() + + # 删除多余的空行或仅有\t和空格的行 + s = re.sub(r'\n\s*\n', '\n', s) + + if len(s) >= 512: # 截取获取到的网页纯文本内容的前512个字 + return s[:512] + + return s + +``` +
+现在这个文件内容应该是这样 + +```python +from pkg.plugin.models import * +from pkg.plugin.host import EventContext, PluginHost + +""" +在收到私聊或群聊消息"hello"时,回复"hello, <发送者id>!"或"hello, everyone!" +""" + + +# 注册插件 +@register(name="Hello", description="hello world", version="0.1", author="RockChinQ") +class HelloPlugin(Plugin): + + # 插件加载时触发 + # plugin_host (pkg.plugin.host.PluginHost) 提供了与主程序交互的一些方法,详细请查看其源码 + def __init__(self, plugin_host: PluginHost): + pass + + @func(name="access_the_web") + def _(url: str): + """Call this function to search about the question before you answer any questions. + - Do not search through baidu.com at any time. + - If you need to search somthing, visit https://www.google.com/search?q=xxx. + - If user ask you to open a url (start with http:// or https://), visit it directly. + - Summary the plain content result by yourself, DO NOT directly output anything in the result you got. + + Args: + url(str): url to visit + + Returns: + str: plain text content of the web page + """ + import requests + from bs4 import BeautifulSoup + # 你需要先使用 + # pip install beautifulsoup4 + # 安装依赖 + + r = requests.get( + url, + timeout=10, + headers={ + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.183" + } + ) + soup = BeautifulSoup(r.text, 'html.parser') + + s = soup.get_text() + + # 删除多余的空行或仅有\t和空格的行 + s = re.sub(r'\n\s*\n', '\n', s) + + if len(s) >= 512: # 截取获取到的网页纯文本内容的前512个字 + return s[:512] + + return s + + # 插件卸载时触发 + def __del__(self): + pass +``` + +
+ +#### 请注意: + +- 函数的注释必须严格按照要求的格式进行书写,具体格式请查看[此文档](https://github.com/RockChinQ/CallingGPT/wiki/1.-Function-Format#function-format) +- 内容函数和`以@on装饰的行为函数`可以同时存在于同一个插件,并同时受到`switch.json`中的插件开关的控制 +- 务必确保您使用的模型支持函数调用功能,可以到`config.py`的`completion_api_params`中修改模型,推荐使用`gpt-3.5-turbo-16k` + +3️⃣ 现在您的程序已具备网络访问功能,重启程序,询问机器人有关在线的内容或直接发送文章链接请求其总结。 + +- 这仅仅是一个示例,需要更高效的网络访问能力支持插件,请查看[WebwlkrPlugin](https://github.com/RockChinQ/WebwlkrPlugin) + ## 📄API参考 ### 说明