Getting Started with AutoGen: AI Agentic Design Patterns (1/3)

CellCS
8 min readJun 6, 2024

--

As a fan of the hit TV show “House, M.D.,” I’ve always been fascinated by the dynamic interplay between the characters, particularly the brilliant but unconventional Dr. Gregory House, the pragmatic Dr. Lisa Cuddy, and the compassionate Dr. James Wilson. This intrigue has inspired me to explore how we can emulate such complex interactions using AI agents. In this post, I’ll introduce you to the world of AutoGen and how to create various AI agents, including ConversableAgent, AssistantAgent, and UserProxyAgent. By leveraging the distinctive personalities of Dr. House, Dr. Cuddy, and Dr. Wilson, we’ll delve into AI agentic design patterns and demonstrate their applications in creating intelligent, interactive systems with single or multiple AI agents.
ConversableAgent will be used in this post. This agent will be designed to facilitate conversations without human input, ideal for automated customer service or interactive FAQ systems.

  1. Load Configuration List: Load the necessary configuration for the LLM from a JSON file (OAI_CONFIG_LIST file). You could use local Open Source LLM or using LLM through OpenAI or Azure OpenAI service API keys.
  2. Define LLM Configuration: Set the model, seed for reproducibility, configuration list, and the temperature for model generation.
config_list = config_list_from_json(env_or_file="../utils/OAI_CONFIG_LIST")

# Configure Large Language Model (LLM)
llm_config = {
"model": "gpt-3.5-turbo",
"seed": 44, # Set seed for reproducibility
"config_list": config_list, # Pass configuration list to LLM
"temperature": 0 # Set temperature for model generation
}

3. Initializing the ConversableAgent

With the LLM configuration in place, we can now initialize the ConversableAgent. This agent will be named “chatbot” and will operate in a mode where it doesn’t require human input.

# Initialize ConversableAgent
agent_conversation = ConversableAgent(
name="chatbot",
llm_config=llm_config,
human_input_mode="NEVER",
)

4. Practical Example

Example 1: Single Agent

Let’s consider another practical scenario where this agent could be utilized. Imagine you’re running the patient information desk at Princeton-Plainsboro Teaching Hospital in House MD TV, and you want to provide patients and visitors with automated assistance for common queries such as doctor availability, appointment scheduling, and hospital navigation.

    # Patient query
patient_query = "Can you tell me when Dr. Gregory House is available for a consultation?"
reply_conversation = agent_conversation.generate_reply(
messages=[{"content": patient_query, "role": "user"}]
)
pprint.pprint(reply_conversation)
reply_conversation

Example 2: Multiple Agents

# Initialize ConversableAgent
agent_cuddy = ConversableAgent(
name="Dr. Cuddy",
system_message="You are Dr. Lisa Cuddy, the Dean of Medicine at Princeton-Plainsboro Teaching Hospital. You are discussing the application of AI in the diagnosis, treatment, and management of Parkinson's disease. Emphasize the importance of evidence-based medicine, patient care, ethical considerations, and the potential benefits and risks associated with AI technologies.",
llm_config=llm_config,
human_input_mode="NEVER",
)

agent_house = ConversableAgent(
name="Dr. House",
system_message="You are Dr. Gregory House, the head of Diagnostic Medicine at Princeton-Plainsboro Teaching Hospital. You are discussing the application of AI in the diagnosis, treatment, and management of Parkinson's disease. Focus on the diagnostic potential of AI, question its limitations, challenge assumptions, and explore innovative uses of AI in complex medical cases."
"When you're ready to end the conversation, say 'I gotta go'.",
llm_config=llm_config,
human_input_mode="NEVER",
is_termination_msg=lambda msg: "I gotta go" in msg["content"],
)
    pprint.pprint(chat_result.chat_history)
pprint.pprint(chat_result.cost)
pprint.pprint(chat_result.summary)

These system messages are designed to guide the agents in their discussions, ensuring that two agents: Dr. Cuddy and Dr. House maintain their distinct personalities and areas of focus while addressing the topic.

Example 3: Sequential Chat with Multiple Agents

Here, we define a sequence of ConversableAgent interactions designed to guide a new customer through the onboarding process for a software product. The setup includes multiple agents, each with specific roles and responsibilities, and orchestrates a conversation workflow. Here is a breakdown of the agents and their tasks.

Onboarding Personal Information Agent: Starts the conversation by asking for the user’s name and the software they are enquiring about.

Purpose: Gather the customer’s name, organization, and the software they are enquiring about.
System Message: Instructs the agent to collect only the specified information and return ‘TERMINATE’ once the information is gathered.
Configuration: Uses the LLM configuration, does not execute code, and does not require human input.

Onboarding Topic Preference Agent: Asks the user about the topics they are interested in regarding the software.

Purpose: Gather information on the topics or questions the customer has about the software.
System Message: Instructs the agent to collect only topic preferences and return ‘TERMINATE’ once the information is gathered.
Configuration: Uses the LLM configuration, does not execute code, and does not require human input.

Customer Proxy Agent to Customer Engagement Agent: Passes the gathered information to the engagement agent to proceed with providing relevant information or guidance.

For Customer Engagement Agent:

Purpose: Provide a software introduction, troubleshooting, FAQ, and user guide based on the gathered information.
System Message: Instructs the agent to provide engaging and clear information to the user, and return ‘TERMINATE’ once the task is completed.
Configuration: Uses the LLM configuration, does not execute code, and does not require human input. Terminates when the message contains ‘terminate’.

For Customer Proxy Agent:

Purpose: Acts as a human proxy, always taking human input instead of generating responses using an LLM.
Configuration: Does not use LLM, does not execute code, and always requires human input. Terminates when the message contains ‘terminate’.

onboarding_personal_information_agent = ConversableAgent(
name="Onboarding Personal Information Agent",
system_message='''You are a helpful customer onboarding agent,
you are here to help new customers get started with our software.
Your job is to gather customer's name, orgnization and enquired software name.
Do not ask for other information. Return 'TERMINATE'
when you have gathered all the information.''',
llm_config=llm_config,
code_execution_config=False,
human_input_mode="NEVER",
)

onboarding_topic_preference_agent = ConversableAgent(
name="Onboarding Topic preference Agent",
system_message='''You are a helpful customer onboarding agent,
you are here to help new customers get started with our software.
Your job is to gather customer's topics about our software.
Do not ask for other information.
Return 'TERMINATE' when you have gathered all the information.''',
llm_config=llm_config,
code_execution_config=False,
human_input_mode="NEVER",
)

customer_engagement_agent = ConversableAgent(
name="Customer Engagement Agent",
system_message='''You are a helpful customer service agent
here to provide software introduction for the customer based on the user's
enquired software information and topic preferences.
This could include software introduction, software troubleshooting, FAQ and user guide.
Make sure to make it engaging and clear to user!
Return 'TERMINATE' when you are done.''',
llm_config=llm_config,
code_execution_config=False,
human_input_mode="NEVER",
is_termination_msg=lambda msg: "terminate" in msg.get("content").lower(),
)

#is designed to always take human input instead of generating responses using a language model.
customer_proxy_agent = ConversableAgent(
name="customer_proxy_agent",
llm_config=False,
code_execution_config=False,
human_input_mode="ALWAYS",
is_termination_msg=lambda msg: "terminate" in msg.get("content").lower(),
)

Then we define one chat. A chat in this context represents a communication exchange or series of exchanges between different agents in a predefined workflow. Each chat object contains information about:

Sender: The agent initiating the message.

Recipient: The agent receiving the message.

Message: The content of the message being sent.

Summary Method: How to summarize or reflect on the conversation, often using an LLM to process the conversation’s content.

Summary Arguments: Parameters for the summary method.

Max Turns: The maximum number of exchanges allowed in this part of the conversation.

Clear History: Whether to clear the conversation history after this chat.

chats = [
{
"sender": onboarding_personal_information_agent,
"recipient": customer_proxy_agent,
"message":
"Hello, I'm here to help you get started with our software."
"Could you tell me your name, orgnization and which software you are enquiring?",
"summary_method": "reflection_with_llm",
"summary_args": {
"summary_prompt" : "Return the customer information "
"into as JSON object only: "
"{'name': '', 'orgnization':'', 'software': ''}",
},
"max_turns": 2,
"clear_history" : True
},
{
"sender": onboarding_topic_preference_agent,
"recipient": customer_proxy_agent,
"message":
"Great! Could you tell me what topics or question you are "
"will to know about this software?",
"summary_method": "reflection_with_llm",
"max_turns": 2,
"clear_history" : False
},
{
"sender": customer_proxy_agent,
"recipient": customer_engagement_agent,
"message": "Let's find software guide to find solution.",
"max_turns": 2,
"summary_method": "reflection_with_llm",
},
]

Then intiate this chats:

    chat_results = initiate_chats(chats)

We also print out detail of this chat:

for chat_result in chat_results:
pprint.pprint(chat_result.summary)
pprint.pprint("\n")

for chat_result in chat_results:
pprint.pprint(chat_result.cost)
pprint.pprint("\n")

Console log after excute this py:

Init chat
Confirm user’s interaction
Confirmation
Human interaction
Chat Results

This example shows a pattern for designing sequential interactions between multiple AI agents, creating a cohesive and structured user experience. By clearly defining the role and behavior of each agent, you ensure that the conversation stays focused and efficient, guiding the user through a complex onboarding process.

Here are some key notes for equential interactions:

Agent Specialization: Each agent has a specific role, which helps in modularizing the conversation flow. This specialization allows for clear and targeted interactions, making the process efficient and user-friendly.
Human Proxy Agent: The inclusion of a human proxy agent highlights the flexibility of the system to incorporate human oversight when necessary. This is crucial in scenarios requiring human judgment or handling sensitive information.
Summary Methods: Using reflection_with_llm to summarize interactions ensures that the gathered information is structured and can be easily used by subsequent agents. This approach leverages the power of language models to extract and format data effectively.
Termination Conditions: Clearly defined termination conditions ensure that each agent knows when its task is complete, preventing unnecessary interactions and maintaining a smooth workflow.

Looks there are a lot about this one type of agents in different designs, will have one post to introduce other agents like AssistantAgent, UserProxyAgent, align with theirs user cases. Also will introduce other design patterns in coming two posts.

Happy reading, Happy Coding.

References:

  1. The House MD Love Chronicles: Unraveling House and Cuddy’s Hilarious Chemistry on Medium
  2. AutoGen Conversable agent
  3. deeplearning.ai: AI Agentic Design Patterns with AutoGen

--

--

CellCS

Software Engineer | Data, DevOps, AI Engineer | Health Tech Innovator | Researcher