Quickstart#
Via AgentChat, you can build applications quickly using preset agents.To illustrate this, we will begin with creating a single agent that canuse tools.
First, we need to install the AgentChat and Extension packages.
pipinstall-U"autogen-agentchat""autogen-ext[openai,azure]"
This example uses an OpenAI model, however, you can use other models as well.Simply update themodel_client
with the desired model or model client class.
To use Azure OpenAI models and AAD authentication,you can follow the instructionshere.To use other models, seeModels.
fromautogen_agentchat.agentsimportAssistantAgentfromautogen_agentchat.uiimportConsolefromautogen_ext.models.openaiimportOpenAIChatCompletionClient# Define a model client. You can use other model client that implements# the `ChatCompletionClient` interface.model_client=OpenAIChatCompletionClient(model="gpt-4o",# api_key="YOUR_API_KEY",)# Define a simple function tool that the agent can use.# For this example, we use a fake weather tool for demonstration purposes.asyncdefget_weather(city:str)->str:"""Get the weather for a given city."""returnf"The weather in{city} is 73 degrees and Sunny."# Define an AssistantAgent with the model, tool, system message, and reflection enabled.# The system message instructs the agent via natural language.agent=AssistantAgent(name="weather_agent",model_client=model_client,tools=[get_weather],system_message="You are a helpful assistant.",reflect_on_tool_use=True,model_client_stream=True,# Enable streaming tokens from the model client.)# Run the agent and stream the messages to the console.asyncdefmain()->None:awaitConsole(agent.run_stream(task="What is the weather in New York?"))# Close the connection to the model client.awaitmodel_client.close()# NOTE: if running this inside a Python script you'll need to use asyncio.run(main()).awaitmain()
---------- user ----------What is the weather in New York?---------- weather_agent ----------[FunctionCall(id='call_bE5CYAwB7OlOdNAyPjwOkej1', arguments='{"city":"New York"}', name='get_weather')]---------- weather_agent ----------[FunctionExecutionResult(content='The weather in New York is 73 degrees and Sunny.', call_id='call_bE5CYAwB7OlOdNAyPjwOkej1', is_error=False)]---------- weather_agent ----------The current weather in New York is 73 degrees and sunny.
What’s Next?#
Now that you have a basic understanding of how to use a single agent, consider following thetutorial for a walkthrough on other features of AgentChat.