Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
" ToolUseAssistantAgent,\n",
")\n",
"from autogen_agentchat.base import Response\n",
"from autogen_agentchat.messages import ChatMessage, StopMessage, TextMessage\n",
"from autogen_agentchat.task import StopMessageTermination\n",
"from autogen_agentchat.messages import ChatMessage, TextMessage\n",
"from autogen_agentchat.task import TextMentionTermination\n",
"from autogen_agentchat.teams import SelectorGroupChat\n",
"from autogen_core.base import CancellationToken\n",
"from autogen_core.components.tools import FunctionTool\n",
Expand All @@ -59,7 +59,7 @@
"metadata": {},
"source": [
"## Defining Agents\n",
"The `UserProxyAgent` allows the user to input messages directly. This agent waits for user input and returns a text message or a stop message if the user decides to terminate the conversation."
"The `UserProxyAgent` allows the user to input messages directly. This agent waits for user input and returns a text message. If the user decides to terminate the conversation by mentioning \"TERMINATE\", it will end the conversation."
]
},
{
Expand All @@ -74,12 +74,12 @@
"\n",
" @property\n",
" def produced_message_types(self) -> List[type[ChatMessage]]:\n",
" return [TextMessage, StopMessage]\n",
" return [TextMessage]\n",
"\n",
" async def on_messages(self, messages: Sequence[ChatMessage], cancellation_token: CancellationToken) -> Response:\n",
" user_input = await asyncio.get_event_loop().run_in_executor(None, input, \"Enter your response: \")\n",
" if \"TERMINATE\" in user_input:\n",
" return Response(chat_message=StopMessage(content=\"User has terminated the conversation.\", source=self.name))\n",
" return Response(chat_message=TextMessage(content=\"User has terminated the conversation.\", source=self.name))\n",
" return Response(chat_message=TextMessage(content=user_input, source=self.name))"
]
},
Expand All @@ -92,62 +92,23 @@
"async def flight_search(start: str, destination: str, date: str) -> str:\n",
" return \"\\n\".join(\n",
" [\n",
" f\"AC24 from {start} to {destination} on {date} is $500\",\n",
" f\"UA23 from {start} to {destination} on {date} is $450\",\n",
" f\"AL21 from {start} to {destination} on {date} is $400\",\n",
" ]\n",
" )\n",
"\n",
"\n",
"async def flight_booking(flight: str, date: str) -> str:\n",
" return f\"Booked flight {flight} on {date}\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `ToolUseAssistantAgent` is responsible for calling external tools. In this example, two tools are defined: `flight_search` and `flight_booking`.\n",
"\n",
"Additionally, the `CodingAssistantAgent` serves as a general travel assistant with predefined behavior specified in the `system_message`."
" f\"AC24 from {start} to {destination} on {date} is $500\",\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--------------------------------------------------------------------------- \n",
"\u001b[91m[2024-10-08T20:35:30.283450]:\u001b[0m\n",
"\n",
"Help user plan a trip and book a flight."
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--------------------------------------------------------------------------- \n",
"\u001b[91m[2024-10-08T20:35:48.275743], User:\u001b[0m\n",
"\n",
"\n",
"--------------------------------------------------------------------------- \n",
"\u001b[91m[2024-10-08T20:35:50.795496], TravelAssistant:\u001b[0m\n",
"\n",
"I'd be happy to help you plan your trip! To get started, could you please provide me with the following details:\n",
"\n",
"1. Your departure city and the destination city.\n",
"2. Your travel dates (departure and return).\n",
"3. The number of travelers and their ages (if any children are involved).\n",
"4. Your budget for flights and accommodations, if you have one in mind.\n",
"5. Any specific activities or attractions you're interested in at the destination.\n",
"\n",
}
]
}
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
" if \"TERMINATE\" in user_input:\n",
" return Response(chat_message=TextMessage(content=\"User has terminated the conversation.\", source=self.name))\n",
" return Response(chat_message=TextMessage(content=user_input, source=self.name))"
]
}
"\n",
"Once I have this information, I can help you find the best options!\n",
"--------------------------------------------------------------------------- \n",
"\u001b[91m[2024-10-08T20:35:59.701486], User:\u001b[0m\n",
Expand Down Expand Up @@ -215,6 +176,39 @@
" - **Date:** December 7, 2023\n",
"\n",
"- **Return:** Toronto to New York\n",
"--------------------------------------------------------------------------- \n",
"\u001b[91m[2024-10-08T20:36:45.875280], User:\u001b[0m\n",
"\n",
"that's it\n",
"--------------------------------------------------------------------------- \n",
"\u001b[91m[2024-10-08T20:36:50.925624], FlightBroker:\u001b[0m\n",
"\n",
"Your flights have been successfully booked! Here are the details:\n",
"\n",
"- **Departure:** New York to Toronto\n",
" - **Flight:** AL21\n",
" - **Date:** December 7, 2023\n",
"\n",
"- **Return:** Toronto to New York\n",
" - **Flight:** AL21\n",
" - **Date:** December 12, 2023\n",
"\n",
"If you need help with accommodations, activities, or anything else for your trip, feel free to let me know! \n",
"\n",
"TERMINATE"
]
},
{
"data": {
"text/plain": [
"\n",
"Your flights have been successfully booked! Here are the details:\n",
"\n",
"- **Departure:** New York to Toronto\n",
" - **Flight:** AL21\n",
" - **Date:** December 7, 2023\n",
"\n",
"- **Return:** Toronto to New York\n",
" - **Flight:** AL21\n",
" - **Date:** December 12, 2023\n",
"\n",
Expand All @@ -235,6 +229,15 @@
}
],
"source": [
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"id": "some-id",
"metadata": {},
"outputs": [],
"source": [
"user_proxy = UserProxyAgent(\"User\")\n",
"flight_broker = ToolUseAssistantAgent(\n",
" \"FlightBroker\",\n",
Expand All @@ -254,7 +257,7 @@
"team = SelectorGroupChat(\n",
" [user_proxy, flight_broker, travel_assistant], model_client=OpenAIChatCompletionClient(model=\"gpt-4o-mini\")\n",
")\n",
"await team.run(\"Help user plan a trip and book a flight.\", termination_condition=StopMessageTermination())"
"await team.run(\"Help user plan a trip and book a flight.\", termination_condition=TextMentionTermination(\"TERMINATE\"))"
]
}
],
Expand All @@ -279,4 +282,4 @@
},
"nbformat": 4,
"nbformat_minor": 2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"from autogen_agentchat import EVENT_LOGGER_NAME\n",
"from autogen_agentchat.agents import CodingAssistantAgent, ToolUseAssistantAgent\n",
"from autogen_agentchat.logging import ConsoleLogHandler\n",
"from autogen_agentchat.task import MaxMessageTermination\n",
"from autogen_agentchat.task import MaxMessageTermination, TextMentionTermination\n",
"from autogen_agentchat.teams import RoundRobinGroupChat, SelectorGroupChat\n",
"from autogen_core.components.models import OpenAIChatCompletionClient\n",
"from autogen_core.components.tools import FunctionTool\n",
Expand Down Expand Up @@ -75,6 +75,40 @@
"A team where agents take turns sending messages (in a round robin fashion) until a termination condition is met. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"--------------------------------------------------------------------------- \n",
"\u001b[91m[2024-10-20T09:01:04.692283]:\u001b[0m\n",
"\n",
"Write a Haiku about the weather in Paris\n",
"--------------------------------------------------------------------------- \n",
"\u001b[91m[2024-10-20T09:01:05.961670], tool_use_agent:\u001b[0m\n",
"\n",
"Golden sun above, \n"
]
}
],
"source": [
"team = RoundRobinGroupChat(participants=[writing_assistant_agent, tool_use_agent])\n",
"result = await team.run(\n",
" \"Write a Haiku about the weather in Paris\",\n",
" termination_condition=TextMentionTermination(\"TERMINATE\")\n",
")\n",
"print(result)"
]
}
]
}
]
},
{
"cell_type": "code",
"execution_count": 2,
Expand Down Expand Up @@ -159,28 +193,8 @@
"Seine flows in sunshine.\n",
"--------------------------------------------------------------------------- \n",
"\u001b[91m[2024-10-20T09:01:08.320789], writing_assistant_agent:\u001b[0m\n",
"\n",
"I can't check the real-time weather, but you can use a weather website or app to find the current weather in Paris. If you need a fresh haiku, here's one for sunny weather:\n",
"\n",
"Paris bathed in sun, \n",
"Gentle warmth embraces all, \n",
"Seine sparkles with light.\n",
"--------------------------------------------------------------------------- \n",
"\u001b[91m[2024-10-20T09:01:08.321296], Termination:\u001b[0m\n",
"\n",
"Maximal number of messages 2 reached, current message count: 2"
]
}
],
"source": [
"llm_team = SelectorGroupChat([tool_use_agent, writing_assistant_agent], model_client=model_client)\n",
"\n",
"llm_team_result = await llm_team.run(\n",
" \"What is the weather in paris right now? Also write a haiku about it.\",\n",
" termination_condition=MaxMessageTermination(max_messages=2),\n",
")"
]
},
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -189,29 +203,8 @@
"\n",
"In this section, we reviewed how to define model clients, agents, and teams in AgentChat. Here are some other concepts to explore further:\n",
"\n",
"- Termination Conditions: Define conditions that determine when a team should stop running. In this sample, we used a `MaxMessageTermination` condition to stop the team after a certain number of messages. Explore other termination conditions supported in the AgentChat package."
"- Termination Conditions: Define conditions that determine when a team should stop running. In this sample, we used a `MaxMessageTermination` condition to stop the team after a certain number of messages. Explore other termination conditions supported in the AgentChat package, such as `TextMentionTermination(\"TERMINATE\")` which stops the team when a specific text mention is detected."
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
]
}
Loading