Files
LangBot/pkg/provider/tools/toolmgr.py

117 lines
3.4 KiB
Python
Raw Normal View History

2024-01-27 21:50:40 +08:00
from __future__ import annotations
import typing
2024-01-29 21:41:20 +08:00
import traceback
2024-01-27 21:50:40 +08:00
from ...core import app, entities as core_entities
from . import entities
from ...plugin import context as plugin_context
2024-01-27 21:50:40 +08:00
class ToolManager:
"""LLM工具管理器
"""
ap: app.Application
def __init__(self, ap: app.Application):
self.ap = ap
self.all_functions = []
async def initialize(self):
pass
async def get_function(self, name: str) -> entities.LLMFunction:
"""获取函数
"""
2024-01-29 21:22:27 +08:00
for function in await self.get_all_functions():
2024-01-27 21:50:40 +08:00
if function.name == name:
return function
return None
async def get_function_and_plugin(self, name: str) -> typing.Tuple[entities.LLMFunction, plugin_context.BasePlugin]:
"""获取函数和插件
"""
for plugin in self.ap.plugin_mgr.plugins:
for function in plugin.content_functions:
if function.name == name:
return function, plugin
return None, None
2024-01-27 21:50:40 +08:00
async def get_all_functions(self) -> list[entities.LLMFunction]:
"""获取所有函数
"""
2024-01-29 21:22:27 +08:00
all_functions: list[entities.LLMFunction] = []
2024-01-27 21:50:40 +08:00
2024-01-29 21:22:27 +08:00
for plugin in self.ap.plugin_mgr.plugins:
all_functions.extend(plugin.content_functions)
return all_functions
2024-02-01 16:35:00 +08:00
async def generate_tools_for_openai(self, use_funcs: entities.LLMFunction) -> str:
2024-01-27 21:50:40 +08:00
"""生成函数列表
"""
tools = []
2024-02-01 16:35:00 +08:00
for function in use_funcs:
2024-01-27 21:50:40 +08:00
if function.enable:
function_schema = {
"type": "function",
"function": {
"name": function.name,
"description": function.description,
"parameters": function.parameters
}
}
tools.append(function_schema)
return tools
async def execute_func_call(
self,
query: core_entities.Query,
name: str,
parameters: dict
) -> typing.Any:
"""执行函数调用
"""
2024-01-29 21:41:20 +08:00
try:
2024-01-27 21:50:40 +08:00
function, plugin = await self.get_function_and_plugin(name)
2024-01-29 21:41:20 +08:00
if function is None:
return None
parameters = parameters.copy()
2024-01-27 21:50:40 +08:00
2024-01-29 21:41:20 +08:00
parameters = {
"query": query,
**parameters
}
return await function.func(plugin, **parameters)
2024-01-29 21:41:20 +08:00
except Exception as e:
self.ap.logger.error(f'执行函数 {name} 时发生错误: {e}')
traceback.print_exc()
return f'error occurred when executing function {name}: {e}'
2024-01-31 00:02:19 +08:00
finally:
plugin = None
for p in self.ap.plugin_mgr.plugins:
if function in p.content_functions:
plugin = p
break
if plugin is not None:
await self.ap.ctr_mgr.usage.post_function_record(
plugin={
'name': plugin.plugin_name,
'remote': plugin.plugin_source,
'version': plugin.plugin_version,
'author': plugin.plugin_author
},
function_name=function.name,
function_description=function.description,
)