agentscope icon indicating copy to clipboard operation
agentscope copied to clipboard

关于 stream_printing_messages: 将智能体在回复过程中,调用 self.print 打印的消息转换为一个异步生成器

Open bjmajic opened this issue 1 month ago • 2 comments

在参考stream_printing_messages的源码中,貌似没有找到显示调用self.print, 且没有看到返回值的二元组的生成过程。该函数接受一个或多个智能体和一个协程任务作为输入,并返回一个异步生成器。 该异步生成器返回一个二元组,包含执行协程任务过程中通过 await self.print(...) 打印的消息,以及一个布尔值,表示该消息是否为一组流式消息中的最后一个

不知道是不是文档过时了。

bjmajic avatar Nov 11 '25 02:11 bjmajic

` # Enable the message queue to get the intermediate messages queue = asyncio.Queue() for agent in agents: # Use one queue to gather messages from all agents agent.set_msg_queue_enabled(True, queue)

# Execute the agent asynchronously
task = asyncio.create_task(coroutine_task)

if task.done():
    await queue.put(end_signal)
else:
    task.add_done_callback(lambda _: queue.put_nowait(end_signal))

# Receive the messages from the agent's message queue
while True:
    # The message obj, and a boolean indicating whether it's the last chunk
    # in a streaming message
    printing_msg = await queue.get()

    # End the loop when the message is None
    if isinstance(printing_msg, str) and printing_msg == end_signal:
        break

    yield printing_msg`

git 源码里yield的是一个str

`async def print(self, msg: Msg, last: bool = True) -> None: """The function to display the message.

Args:
    msg (`Msg`):
        The message object to be printed.
    last (`bool`, defaults to `True`):
        Whether this is the last one in streaming messages. For
        non-streaming message, this should always be `True`.
"""
if not self._disable_msg_queue:
    await self.msg_queue.put((deepcopy(msg), last))`

请问哪里显示调用了这个函数,agent的 call() 方法貌似只调用了 reply()

即使调用,貌似类型也对不上

bjmajic avatar Nov 11 '25 02:11 bjmajic

stream_printing_messages uses an async queue where print() is defined in AgentBase and called via await self.print(msg) in agent classes. When invoked, messages are both printed and enqueued, making them accessible through the stream_printing_messages pipeline for real-time streaming.

DavdGao avatar Nov 11 '25 07:11 DavdGao

This issue is marked as stale because there has been no activity for 21 days. Remove stale label or add new comments or this issue will be closed in 3 day.

github-actions[bot] avatar Dec 02 '25 09:12 github-actions[bot]

Close this stale issue.

github-actions[bot] avatar Dec 06 '25 09:12 github-actions[bot]