|
| 1 | +"""内置 ToolResource 集成函数 / Built-in ToolResource Integration Functions |
| 2 | +
|
| 3 | +提供快速创建通用工具集对象的便捷函数(基于新版 Tool 模块)。 |
| 4 | +Provides convenient functions for quickly creating common toolset objects (based on new Tool module). |
| 5 | +""" |
| 6 | + |
| 7 | +from typing import Optional, Union |
| 8 | + |
| 9 | +from agentrun.integration.utils.tool import CommonToolSet |
| 10 | +from agentrun.tool.client import ToolClient |
| 11 | +from agentrun.tool.tool import Tool as ToolResourceType |
| 12 | +from agentrun.utils.config import Config |
| 13 | + |
| 14 | + |
| 15 | +def tool_resource( |
| 16 | + input: Union[str, ToolResourceType], config: Optional[Config] = None |
| 17 | +) -> CommonToolSet: |
| 18 | + """将 ToolResource 封装为通用工具集 / Wrap ToolResource as CommonToolSet |
| 19 | +
|
| 20 | + 支持从工具名称或 ToolResource 实例创建通用工具集。 |
| 21 | + Supports creating CommonToolSet from tool name or ToolResource instance. |
| 22 | +
|
| 23 | + Args: |
| 24 | + input: 工具名称或 ToolResource 实例 / Tool name or ToolResource instance |
| 25 | + config: 配置对象 / Configuration object |
| 26 | +
|
| 27 | + Returns: |
| 28 | + CommonToolSet: 通用工具集实例 / CommonToolSet instance |
| 29 | +
|
| 30 | + Examples: |
| 31 | + >>> # 从工具名称创建 / Create from tool name |
| 32 | + >>> ts = tool_resource("my-tool") |
| 33 | + >>> |
| 34 | + >>> # 从 ToolResource 实例创建 / Create from ToolResource instance |
| 35 | + >>> tool = ToolClient().get(name="my-tool") |
| 36 | + >>> ts = tool_resource(tool) |
| 37 | + >>> |
| 38 | + >>> # 转换为 LangChain 工具 / Convert to LangChain tools |
| 39 | + >>> lc_tools = ts.to_langchain() |
| 40 | + """ |
| 41 | + |
| 42 | + resource = ( |
| 43 | + input |
| 44 | + if isinstance(input, ToolResourceType) |
| 45 | + else ToolClient().get(name=input, config=config) |
| 46 | + ) |
| 47 | + |
| 48 | + return CommonToolSet.from_agentrun_tool(resource, config=config) |
0 commit comments