A programming framework for agentic AI
Console pretty print for on_messages_stream (#4239)
Eric Zhu5da24e51e0c7
diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.pyindex 4b260d1b8..7e502498f 100644--- a/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py@@ -126,7 +126,8 @@ class AssistantAgent(BaseChatAgent):The following example demonstrates how to create an assistant agent with- a model client and a tool, and generate a stream of messages for a task.+ a model client and a tool, generate a stream of messages for a task, and+ print the messages to the console... code-block:: python@@ -134,6 +135,7 @@ class AssistantAgent(BaseChatAgent):from autogen_ext.models import OpenAIChatCompletionClientfrom autogen_agentchat.agents import AssistantAgentfrom autogen_agentchat.messages import TextMessage+ from autogen_agentchat.task import Consolefrom autogen_core.base import CancellationToken@@ -145,13 +147,12 @@ class AssistantAgent(BaseChatAgent):model_client = OpenAIChatCompletionClient(model="gpt-4o")agent = AssistantAgent(name="assistant", model_client=model_client, tools=[get_current_time])- stream = agent.on_messages_stream(- [TextMessage(content="What is the current time?", source="user")], CancellationToken()+ await Console(+ agent.on_messages_stream(+ [TextMessage(content="What is the current time?", source="user")], CancellationToken()+ ))- async for message in stream:- print(message)-asyncio.run(main())diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/task/_console.py b/python/packages/autogen-agentchat/src/autogen_agentchat/task/_console.pyindex 3ea14d241..3c6464b1f 100644--- a/python/packages/autogen-agentchat/src/autogen_agentchat/task/_console.py+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/task/_console.py@@ -6,7 +6,7 @@ from typing import AsyncGenerator, Listfrom autogen_core.components import Imagefrom autogen_core.components.models import RequestUsage-from autogen_agentchat.base import TaskResult+from autogen_agentchat.base import Response, TaskResultfrom autogen_agentchat.messages import AgentMessage, MultiModalMessage@@ -18,12 +18,17 @@ def _is_output_a_tty() -> bool:return sys.stdout.isatty()-async def Console(stream: AsyncGenerator[AgentMessage | TaskResult, None], *, no_inline_images: bool = False) -> None:- """Consume the stream from :meth:`~autogen_agentchat.teams.Team.run_stream`+async def Console(+ stream: AsyncGenerator[AgentMessage | TaskResult, None] | AsyncGenerator[AgentMessage | Response, None],+ *,+ no_inline_images: bool = False,+) -> None:+ """Consume the stream from :meth:`~autogen_agentchat.base.Team.run_stream`+ or :meth:`~autogen_agentchat.base.ChatAgent.on_messages_stream`and print the messages to the console.Args:- stream (AsyncGenerator[AgentMessage | TaskResult, None]): Stream to render+ stream (AsyncGenerator[AgentMessage | TaskResult, None] | AsyncGenerator[AgentMessage | Response, None]): Stream to renderno_inline_images (bool, optional): If terminal is iTerm2 will render images inline. Use this to disable this behavior. Defaults to False."""@@ -42,6 +47,30 @@ async def Console(stream: AsyncGenerator[AgentMessage | TaskResult, None], *, nof"Duration: {duration:.2f} seconds\n")sys.stdout.write(output)+ elif isinstance(message, Response):+ duration = time.time() - start_time++ # Print final response.+ output = f"{'-' * 10} {message.chat_message.source} {'-' * 10}\n{_message_to_str(message.chat_message, render_image_iterm=render_image_iterm)}\n"+ if message.chat_message.models_usage:+ output += f"[Prompt tokens: {message.chat_message.models_usage.prompt_tokens}, Completion tokens: {message.chat_message.models_usage.completion_tokens}]\n"+ total_usage.completion_tokens += message.chat_message.models_usage.completion_tokens+ total_usage.prompt_tokens += message.chat_message.models_usage.prompt_tokens+ sys.stdout.write(output)++ # Print summary.+ if message.inner_messages is not None:+ num_inner_messages = len(message.inner_messages)+ else:+ num_inner_messages = 0+ output = (+ f"{'-' * 10} Summary {'-' * 10}\n"+ f"Number of inner messages: {num_inner_messages}\n"+ f"Total prompt tokens: {total_usage.prompt_tokens}\n"+ f"Total completion tokens: {total_usage.completion_tokens}\n"+ f"Duration: {duration:.2f} seconds\n"+ )+ sys.stdout.write(output)else:output = f"{'-' * 10} {message.source} {'-' * 10}\n{_message_to_str(message, render_image_iterm=render_image_iterm)}\n"if message.models_usage:diff --git a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/agents.ipynb b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/agents.ipynbindex db3177f90..a4c92c8da 100644--- a/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/agents.ipynb+++ b/python/packages/autogen-core/docs/src/user-guide/agentchat-user-guide/tutorial/agents.ipynb@@ -29,7 +29,7 @@},{"cell_type": "code",- "execution_count": 11,+ "execution_count": 1,"metadata": {},"outputs": [],"source": [@@ -107,31 +107,55 @@"### Stream Messages\n","\n","We can also stream each message as it is generated by the agent by using the\n",- "{py:meth}`~autogen_agentchat.agents.AssistantAgent.on_messages_stream` method."+ "{py:meth}`~autogen_agentchat.agents.AssistantAgent.on_messages_stream` method,\n",+ "and use {py:class}`~autogen_agentchat.task.Console` to print the messages\n",+ "as they appear to the console."]},{"cell_type": "code",- "execution_count": 12,+ "execution_count": 2,"metadata": {},"outputs": [{"name": "stdout","output_type": "stream","text": [- "source='assistant' models_usage=RequestUsage(prompt_tokens=61, completion_tokens=15) content=[FunctionCall(id='call_fXhM4PeZsodhhUOlNiFkoBXF', arguments='{\"query\":\"AutoGen\"}', name='web_search')]\n",- "source='assistant' models_usage=None content=[FunctionExecutionResult(content='AutoGen is a programming framework for building multi-agent applications.', call_id='call_fXhM4PeZsodhhUOlNiFkoBXF')]\n",- "Response(chat_message=TextMessage(source='assistant', models_usage=RequestUsage(prompt_tokens=92, completion_tokens=31), content='AutoGen is a programming framework designed for building multi-agent applications. If you need more specific information about its features or usage, feel free to ask!'), inner_messages=[ToolCallMessage(source='assistant', models_usage=RequestUsage(prompt_tokens=61, completion_tokens=15), content=[FunctionCall(id='call_fXhM4PeZsodhhUOlNiFkoBXF', arguments='{\"query\":\"AutoGen\"}', name='web_search')]), ToolCallResultMessage(source='assistant', models_usage=None, content=[FunctionExecutionResult(content='AutoGen is a programming framework for building multi-agent applications.', call_id='call_fXhM4PeZsodhhUOlNiFkoBXF')])])\n"+ "---------- assistant ----------\n",+ "[FunctionCall(id='call_fSp5iTGVm2FKw5NIvfECSqNd', arguments='{\"query\":\"AutoGen information\"}', name='web_search')]\n",+ "[Prompt tokens: 61, Completion tokens: 16]\n",+ "---------- assistant ----------\n",+ "[FunctionExecutionResult(content='AutoGen is a programming framework for building multi-agent applications.', call_id='call_fSp5iTGVm2FKw5NIvfECSqNd')]\n",+ "---------- assistant ----------\n",+ "AutoGen is a programming framework designed for building multi-agent applications. If you need more detailed information or specific aspects about AutoGen, feel free to ask!\n",+ "[Prompt tokens: 93, Completion tokens: 32]\n",+ "---------- Summary ----------\n",+ "Number of inner messages: 2\n",+ "Total prompt tokens: 154\n",+ "Total completion tokens: 48\n",+ "Duration: 4.30 seconds\n"]}],"source": [+ "from autogen_agentchat.task import Console\n",+ "\n",+ "\n","async def assistant_run_stream() -> None:\n",- " async for message in agent.on_messages_stream(\n",- " [TextMessage(content=\"Find information on AutoGen\", source=\"user\")],\n",- " cancellation_token=CancellationToken(),\n",- " ):\n",- " print(message)\n",+ " # Option 1: read each message from the stream.\n",+ " # async for message in agent.on_messages_stream(\n",+ " # [TextMessage(content=\"Find information on AutoGen\", source=\"user\")],\n",+ " # cancellation_token=CancellationToken(),\n",+ " # ):\n",+ " # print(message)\n",+ "\n",+ " # Option 2: use Console to print all messages as they appear.\n",+ " await Console(\n",+ " agent.on_messages_stream(\n",+ " [TextMessage(content=\"Find information on AutoGen\", source=\"user\")],\n",+ " cancellation_token=CancellationToken(),\n",+ " )\n",+ " )\n","\n","\n","# Use asyncio.run(assistant_run_stream()) when running in a script.\n",