Skip to content

OpenAPI#

FastAgency can automatically create functions properly annotated for use with LLM-s from OpenAPI specification.

This example demonstrates how to integrate external REST API calls into AutoGen agents using FastAgency. We'll create a weather agent that interacts with a weather REST API and a user agent to facilitate the conversation. This example will help you understand how to set up agents and facilitate agent communication through an external REST API. To interact with the REST API, the AutoGen agent needs to understand the available routes, so it requires the OpenAPI specification (openapi.json file) for the external REST API.

In this example, we'll use a simple weather API and its specification available at https://weather.tools.fastagency.ai/openapi.json.

Note

The weather API has two routes: one for the daily weather forecast, which has no security, and another for the hourly forecast, which is secured. We will learn how to access external APIs that are secured in the next chapter.

Install#

We strongly recommend using Cookiecutter for setting up the project. Cookiecutter creates the project folder structure, default workflow, automatically installs all the necessary requirements, and creates a devcontainer that can be used with Visual Studio Code.

You can setup the project using Cookiecutter by following the project setup guide.

Alternatively, you can use pip + venv. Before getting started, you need to install FastAgency with OpenAPI submodule. You can do this using pip, Python's package installer.

pip install "fastagency[autogen,openapi]"

Imports#

These imports are similar to the imports section we have already covered, with the only difference being the additional imports of the OpenAPI Client and UserProxyAgent:

import os

from autogen import UserProxyAgent
from autogen.agentchat import ConversableAgent

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

Configure the Language Model (LLM)#

Here, the large language model is configured to use the gpt-4o-mini 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-mini",
            "api_key": os.getenv("OPENAI_API_KEY"),
        }
    ],
    "temperature": 0.8,
}

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.

WEATHER_OPENAPI_URL = "https://weather.tools.fastagency.ai/openapi.json"
weather_api = OpenAPI.create(openapi_url=WEATHER_OPENAPI_URL)

Define the Workflow and Agents#

In this step, we define two agents and specify the initial message that will be displayed to users when the workflow starts.

  • 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.

wf = AutoGenWorkflows()


@wf.register(name="simple_weather", description="Weather chat")
def weather_workflow(
    ui: UI, params: dict[str, str]
) -> str:
    initial_message = ui.text_input(
        sender="Workflow",
        recipient="User",
        prompt="What do you want to know about the weather?",
    )

    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",
    )

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 to retrieve the required weather data.

    wf.register_api(
        api=weather_api,
        callers=user_agent,
        executors=weather_agent,
    )

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]

Define FastAgency Application#

Next, define your FastAgency application.

app = FastAgency(provider=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.runtimes.autogen import AutoGenWorkflows
from fastagency.ui.console import ConsoleUI

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

WEATHER_OPENAPI_URL = "https://weather.tools.fastagency.ai/openapi.json"
weather_api = OpenAPI.create(openapi_url=WEATHER_OPENAPI_URL)

wf = AutoGenWorkflows()


@wf.register(name="simple_weather", description="Weather chat")
def weather_workflow(
    ui: UI, params: dict[str, str]
) -> str:
    initial_message = ui.text_input(
        sender="Workflow",
        recipient="User",
        prompt="What do you want to know about the weather?",
    )

    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(
        api=weather_api,
        callers=user_agent,
        executors=weather_agent,
    )

    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(provider=wf, ui=ConsoleUI())

Run Application#

You can run this chapter's FastAgency application using the following command:

fastagency run

Output#

The output will vary based on the city and the current weather conditions:

╭─ Python module file ─╮
│                      │
│  🐍 main.py          │
│                      │
╰──────────────────────╯


╭─ Importable FastAgency app ─╮
│                             │
│  from main import app       │
│                             │
╰─────────────────────────────╯

╭─ FastAgency -> user [workflow_started] ──────────────────────────────────────╮
│                                                                              │
│ {                                                                            │
│   "name": "simple_weather",                                                  │
│   "description": "Weather chat",                                             │
│                                                                              │
│ "params": {}                                                                 │
│ }                                                                            │
╰──────────────────────────────────────────────────────────────────────────────╯

    ╭─ Workflow -> User [text_input] ──────────────────────────────────────────────╮
    │                                                                              │
    │ What do you want to know about the weather?:                                 │
    ╰──────────────────────────────────────────────────────────────────────────────╯

Get me daily weather forecast for Chennai city
    ╭─ User_Agent -> Weather_Agent [text_message] ─────────────────────────────────╮
    │                                                                              │
    │ Get me daily weather forecast for Chennai city                               │
    ╰──────────────────────────────────────────────────────────────────────────────╯

    ╭─ Weather_Agent -> User_Agent [text_message] ─────────────────────────────────╮
    │                                                                              │
    │ I'm unable to provide real-time weather forecasts. However, you can          │
    │ easily find the daily weather forecast for Chennai by checking               │
    │ reliable weather websites, using weather apps, or searching for              │
    │ "Chennai weather forecast" in your preferred search engine. If you           │
    │ have any other questions or need information about typical weather           │
    │ patterns in Chennai, feel free to ask!                                       │
    ╰──────────────────────────────────────────────────────────────────────────────╯

    ╭─ User_Agent -> Weather_Agent [suggested_function_call] ──────────────────────╮
    │                                                                              │
    │ {                                                                            │
    │   "function_name": "get_daily_weather_daily_get",                            │
    │   "call_id":                                                                 │
    │ "call_lbik8BJJREriUyhbpuKE5hhC",                                             │
    │   "arguments": {                                                             │
    │     "city":                                                                  │
    │ "Chennai"                                                                    │
    │   }                                                                          │
    │ }                                                                            │
    ╰──────────────────────────────────────────────────────────────────────────────╯

    ╭─ Weather_Agent -> User_Agent [function_call_execution] ──────────────────────╮
    │                                                                              │
    │ {                                                                            │
    │   "function_name": "get_daily_weather_daily_get",                            │
    │   "call_id":                                                                 │
    │ "call_lbik8BJJREriUyhbpuKE5hhC",                                             │
    │   "retval": "{\"city\": \"Chennai\",                                         │
    │ \"temperature\": 30, \"daily_forecasts\": [{\"forecast_date\":               │
    │ \"2024-10-10\", \"temperature\": 29, \"hourly_forecasts\": null},            │
    │ {\"forecast_date\": \"2024-10-11\", \"temperature\": 29,                     │
    │ \"hourly_forecasts\": null}, {\"forecast_date\": \"2024-10-12\",             │
    │ \"temperature\": 28, \"hourly_forecasts\": null}]}\n"                        │
    │ }                                                                            │
    ╰──────────────────────────────────────────────────────────────────────────────╯

    ╭─ User_Agent -> Weather_Agent [text_message] ─────────────────────────────────╮
    │                                                                              │
    │ Here is the daily weather forecast for Chennai:                              │
    │                                                                              │
    │ - **October 10,                                                              │
    │ 2024**: Temperature - 29°C                                                   │
    │ - **October 11, 2024**: Temperature - 29°C                                   │
    │                                                                              │
    │ - **October 12, 2024**: Temperature - 28°C                                   │
    │                                                                              │
    │ If you need more details                                                     │
    │ or hourly forecasts, let me know!                                            │
    ╰──────────────────────────────────────────────────────────────────────────────╯

    ╭─ Weather_Agent -> User_Agent [text_message] ─────────────────────────────────╮
    │                                                                              │
    │ Here is the daily weather forecast for Chennai:                              │
    │                                                                              │
    │ - **October 10,                                                              │
    │ 2024**: Temperature - 29°C                                                   │
    │ - **October 11, 2024**: Temperature - 29°C                                   │
    │                                                                              │
    │ - **October 12, 2024**: Temperature - 28°C                                   │
    │                                                                              │
    │ If you need more details                                                     │
    │ or hourly forecasts, feel free to ask!                                       │
    ╰──────────────────────────────────────────────────────────────────────────────╯

╭─ workflow -> user [workflow_completed] ──────────────────────────────────────╮
│                                                                              │
│ {                                                                            │
│   "result": "The user requested the daily weather forecast for               │
│ Chennai, and the assistant provided the forecast for October 10, 11,         │
│ and 12, 2024, with temperatures of 29\u00b0C, 29\u00b0C, and                 │
│ 28\u00b0C, respectively. The assistant also offered to provide more          │
│ details or hourly forecasts if needed."                                      │
│ }                                                                            │
╰──────────────────────────────────────────────────────────────────────────────╯