autogen_core.tools#
- class BaseTool(args_type: 类型[ArgsT], return_type: 类型[ReturnT], name: str, description: str, strict: bool = False)[源代码]#
基类:
ABC
,Tool
,Generic
[ArgsT
,ReturnT
],ComponentBase
[BaseModel
]- component_type: ClassVar[ComponentType] = 'tool'#
组件的逻辑类型。
- abstract async run(args: ArgsT, cancellation_token: CancellationToken) ReturnT [源代码]#
- class BaseToolWithState(args_type: 类型[ArgsT], return_type: 类型[ReturnT], state_type: 类型[StateT], name: str, description: str)[源代码]#
基础:
BaseTool
[ArgsT
,ReturnT
],ABC
,Generic
[ArgsT
,ReturnT
,StateT
],ComponentBase
[BaseModel
]- component_type: ClassVar[ComponentType] = 'tool'#
组件的逻辑类型。
- class FunctionTool(func: Callable[[...], 任何], description: str, name: str | 无 = None, global_imports: Sequence[str | ImportFromModule | 别名] = [], strict: bool = False)[源代码]#
基础:
BaseTool
[BaseModel
,BaseModel
],Component
[FunctionToolConfig
]通过包装标准Python函数创建自定义工具。
FunctionTool 提供了一个接口,用于异步或同步执行 Python 函数。 每个函数必须为所有参数及其返回类型包含类型注解。这些注解使 FunctionTool 能够生成一个模式,该模式对于输入验证、序列化以及向 LLM 告知预期参数是必要的。当 LLM 准备函数调用时,它会利用此模式生成符合函数规范的参数。
注意
用户有责任验证工具的输出类型是否与预期类型匹配。
- Parameters:
示例
import random from autogen_core import CancellationToken from autogen_core.tools import FunctionTool from typing_extensions import Annotated import asyncio async def get_stock_price(ticker: str, date: Annotated[str, "Date in YYYY/MM/DD"]) -> float: # Simulates a stock price retrieval by returning a random float within a specified range. return random.uniform(10, 200) async def example(): # Initialize a FunctionTool instance for retrieving stock prices. stock_price_tool = FunctionTool(get_stock_price, description="Fetch the stock price for a given ticker.") # Execute the tool with cancellation support. cancellation_token = CancellationToken() result = await stock_price_tool.run_json({"ticker": "AAPL", "date": "2021/01/01"}, cancellation_token) # Output the result as a formatted string. print(stock_price_tool.return_value_as_string(result)) asyncio.run(example())
- classmethod _from_config(config: FunctionToolConfig) 自我 [源代码]#
从配置对象创建组件的新实例。
- Parameters:
config (T) – 配置对象。
- Returns:
Self – 组件的新实例。
- component_config_schema#
别名
FunctionToolConfig
- component_provider_override: ClassVar[str | 无] = 'autogen_core.tools.FunctionTool'#
覆盖组件的提供商字符串。这应用于防止内部模块名称成为模块名称的一部分。
- async run(args: BaseModel, cancellation_token: CancellationToken) 任何 [源代码]#
- class ParametersSchema[源代码]#
基础:
TypedDict
- additionalProperties: NotRequired[bool]#
- required: NotRequired[Sequence[str]]#
- class ToolSchema[源代码]#
基础:
TypedDict
- description: NotRequired[str]#
- parameters: NotRequired[ParametersSchema]#
- strict: NotRequired[bool]#