Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support declarative configurations for FunctionTools #5036

Open
Tracked by #4439
victordibia opened this issue Jan 14, 2025 · 0 comments · May be fixed by #5052
Open
Tracked by #4439

Support declarative configurations for FunctionTools #5036

victordibia opened this issue Jan 14, 2025 · 0 comments · May be fixed by #5052

Comments

@victordibia
Copy link
Collaborator

victordibia commented Jan 14, 2025

Enable declarative representation of tools

from typing import Literal
from autogen_core import CancellationToken
from autogen_core.tools import FunctionTool, ImportFromModule


async def calculate(
    operation:  Literal["add", "subtract", "multiply", "divide"],
    x: float,
    y: float,
) -> float:
    """Perform basic arithmetic operations on two numbers."""
    if operation == "add":
        return x + y
    elif operation == "subtract":
        return x - y
    elif operation == "multiply":
        return x * y
    elif operation == "divide":
        if y == 0:
            raise ValueError("Cannot divide by zero")
        return x / y
    else:
        raise ValueError(f"Unknown operation: {operation}")


# Create the calculator tool
calculator_tool = FunctionTool(
    func=calculate,
    description="A calculator that can add, subtract, multiply, or divide two numbers.",
    global_imports=[
        ImportFromModule("typing", ("Literal",)),
        ImportFromModule("autogen_core", ("CancellationToken",)),
        ImportFromModule("autogen_core.tools", ("FunctionTool",))
    ]
)

tool_config = calculator_tool.dump_component()
print(tool_config.model_dump())
 
{'provider': 'autogen_core.tools.FunctionTool', 'component_type': 'tool', 'version': 1, 'component_version': 1, 'description': None, 'config': {'source_code': 'async def calculate(\n    operation:  Literal["add", "subtract", "multiply", "divide"],\n    x: float,\n    y: float,\n) -> float:\n    """Perform basic arithmetic operations on two numbers."""\n    if operation == "add":\n        return x + y\n    elif operation == "subtract":\n        return x - y\n    elif operation == "multiply":\n        return x * y\n    elif operation == "divide":\n        if y == 0:\n            raise ValueError("Cannot divide by zero")\n        return x [/](https://file+.vscode-resource.vscode-cdn.net/) y\n    else:\n        raise ValueError(f"Unknown operation: {operation}")\n', 'name': 'calculate', 'description': 'A calculator that can add, subtract, multiply, or divide two numbers.', 'global_imports': [{'module': 'typing', 'imports': ('Literal',)}, {'module': 'autogen_core', 'imports': ('CancellationToken',)}, {'module': 'autogen_core.tools', 'imports': ('FunctionTool',)}], 'has_cancellation_support': False}}
# load
loaded_calculator = FunctionTool.load_component(tool_config)
@ekzhu ekzhu added this to the 0.4.x milestone Jan 14, 2025
@victordibia victordibia modified the milestones: 0.4.x, 0.4.2 Jan 14, 2025
@victordibia victordibia changed the title Support declarative configurations for Tools Support declarative configurations for FunctionTools Jan 14, 2025
@victordibia victordibia linked a pull request Jan 14, 2025 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants