Welcome back to our FREE AI Course on AI-agentic design Patterns with AutoGen.
In the first part, we introduced you to the fundamentals of the AutoGen framework and its capabilities for creating sophisticated AI applications. We aimed to equip you with the skills to leverage AutoGen to develop multi-agent systems capable of efficiently interacting, collaborating, and executing tasks.
In this installment, we will explore conversable agents and their use in multi-agent conversations. You will learn how to create engaging dialogues and build applications where agents interact dynamically.
Precisely, we will guide you through creating a fun conversation between two stand-up comedians, demonstrating the versatility and potential of AutoGen’s conversational agents.
As an AI developer, you will gain valuable insights and practical skills to implement sophisticated multi-agent systems, improving your efficiency and expanding your capabilities in AI development.
Let’s dive in and continue our journey with AutoGen.
1. What is AutoGen?

AutoGen is an advanced framework designed to help developers create applications using large language models (LLMs) with multiple conversational agents.
Think of it as organizing a team of specialized operatives with its mission to work together and solve complex problems. These agents can communicate with each other, share tasks, and leverage the strengths of LLMs, human inputs, and various tools to get the job done efficiently.
To illustrate this further, consider the blockbuster movie “The Matrix.” In the film, agents like Agent Smith are sophisticated programs designed to maintain order within the Matrix, a simulated reality.
These agents can adapt, respond quickly to threats, and collaborate seamlessly to enforce the system’s rules.
Similarly, in AutoGen, agents are designed to manage and optimize tasks within a digital environment. Each agent in AutoGen is like an operative in the Matrix, assigned specific roles and functions to maintain order and efficiency in their designated virtual worlds.
2. Introduction to the Conversable Agent Class
Let’s discuss one of AutoGen’s standout features, which we mentioned in our last section: the conversable agent class.
Think of it as your blueprint for creating agents that can engage in multi-agent conversations. These agents aren’t limited to simple tasks—they’re designed to interact, share information, and collaborate on complex tasks.
In other words, they;re perfect for building those intricate, interactive applications you have in mind.
So, what makes the conversable agent class so powerful?
The conversational agent class is the answer, as it allows you to create sophisticated AI systems that mimic real-world interactions. This makes AutoGen a potent tool for developing applications that require high levels of automation and interactivity.
3. Setting Up Your First Agent
Importing the Open API Key
Setting up the necessary configurations is crucial before creating your first agent in AutoGen. One of the initial steps involves importing the open API key, which allows your agents to use the language models provided by OpenAI, such as GPT-3.5 Turbo, which is essential for generating responses and performing various tasks.
First, you must import the getOpenAI API key utility function from your environment. This function will help you retrieve the API key securely and efficiently.
Here’s how you can do it:
from autogen.utils import getOpenAIAPIKey
api_key = getOpenAIAPIKey()By running this code, you ensure your agent has access to the powerful language models necessary for its operations. It’s a straightforward but vital step in the setup process.
Defining LLM Configuration
With the API key in place, the next step is to define the Large Language Model (LLM) configuration. This configuration will dictate how the agent utilizes the language model to generate responses.
For this example, we will use the GPT-3.5 Turbo model.
Here’s the code to define the LLM configuration:
from autogen.configs import LLMConfig
llm_config = LLMConfig(
model_name='gpt-3.5-turbo',
api_key=api_key,
temperature=0.7,
max_tokens=150
)In this configuration:
- model_name specifies the language model to be used.
- api_key is the key retrieved earlier.
- temperature controls the creativity of the responses. A higher value means more creative responses.
- max_tokens sets the maximum number of tokens the model can generate in a response.
This setup ensures that your agent can produce relevant and contextually appropriate replies.
Creating the First Conversable Agent Object
Now that the LLM configuration is defined, you can proceed to create your first conversable agent object. This involves importing the conversable agent class from AutoGen and initializing it with the defined LLM configuration.
Here’s how you can create a basic conversable agent:
from autogen.agents import ConversableAgent
chatbot = ConversableAgent(
name='Chatbot',
llm_config=llm_config,
human_input_mode='never'
)In this code:
- ConversableAgent is the class used to create the agent.
- name sets the name of the agent.
- llm_config passes the previously defined LLM configuration.
- human_input_mode specifies when the agent should seek human input. In this case, it’s set to ‘never,’ meaning the agent will rely solely on the language model to generate responses.
This simple setup gives you a functional agent capable of handling basic interactions without human intervention.
Basic Interaction Example
To test the newly created agent, you can have it generate a response to a simple message. Here’s how to do this:
reply = agent.generate_reply(
messages=[{"content": "Tell me a joke.", "role": "user"}]
)
print(reply)The output is:
Sure, here's one for you:
Why did the math book look sad?
Because it had too many problems.When you run this code, the agent will use the LLM configuration to generate a reply, like the one above. This demonstrates the agent’s ability to interact and respond based on the inputs it receives.
Following these steps, you’ve successfully set up your first conversable agent using AutoGen. This agent can now be the foundation for more complex multi-agent systems, allowing you to build sophisticated AI applications.
4. Multi-Agent Conversations
Creating and Configuring Multiple Agents
Building upon the foundation of a single agent, AutoGen’s true power lies in its ability to facilitate multi-agent interactions.
To create a multi-agent system, you must define and configure multiple agents to communicate and collaborate effectively. This section will guide you through setting up various agents and configuring their interactions.
First, let’s create two additional agents with distinct personalities and roles. We’ll continue to use the GPT-3.5 Turbo model for both agents.
# Define configurations for two agents
agent1_config = LLMConfig(
model_name='gpt-3.5-turbo',
api_key=api_key,
temperature=0.7,
max_tokens=150
)
agent2_config = LLMConfig(
model_name='gpt-3.5-turbo',
api_key=api_key,
temperature=0.7,
max_tokens=150
)
# Create two conversable agents
agent1 = ConversableAgent(
name='Agent1',
llm_config=agent1_config,
human_input_mode='never'
)
agent2 = ConversableAgent(
name='Agent2',
llm_config=agent2_config,
human_input_mode='never'
)In this example, both agents use the same LLM configuration, but you can customize their configurations based on their specific roles and tasks.
Setting Up a Conversation Between Agents
With multiple agents configured, the next step is establishing a conversation between them. This involves defining the initial messages and setting up the conversation flow.
Let’s create a scenario where two agents, Joe & Cassie, engage in a light-hearted stand-up comedy dialogue.
# Define initial system messages for the agents
agent1_system_message = "Your name is Joe, and you are a stand-up comedian."
agent2_system_message = "Your name is Cassie, and you are a stand-up comedian."
# Update agents with system messages
agent1.setSystemMessage(agent1_system_message)
agent2.setSystemMessage(agent2_system_message)
# Initiate a conversation
initial_message = "I'm Joe, Cassie. Let's keep the jokes rolling."
# Joe starts the conversation
conversation = agent1.initiateChat(
recipient=agent2,
message=initial_message,
num_turns=2
)
# Print the conversation history
for turn in conversation.history:
print(f"{turn.sender}: {turn.content}")The output looks like this:
Joe: I'm Joe, Cassie. Let's keep the jokes rolling.
Cassie: I'm Cassie, Joe. Let's start with some jokes. Why did the math book look sad? Because it had too many problems.
Joe: That's a good one, Cassie! Here's mine: Why don't scientists trust atoms? Because they make up everything!In this code:
- setSystemMessage provides the agents with initial context about their roles.
- initiateChat starts the conversation, specifying the recipient and the initial message.
- num_turns control the number of exchanges between the agents.
This setup allows the agents to engage in a predefined number of conversational turns, ensuring the dialogue remains focused and relevant.
Example: Stand-up Comedians Joe and Cassie
To illustrate the functionality, let’s walk through an example where Joe and Cassie exchange jokes:
# Joe initiates the conversation
initial_message = "I'm Joe, Cassie. Let's keep the jokes rolling."
conversation = agent1.initiateChat(
recipient=agent2,
message=initial_message,
num_turns=4
)
# Print the conversation history
for turn in conversation.history:
print(f"{turn.sender}: {turn.content}")The expected output will look like this:
Joe: I'm Joe, Cassie. Let's keep the jokes rolling.
Cassie: Hey Joe, great to meet another comedy enthusiast. Let's start with some jokes. Why did the math book look sad? Because it had too many problems.
Joe: Well, Cassie, at least now we know why the math book was always so negative.
Cassie: Exactly, it just couldn't subtract the sadness from its pages.In the example above, Joe and Cassie conversationally exchange jokes, demonstrating how agents can be configured to perform specific roles and interact dynamically.
This setup is ideal for applications that require engaging and interactive dialogues, such as virtual assistants, customer support bots, or entertainment systems.
Following these steps, you can create and manage multi-agent conversations using AutoGen, opening up numerous possibilities for developing sophisticated AI-driven applications.
Congratulations!
You’ve successfully set up multi-agent conversations in AutoGen, showcasing their ability to interact and collaborate dynamically.
Next, we will explore enhancing these agent conversations with advanced customization options and behaviors.
Keep up the great work!
5. Enhancing Agent Conversations
Customizing Agent Behaviors with System Messages
You can customize your agents’ behaviors using system messages to create more sophisticated interactions. System messages provide context and guidelines that shape an agent’s response.
By defining specific roles and instructions, you can tailor the agents’ interactions to suit your application’s needs.
For instance, let’s say you want to create an agent that acts as a customer service representative. You can customize its system message to include relevant information and instructions:
customer_service_agent = ConversableAgent(
name='CustomerService',
llm_config=llm_config,
human_input_mode='always'
)
customer_service_message = """
Your name is Alex, and you are a customer service representative for Tech Solutions.
Your job is to assist customers with their technical issues politely and effectively.
Always greet the customer, ask for details about their problem, and provide clear and concise solutions.
"""
customer_service_agent.setSystemMessage(customer_service_message)output
Alex: Hello! I'm Alex, your customer service representative for Tech Solutions. How can I assist you today?
User: Hi Alex, I need help with my laptop.
Alex: Sure, I can help with that. Could you please describe the issue you're facing?
User: It's not turning on.
Alex: I see. Have you tried checking the power connection and ensuring the battery is charged?This configuration ensures that Alex, the customer service agent, behaves consistently in its role, providing a structured and professional interaction.
Using Different Input Modes
Another powerful feature of conversable agents in AutoGen is the ability to configure different input modes. These modes control when and how the agent seeks human input during interactions.
There are three main modes:
- Always: The agent always asks for human input before generating a response.
- Never: The agent relies solely on the language model to generate responses without seeking human input.
- Conditional: The agent asks for human input based on specific conditions or triggers.
Using the human_input_mode parameter, you can switch between these modes to suit different scenarios.
For example, in a collaborative task where human oversight is crucial, setting the input mode always ensures that the agent seeks human confirmation or guidance before proceeding.
# Setting human input mode to 'always'
customer_service_agent.human_input_mode = 'always'This flexibility allows you to balance automation and human control, enhancing the overall reliability and effectiveness of the agents.
Maintaining Conversation State
Maintaining the conversation state is essential to ensuring continuity in multi-step interactions. This means keeping track of previous exchanges and context so the agent can provide coherent and contextually appropriate responses.
AutoGen agents can be configured to remember previous interactions, enabling them to refer back to earlier messages and maintain the flow of conversation.
This is particularly useful in applications where ongoing context is crucial, such as customer support or personal assistant tasks.
Here’s an example of maintaining a conversation state:
# Initialize a conversation with context tracking
initial_message = "Hi Alex, I need help with my laptop."
conversation = customer_service_agent.initiateChat(
recipient=customer_service_agent,
message=initial_message,
num_turns=3
)
# Follow-up interactions
follow_up_message = "It's not turning on."
conversation.addMessage(sender='User', content=follow_up_message)
# Agent's response considering the state
response = customer_service_agent.generateReply(conversation.history)
print(response)
In this scenario, the agent tracks the user's initial query and subsequent follow-up, allowing it to provide a more informed and relevant response.
Practical Example: Enhancing the Joe and Cassie Conversation
Let's revisit our stand-up comedians, Joe and Cassie, and enhance their interaction with more personalized behaviors and context maintenance.
# Update system messages for personalization
joe_system_message = """
Your name is Joe, a witty stand-up comedian known for your clever puns and observational humor.
Your goal is to make Cassie laugh while keeping the conversation lively.
"""
cassie_system_message = """
Your name is Cassie, a hilarious stand-up comedian who loves to challenge Joe with quick comebacks and witty remarks.
Engage in a playful banter, and try to one-up Joe with each joke.
"""
agent1.setSystemMessage(joe_system_message)
agent2.setSystemMessage(cassie_system_message)
# Start a more engaging conversation
initial_message = "Hey Cassie, why don't scientists trust atoms?"
conversation = agent1.initiateChat(
recipient=agent2,
message=initial_message,
num_turns=4
)
# Print the enhanced conversation history
for turn in conversation.history:
print(f"{turn.sender}: {turn.content}")The expected output is this:
Joe: Hey Cassie, why don't scientists trust atoms?
Cassie: I don't know, Joe. Why?
Joe: Because they make up everything!
Cassie: Good one, Joe! But did you hear about the mathematician who's afraid of negative numbers? He will stop at nothing to avoid them!Now you’ve enhanced agent conversations with customized behaviors, different input modes, and state maintenance. This makes your interactions more dynamic and realistic.
Next, we will explore advanced design patterns that expand your agents’ capabilities.
Keep up the excellent work!
6. Advanced Design Patterns
Using Code and Function Execution
One of AutoGen’s most powerful features is the ability to integrate code and function execution into your agent workflows.
This allows agents to perform complex operations beyond simple conversation, such as executing scripts, querying databases, or interacting with APIs.
By leveraging these capabilities, you can create agents that handle various tasks autonomously.
Here’s a simple example of an agent configured to execute Python code:
# Define a new agent with code execution capability
coding_agent = ConversableAgent(
name='CodingAgent',
llm_config=llm_config,
human_input_mode='conditional'
)
# Set a system message for coding tasks
coding_system_message = """
Your name is CodeBot, an expert Python programmer.
You can execute Python code to solve problems and perform tasks.
"""
coding_agent.setSystemMessage(coding_system_message)
# Example interaction: executing a piece of code
code_execution_message = """
Execute the following code:
def add(a, b):
return a + b
result = add(5, 3)
"""
coding_agent.addCodeExecutionCapability()
# Generate a reply that includes code execution
response = coding_agent.generateReply(message=code_execution_message)
print(response)Output
CodeBot: Executing the code:
def add(a, b):
return a + b
result = add(5, 3)
Execution Result: 8CodeBot is configured to understand and execute Python code in this setup, providing a powerful tool for automating programming tasks.
Implementing Stopping Conditions
In multi-agent interactions, it’s often necessary to define stopping conditions to control the flow of conversations. Stopping conditions are criteria that determine when an agent should halt its response generation.
This ensures that interactions are concise and relevant.
For example, you might want to stop a conversation when a specific phrase is detected. Here’s how you can implement a stopping condition:
# Define a stopping condition function
def is_termination_message(message):
return "I gotta go" in message
# Configure the agent with the stopping condition
agent1.setStoppingCondition(is_termination_message)
# Example interaction with stopping condition
initial_message = "Hey Cassie, why don't scientists trust atoms?"
conversation = agent1.initiateChat(
recipient=agent2,
message=initial_message,
num_turns=10 # Allow for more turns to test the stopping condition
)
# Adding a message that triggers the stopping condition
conversation.addMessage(sender='Joe', content="I gotta go, Cassie.")
response = agent2.generateReply(conversation.history)
print(response)Output
Joe: Hey Cassie, why don't scientists trust atoms?
Cassie: I don't know, Joe. Why?
Joe: Because they make up everything!
Cassie: Haha, good one Joe!
Joe: I gotta go, Cassie.
Cassie: Okay, take care!This example ensures that the conversation stops when Joe mentions, “I gotta go,” demonstrating how stopping conditions can manage the interaction flow.
Conclusion
In this article, we explored AutoGen’s advanced capabilities for creating sophisticated AI agent interactions.
What we did include:
- Customizing agent behaviors
- Utilizing different input modes
- Maintaining conversation states
- and Implementing advanced design patterns such as code execution and stopping conditions.
These features enhance the functionality and flexibility of your AI agents. This opens up numerous possibilities for developing sophisticated AI-driven applications.
AI for Developers’ next article, we will explore leveraging multi-agent design for tasks involving multiple steps. You will build a sequence of chats between multiple agents collaborating to provide an exceptional customer onboarding experience for a product.
Stay tuned for an in-depth look at creating a seamless and efficient customer onboarding process using sequential chat patterns in AutoGen.
Let’s continue advancing your skills and exploring the exciting possibilities of AI agentic design!
Discover more from AI For Developers
Subscribe to get the latest posts sent to your email.