mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-14 22:40:59 +00:00
feat: 在插件层面初步支持内容函数
This commit is contained in:
@@ -47,7 +47,7 @@ def init_db():
|
|||||||
|
|
||||||
def ensure_dependencies():
|
def ensure_dependencies():
|
||||||
import pkg.utils.pkgmgr as pkgmgr
|
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",
|
"-i", "https://pypi.tuna.tsinghua.edu.cn/simple",
|
||||||
"--trusted-host", "pypi.tuna.tsinghua.edu.cn"])
|
"--trusted-host", "pypi.tuna.tsinghua.edu.cn"])
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ import pkg.qqbot.adapter as msadapter
|
|||||||
|
|
||||||
from mirai import Mirai
|
from mirai import Mirai
|
||||||
|
|
||||||
|
from CallingGPT.session.session import Session
|
||||||
|
|
||||||
__plugins__ = {}
|
__plugins__ = {}
|
||||||
"""插件列表
|
"""插件列表
|
||||||
|
|
||||||
@@ -42,6 +44,9 @@ __plugins__ = {}
|
|||||||
__plugins_order__ = []
|
__plugins_order__ = []
|
||||||
"""插件顺序"""
|
"""插件顺序"""
|
||||||
|
|
||||||
|
__callable_functions__ = []
|
||||||
|
"""供GPT调用的函数"""
|
||||||
|
|
||||||
|
|
||||||
def generate_plugin_order():
|
def generate_plugin_order():
|
||||||
"""根据__plugin__生成插件初始顺序,无视是否启用"""
|
"""根据__plugin__生成插件初始顺序,无视是否启用"""
|
||||||
@@ -300,7 +305,9 @@ class PluginHost:
|
|||||||
"""插件宿主"""
|
"""插件宿主"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
"""初始化插件宿主"""
|
||||||
context.set_plugin_host(self)
|
context.set_plugin_host(self)
|
||||||
|
self.calling_gpt_session = Session([])
|
||||||
|
|
||||||
def get_runtime_context(self) -> context:
|
def get_runtime_context(self) -> context:
|
||||||
"""获取运行时上下文(pkg.utils.context模块的对象)
|
"""获取运行时上下文(pkg.utils.context模块的对象)
|
||||||
|
|||||||
+33
-9
@@ -132,6 +132,13 @@ KeySwitched = "key_switched"
|
|||||||
key_list: list[str] api-key列表
|
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):
|
def on(event: str):
|
||||||
"""注册事件监听器
|
"""注册事件监听器
|
||||||
@@ -161,20 +168,37 @@ class Plugin:
|
|||||||
"""
|
"""
|
||||||
global __current_registering_plugin__
|
global __current_registering_plugin__
|
||||||
|
|
||||||
def wrapper(func):
|
if event != ContentFunction:
|
||||||
plugin_hooks = host.__plugins__[__current_registering_plugin__]["hooks"]
|
def wrapper(func):
|
||||||
|
plugin_hooks = host.__plugins__[__current_registering_plugin__]["hooks"]
|
||||||
|
|
||||||
if event not in plugin_hooks:
|
if event not in plugin_hooks:
|
||||||
plugin_hooks[event] = []
|
plugin_hooks[event] = []
|
||||||
plugin_hooks[event].append(func)
|
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):
|
def register(name: str, description: str, version: str, author: str):
|
||||||
|
|||||||
+2
-1
@@ -7,4 +7,5 @@ websockets
|
|||||||
urllib3~=1.26.10
|
urllib3~=1.26.10
|
||||||
func_timeout~=4.3.5
|
func_timeout~=4.3.5
|
||||||
Pillow
|
Pillow
|
||||||
nakuru-project-idk
|
nakuru-project-idk
|
||||||
|
CallingGPT
|
||||||
Reference in New Issue
Block a user