Chatbots have evolved exponentially with advances in artificial intelligence (ai). Now, with the emergence of ai agents, they have become capable of handling more complex and layered interactions, far beyond traditional conversational boundaries. In our previous article on Building Multi-Agent Chatbots with AutoGen, we explored the concept of sequential chat using AutoGen, which allows structured turn-taking communication between multiple agents. Now, building on that, we'll move on to a more complex feature: nested chat. With AutoGen's robust framework, nested conversations allow robots to maintain fluid exchanges rather than following a fixed sequence. They can drill down into other tools, handle interruptions, and resume seamlessly, all within a single conversation flow. This article will guide you through implementing nested chat in AutoGen and highlight its relevance in creating dynamic and responsive agent interactions.
What is nested chat?
Let's start by understanding what a nested chat is.
Consider a three-agent chat where two agents need to repeatedly talk to each other in a loop. This chat between two agents can be added in a nested chat. Once this separate conversation is finished, the agent can return the context to the main conversation.
The following figure shows the conversion flow of a nested chat.
When the incoming message triggers a condition, that message is moved to the nested chat. That nesting can be a two-agent chat, a sequential chat, or any other. The results of the nested chat are then sent back to the main conversation.
Nested Chat Implementation in AutoGen
In this article, we will create an article writing system using a nested chat. To do this, we will create three agents: one to write an outline of the article, another to write the article based on this outline, and another to review the article. We'll want the writer and reviewer to talk to each other multiple times, so we'll have them in a nested chat.
In addition, we will also provide the scheme agent with access to a web query tool.
Now, let's implement this with code.
Prerequisites
Before creating AutoGen agents, ensure you have the necessary API keys for the required LLMs. For this exercise, we will also use Tavily to search the web.
Upload the .env file with the necessary API keys. Here we will use OpenAI and they melted API Keys ().
from dotenv import load_dotenv
load_dotenv('/home/santhosh/Projects/courses/Pinnacle/.env')
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
Tavily-python – 0.5.0
Now, let's get to the implementation.
Step 1: Define the schema agent using the tool
Define the user_proxy agent that will also run the tool. Then define the Outline Agent using the LLM to generate the item outline.
from autogen import ConversableAgent
user_proxy = ConversableAgent(
name="User",
llm_config=False,
is_termination_msg=lambda msg: msg.get("content") is not None and "TERMINATE" in msg("content"),
human_input_mode="TERMINATE")
outline = ConversableAgent(
name="Article_outline",
system_message="""You are an expert content strategist tasked with creating a detailed outline
for an article on a specified topic. Your goal is to organize the article into
logical sections that help convey the main ideas clearly and effectively.
Use the web_search tool if needed.
Return 'TERMINATE' when the task is done.""",
llm_config=config_list,
silent=False,
)
Define the web_search function to query the web.
def web_search(query: str) -> str:
tavily_client = TavilyClient()
response = tavily_client.search(query, max_results=3, days=10, include_raw_content=True)
return response('results')
Register the web_search function in the schema agent with the executor as user_proxy.
We are making the executor user_proxy so we can review the schema that goes to the writer agent.
register_function(
web_search,
caller=outline, # The assistant agent can suggest calls.
executor=user_proxy, # The user proxy agent can execute the calls.
name="web_search", # By default, the function name is used as the tool name.
description="Searches internet to get the results a for given query", # A description of the tool.
)
Step 2: Define the writer and reviewer agents
Define one agent to generate the article content and another to review the article and provide suggestions for improvement.
writer = ConversableAgent(
name="Article_Writer",
system_message="""You are a skilled writer assigned to create a comprehensive, engaging article
based on a given outline. Your goal is to follow the structure provided in the outline,
expanding on each section with well-researched, clear, and informative content.
Keep the article length to around 500 words.
Use the web_search tool if needed.
Return 'TERMINATE' when the task is done.""",
llm_config=config_list,
silent=False,
)
reviewer = ConversableAgent(
name="Article_Reviewer",
system_message="""You are a skilled article reviewer who can review technical articles.
Review the given article and provide suggestions to make the article more engaging and interesting.
""",
llm_config=config_list,
silent=False,
)
Step 3: Register the nested chat
Now we can register the nested chats for both agents.
writer.register_nested_chats(
trigger=user_proxy,
chat_queue=(
{
"sender": reviewer,
"recipient": writer,
"summary_method": "last_msg",
"max_turns": 2,
}
),
)
In the above code, when user_proxy sends any message to the writer agent, this will trigger the nested chat. Then the writing agent will write the article and the reviewing agent will review the article as many times as max_turns, two in this case. Finally, the result of the nested chat is sent to the user agent.
Step 4: Start Nested Chat
Now that everything is set up, let's start the chat.
chat_results = user_proxy.initiate_chats(
({"recipient": outline,
"message": "Write an article on Magentic-One agentic system released by Microsoft.",
"summary_method": "last_msg",
},
{"recipient": writer,
"message": "This is the article outline",
"summary_method": "last_msg",
}))
Here we are going to write an article about Magentic-One agent system. First, the user_proxy agent will start a chat with the Outline Agent and then with the Writer Agent.
Now, the output of the above code will be like this:
As we can see, user_proxy first sends a message indicating the topic of the article to the Outline Agent. This triggers the tool call and user_proxy runs the tool. Based on these results, the Outline Agent will generate the outline and send it to the writing agent. After that, the nested chat between the writing agent and the reviewing agent continues as discussed above.
Now let's print the final result, which is the article about magentic-one.
print(chat_results(1).chat_history(-2)('content'))
Conclusion
Nested chat in AutoGen enhances chatbot capabilities by enabling complex interactions and multitasking within a single conversation flow. Nested chat allows bots to start separate specialized chats and integrate their results seamlessly. This feature supports dynamic and targeted responses in various applications, from e-commerce to healthcare. With nested chat, AutoGen paves the way for more responsive and context-sensitive ai systems. This allows developers to create sophisticated chatbots that meet the various needs of users efficiently.
If you want to learn more about ai agents, check out our exclusive Agentic ai Pioneer program.
Frequently asked questions
A. Nested chat in AutoGen allows a chatbot to manage multiple sub-conversations within a single chat flow, often involving other agents or tools to retrieve specific information. Unlike sequential chat, which follows a structured turn-based approach, nested chat allows bots to handle interruptions and parallel tasks, integrating their results into the main conversation.
A. Nested chat improves customer service by allowing bots to delegate tasks to specialized agents. For example, in e-commerce, a chatbot can consult an independent agent to check order status or product information and then seamlessly transmit the information to the user, ensuring faster and more accurate responses.
A. Nested chat can be applied in various industries. In banking, it provides specialized support for account and loan inquiries; in Human Resources, helps with onboarding tasks; and in the healthcare sector, she is responsible for scheduling appointments and billing inquiries. This flexibility makes nested chat suitable for any domain that requires multitasking and detailed information handling.
A. Yes, implementing nested chat in AutoGen requires configuring agents with specific API keys, such as language models or web search tools like Tavily. Furthermore, each agent must be defined with appropriate tasks and tools for the smooth execution of nested conversations.
A. Yes, AutoGen allows you to track the cost incurred by each agent in a nested chat. By accessing the “cost” attribute in chat results, developers can monitor expenses related to agent interactions, helping to optimize chatbot resource usage and efficiency.