diff --git a/main.py b/main.py index 218021c8..2f184215 100644 --- a/main.py +++ b/main.py @@ -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"]) diff --git a/pkg/plugin/host.py b/pkg/plugin/host.py index 4249c37e..7343fcfb 100644 --- a/pkg/plugin/host.py +++ b/pkg/plugin/host.py @@ -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模块的对象) diff --git a/pkg/plugin/models.py b/pkg/plugin/models.py index 180f0745..3f5f9ec3 100644 --- a/pkg/plugin/models.py +++ b/pkg/plugin/models.py @@ -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): diff --git a/requirements.txt b/requirements.txt index 64160e9f..60a072f8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,4 +7,5 @@ websockets urllib3~=1.26.10 func_timeout~=4.3.5 Pillow -nakuru-project-idk \ No newline at end of file +nakuru-project-idk +CallingGPT \ No newline at end of file