Make import self-consistent in large code blocks in agent chat tutorial.
What happened?
trying to run the sample code:
from typing import AsyncGenerator, List, Sequence
from autogen_agentchat.agents import BaseChatAgent
from autogen_agentchat.base import Response
from autogen_agentchat.messages import AgentMessage, ChatMessage
import asyncio
class CountDownAgent(BaseChatAgent):
def __init__(self, name: str, count: int = 3):
super().__init__(name, "A simple agent that counts down.")
self._count = count
@property
def produced_message_types(self) -> List[type[ChatMessage]]:
return [TextMessage]
async def on_messages(self, messages: Sequence[ChatMessage], cancellation_token: CancellationToken) -> Response:
# Calls the on_messages_stream.
response: Response | None = None
async for message in self.on_messages_stream(messages, cancellation_token):
if isinstance(message, Response):
response = message
assert response is not None
return response
async def on_messages_stream(
self, messages: Sequence[ChatMessage], cancellation_token: CancellationToken
) -> AsyncGenerator[AgentMessage | Response, None]:
inner_messages: List[AgentMessage] = []
for i in range(self._count, 0, -1):
msg = TextMessage(content=f"{i}...", source=self.name)
inner_messages.append(msg)
yield msg
# The response is returned at the end of the stream.
# It contains the final message and all the inner messages.
yield Response(chat_message=TextMessage(content="Done!", source=self.name), inner_messages=inner_messages)
async def on_reset(self, cancellation_token: CancellationToken) -> None:
pass
async def run_countdown_agent() -> None:
# Create a countdown agent.
countdown_agent = CountDownAgent("countdown")
# Run the agent with a given task and stream the response.
async for message in countdown_agent.on_messages_stream([], CancellationToken()):
if isinstance(message, Response):
print(message.chat_message.content)
else:
print(message.content)
# Use asyncio.run(run_countdown_agent()) when running in a script.
asyncio.run(run_countdown_agent())
however it fails with
(autogenDev6) VSKUMAR@VSKUMAR-mac autogen % python counter.py
Traceback (most recent call last):
File "/Users/VSKUMAR/code/research/autogen/counter.py", line 9, in
What did you expect to happen?
it should run
How can we reproduce it (as minimally and precisely as possible)?
conda create -n autogenDev6 python=3.10
conda activate autogenDev6
pip install 'autogen-agentchat==0.4.0.dev6'
pip install 'autogen-ext[openai]==0.4.0.dev6'
python counter.py
AutoGen version
0.4.0dev6
Which package was this bug in
AgentChat
Model used
gpt4o
Python version
3.10
Operating system
macosx
Any additional info you think would be helpful for fixing this bug
No response
Looks like this is from: https://microsoft.github.io/autogen/dev/user-guide/agentchat-user-guide/tutorial/agents.html#counterdownagent
In your snippet you're missing:
from autogen_agentchat.messages import TextMessage
from autogen_core.base import CancellationToken
Looks like this import was done earlier in the notebook, so CI didn't catch this. But it's certainly confusing and that snippet is meant to be a self contained example, so all imports should be present for it.