feat: 在插件层面初步支持内容函数

This commit is contained in:
Rock Chin 2023-07-27 14:27:36 +08:00
parent 46cc9220c3
commit caa72fa40c
4 changed files with 43 additions and 11 deletions

View File

@ -47,7 +47,7 @@ def init_db():
def ensure_dependencies():
import pkg.utils.pkgmgr as pkgmgr
pkgmgr.run_pip(["install", "openai", "Pillow", "nakuru-project-idk", "--upgrade",
pkgmgr.run_pip(["install", "openai", "Pillow", "nakuru-project-idk", "CallingGPT", "--upgrade",
"-i", "https://pypi.tuna.tsinghua.edu.cn/simple",
"--trusted-host", "pypi.tuna.tsinghua.edu.cn"])

View File

@ -16,6 +16,8 @@ import pkg.qqbot.adapter as msadapter
from mirai import Mirai
from CallingGPT.session.session import Session
__plugins__ = {}
"""插件列表
@ -42,6 +44,9 @@ __plugins__ = {}
__plugins_order__ = []
"""插件顺序"""
__callable_functions__ = []
"""供GPT调用的函数"""
def generate_plugin_order():
"""根据__plugin__生成插件初始顺序无视是否启用"""
@ -300,7 +305,9 @@ class PluginHost:
"""插件宿主"""
def __init__(self):
"""初始化插件宿主"""
context.set_plugin_host(self)
self.calling_gpt_session = Session([])
def get_runtime_context(self) -> context:
"""获取运行时上下文pkg.utils.context模块的对象

View File

@ -132,6 +132,13 @@ KeySwitched = "key_switched"
key_list: list[str] api-key列表
"""
ContentFunction = "content_function"
"""声明此函数为一个内容函数在对话中将发送此函数给GPT以供其调用
此函数可以具有任意的参数但必须按照[此文档](https://github.com/RockChinQ/CallingGPT/wiki/1.-Function-Format#function-format)
所述的格式编写函数的docstring
此功能仅支持在使用gpt-3.5或gpt-4系列模型时使用
"""
def on(event: str):
"""注册事件监听器
@ -161,20 +168,37 @@ class Plugin:
"""
global __current_registering_plugin__
def wrapper(func):
plugin_hooks = host.__plugins__[__current_registering_plugin__]["hooks"]
if event != ContentFunction:
def wrapper(func):
plugin_hooks = host.__plugins__[__current_registering_plugin__]["hooks"]
if event not in plugin_hooks:
plugin_hooks[event] = []
plugin_hooks[event].append(func)
if event not in plugin_hooks:
plugin_hooks[event] = []
plugin_hooks[event].append(func)
# print("registering hook: p='{}', e='{}', f={}".format(__current_registering_plugin__, event, func))
# print("registering hook: p='{}', e='{}', f={}".format(__current_registering_plugin__, event, func))
host.__plugins__[__current_registering_plugin__]["hooks"] = plugin_hooks
host.__plugins__[__current_registering_plugin__]["hooks"] = plugin_hooks
return func
return func
return wrapper
return wrapper
else:
from CallingGPT.entities.namespace import get_func_schema
def wrapper(func):
function_schema = get_func_schema(func)
# logging.debug("registering content function: p='{}', f='{}', s={}".format(__current_registering_plugin__, func, function_schema))
host.__callable_functions__.append(
function_schema
)
return func
return wrapper
def register(name: str, description: str, version: str, author: str):

View File

@ -7,4 +7,5 @@ websockets
urllib3~=1.26.10
func_timeout~=4.3.5
Pillow
nakuru-project-idk
nakuru-project-idk
CallingGPT