microsoft / autogen

A programming framework for agentic AI 🤖
https://microsoft.github.io/autogen/
Creative Commons Attribution 4.0 International
34.68k stars 5.01k forks source link

Add Console function to stream result to pretty print console output #4115

Closed ekzhu closed 2 weeks ago

ekzhu commented 2 weeks ago
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.task import Console, TextMentionTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_ext.models import OpenAIChatCompletionClient

# Define a tool
async def get_weather(city: str) -> str:
    return f"The weather in {city} is 73 degrees and Sunny."

async def main() -> None:
    # Define an agent
    weather_agent = AssistantAgent(
        name="weather_agent",
        model_client=OpenAIChatCompletionClient(model="gpt-4o-2024-08-06"),
        tools=[get_weather],
    )

    # Define termination condition
    termination = TextMentionTermination("TERMINATE")

    # Define a team
    agent_team = RoundRobinGroupChat([weather_agent], termination_condition=termination)

    # Run the team and stream messages
    stream = agent_team.run_stream(task="What is the weather in New York?")
    await Console(stream)

asyncio.run(main())
---------- user ----------
What is the weather in New York?
---------- weather_agent ----------
[FunctionCall(id='call_AhTZ2q3TNL8x0qs00e3wIZ7y', arguments='{"city":"New York"}', name='get_weather')]
[Prompt tokens: 79, Completion tokens: 15]
---------- weather_agent ----------
[FunctionExecutionResult(content='The weather in New York is 73 degrees and Sunny.', call_id='call_AhTZ2q3TNL8x0qs00e3wIZ7y')]
---------- weather_agent ----------
The weather in New York is currently 73 degrees and sunny.
[Prompt tokens: 90, Completion tokens: 14]
---------- weather_agent ----------
TERMINATE
[Prompt tokens: 137, Completion tokens: 4]
---------- Summary ----------
Number of messages: 5
Finish reason: Text 'TERMINATE' mentioned
Total prompt tokens: 306
Total completion tokens: 33
Duration: 1.43 seconds