autogen_core.tools#

class BaseTool(args_type: 类型[ArgsT], return_type: 类型[ReturnT], name: str, description: str, strict: bool = False)[源代码]#

基类:ABCToolGeneric[ArgsTReturnT],ComponentBase[BaseModel]

args_type() 类型[BaseModel][源代码]#
component_type: ClassVar[ComponentType] = 'tool'#

组件的逻辑类型。

property description: str#
load_state_json(state: 映射[str, 任何]) [源代码]#
property name: str#
return_type() 类型[任何][源代码]#
return_value_as_string(value: 任何) str[源代码]#
abstract async run(args: ArgsT, cancellation_token: CancellationToken) ReturnT[源代码]#
async run_json(args: 映射[str, 任何], cancellation_token: CancellationToken) 任何[源代码]#
save_state_json() 映射[str, 任何][源代码]#
property schema: 工具模式#
state_type() 类型[BaseModel] | [源代码]#
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'#

组件的逻辑类型。

abstract load_state(state: StateT) [源代码]#
load_state_json(state: 映射[str, 任何]) [源代码]#
abstract save_state() StateT[源代码]#
save_state_json() 映射[str, 任何][源代码]#
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:
  • func (Callable[..., ReturnT | Awaitable[ReturnT]]) – 要包装并作为工具公开的函数。

  • description (str) – 描述函数的目的,说明它的功能以及应该调用的上下文。

  • name (str, optional) – 工具的可选自定义名称。如果没有提供,默认为函数的原始名称。

  • strict (bool, optional) – 如果设置为 True,工具模式将仅包含在函数签名中明确定义的参数,并且不允许使用默认值。默认为 False。 在结构化输出模式下与模型一起使用时,必须将其设置为 True。

示例

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 – 组件的新实例。

_to_config() FunctionToolConfig[源代码]#

导出配置,该配置将用于创建一个与此实例配置相匹配的组件新实例。

Returns:

T – 组件的配置。

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]#
properties: 字典[str, 任何]#
required: NotRequired[Sequence[str]]#
type: str#
class Tool(*args, **kwargs)[源代码]#

基础类: Protocol

args_type() 类型[BaseModel][源代码]#
property description: str#
load_state_json(state: 映射[str, 任何]) [源代码]#
property name: str#
return_type() 类型[任何][源代码]#
return_value_as_string(value: 任何) str[源代码]#
async run_json(args: 映射[str, 任何], cancellation_token: CancellationToken) 任何[源代码]#
save_state_json() 映射[str, 任何][源代码]#
property schema: 工具模式#
state_type() 类型[BaseModel] | [源代码]#
class ToolSchema[源代码]#

基础:TypedDict

description: NotRequired[str]#
name: str#
parameters: NotRequired[ParametersSchema]#
strict: NotRequired[bool]#