Skip to content

Commit

Permalink
Update agentchat quickstart to show how to handle multi-round user in…
Browse files Browse the repository at this point in the history
…puts (microsoft#4735)

Update agentchat quickstart to show how to handle multi-round user inputs.
  • Loading branch information
ekzhu authored Dec 20, 2024
1 parent dda208f commit 4bce1e9
Showing 1 changed file with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"metadata": {},
"source": [
"Via AgentChat, you can build applications quickly using preset agents.\n",
"To illustrate this, we will begin with creating a team of a single agent\n",
"that can use tools and respond to messages.\n",
"To illustrate this, we will begin with creating a team of a single tool-use\n",
"agent that you can chat with.\n",
"\n",
"The following code uses the OpenAI model. If you haven't already, you need to\n",
"install the following package and extension:"
Expand Down Expand Up @@ -42,38 +42,48 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"---------- user ----------\n",
"What is the weather in New York?\n",
"What is the weather in NYC?\n",
"---------- weather_agent ----------\n",
"[FunctionCall(id='call_vN04UiNJgqSz6g3MHt7Renig', arguments='{\"city\":\"New York City\"}', name='get_weather')]\n",
"[Prompt tokens: 75, Completion tokens: 16]\n",
"---------- weather_agent ----------\n",
"[FunctionExecutionResult(content='The weather in New York City is 73 degrees and Sunny.', call_id='call_vN04UiNJgqSz6g3MHt7Renig')]\n",
"---------- weather_agent ----------\n",
"[FunctionCall(id='call_AhTZ2q3TNL8x0qs00e3wIZ7y', arguments='{\"city\":\"New York\"}', name='get_weather')]\n",
"[Prompt tokens: 79, Completion tokens: 15]\n",
"The weather in New York City is 73 degrees and Sunny.\n",
"---------- Summary ----------\n",
"Number of messages: 4\n",
"Finish reason: Maximum number of turns 1 reached.\n",
"Total prompt tokens: 75\n",
"Total completion tokens: 16\n",
"Duration: 1.15 seconds\n",
"---------- user ----------\n",
"What is the weather in Seattle?\n",
"---------- weather_agent ----------\n",
"[FunctionExecutionResult(content='The weather in New York is 73 degrees and Sunny.', call_id='call_AhTZ2q3TNL8x0qs00e3wIZ7y')]\n",
"[FunctionCall(id='call_BesYutZXJIMfu2TlDZgodIEj', arguments='{\"city\":\"Seattle\"}', name='get_weather')]\n",
"[Prompt tokens: 127, Completion tokens: 14]\n",
"---------- weather_agent ----------\n",
"The weather in New York is currently 73 degrees and sunny.\n",
"[Prompt tokens: 90, Completion tokens: 14]\n",
"[FunctionExecutionResult(content='The weather in Seattle is 73 degrees and Sunny.', call_id='call_BesYutZXJIMfu2TlDZgodIEj')]\n",
"---------- weather_agent ----------\n",
"TERMINATE\n",
"[Prompt tokens: 137, Completion tokens: 4]\n",
"The weather in Seattle is 73 degrees and Sunny.\n",
"---------- Summary ----------\n",
"Number of messages: 5\n",
"Finish reason: Text 'TERMINATE' mentioned\n",
"Total prompt tokens: 306\n",
"Total completion tokens: 33\n",
"Duration: 1.43 seconds\n"
"Number of messages: 4\n",
"Finish reason: Maximum number of turns 1 reached.\n",
"Total prompt tokens: 127\n",
"Total completion tokens: 14\n",
"Duration: 2.38 seconds\n"
]
}
],
"source": [
"from autogen_agentchat.agents import AssistantAgent\n",
"from autogen_agentchat.conditions import TextMentionTermination\n",
"from autogen_agentchat.teams import RoundRobinGroupChat\n",
"from autogen_agentchat.ui import Console\n",
"from autogen_ext.models.openai import OpenAIChatCompletionClient\n",
Expand All @@ -95,15 +105,17 @@
" tools=[get_weather],\n",
" )\n",
"\n",
" # Define termination condition\n",
" termination = TextMentionTermination(\"TERMINATE\")\n",
"\n",
" # Define a team\n",
" agent_team = RoundRobinGroupChat([weather_agent], termination_condition=termination)\n",
" # Define a team with a single agent and maximum auto-gen turns of 1.\n",
" agent_team = RoundRobinGroupChat([weather_agent], max_turns=1)\n",
"\n",
" # Run the team and stream messages to the console\n",
" stream = agent_team.run_stream(task=\"What is the weather in New York?\")\n",
" await Console(stream)\n",
" while True:\n",
" # Get user input from the console.\n",
" user_input = input(\"Enter a message (type 'exit' to leave): \")\n",
" if user_input.strip().lower() == \"exit\":\n",
" break\n",
" # Run the team and stream messages to the console.\n",
" stream = agent_team.run_stream(task=user_input)\n",
" await Console(stream)\n",
"\n",
"\n",
"# NOTE: if running this inside a Python script you'll need to use asyncio.run(main()).\n",
Expand Down

0 comments on commit 4bce1e9

Please sign in to comment.