airtai / fastagency

The fastest way to bring multi-agent workflows to production.
https://fastagency.ai/latest
Apache License 2.0
261 stars 19 forks source link

Feature: Refactor API usage #191

Closed davorrunje closed 1 month ago

davorrunje commented 1 month ago
import os

from autogen import UserProxyAgent
from autogen.agentchat import ConversableAgent

from fastagency import FastAgency
from fastagency import Chatable
from fastagency.ui.console import ConsoleUI
from fastagency.runtimes.autogen.base import AutoGenWorkflows

from fastagency.api.openapi import OpenAPI

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

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

wf = AutoGenWorkflows()

@wf.register(name="simple_weather", description="Weather chat")
def weather_workflow(wf: AutoGenWorkflows, io: Chatable, initial_message: str, session_id: str) -> str: # ToDo: Accept wf: AutoGenWorkflows as parameter

    weather_api = OpenAPI.create(openapi_url=WEATHER_OPENAPI_URL)

    # ToDo: add this
    function_names = weather_api.get_function_names()

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

    # ToDo: rename to private methods -> move to AutogenWorkflows class
    weather_api.register_for_llm(weather_agent, functions=["get_daily_weather_daily_get"])
    weather_api.register_for_execution(user_agent)

    # ToDo: new registration method
    wf.register_api(
        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_daily_weather_weekly_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, io=ConsoleUI())