mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-18 03:34:20 +00:00
feat: 插件开关对其内容函数生效
This commit is contained in:
@@ -14,7 +14,15 @@ def get_func_schema_list() -> list:
|
|||||||
if not host.__enable_content_functions__:
|
if not host.__enable_content_functions__:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
schemas = host.__callable_functions__
|
schemas = []
|
||||||
|
|
||||||
|
for func in host.__callable_functions__:
|
||||||
|
if func['enabled']:
|
||||||
|
fun_cp = func.copy()
|
||||||
|
|
||||||
|
del fun_cp['enabled']
|
||||||
|
|
||||||
|
schemas.append(fun_cp)
|
||||||
|
|
||||||
return schemas
|
return schemas
|
||||||
|
|
||||||
|
|||||||
+15
-39
@@ -132,13 +132,6 @@ 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(*args, **kwargs):
|
def on(*args, **kwargs):
|
||||||
"""注册事件监听器
|
"""注册事件监听器
|
||||||
@@ -146,7 +139,10 @@ def on(*args, **kwargs):
|
|||||||
return Plugin.on(*args, **kwargs)
|
return Plugin.on(*args, **kwargs)
|
||||||
|
|
||||||
def func(*args, **kwargs):
|
def func(*args, **kwargs):
|
||||||
"""注册内容函数
|
"""注册内容函数,声明此函数为一个内容函数,在对话中将发送此函数给GPT以供其调用
|
||||||
|
此函数可以具有任意的参数,但必须按照[此文档](https://github.com/RockChinQ/CallingGPT/wiki/1.-Function-Format#function-format)
|
||||||
|
所述的格式编写函数的docstring。
|
||||||
|
此功能仅支持在使用gpt-3.5或gpt-4系列模型时使用。
|
||||||
"""
|
"""
|
||||||
return Plugin.func(*args, **kwargs)
|
return Plugin.func(*args, **kwargs)
|
||||||
|
|
||||||
@@ -171,42 +167,20 @@ class Plugin:
|
|||||||
"""
|
"""
|
||||||
global __current_registering_plugin__
|
global __current_registering_plugin__
|
||||||
|
|
||||||
if event != ContentFunction:
|
def wrapper(func):
|
||||||
def wrapper(func):
|
plugin_hooks = host.__plugins__[__current_registering_plugin__]["hooks"]
|
||||||
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)
|
|
||||||
function_schema['name'] = __current_registering_plugin__ + '-' + func.__name__
|
|
||||||
|
|
||||||
host.__function_inst_map__[function_schema['name']] = function_schema['function']
|
|
||||||
|
|
||||||
del function_schema['function']
|
|
||||||
|
|
||||||
# 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
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def func(cls, name: str=None):
|
def func(cls, name: str=None):
|
||||||
@@ -220,6 +194,8 @@ class Plugin:
|
|||||||
function_schema = get_func_schema(func)
|
function_schema = get_func_schema(func)
|
||||||
function_schema['name'] = __current_registering_plugin__ + '-' + (func.__name__ if name is None else name)
|
function_schema['name'] = __current_registering_plugin__ + '-' + (func.__name__ if name is None else name)
|
||||||
|
|
||||||
|
function_schema['enabled'] = True
|
||||||
|
|
||||||
host.__function_inst_map__[function_schema['name']] = function_schema['function']
|
host.__function_inst_map__[function_schema['name']] = function_schema['function']
|
||||||
|
|
||||||
del function_schema['function']
|
del function_schema['function']
|
||||||
|
|||||||
@@ -28,6 +28,11 @@ def apply_switch(switch: dict):
|
|||||||
for plugin_name in switch:
|
for plugin_name in switch:
|
||||||
host.__plugins__[plugin_name]["enabled"] = switch[plugin_name]["enabled"]
|
host.__plugins__[plugin_name]["enabled"] = switch[plugin_name]["enabled"]
|
||||||
|
|
||||||
|
# 查找此插件的所有内容函数
|
||||||
|
for func in host.__callable_functions__:
|
||||||
|
if func['name'].startswith(plugin_name + '-'):
|
||||||
|
func['enabled'] = switch[plugin_name]["enabled"]
|
||||||
|
|
||||||
|
|
||||||
def dump_switch():
|
def dump_switch():
|
||||||
"""保存开关数据"""
|
"""保存开关数据"""
|
||||||
|
|||||||
Reference in New Issue
Block a user