Skip to content

Commit

Permalink
Have Console print multimodal messages nicely. (microsoft#4179)
Browse files Browse the repository at this point in the history
  • Loading branch information
afourney authored Nov 14, 2024
1 parent 36b822c commit 4073dd4
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import sys
import time
from typing import AsyncGenerator
from typing import AsyncGenerator, List

from autogen_core.components.models import RequestUsage

from autogen_agentchat.base import TaskResult
from autogen_agentchat.messages import AgentMessage
from autogen_agentchat.messages import AgentMessage, MultiModalMessage


async def Console(stream: AsyncGenerator[AgentMessage | TaskResult, None]) -> None:
Expand All @@ -27,9 +27,22 @@ async def Console(stream: AsyncGenerator[AgentMessage | TaskResult, None]) -> No
)
sys.stdout.write(output)
else:
output = f"{'-' * 10} {message.source} {'-' * 10}\n{message.content}\n"
output = f"{'-' * 10} {message.source} {'-' * 10}\n{_message_to_str(message)}\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)


def _message_to_str(message: AgentMessage) -> str:
if isinstance(message, MultiModalMessage):
result: List[str] = []
for c in message.content:
if isinstance(c, str):
result.append(c)
else:
result.append("<image>")
return "\n".join(result)
else:
return f"{message.content}"

0 comments on commit 4073dd4

Please sign in to comment.