Skip to content

Async function calls? #103

@jhrystrom

Description

@jhrystrom

Feature request:

Support for Coroutines in function_map of UserProxyAgent

Issue Description:

It would be great if you could supply coroutines as arguments to function_map in the agents. Below is a reproducible example of the desired behaviour.

Reproducible Example:

import autogen
import json

# Define a weather function
weather_func = {
    "name": "get_current_weather",
    "description": "Get the current weather in a given location",
    "parameters": {
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA",
            },
            "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
        },
        "required": ["location"],
    },
}

# Dummy async function to simulate weather fetching
async def get_current_weather(location: str, unit: str = "fahrenheit") -> str:
    weather_info = {
        "location": location,
        "temperature": "72",
        "unit": unit,
        "forecast": ["sunny", "windy"],
    }
    return json.dumps(weather_info)

# LLM Config
llm_config = {
    "functions": [weather_func],
    "config_list": [],  # Omitted for brevity
}

# Assistant Agent
chatbot = autogen.AssistantAgent(
    name="chatbot",
    system_message="Only use the functions you have been provided with...",
    llm_config=llm_config,
)

# User Proxy Agent with function_map containing an async function
user_proxy = autogen.UserProxyAgent(
    "user_proxy",
    max_consecutive_auto_reply=3,
    human_input_mode="TERMINATE",
    function_map={"get_current_weather": get_current_weather},
)

chat = await user_proxy.a_initiate_chat(chatbot, message="What's the weather in Aarhus?")

Current output:

user_proxy (to chatbot):

What's the weather in Aarhus?

--------------------------------------------------------------------------------
chatbot (to user_proxy):

***** Suggested function Call: get_current_weather *****
Arguments: 
{
  "location": "Aarhus"
}
********************************************************

--------------------------------------------------------------------------------

>>>>>>>> USING AUTO REPLY...

>>>>>>>> EXECUTING FUNCTION get_current_weather...
user_proxy (to chatbot):

***** Response from calling function "get_current_weather" *****
<coroutine object get_current_weather at 0x7fdcacaf9b60>
****************************************************************

Expected Behavior:

The coroutine get_current_weather should be awaited and executed, returning the weather information.

I hope you can help out!

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions