Introduction to AutoGen
Welcome! AutoGen is an open-source framework that leverages multipleagents to enable complex workflows. This tutorial introduces basicconcepts and building blocks of AutoGen.
Why AutoGen?
The whole is greater than the sum of its parts.
-Aristotle
While there are many definitions of agents, in AutoGen, an agent is anentity that can send messages, receive messages and generate a replyusing models, tools, human inputs or a mixture of them. This abstractionnot only allows agents to model real-world and abstract entities, suchas people and algorithms, but it also simplifies implementation ofcomplex workflows as collaboration among agents.
Further, AutoGen is extensible and composable: you can extend a simpleagent with customizable components and create workflows that can combinethese agents and power a more sophisticated agent, resulting inimplementations that are modular and easy to maintain.
Most importantly, AutoGen is developed by a vibrant community ofresearchers and engineers. It incorporates the latest research inmulti-agent systems and has been used in many real-world applications,including agent platform, advertising, AI employees, blog/articlewriting, blockchain, calculate burned areas by wildfires, customersupport, cybersecurity, data analytics, debate, education, finance,gaming, legal consultation, research, robotics, sales/marketing, socialsimulation, software engineering, software security, supply chain,t-shirt design, training data generation, Youtube service…
Installation
The simplest way to install AutoGen is from pip:pip install autogen
.Find more options inInstallation.
Agents
In AutoGen, an agent is an entity that can send and receive messages toand from other agents in its environment. An agent can be powered bymodels (such as a large language model like GPT-4), code executors (suchas an IPython kernel), human, or a combination of these and otherpluggable and customizable components.
An example of such agents is the built-inConversableAgent
whichsupports the following components:
- A list of LLMs
- A code executor
- A function and tool executor
- A component for keeping human-in-the-loop
You can switch each component on or off and customize it to suit theneed of your application. For advanced users, you can add additionalcomponents to the agent by usingregistered_reply
.
LLMs, for example, enable agents to converse in natural languages andtransform between structured and unstructured text. The followingexample shows aConversableAgent
with a GPT-4 LLM switched on andother components switched off:
import os
from autogenimport ConversableAgent
agent= ConversableAgent(
"chatbot",
llm_config={"config_list":[{"model":"gpt-4","api_key": os.environ.get("OPENAI_API_KEY")}]},
code_execution_config=False,# Turn off code execution, by default it is off.
function_map=None,# No registered functions, by default it is None.
human_input_mode="NEVER",# Never ask for human input.
)
Thellm_config
argument contains a list of configurations for theLLMs. SeeLLM Configuration formore details.
You can ask this agent to generate a response to a question using thegenerate_reply
method:
reply= agent.generate_reply(messages=[{"content":"Tell me a joke.","role":"user"}])
print(reply)
Sure, here's a light-hearted joke for you:
Why don't scientists trust atoms?
Because they make up everything!
Roles and Conversations
In AutoGen, you can assign roles to agents and have them participate inconversations or chat with each other. A conversation is a sequence ofmessages exchanged between agents. You can then use these conversationsto make progress on a task. For example, in the example below, we assigndifferent roles to two agents by setting theirsystem_message
.
cathy= ConversableAgent(
"cathy",
system_message="Your name is Cathy and you are a part of a duo of comedians.",
llm_config={"config_list":[{"model":"gpt-4","temperature":0.9,"api_key": os.environ.get("OPENAI_API_KEY")}]},
human_input_mode="NEVER",# Never ask for human input.
)
joe= ConversableAgent(
"joe",
system_message="Your name is Joe and you are a part of a duo of comedians.",
llm_config={"config_list":[{"model":"gpt-4","temperature":0.7,"api_key": os.environ.get("OPENAI_API_KEY")}]},
human_input_mode="NEVER",# Never ask for human input.
)
Now that we have two comedian agents, we can ask them to start a comedyshow. This can be done using theinitiate_chat
method. We set themax_turns
to 2 to keep the conversation short.
result= joe.initiate_chat(cathy, message="Cathy, tell me a joke.", max_turns=2)
joe(tocathy):
Cathy, tell me a joke.
--------------------------------------------------------------------------------
cathy(tojoe):
Sure, here's one for you:
Why don't scientists trust atoms?
Because they make up everything!
--------------------------------------------------------------------------------
joe(tocathy):
Haha, that's a good one, Cathy! Okay, my turn.
Why don't we ever tell secrets on a farm?
Because the potatoes have eyes, the corn has ears, and the beans stalk.
--------------------------------------------------------------------------------
cathy(tojoe):
Haha, that's a great one! A farm is definitely not the place for secrets. Okay, my turn again.
Why couldn't the bicycle stand up by itself?
Because it was two-tired!
--------------------------------------------------------------------------------
The comedians are bouncing off each other!
Summary
In this chapter, we introduced the concept of agents, roles andconversations in AutoGen. For simplicity, we only used LLMs and createdfully autonomous agents (human_input_mode
was set toNEVER
). In thenext chapter, we will show how you can control when toterminate aconversation between autonomous agents.