forked from microsoft/autogen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Console function to stream result to pretty print console output (m…
- Loading branch information
Showing
5 changed files
with
63 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
python/packages/autogen-agentchat/src/autogen_agentchat/task/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
from ._console import Console | ||
from ._terminations import MaxMessageTermination, StopMessageTermination, TextMentionTermination, TokenUsageTermination | ||
|
||
__all__ = [ | ||
"MaxMessageTermination", | ||
"TextMentionTermination", | ||
"StopMessageTermination", | ||
"TokenUsageTermination", | ||
"Console", | ||
] |
35 changes: 35 additions & 0 deletions
35
python/packages/autogen-agentchat/src/autogen_agentchat/task/_console.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import sys | ||
import time | ||
from typing import AsyncGenerator | ||
|
||
from autogen_core.components.models import RequestUsage | ||
|
||
from autogen_agentchat.base import TaskResult | ||
from autogen_agentchat.messages import AgentMessage | ||
|
||
|
||
async def Console(stream: AsyncGenerator[AgentMessage | TaskResult, None]) -> None: | ||
"""Consume the stream from :meth:`~autogen_agentchat.teams.Team.run_stream` | ||
and print the messages to the console.""" | ||
|
||
start_time = time.time() | ||
total_usage = RequestUsage(prompt_tokens=0, completion_tokens=0) | ||
async for message in stream: | ||
if isinstance(message, TaskResult): | ||
duration = time.time() - start_time | ||
output = ( | ||
f"{'-' * 10} Summary {'-' * 10}\n" | ||
f"Number of messages: {len(message.messages)}\n" | ||
f"Finish reason: {message.stop_reason}\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.content}\n" | ||
if message.models_usage: | ||
output += f"[Prompt tokens: {message.models_usage.prompt_tokens}, Completion tokens: {message.models_usage.completion_tokens}]\n" | ||
total_usage.completion_tokens += message.models_usage.completion_tokens | ||
total_usage.prompt_tokens += message.models_usage.prompt_tokens | ||
sys.stdout.write(output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters