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

Make FunctionTools Serializable (Declarative) #5052

Merged
merged 14 commits into from
Jan 24, 2025
Merged

Conversation

victordibia
Copy link
Collaborator

@victordibia victordibia commented Jan 14, 2025

Why are these changes needed?

Enable declarative representation of FunctionTool.

Open questions
@jackgerrits (thoughts welcome)

  • Currently we use exec to load passed in global_imports and also to get function from serialized string. suggestions welcome on how to improve this if at all.
  • Do we add specific warnings for cases where where the specified imports fail? What do we do to make it less easy to write functions that cannot be serialized properly (or detect when this is the case)?
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)

Related issue number

Closes #5036

Checks

Copy link

codecov bot commented Jan 14, 2025

Codecov Report

Attention: Patch coverage is 90.19608% with 5 lines in your changes missing coverage. Please review.

Project coverage is 70.53%. Comparing base (44b9bff) to head (1edf63c).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...ogen-core/src/autogen_core/tools/_function_tool.py 88.09% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5052      +/-   ##
==========================================
+ Coverage   70.39%   70.53%   +0.14%     
==========================================
  Files         174      174              
  Lines       11083    11129      +46     
==========================================
+ Hits         7802     7850      +48     
+ Misses       3281     3279       -2     
Flag Coverage Δ
unittests 70.53% <90.19%> (+0.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@victordibia victordibia changed the title Make FunctionTools Declarative [Draft] Make FunctionTools Declarative Jan 15, 2025
@victordibia victordibia mentioned this pull request Jan 15, 2025
3 tasks
@EItanya
Copy link

EItanya commented Jan 21, 2025

Maybe this isn't directly related to this PR, but I was wondering if any thought has been put into alternate ways of running these tools. Perhaps a REST API, or proto. Maybe even using a system like MCP?

@jackgerrits
Copy link
Member

There is a great extension for using MCP tools with AutoGen here https://github.com/richard-gyiko/autogen-ext-mcp

@ekzhu
Copy link
Collaborator

ekzhu commented Jan 22, 2025

Repeat another comment: it is not making function tools declartive, it's serializable configuration. The function is still written in procedure code so it's not delcarative, it's still procedural.

@EItanya
Copy link

EItanya commented Jan 22, 2025

There is a great extension for using MCP tools with AutoGen here https://github.com/richard-gyiko/autogen-ext-mcp

I have seen this extension, and I understand that it's not declarative, my point here is more of a larger question around the UI as a generally useful tool going forward. I for example would love to use it as a driver for building agents with tools, but I cannot because realistically the framework is limited by not being able to bring custom code. Custom code for both tools and agents. The GRPC runtime already exists, and things like MCP, so I'm envisioning a world where I as a user of the UI could include additional agents/tools via the network. Does that make sense?

@ekzhu
Copy link
Collaborator

ekzhu commented Jan 22, 2025

@Eltanya

My comment was for the PR not to you. Apologize for the confusion.

I agree MCP or remote API tools are necessary. Would you like to create an issue with a rough sketch of how it may look like, implementation wise? We are just getting started here. We are happy to have you contribute to this effort also, whether through discussion or code.

@victordibia victordibia changed the title [Draft] Make FunctionTools Declarative [Draft] Make FunctionTools Serializable (Declarative) Jan 22, 2025
@EItanya
Copy link

EItanya commented Jan 22, 2025

@Eltanya

My comment was for the PR not to you. Apologize for the confusion.

I agree MCP or remote API tools are necessary. Would you like to create an issue with a rough sketch of how it may look like, implementation wise? We are just getting started here. We are happy to have you contribute to this effort also, whether through discussion or code.

Absolutely, I can do that, when I'm done I'll post the issue link here

@victordibia victordibia changed the title [Draft] Make FunctionTools Serializable (Declarative) Make FunctionTools Serializable (Declarative) Jan 23, 2025
@victordibia victordibia requested a review from ekzhu January 23, 2025 17:43
@EItanya
Copy link

EItanya commented Jan 23, 2025

@ekzhu here is the issue I mentioned about the remote tool calling, I should hopefully have a PR up tomorrow with the first iteration which I will link as well.

#5170

@victordibia victordibia merged commit 58d789a into main Jan 24, 2025
64 checks passed
@victordibia victordibia deleted the declarative_tools_vd branch January 24, 2025 03:52
@EItanya
Copy link

EItanya commented Jan 24, 2025

Here is the link to the draft PR I mentioned: #5181

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support declarative configurations for FunctionTools
5 participants