Group Chat with Tools
pip install -U ag2[openai]
Note: If you have been usingautogen
orag2
, all you need to do is upgrade it using:orCopypip install -U autogen[openai]
asCopypip install -U ag2[openai]
autogen
, andag2
are aliases for the same PyPI package.
import random
from autogen import (
ConversableAgent,
GroupChat,
GroupChatManager,
LLMConfig,
UserProxyAgent,
register_function,
)
GroupChat
will contain three agents: - sales_agent
- Responsible
for selling tickets. - cancellation_agent
- Handles ticket
cancellations. - user_proxy
- Acts as an intermediary between the user
and other agents.
llm_config = LLMConfig.from_json(path="OAI_CONFIG_LIST").where(model=["gpt-4o-mini"])
sales_agent = ConversableAgent(
name="SalesAgent",
llm_config=llm_config,
)
cancellation_agent = ConversableAgent(
name="CanelationAgent",
llm_config=llm_config,
)
user_proxy = UserProxyAgent(
name="user_proxy",
human_input_mode="ALWAYS",
)
buy_airplane_ticket
: Suggested by
sales_agent
and executed by user_proxy
after user verification. -
cancel_airplane_ticket
: Suggested by cancellation_agent
and executed
by user_proxy
after user verification.
def buy_airplane_ticket(from_location: str, to_location: str, date: str) -> str:
ticket_number = random.randint(1000, 9999)
return f"""Your ticket from {from_location} to {to_location} on {date} has been booked.
Your ticket number is {ticket_number}.
Please keep this number for future reference.
"""
register_function(
buy_airplane_ticket,
caller=sales_agent,
executor=user_proxy,
description="Buy an airplane ticket",
)
def cancel_airplane_ticket(ticket_number: str) -> str:
return f"Your ticket with ticket number {ticket_number} has been canceled"
register_function(
cancel_airplane_ticket,
caller=cancellation_agent,
executor=user_proxy,
description="Cancel an airplane ticket",
)
GroupChat
with the three agents.
groupchat = GroupChat(
agents=[user_proxy, cancellation_agent, sales_agent],
speaker_selection_method="auto",
messages=[],
)
manager = GroupChatManager(
name="group_manager",
groupchat=groupchat,
llm_config=llm_config,
)
user_proxy.initiate_chat(
recipient=manager,
message="I need to buy a plane ticket from New York to Los Angeles on 12th of April 2025",
)