Quickstart#

Via AgentChat, you can build applications quickly using preset agents. To illustrate this, we will begin with creating a team of a single agent that can use tools and respond to messages.

The following code uses the OpenAI model. If you haven’t already, you need to install the following package and extension:

pip install 'autogen-agentchat==0.4.0.dev8' 'autogen-ext[openai]==0.4.0.dev8'

To use Azure OpenAI models and AAD authentication, you can follow the instructions here.

from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.conditions import TextMentionTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_ext.models import OpenAIChatCompletionClient


# Define a tool
async def get_weather(city: str) -> str:
    return f"The weather in {city} is 73 degrees and Sunny."


async def main() -> None:
    # Define an agent
    weather_agent = AssistantAgent(
        name="weather_agent",
        model_client=OpenAIChatCompletionClient(
            model="gpt-4o-2024-08-06",
            # api_key="YOUR_API_KEY",
        ),
        tools=[get_weather],
    )

    # Define termination condition
    termination = TextMentionTermination("TERMINATE")

    # Define a team
    agent_team = RoundRobinGroupChat([weather_agent], termination_condition=termination)

    # Run the team and stream messages to the console
    stream = agent_team.run_stream(task="What is the weather in New York?")
    await Console(stream)


# NOTE: if running this inside a Python script you'll need to use asyncio.run(main()).
await main()
---------- user ----------
What is the weather in New York?
---------- weather_agent ----------
[FunctionCall(id='call_AhTZ2q3TNL8x0qs00e3wIZ7y', arguments='{"city":"New York"}', name='get_weather')]
[Prompt tokens: 79, Completion tokens: 15]
---------- weather_agent ----------
[FunctionExecutionResult(content='The weather in New York is 73 degrees and Sunny.', call_id='call_AhTZ2q3TNL8x0qs00e3wIZ7y')]
---------- weather_agent ----------
The weather in New York is currently 73 degrees and sunny.
[Prompt tokens: 90, Completion tokens: 14]
---------- weather_agent ----------
TERMINATE
[Prompt tokens: 137, Completion tokens: 4]
---------- Summary ----------
Number of messages: 5
Finish reason: Text 'TERMINATE' mentioned
Total prompt tokens: 306
Total completion tokens: 33
Duration: 1.43 seconds

The code snippet above introduces two high level concepts in AgentChat: Agent and Team. An Agent helps us define what actions are taken when a message is received. Specifically, we use the AssistantAgent preset - an agent that can be given access to a model (e.g., LLM) and tools (functions) that it can then use to address tasks. A Team helps us define the rules for how agents interact with each other. In the RoundRobinGroupChat team, agents respond in a sequential round-robin fashion. In this case, we have a single agent, so the same agent is used for each round.

What’s Next?#

Now that you have a basic understanding of how to define an agent and a team, consider following the tutorial for a walkthrough on other features of AgentChat.