Skip to content

AutoGen in FastAgency#

The AutoGen runtime is a key component of FastAgency, empowering developers to create intelligent, multi-agent systems powered by large language models (LLMs). AutoGen allows agents to communicate, collaborate, and perform complex tasks autonomously while easily integrating with external APIs for real-time data and functionality.

In this example, we will create a simple weather chatbot using AutoGen in FastAgency. The chatbot will enable a user to interact with a weather agent that fetches real-time weather information from an external API using OpenAPI specifications.

Installation#

Before getting started, make sure you have installed FastAgency with support for the AutoGen runtime by running the following command:

pip install "fastagency[autogen,openapi]"

This installation includes the AutoGen runtime, allowing you to build multi-agent workflows and integrate external APIs seamlessly.

Example: Integrating a Weather API with AutoGen#

Step-by-Step Breakdown#

1. Import Required Modules#

The example starts by importing the necessary modules from AutoGen and FastAgency. These imports lay the foundation for building and running multi-agent workflows.

import os

from autogen import UserProxyAgent
from autogen.agentchat import ConversableAgent

from fastagency import UI, FastAgency
from fastagency.api.openapi import OpenAPI
from fastagency.runtime.autogen.base import AutoGenWorkflows
from fastagency.ui.console import ConsoleUI

2. Configure the Language Model (LLM)#

Here, the large language model is configured to use the gpt-4o model, and the API key is retrieved from the environment. This setup ensures that both the user and weather agents can interact effectively.

llm_config = {
    "config_list": [
        {
            "model": "gpt-4o",
            "api_key": os.getenv("OPENAI_API_KEY"),
        }
    ],
    "temperature": 0.0,
}

3. Set Up the Weather API#

We define the OpenAPI specification URL for the weather service. This API will later be used by the weather agent to fetch real-time weather data.

openapi_url = "https://weather.tools.fastagency.ai/openapi.json"

weather_api = OpenAPI.create(openapi_url=openapi_url)

4. Define the Workflow and Agents#

In this step, we create two agents:

  • UserProxyAgent: This agent simulates the user interacting with the system.

  • ConversableAgent: This agent acts as the weather agent, responsible for fetching weather data from the API.

The workflow is registered using AutoGenWorkflows.

@wf.register(name="simple_weather", description="Weather chat")  # type: ignore[type-var]
def weather_workflow(
    wf: AutoGenWorkflows, ui: UI, initial_message: str, session_id: str
) -> str:
    user_agent = UserProxyAgent(
        name="User_Agent",
        system_message="You are a user agent",
        llm_config=llm_config,
        human_input_mode="NEVER",
    )
    weather_agent = ConversableAgent(
        name="Weather_Agent",
        system_message="You are a weather agent",
        llm_config=llm_config,
        human_input_mode="NEVER",
    )

5. Register API Functions with the Agents#

In this step, we register the weather API functions to ensure that the weather agent can call the correct functions, such as get_daily_weather and get_daily_weather_weekly_get, to retrieve the required weather data.

    wf.register_api(  # type: ignore[attr-defined]
        api=weather_api,
        callers=[user_agent],
        executors=[weather_agent],
        functions=[
            {
                "get_daily_weather_daily_get": {
                    "name": "get_daily_weather",
                    "description": "Get the daily weather",
                }
            },
            "get_hourly_weather_hourly_get",
        ],
    )

6. Enable Agent Interaction and Chat#

Here, the user agent initiates a chat with the weather agent, which queries the API and returns the weather information. The conversation is summarized using a method provided by the LLM.

    chat_result = user_agent.initiate_chat(
        weather_agent,
        message=initial_message,
        summary_method="reflection_with_llm",
        max_turns=3,
    )

    return chat_result.summary  # type: ignore[no-any-return]

7. Create and Run the Application#

Finally, we create the FastAgency application and launch it using the console interface.

app = FastAgency(wf=wf, ui=ConsoleUI())

Complete Application Code#

main.py
import os

from autogen import UserProxyAgent
from autogen.agentchat import ConversableAgent

from fastagency import UI, FastAgency
from fastagency.api.openapi import OpenAPI
from fastagency.runtime.autogen.base import AutoGenWorkflows
from fastagency.ui.console import ConsoleUI

llm_config = {
    "config_list": [
        {
            "model": "gpt-4o",
            "api_key": os.getenv("OPENAI_API_KEY"),
        }
    ],
    "temperature": 0.0,
}

openapi_url = "https://weather.tools.fastagency.ai/openapi.json"

weather_api = OpenAPI.create(openapi_url=openapi_url)

wf = AutoGenWorkflows()


@wf.register(name="simple_weather", description="Weather chat")  # type: ignore[type-var]
def weather_workflow(
    wf: AutoGenWorkflows, ui: UI, initial_message: str, session_id: str
) -> str:
    user_agent = UserProxyAgent(
        name="User_Agent",
        system_message="You are a user agent",
        llm_config=llm_config,
        human_input_mode="NEVER",
    )
    weather_agent = ConversableAgent(
        name="Weather_Agent",
        system_message="You are a weather agent",
        llm_config=llm_config,
        human_input_mode="NEVER",
    )

    wf.register_api(  # type: ignore[attr-defined]
        api=weather_api,
        callers=[user_agent],
        executors=[weather_agent],
        functions=[
            {
                "get_daily_weather_daily_get": {
                    "name": "get_daily_weather",
                    "description": "Get the daily weather",
                }
            },
            "get_hourly_weather_hourly_get",
        ],
    )

    chat_result = user_agent.initiate_chat(
        weather_agent,
        message=initial_message,
        summary_method="reflection_with_llm",
        max_turns=3,
    )

    return chat_result.summary  # type: ignore[no-any-return]


app = FastAgency(wf=wf, ui=ConsoleUI())

Running the Application#

fastagency run

Ensure you have set your OpenAI API key in the environment and that the weather API URL is accessible. The command will launch a console interface where users can input their requests and interact with the weather agent.


This example demonstrates the power of the AutoGen runtime within FastAgency, showing how easy it is to integrate LLM-powered agents with real-time API services. By leveraging FastAgency, developers can quickly create interactive, scalable applications that interact with external data sources in real-time.

For more detailed documentation, visit the AutoGen Reference.