LangChain 集成¶
本节介绍 ToolRegistry 库的 LangChain 集成功能。
架构概览¶
LangChain 集成实现了 LangChain 工具与 ToolRegistry 生态系统之间的无缝互操作。该集成允许在 ToolRegistry 框架中使用 LangChain 丰富的工具生态系统:
核心组件¶
-
LangChainToolWrapper:一个包装器类,桥接 LangChain 工具与 ToolRegistry 的统一接口
-
提供同步(
_run)和异步(_arun)执行方法 - 管理 LangChain 和 ToolRegistry 格式之间的参数映射
-
处理错误传播和日志记录
-
LangChainTool:一个工具类,包装 LangChain BaseTool 实例
-
保留原始工具元数据和描述
- 将 LangChain 输入模式转换为 ToolRegistry 格式
-
支持命名空间,用于工具组织
-
LangChainIntegration:主集成类,协调桥接流程
- 管理从 LangChain 工具到 ToolRegistry 工具的转换
- 支持单个工具和批量注册模式
- 处理模式转换和规范化
设计理念¶
- 非侵入式集成:保留原始 LangChain 工具行为
- 模式兼容性:LangChain 和 ToolRegistry 模式之间的自动转换
- 错误透明性:保留原始 LangChain 异常并增强上下文信息
- 异步支持:完全兼容 LangChain 的异步执行模型
主要特性¶
- 直接与 LangChain 的
BaseTool实例集成 - 从 LangChain 到 ToolRegistry 格式的自动模式转换
- 支持同步和异步执行模式
- 命名空间支持,用于组织 LangChain 工具
- 保留原始 LangChain 工具的错误处理和日志记录
- 最小开销——无需额外依赖或转换
使用模式¶
- 单工具集成:注册单个 LangChain 工具
- 工具集合:从集合中集成多个 LangChain 工具
- 命名空间组织:将 LangChain 工具分组到公共命名空间下
- 错误处理:保持 LangChain 原始异常行为并增强上下文
API 参考¶
LangChainToolWrapper¶
提供异步和同步版本的 LangChain 工具调用的包装器类。
toolregistry.integrations.langchain.integration.LangChainToolWrapper ¶
Bases: BaseToolWrapper
Wrapper class providing both async and sync versions of LangChain tool calls.
Attributes:
| Name | Type | Description |
|---|---|---|
tool |
BaseTool
|
The LangChain tool instance. |
name |
str
|
Name of the tool. |
description |
str
|
Description of the tool. |
params |
List[str]
|
List of parameter names. |
Initialize LangChain tool wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
BaseTool
|
The LangChain tool instance. |
required |
call_async
async
¶
Async implementation of LangChain tool call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Any
|
Positional arguments to pass to the tool. |
()
|
kwargs
|
Any
|
Keyword arguments to pass to the tool. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Result from tool execution. |
Raises:
| Type | Description |
|---|---|
ToolException
|
If tool execution fails. |
call_sync ¶
Synchronous implementation of LangChain tool call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Any
|
Positional arguments to pass to the tool. |
()
|
kwargs
|
Any
|
Keyword arguments to pass to the tool. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Result from tool execution. |
Raises:
| Type | Description |
|---|---|
ToolException
|
If tool execution fails. |
LangChainTool¶
保留原始函数元数据的 LangChain 工具包装器类。
toolregistry.integrations.langchain.integration.LangChainTool ¶
Bases: Tool
Wrapper class for LangChain tools that preserves original function metadata.
from_langchain_tool
classmethod
¶
Create a LangChainTool instance from a LangChain LCBaseTool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
BaseTool
|
The LangChain tool instance. |
required |
namespace
|
Optional[str]
|
An optional namespace to prefix the tool name. If provided, the tool name will be formatted as "{namespace}.{name}". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
LangChainTool |
LangChainTool
|
A new instance of LangChainTool. |
LangChainIntegration¶
处理与 LangChain 工具集成以进行注册的类。
toolregistry.integrations.langchain.integration.LangChainIntegration ¶
Handles integration with LangChain tools for registration.
Attributes:
| Name | Type | Description |
|---|---|---|
registry |
ToolRegistry
|
Tool registry instance. |
register_langchain_tools ¶
Register LangChain tool (synchronous entry point).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
BaseTool
|
Single LangChain tool |
required |
namespace
|
Union[bool, str]
|
Whether to prefix tool names with a namespace.
- If |
False
|
register_langchain_tools_async
async
¶
Async implementation to register LangChain tool using asyncio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
BaseTool
|
Single LangChain tool. |
required |
namespace
|
Union[bool, str]
|
Whether to prefix tool names with a namespace.
- If |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If tools argument is invalid. |
模块概览¶
LangChain 模块¶
LangChain 集成主模块。
toolregistry.integrations.langchain ¶
LangChainIntegration ¶
Handles integration with LangChain tools for registration.
Attributes:
| Name | Type | Description |
|---|---|---|
registry |
ToolRegistry
|
Tool registry instance. |
register_langchain_tools ¶
Register LangChain tool (synchronous entry point).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
BaseTool
|
Single LangChain tool |
required |
namespace
|
Union[bool, str]
|
Whether to prefix tool names with a namespace.
- If |
False
|
register_langchain_tools_async
async
¶
Async implementation to register LangChain tool using asyncio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
BaseTool
|
Single LangChain tool. |
required |
namespace
|
Union[bool, str]
|
Whether to prefix tool names with a namespace.
- If |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If tools argument is invalid. |
LangChainTool ¶
Bases: Tool
Wrapper class for LangChain tools that preserves original function metadata.
from_langchain_tool
classmethod
¶
Create a LangChainTool instance from a LangChain LCBaseTool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
BaseTool
|
The LangChain tool instance. |
required |
namespace
|
Optional[str]
|
An optional namespace to prefix the tool name. If provided, the tool name will be formatted as "{namespace}.{name}". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
LangChainTool |
LangChainTool
|
A new instance of LangChainTool. |
LangChainToolWrapper ¶
Bases: BaseToolWrapper
Wrapper class providing both async and sync versions of LangChain tool calls.
Attributes:
| Name | Type | Description |
|---|---|---|
tool |
BaseTool
|
The LangChain tool instance. |
name |
str
|
Name of the tool. |
description |
str
|
Description of the tool. |
params |
List[str]
|
List of parameter names. |
Initialize LangChain tool wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool
|
BaseTool
|
The LangChain tool instance. |
required |
call_async
async
¶
Async implementation of LangChain tool call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Any
|
Positional arguments to pass to the tool. |
()
|
kwargs
|
Any
|
Keyword arguments to pass to the tool. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Result from tool execution. |
Raises:
| Type | Description |
|---|---|
ToolException
|
If tool execution fails. |
call_sync ¶
Synchronous implementation of LangChain tool call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Any
|
Positional arguments to pass to the tool. |
()
|
kwargs
|
Any
|
Keyword arguments to pass to the tool. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Result from tool execution. |
Raises:
| Type | Description |
|---|---|
ToolException
|
If tool execution fails. |