Chatbots have evolved from simple question-and-answer systems to sophisticated, intelligent agents capable of handling complex conversations. As interactions in various fields become more nuanced, demand grows for chatbots that can seamlessly manage multiple participants and complex workflows. Thanks to frameworks like AutoGen, creating dynamic multi-agent environments is now more accessible. In our previous article, we looked at creating a two-agent chatbot using AutoGen. However, there is a growing need for capabilities beyond standard two-person chat. With AutoGen, we can implement conversion patterns such as sequential and nested chat. These capabilities create seamless exchanges with multiple participants that can handle complex workflows and dynamic interactions. In this article, we will explore how AutoGen facilitates these advanced conversation patterns and discuss their practical applications.
What are multi-agent chatbots?
Multi-agent chatbots are artificial intelligence systems in which multiple specialized agents work together to complete tasks or manage complex conversations. Each agent focuses on a specific function, such as answering questions, providing recommendations, or analyzing data. This division of experience allows the chatbot system to respond more accurately and efficiently. By coordinating with multiple agents, the chatbot can offer more versatile and in-depth responses compared to a single-agent system.
Multi-agent chatbots are ideal for complex environments such as customer service, e-commerce, and education. Each agent can take on a different role, such as handling returns, making product suggestions, or helping with learning materials. When done right, multi-agent chatbots provide a smoother, faster, and more personalized user experience.
What are conversation patterns in Autogen?
To coordinate conversations between multiple agents, AutoGen has the following conversation patterns that involve more than two agents.
- Sequential chat: It is a series of conversations between two agents, each linked to the next. A transfer mechanism brings a summary of the previous conversation into the context of the next.
- Group chat: This is a single conversation that includes more than two agents. A key consideration is deciding which agent should respond next, and AutoGen offers multiple ways to organize agent interactions to fit various scenarios.
- Nested chat: Nested chat involves packaging a workflow into a single agent, allowing it to be reused within a larger workflow.
In this blog, we will learn how to implement sequential chat.
What is sequential chat?
In a sequential conversation pattern, one agent starts a two-agent chat and then the chat summary moves to the next two-agent chat. In this way, the conversation follows a sequence of chats between two agents.
As shown in the image above, the conversation starts with a conversation between Agent A and Agent B with the given context and message. A summary of this chat is then provided to the other two agent chats as a transfer.
In this image, Agent A is common among all chats. But we can also use different agents in each two-agent chat.
Now, why do we need this, instead of a simple chat between two agents? This type of conversation is useful when a task can be divided into interdependent subtasks and different agents can better handle each subtask.
Prerequisites
Before creating AutoGen agents, make sure you have the API keys required for LLMs. We will also use Tavily to search the web.
Upload the .env file with the necessary API keys.
Define the LLM to be used as config_list
config_list = {
"config_list": ({"model": "gpt-4o-mini", "temperature": 0.2})
}
Required key libraries
autogen-agentchat – 0.2.37
Implementation
Let's see how you can create a sequential chat with multiple agents in Autogen. In this example, we will create a stock analysis agent system. The system will be able to get the prices of the stocks, get recent news related to them and write an article about the stocks. We'll use Nvidia and Apple as an example, but you can use it for other actions as well.
Define tasks
financial_tasks = (
"""What are the current stock prices of NVDA and AAPL, and how is the performance over the past month in terms of percentage change?""",
"""Investigate possible reasons for the stock performance leveraging market news.""",
)
writing_tasks = ("""Develop an engaging blog post using any information provided.""")
Define the agents
We will define two assistants for each of the financial tasks and another assistant for writing the article.
import autogen
financial_assistant = autogen.AssistantAgent(
name="Financial_assistant",
llm_config=config_list,
)
research_assistant = autogen.AssistantAgent(
name="Researcher",
llm_config=config_list,
)
writer = autogen.AssistantAgent(
name="writer",
llm_config=config_list,
system_message="""
You are a professional writer, known for
your insightful and engaging articles.
You transform complex concepts into compelling narratives.
Reply "TERMINATE" in the end when everything is done.
""",
)
Since obtaining stock data and news requires a web search, we will define an agent capable of executing code.
user_proxy_auto = autogen.UserProxyAgent(
name="User_Proxy_Auto",
human_input_mode="ALWAYS",
is_termination_msg=lambda x: x.get("content", "") and x.get("content", "").rstrip().endswith("TERMINATE"),
code_execution_config={
"work_dir": "tasks",
"use_docker": False,
})
We will use human_input_mode as “ALWAYS”, so that we can check the generated code and ask the agent to make changes if necessary.
The generated code is saved in the 'tasks' folder.
We can also use Docker to run the code for security.
Financial_assistant and research_assistant will generate the necessary code and send it to user_proxy_auto for execution.
Since 'writer' does not need to generate any code, we will define another user agent to chat with 'writer'.
user_proxy = autogen.UserProxyAgent(
name="User_Proxy",
human_input_mode="ALWAYS",
is_termination_msg=lambda x: x.get("content", "") and x.get("content", "").rstrip().endswith("TERMINATE"),
code_execution_config=False)
Here we will also use human_input_mode as 'ALWAYS' to provide feedback to the agent.
Sample conversation
Now we can start the conversation.
chat_results = autogen.initiate_chats(
(
{
"sender": user_proxy_auto,
"recipient": financial_assistant,
"message": financial_tasks(0),
"clear_history": True,
"silent": False,
"summary_method": "last_msg",
},
{
"sender": user_proxy_auto,
"recipient": research_assistant,
"message": financial_tasks(1),
"summary_method": "reflection_with_llm",
},
{
"sender": user_proxy,
"recipient": writer,
"message": writing_tasks(0)
},
))
As defined above, the first two-agent chat is between user_proxy_auto and Financial_assistant, the second chat is between user_proxy_auto and research_assistant, and the third is between user_proxy and writer.
The initial output will be as shown in this image.
If you are satisfied with the results of each of the agents, type exit in the human input; Otherwise, provide helpful feedback to agents.
Chat results
Now let's get the chat results. We can access the results of each agent.
len(chat_results)
>> 3 # for each agent
We see that we have 3 results for each of the agents. To get the chat result of a particular agent, we can use proper indexing. Here is the response we received from the last agent, who is a writing agent.
As you can see above, our writing agent has reached out to Financial Assistant and Research Assistant agents to provide us with a complete analysis of NVIDIA and Apple stock performance.
Conclusion
AutoGen conversation patterns, such as sequential, allow us to create complex multi-agent interactions beyond standard two-person chats. These patterns enable seamless coordination of tasks, breaking down complex workflows into manageable steps handled by specialized agents. With AutoGen, finance, content generation, and customer service applications can benefit from improved collaboration between agents. This allows us to create adaptable and efficient conversational solutions tailored to specific needs.
If you want to learn more about ai agents, check out our exclusive Agentic ai Pioneer program.
Frequently asked questions
A. Multi-agent chatbots use multiple specialized agents, each of which focuses on a specific task, such as answering questions or giving recommendations. This structure allows the chatbot to handle complex conversations by dividing tasks.
A. AutoGen supports patterns such as sequential, group, and nested chat. These allow chatbots to coordinate tasks between multiple agents, which is essential for complex interactions in customer service, content creation, etc.
A. Sequential chat links a series of conversations between two agents by moving one summary to the next. It is ideal for tasks that can be divided into dependent steps managed by different agents.
A. Multi-agent patterns in AutoGen are useful for industries like customer service, finance, and e-commerce, where chatbots manage complex, adaptable tasks between specialized agents.