Introduction#

AgentChat provides intuitive defaults, such as Agents with preset behaviors and Teams with predefined communication protocols, to simplify building multi-agent applications.

Note

For installation instructions, please refer to the installation guide.

Defining a Model Client#

In many cases, an agent will require access to a generative model. AgentChat utilizes the model clients provided by the autogen-core package to access models.

from autogen_core.components.models import OpenAIChatCompletionClient, UserMessage

# Create an OpenAI model client.
model_client = OpenAIChatCompletionClient(
    model="gpt-4o-2024-08-06",
    # api_key="sk-...", # Optional if you have an OPENAI_API_KEY env variable set.
)
model_client_result = await model_client.create(
    messages=[
        UserMessage(content="What is the capital of France?", source="user"),
    ]
)
print(model_client_result)  # "Paris"
CreateResult(finish_reason='stop', content='The capital of France is Paris.', usage=RequestUsage(prompt_tokens=15, completion_tokens=7), cached=False, logprobs=None)

Handling Logs#

As agents interact with each other, they generate logs that can be useful in building and debugging multi-agent systems. Your application can consume these logs by attaching a log handler to the AgentChat events. AgentChat also provides a default log handler that writes logs to the console and file.

Attache the log handler before running your application to view agent message logs.

Tip

You can implement your own log handler and attach it to the AgentChat events.

import logging

from autogen_agentchat import EVENT_LOGGER_NAME
from autogen_agentchat.logging import ConsoleLogHandler

logger = logging.getLogger(EVENT_LOGGER_NAME)
logger.addHandler(ConsoleLogHandler())
logger.setLevel(logging.INFO)

Whats Next ?#

Now that we have installed the autogen-agentchat package, let’s move on to exploring how to build agents using the agent presets provided by the package.