mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-18 10:26:07 +00:00
feat: switch tool entities and format
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import typing
|
||||
|
||||
import pydantic.v1 as pydantic
|
||||
|
||||
|
||||
class LLMFunction(pydantic.BaseModel):
|
||||
"""函数"""
|
||||
|
||||
name: str
|
||||
"""函数名"""
|
||||
|
||||
human_desc: str
|
||||
|
||||
description: str
|
||||
"""给LLM识别的函数描述"""
|
||||
|
||||
parameters: dict
|
||||
|
||||
func: typing.Callable
|
||||
"""供调用的python异步方法
|
||||
|
||||
此异步方法第一个参数接收当前请求的query对象,可以从其中取出session等信息。
|
||||
query参数不在parameters中,但在调用时会自动传入。
|
||||
但在当前版本中,插件提供的内容函数都是同步的,且均为请求无关的,故在此版本的实现(以及考虑了向后兼容性的版本)中,
|
||||
对插件的内容函数进行封装并存到这里来。
|
||||
"""
|
||||
|
||||
class Config:
|
||||
arbitrary_types_allowed = True
|
||||
@@ -4,7 +4,7 @@ import abc
|
||||
import typing
|
||||
|
||||
from ...core import app, entities as core_entities
|
||||
from . import entities as tools_entities
|
||||
import langbot_plugin.api.entities.builtin.resource.tool as resource_tool
|
||||
|
||||
|
||||
preregistered_loaders: list[typing.Type[ToolLoader]] = []
|
||||
@@ -35,7 +35,7 @@ class ToolLoader(abc.ABC):
|
||||
pass
|
||||
|
||||
@abc.abstractmethod
|
||||
async def get_tools(self, enabled: bool = True) -> list[tools_entities.LLMFunction]:
|
||||
async def get_tools(self, enabled: bool = True) -> list[resource_tool.LLMTool]:
|
||||
"""获取所有工具"""
|
||||
pass
|
||||
|
||||
|
||||
@@ -7,8 +7,9 @@ from mcp import ClientSession, StdioServerParameters
|
||||
from mcp.client.stdio import stdio_client
|
||||
from mcp.client.sse import sse_client
|
||||
|
||||
from .. import loader, entities as tools_entities
|
||||
from .. import loader
|
||||
from ....core import app, entities as core_entities
|
||||
import langbot_plugin.api.entities.builtin.resource.tool as resource_tool
|
||||
|
||||
|
||||
class RuntimeMCPSession:
|
||||
@@ -24,7 +25,7 @@ class RuntimeMCPSession:
|
||||
|
||||
exit_stack: AsyncExitStack
|
||||
|
||||
functions: list[tools_entities.LLMFunction] = []
|
||||
functions: list[resource_tool.LLMTool] = []
|
||||
|
||||
def __init__(self, server_name: str, server_config: dict, ap: app.Application):
|
||||
self.server_name = server_name
|
||||
@@ -91,7 +92,7 @@ class RuntimeMCPSession:
|
||||
func.__name__ = tool.name
|
||||
|
||||
self.functions.append(
|
||||
tools_entities.LLMFunction(
|
||||
resource_tool.LLMTool(
|
||||
name=tool.name,
|
||||
human_desc=tool.description,
|
||||
description=tool.description,
|
||||
@@ -114,7 +115,7 @@ class MCPLoader(loader.ToolLoader):
|
||||
|
||||
sessions: dict[str, RuntimeMCPSession] = {}
|
||||
|
||||
_last_listed_functions: list[tools_entities.LLMFunction] = []
|
||||
_last_listed_functions: list[resource_tool.LLMTool] = []
|
||||
|
||||
def __init__(self, ap: app.Application):
|
||||
super().__init__(ap)
|
||||
@@ -130,7 +131,7 @@ class MCPLoader(loader.ToolLoader):
|
||||
# self.ap.event_loop.create_task(session.initialize())
|
||||
self.sessions[server_config['name']] = session
|
||||
|
||||
async def get_tools(self, enabled: bool = True) -> list[tools_entities.LLMFunction]:
|
||||
async def get_tools(self, enabled: bool = True) -> list[resource_tool.LLMTool]:
|
||||
all_functions = []
|
||||
|
||||
for session in self.sessions.values():
|
||||
|
||||
@@ -3,9 +3,10 @@ from __future__ import annotations
|
||||
import typing
|
||||
import traceback
|
||||
|
||||
from .. import loader, entities as tools_entities
|
||||
from .. import loader
|
||||
from ....core import entities as core_entities
|
||||
from ....plugin import context as plugin_context
|
||||
import langbot_plugin.api.entities.builtin.resource.tool as resource_tool
|
||||
|
||||
|
||||
@loader.loader_class('plugin-tool-loader')
|
||||
@@ -15,9 +16,9 @@ class PluginToolLoader(loader.ToolLoader):
|
||||
本加载器中不存储工具信息,仅负责从插件系统中获取工具信息。
|
||||
"""
|
||||
|
||||
async def get_tools(self, enabled: bool = True) -> list[tools_entities.LLMFunction]:
|
||||
async def get_tools(self, enabled: bool = True) -> list[resource_tool.LLMTool]:
|
||||
# 从插件系统获取工具(内容函数)
|
||||
all_functions: list[tools_entities.LLMFunction] = []
|
||||
all_functions: list[resource_tool.LLMTool] = []
|
||||
|
||||
for plugin in self.ap.plugin_mgr.plugins(
|
||||
enabled=enabled, status=plugin_context.RuntimeContainerStatus.INITIALIZED
|
||||
@@ -38,7 +39,7 @@ class PluginToolLoader(loader.ToolLoader):
|
||||
|
||||
async def _get_function_and_plugin(
|
||||
self, name: str
|
||||
) -> typing.Tuple[tools_entities.LLMFunction, plugin_context.BasePlugin]:
|
||||
) -> typing.Tuple[resource_tool.LLMTool, plugin_context.BasePlugin]:
|
||||
"""获取函数和插件实例"""
|
||||
for plugin in self.ap.plugin_mgr.plugins(
|
||||
enabled=True, status=plugin_context.RuntimeContainerStatus.INITIALIZED
|
||||
|
||||
@@ -3,9 +3,10 @@ from __future__ import annotations
|
||||
import typing
|
||||
|
||||
from ...core import app, entities as core_entities
|
||||
from . import entities, loader as tools_loader
|
||||
from . import loader as tools_loader
|
||||
from ...utils import importutil
|
||||
from . import loaders
|
||||
import langbot_plugin.api.entities.builtin.resource.tool as resource_tool
|
||||
|
||||
importutil.import_modules_in_pkg(loaders)
|
||||
|
||||
@@ -28,16 +29,16 @@ class ToolManager:
|
||||
await loader_inst.initialize()
|
||||
self.loaders.append(loader_inst)
|
||||
|
||||
async def get_all_functions(self, plugin_enabled: bool = None) -> list[entities.LLMFunction]:
|
||||
async def get_all_functions(self, plugin_enabled: bool = None) -> list[resource_tool.LLMTool]:
|
||||
"""获取所有函数"""
|
||||
all_functions: list[entities.LLMFunction] = []
|
||||
all_functions: list[resource_tool.LLMTool] = []
|
||||
|
||||
for loader in self.loaders:
|
||||
all_functions.extend(await loader.get_tools(plugin_enabled))
|
||||
|
||||
return all_functions
|
||||
|
||||
async def generate_tools_for_openai(self, use_funcs: list[entities.LLMFunction]) -> list:
|
||||
async def generate_tools_for_openai(self, use_funcs: list[resource_tool.LLMTool]) -> list:
|
||||
"""生成函数列表"""
|
||||
tools = []
|
||||
|
||||
@@ -54,7 +55,7 @@ class ToolManager:
|
||||
|
||||
return tools
|
||||
|
||||
async def generate_tools_for_anthropic(self, use_funcs: list[entities.LLMFunction]) -> list:
|
||||
async def generate_tools_for_anthropic(self, use_funcs: list[resource_tool.LLMTool]) -> list:
|
||||
"""为anthropic生成函数列表
|
||||
|
||||
e.g.
|
||||
|
||||
Reference in New Issue
Block a user