Skip to content

Build and run agents you can see, understand and trust.

Support to obtain the printing messages from an agent instance as an async generator (#814)

DavdGao8845e4b2e050
diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml
index 3b31d25d1..26bed3d3b 100644
--- a/.github/workflows/stale.yml
+++ b/.github/workflows/stale.yml
@@ -22,10 +22,13 @@ jobs:
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: '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.'
- stale-pr-message: 'This PR is marked as stale because there has been no activity for 21 days. Remove stale label or add new comments or this PR will be closed in 3 day.'
close-issue-message: 'Close this stale issue.'
- close-pr-message: 'Close this stale PR.'
stale-issue-label: 'stale-issue'
- stale-pr-label: 'stale-pr'
days-before-stale: 21
- days-before-close: 3
\ No newline at end of file
+ days-before-close: 3
+
+ stale-pr-message: 'This PR is marked as stale because there has been no activity for 30 days. Remove stale label or add new comments or this PR will be closed in 5 day.'
+ close-pr-message: 'Close this stale PR.'
+ stale-pr-label: 'stale-pr'
+ days-before-pr-stale: 40
+ days-before-pr-close: 5
\ No newline at end of file
diff --git a/README.md b/README.md
index 0f222a10b..a61a0d3b0 100644
--- a/README.md
+++ b/README.md
@@ -351,12 +351,20 @@ as_studio
- API
- [API Docs](https://doc.agentscope.io/api/agentscope.html)
- [Examples](https://github.com/agentscope-ai/agentscope/tree/main/examples)
+ - Functional
+ - [MCP](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/mcp)
+ - [Plan](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/plan)
+ - [Structured Output](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/structured_output)
+ - [RAG](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/rag)
+ - [Long-Term Memory](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/long_term_memory)
+ - [Session with SQLite](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/session_with_sqlite)
+ - [Stream Printing Messages](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/stream_printing_messages)
- Agent
- [ReAct Agent](https://github.com/agentscope-ai/agentscope/tree/main/examples/react_agent)
+ - [Voice Agent](https://github.com/agentscope-ai/agentscope/tree/main/examples/agent/voice_agent)
- [Deep Research Agent](https://github.com/agentscope-ai/agentscope/tree/main/examples/agent_deep_research)
- [Browser-use Agent](https://github.com/agentscope-ai/agentscope/tree/main/examples/agent_browser)
- [Meta Planner Agent](https://github.com/agentscope-ai/agentscope/tree/main/examples/meta_planner_agent)
- - [Voice Agent](https://github.com/agentscope-ai/agentscope/tree/main/examples/agent/voice_agent)
- Game
- [Nine-player Werewolves](https://github.com/agentscope-ai/agentscope/tree/main/examples/game/werewolves)
- Workflow
@@ -365,13 +373,7 @@ as_studio
- [Multi-agent Concurrent](https://github.com/agentscope-ai/agentscope/tree/main/examples/workflows/multiagent_concurrent)
- Evaluation
- [ACEBench](https://github.com/agentscope-ai/agentscope/tree/main/examples/evaluation/ace_bench)
- - Functional
- - [MCP](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/mcp)
- - [Plan](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/plan)
- - [Structured Output](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/structured_output)
- - [RAG](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/rag)
- - [Long-Term Memory](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/long_term_memory)
- - [Session with SQLite](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/session_with_sqlite)
+
## ⚖️ License
diff --git a/README_zh.md b/README_zh.md
index fe6f8c00f..5ac3656b8 100644
--- a/README_zh.md
+++ b/README_zh.md
@@ -353,12 +353,20 @@ as_studio
- API
- [API文档](https://doc.agentscope.io/zh_CN/api/agentscope.html)
- [示例](https://github.com/agentscope-ai/agentscope/tree/main/examples)
+ - 功能演示
+ - [MCP](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/mcp)
+ - [计划](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/plan)
+ - [结构化输出](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/structured_output)
+ - [RAG](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/rag)
+ - [长期记忆](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/long_term_memory)
+ - [基于DB的会话管理](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/session_with_sqlite)
+ - [流式获取打印消息](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/stream_printing_messages)
- 智能体
- [ReAct 智能体](https://github.com/agentscope-ai/agentscope/tree/main/examples/react_agent)
+ - [语音智能体](https://github.com/agentscope-ai/agentscope/tree/main/examples/agent/voice_agent)
- [Deep Research 智能体](https://github.com/agentscope-ai/agentscope/tree/main/examples/agent_deep_research)
- [Browser-use 智能体](https://github.com/agentscope-ai/agentscope/tree/main/examples/agent_browser)
- [Meta Planner 智能体](https://github.com/agentscope-ai/agentscope/tree/main/examples/meta_planner_agent)
- - [语音智能体](https://github.com/agentscope-ai/agentscope/tree/main/examples/agent/voice_agent)
- 游戏
- [九人制狼人杀](https://github.com/agentscope-ai/agentscope/tree/main/examples/game/werewolves)
- 工作流
@@ -367,13 +375,7 @@ as_studio
- [多智能体并发](https://github.com/agentscope-ai/agentscope/tree/main/examples/workflows/multiagent_concurrent)
- 评测
- [ACEBench](https://github.com/agentscope-ai/agentscope/tree/main/examples/evaluation/ace_bench)
- - 功能演示
- - [MCP](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/mcp)
- - [计划](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/plan)
- - [结构化输出](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/structured_output)
- - [RAG](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/rag)
- - [长期记忆](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/long_term_memory)
- - [基于DB的会话管理](https://github.com/agentscope-ai/agentscope/tree/main/examples/functionality/session_with_sqlite)
+
## ⚖️ 许可
diff --git a/docs/tutorial/en/index.rst b/docs/tutorial/en/index.rst
index 7fa644723..ff076b475 100644
--- a/docs/tutorial/en/index.rst
+++ b/docs/tutorial/en/index.rst
@@ -67,6 +67,10 @@ Welcome to AgentScope's documentation!
api/agentscope.memory
api/agentscope.tool
api/agentscope.mcp
+ api/agentscope.embedding
+ api/agentscope.evaluate
+ api/agentscope.plan
+ api/agentscope.rag
api/agentscope.token
api/agentscope.tracing
api/agentscope.session
diff --git a/docs/tutorial/en/src/task_pipeline.py b/docs/tutorial/en/src/task_pipeline.py
index 55f1f9971..34a41faed 100644
--- a/docs/tutorial/en/src/task_pipeline.py
+++ b/docs/tutorial/en/src/task_pipeline.py
@@ -11,6 +11,7 @@ as syntax sugar for chaining agents together, including
- **MsgHub**: a message hub for broadcasting messages among multiple agents
- **sequential_pipeline** and **SequentialPipeline**: a functional and class-based implementation that chains agents in a sequential manner
- **fanout_pipeline** and **FanoutPipeline**: a functional and class-based implementation that distributes the same input to multiple agents
+- **stream_printing_messages**: a utility function that convert the printing messages from agent(s) into an async generator
"""
@@ -20,7 +21,8 @@ from agentscope.formatter import DashScopeMultiAgentFormatter
from agentscope.message import Msg
from agentscope.model import DashScopeChatModel
from agentscope.agent import ReActAgent
-from agentscope.pipeline import MsgHub
+from agentscope.pipeline import MsgHub, stream_printing_messages
+
# %%
# Broadcasting with MsgHub
@@ -136,10 +138,11 @@ asyncio.run(check_broadcast_message())
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Pipeline serves as a syntax sugar for multi-agent orchestration.
#
-# Currently, AgentScope provides two main pipeline implementations:
+# Currently, AgentScope provides three main pipeline implementations:
#
# 1. **Sequential Pipeline**: Execute agents one by one in a predefined order
# 2. **Fanout Pipeline**: Distribute the same input to multiple agents and collect their responses
+# 3. **Stream Printing Messages**: Convert the printing messages from an agent into an async generator
#
# Sequential Pipeline
# ------------------------
@@ -212,10 +215,45 @@ asyncio.run(check_broadcast_message())
#
# .. tip::
# By combining ``MsgHub`` and ``sequential_pipeline`` or ``fanout_pipeline``, you can create more complex workflows very easily.
+#
+#
+# Stream Printing Messages
+# -------------------------------------
+# The ``stream_printing_messages`` function converts the printing messages from agent(s) into an async generator.
+# It will help you to obtain the intermediate messages from the agent(s) in a streaming way.
+#
+# It accepts a list of agents and a coroutine task, then returns an async generator that yields tuples of ``(Msg, bool)``,
+# containing the printing message during execution of the coroutine task.
+#
+# Note the messages with the same ``id`` are considered as the same message, and the ``last`` flag indicates whether it's the last chunk of this message.
+#
+# Taking the following code snippet as an example:
+
+
+async def run_example_pipeline() -> None:
+ """Run an example of streaming printing messages."""
+ agent = create_agent("Alice", 20, "student")
+
+ # We disable the terminal printing to avoid messy outputs
+ agent.set_console_output_enabled(False)
+
+ async for msg, last in stream_printing_messages(
+ agents=[agent],
+ coroutine_task=agent(
+ Msg("user", "Hello, who are you?", "user"),
+ ),
+ ):
+ print(msg, last)
+ if last:
+ print()
+
+
+asyncio.run(run_example_pipeline())
+
# %%
# Advanced Pipeline Features
-# ~~~~~~~~~~~~~~~~~~~~~~~~~
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Additionally, for reusability, we also provide a class-based implementation:
#
diff --git a/docs/tutorial/en/src/task_prompt.py b/docs/tutorial/en/src/task_prompt.py
index 26f3d6ff1..59bc489ae 100644
--- a/docs/tutorial/en/src/task_prompt.py
+++ b/docs/tutorial/en/src/task_prompt.py
@@ -260,7 +260,7 @@ async def run_token_counter() -> int:
# We use huggingface token counter for dashscope models.
token_counter = HuggingFaceTokenCounter(
"Qwen/Qwen2.5-VL-3B-Instruct",
- use_mirror=True,
+ use_mirror=False,
)
return await token_counter.count(formatted_message)
@@ -279,7 +279,7 @@ async def run_truncated_formatter() -> None:
"""Example of how to format messages with truncation."""
token_counter = HuggingFaceTokenCounter(
pretrained_model_name_or_path="Qwen/Qwen2.5-VL-3B-Instruct",
- use_mirror=True,
+ use_mirror=False,
)
formatter = DashScopeMultiAgentFormatter(
token_counter=token_counter,
diff --git a/docs/tutorial/zh_CN/index.rst b/docs/tutorial/zh_CN/index.rst
index 7fa644723..ff076b475 100644
--- a/docs/tutorial/zh_CN/index.rst
+++ b/docs/tutorial/zh_CN/index.rst
@@ -67,6 +67,10 @@ Welcome to AgentScope's documentation!
api/agentscope.memory
api/agentscope.tool
api/agentscope.mcp
+ api/agentscope.embedding
+ api/agentscope.evaluate
+ api/agentscope.plan
+ api/agentscope.rag
api/agentscope.token
api/agentscope.tracing
api/agentscope.session
diff --git a/docs/tutorial/zh_CN/src/task_pipeline.py b/docs/tutorial/zh_CN/src/task_pipeline.py
index 97667f828..10c752a7d 100644
--- a/docs/tutorial/zh_CN/src/task_pipeline.py
+++ b/docs/tutorial/zh_CN/src/task_pipeline.py
@@ -11,6 +11,7 @@
- **MsgHub**: 用于多个智能体之间消息的广播
- **sequential_pipeline** 和 **SequentialPipeline**: 以顺序方式执行多个智能体的函数式和类式实现
- **fanout_pipeline** 和 **FanoutPipeline**: 将相同输入分发给多个智能体的函数式和类式实现
+- **stream_printing_messages**: 将智能体在回复过程中,调用 ``self.print`` 打印的消息转换为一个异步生成器
"""
@@ -20,7 +21,8 @@ from agentscope.formatter import DashScopeMultiAgentFormatter
from agentscope.message import Msg
from agentscope.model import DashScopeChatModel
from agentscope.agent import ReActAgent
-from agentscope.pipeline import MsgHub
+from agentscope.pipeline import MsgHub, stream_printing_messages
+
# %%
# 使用 MsgHub 进行广播
@@ -134,10 +136,11 @@ asyncio.run(check_broadcast_message())
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 管道是 AgentScope 中多智能体编排的一种语法糖。
#
-# 目前,AgentScope 提供了两种主要的管道实现:
+# 目前,AgentScope 提供三种管道,用于减轻开发者的负担:
#
# 1. **顺序管道 (Sequential Pipeline)**: 按预定义顺序逐个执行智能体
# 2. **扇出管道 (Fanout Pipeline)**: 将相同输入分发给多个智能体并收集它们的响应
+# 3. **流式获取打印消息 (stream printing messages)**: 将智能体在回复过程中,调用 ``self.print`` 打印的消息转换为一个异步生成器
#
# 顺序管道
# ------------------------
@@ -210,6 +213,39 @@ asyncio.run(check_broadcast_message())
# .. tip::
# 通过结合 ``MsgHub`` 和 ``sequential_pipeline`` 或 ``fanout_pipeline``,你可以非常容易地创建更复杂的工作流。
+# %%
+# 流式获取打印消息
+# ------------------------
+# ``stream_printing_messages`` 函数将智能体在回复过程中调用 ``self.print`` 打印的消息转换为一个异步生成器。
+# 可以帮助开发者快速以流式方式获取智能体的中间消息。
+#
+# 该函数接受一个或多个智能体和一个协程任务作为输入,并返回一个异步生成器。
+# 该异步生成器返回一个二元组,包含执行协程任务过程中通过 ``await self.print(...)`` 打印的消息,以及一个布尔值,表示该消息是否为一组流式消息中的最后一个。
+#
+# 需要注意的是,生成器返回的元组中,布尔值表示该消息是否为一组流式消息中的最后一个,而非此次智能体调用的最后一条消息。
+
+
+async def run_example_pipeline() -> None:
+ """运行流式打印消息的示例。"""
+ agent = create_agent("Alice", 20, "student")
+
+ # 我们关闭agent的终端打印以避免输出混乱
+ agent.set_console_output_enabled(False)
+
+ async for msg, last in stream_printing_messages(
+ agents=[agent],
+ coroutine_task=agent(
+ Msg("user", "你好,你是谁?", "user"),
+ ),
+ ):
+ print(msg, last)
+ if last:
+ print()
+
+
+asyncio.run(run_example_pipeline())
+
+
# %%
# 高级管道特性
# ~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/docs/tutorial/zh_CN/src/task_prompt.py b/docs/tutorial/zh_CN/src/task_prompt.py
index fca0403a7..1e4196cef 100644
--- a/docs/tutorial/zh_CN/src/task_prompt.py
+++ b/docs/tutorial/zh_CN/src/task_prompt.py
@@ -260,7 +260,7 @@ async def run_token_counter() -> int:
# 我们使用 huggingface token 计数器用于 dashscope 模型。
token_counter = HuggingFaceTokenCounter(
"Qwen/Qwen2.5-VL-3B-Instruct",
- use_mirror=True,
+ use_mirror=False,
)
return await token_counter.count(formatted_message)
@@ -279,7 +279,7 @@ async def run_truncated_formatter() -> None:
"""带截断的消息格式化示例。"""
token_counter = HuggingFaceTokenCounter(
pretrained_model_name_or_path="Qwen/Qwen2.5-VL-3B-Instruct",
- use_mirror=True,
+ use_mirror=False,
)
formatter = DashScopeMultiAgentFormatter(
token_counter=token_counter,
diff --git a/examples/functionality/stream_printing_messages/README.md b/examples/functionality/stream_printing_messages/README.md
new file mode 100644
index 000000000..be04293cb
--- /dev/null
+++ b/examples/functionality/stream_printing_messages/README.md
@@ -0,0 +1,28 @@
+# Stream Printing Messages
+
+The AgentScope agent is designed to communicate with the user and the other agents by passing messages explicitly.
+However, we notice the requirements that obtain the printing messages from the agent in a streaming manner.
+Therefore, in example we demonstrate how to gather and yield the printing messages from a single agent and
+multi-agent systems in a streaming manner.
+
+
+## Quick Start
+
+Run the following command to see the streaming printing messages from the agent.
+Note the messages with the same ID are the chunks of the same message in accumulated manner.
+
+- For single-agent:
+
+```bash
+python single_agent.py
+```
+
+- For multi-agent:
+
+```bash
+python multi_agent.py
+```
+
+> Note: The example is built with DashScope chat model. If you want to change the model in this example, don't forget
+> to change the formatter at the same time! The corresponding relationship between built-in models and formatters are
+> list in [our tutorial](https://doc.agentscope.io/tutorial/task_prompt.html#id1)
\ No newline at end of file
diff --git a/examples/functionality/stream_printing_messages/multi_agent.py b/examples/functionality/stream_printing_messages/multi_agent.py
new file mode 100644
index 000000000..4925af6e9
--- /dev/null
+++ b/examples/functionality/stream_printing_messages/multi_agent.py
@@ -0,0 +1,62 @@
+# -*- coding: utf-8 -*-
+"""Example for gather the printing messages from multiple agents."""
+import asyncio
+import os
+
+from agentscope.agent import ReActAgent
+from agentscope.formatter import DashScopeMultiAgentFormatter
+from agentscope.message import Msg
+from agentscope.model import DashScopeChatModel
+from agentscope.pipeline import MsgHub, stream_printing_messages
+
+
+def create_agent(name: str) -> ReActAgent:
+ """Create an agent with the given name."""
+ return ReActAgent(
+ name=name,
+ sys_prompt=f"You are a student named {name}.",
+ model=DashScopeChatModel(
+ api_key=os.environ["DASHSCOPE_API_KEY"],
+ model_name="qwen-max",
+ stream=False, # close streaming for simplicity
+ ),
+ formatter=DashScopeMultiAgentFormatter(),
+ )
+
+
+async def workflow(
+ alice: ReActAgent,
+ bob: ReActAgent,
+ charlie: ReActAgent,
+) -> None:
+ """The example workflow for multiple agents."""
+ async with MsgHub(
+ participants=[alice, bob, charlie],
+ announcement=Msg(
+ "user",
+ "Alice, Bob and Charlie, welcome to the meeting! Let's "
+ "meet each other first.",
+ "user",
+ ),
+ ):
+ # agent speaks in turn
+ await alice()
+ await bob()
+ await charlie()
+
+
+async def main() -> None:
+ """The main entry for the example."""
+ # Create agents
+ alice, bob, charlie = [
+ create_agent(_) for _ in ["Alice", "Bob", "Charlie"]
+ ]
+
+ async for msg, last in stream_printing_messages(
+ agents=[alice, bob, charlie],
+ coroutine_task=workflow(alice, bob, charlie),
+ ):
+ print(msg, last)
+
+
+asyncio.run(main())
diff --git a/examples/functionality/stream_printing_messages/single_agent.py b/examples/functionality/stream_printing_messages/single_agent.py
new file mode 100644
index 000000000..7fa6afe1a
--- /dev/null
+++ b/examples/functionality/stream_printing_messages/single_agent.py
@@ -0,0 +1,62 @@
+# -*- coding: utf-8 -*-
+"""The example demonstrating how to obtain the messages from the agent in a
+streaming way."""
+import asyncio
+import os
+
+from agentscope.agent import ReActAgent
+from agentscope.formatter import DashScopeChatFormatter
+from agentscope.memory import InMemoryMemory
+from agentscope.message import Msg
+from agentscope.model import DashScopeChatModel
+from agentscope.pipeline import stream_printing_messages
+from agentscope.tool import (
+ Toolkit,
+ execute_shell_command,
+ view_text_file,
+ execute_python_code,
+)
+
+
+async def main() -> None:
+ """The main function."""
+ toolkit = Toolkit()
+ toolkit.register_tool_function(execute_shell_command)
+ toolkit.register_tool_function(execute_python_code)
+ toolkit.register_tool_function(view_text_file)
+
+ agent = ReActAgent(
+ name="Friday",
+ sys_prompt="You are a helpful assistant named Friday.",
+ # Change the model and formatter together if you want to try other
+ # models
+ model=DashScopeChatModel(
+ api_key=os.environ.get("DASHSCOPE_API_KEY"),
+ model_name="qwen-max",
+ enable_thinking=False,
+ stream=True,
+ ),
+ formatter=DashScopeChatFormatter(),
+ toolkit=toolkit,
+ memory=InMemoryMemory(),
+ )
+
+ # Prepare a user message
+ user_msg = Msg(
+ "user",
+ "Hi! Who are you?",
+ "user",
+ )
+
+ # We disable the terminal printing to avoid messy outputs
+ agent.set_console_output_enabled(False)
+
+ # obtain the printing messages from the agent in a streaming way
+ async for msg, last in stream_printing_messages(
+ agents=[agent],
+ coroutine_task=agent(user_msg),
+ ):
+ print(msg, last)
+
+
+asyncio.run(main())
diff --git a/src/agentscope/_version.py b/src/agentscope/_version.py
index a92a89fb3..757782df4 100644
--- a/src/agentscope/_version.py
+++ b/src/agentscope/_version.py
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
"""The version of agentscope."""
-__version__ = "1.0.4"
+__version__ = "1.0.5dev"
diff --git a/src/agentscope/agent/_agent_base.py b/src/agentscope/agent/_agent_base.py
index dac803bd2..c90c07516 100644
--- a/src/agentscope/agent/_agent_base.py
+++ b/src/agentscope/agent/_agent_base.py
@@ -2,12 +2,13 @@
"""The agent base class in agentscope."""
import asyncio
import json
-from asyncio import Task
+from asyncio import Task, Queue
from collections import OrderedDict
from typing import Callable, Any
import base64
import shortuuid
import numpy as np
+from typing_extensions import deprecated
from ._agent_meta import _AgentMeta
from .._logging import logger
@@ -167,6 +168,11 @@ class AgentBase(StateModule, metaclass=_AgentMeta):
# output of the agent, e.g., in a production environment.
self._disable_console_output: bool = False
+ # The streaming message queue used to export the messages as a
+ # generator
+ self._disable_msg_queue: bool = True
+ self.msg_queue = None
+
async def observe(self, msg: Msg | list[Msg] | None) -> None:
"""Receive the given message(s) without generating a reply.
@@ -197,6 +203,9 @@ class AgentBase(StateModule, metaclass=_AgentMeta):
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((msg, last))
+
if self._disable_console_output:
return
@@ -622,7 +631,46 @@ class AgentBase(StateModule, metaclass=_AgentMeta):
else:
self._subscribers.pop(msghub_name)
+ @deprecated("Please use set_console_output_enabled() instead.")
def disable_console_output(self) -> None:
"""This function will disable the console output of the agent, e.g.
in a production environment to avoid messy logs."""
self._disable_console_output = True
+
+ def set_console_output_enabled(self, enabled: bool) -> None:
+ """Enable or disable the console output of the agent. E.g. in a
+ production environment, you may want to disable the console output to
+ avoid messy logs.
+
+ Args:
+ enabled (`bool`):
+ If `True`, enable the console output. If `False`, disable
+ the console output.
+ """
+ self._disable_console_output = not enabled
+
+ def set_msg_queue_enabled(
+ self,
+ enabled: bool,
+ queue: Queue | None = None,
+ ) -> None:
+ """Enable or disable the message queue for streaming outputs.
+
+ Args:
+ enabled (`bool`):
+ If `True`, enable the message queue to allow streaming
+ outputs. If `False`, disable the message queue.
+ queue (`Queue | None`, optional):
+ The queue instance that will be used to initialize the
+ message queue when `enable` is `True`.
+ """
+ if enabled:
+ if queue is None:
+ if self.msg_queue is None:
+ self.msg_queue = asyncio.Queue(maxsize=100)
+ else:
+ self.msg_queue = queue
+ else:
+ self.msg_queue = None
+
+ self._disable_msg_queue = not enabled
diff --git a/src/agentscope/pipeline/__init__.py b/src/agentscope/pipeline/__init__.py
index 9e0f5b801..9ab9b581e 100644
--- a/src/agentscope/pipeline/__init__.py
+++ b/src/agentscope/pipeline/__init__.py
@@ -4,7 +4,11 @@ complex workflows and multi-agent conversations."""
from ._msghub import MsgHub
from ._class import SequentialPipeline, FanoutPipeline
-from ._functional import sequential_pipeline, fanout_pipeline
+from ._functional import (
+ sequential_pipeline,
+ fanout_pipeline,
+ stream_printing_messages,
+)
__all__ = [
"MsgHub",
@@ -12,4 +16,5 @@ __all__ = [
"sequential_pipeline",
"FanoutPipeline",
"fanout_pipeline",
+ "stream_printing_messages",
]
diff --git a/src/agentscope/pipeline/_functional.py b/src/agentscope/pipeline/_functional.py
index a6fd374cb..e9e96f720 100644
--- a/src/agentscope/pipeline/_functional.py
+++ b/src/agentscope/pipeline/_functional.py
@@ -2,7 +2,7 @@
"""Functional counterpart for Pipeline"""
import asyncio
from copy import deepcopy
-from typing import Any
+from typing import Any, AsyncGenerator, Tuple, Coroutine
from ..agent import AgentBase
from ..message import Msg
@@ -102,3 +102,68 @@ async def fanout_pipeline(
return await asyncio.gather(*tasks)
else:
return [await agent(deepcopy(msg), **kwargs) for agent in agents]
+
+
+async def stream_printing_messages(
+ agents: list[AgentBase],
+ coroutine_task: Coroutine,
+ end_signal: str = "[END]",
+) -> AsyncGenerator[Tuple[Msg, bool], None]:
+ """This pipeline will gather the printing messages from agents when
+ execute the given coroutine task, and yield them one by one.
+ Only the messages that are printed by `await self.print(msg)` in the agent
+ will be forwarded to the message queue and yielded by this pipeline.
+
+ .. note:: The boolean in the yielded tuple indicates whether the message
+ is the last **chunk** for a streaming message, not the last message
+ returned by the agent. That means, there'll be multiple tuples with
+ `is_last_chunk=True` if the agent prints multiple messages.
+
+ .. note:: The messages with the same ``id`` is considered as the same
+ message, e.g., the chunks of a streaming message.
+
+ Args:
+ agents (`list[AgentBase]`):
+ A list of agents whose printing messages will be gathered and
+ yielded.
+ coroutine_task (`Coroutine`):
+ The coroutine task to be executed. This task should involve the
+ execution of the provided agents, so that their printing messages
+ can be captured and yielded.
+ end_signal (`str`, defaults to `"[END]"`):
+ A special signal to indicate the end of message streaming. When
+ this signal is received from the message queue, the generator will
+ stop yielding messages and exit the loop.
+
+ Returns:
+ `AsyncGenerator[Tuple[Msg, bool], None]`:
+ An async generator that yields tuples of (message, is_last_chunk).
+ The `is_last_chunk` boolean indicates whether the message is the
+ last chunk in a streaming message.
+ """
+
+ # 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
diff --git a/tests/pipeline_test.py b/tests/pipeline_test.py
index ce2aca09a..af6429b1b 100644
--- a/tests/pipeline_test.py
+++ b/tests/pipeline_test.py
@@ -9,6 +9,7 @@ from agentscope.pipeline import (
FanoutPipeline,
sequential_pipeline,
fanout_pipeline,
+ stream_printing_messages,
)
from agentscope.agent import AgentBase
@@ -41,6 +42,50 @@ class AddAgent(AgentBase):
"""Handle interrupt"""
+class StreamAgent(AgentBase):
+ """Add agent class."""
+
+ def __init__(self) -> None:
+ """Initialize the agent"""
+ super().__init__()
+ self.name = "Stream"
+
+ async def reply(self) -> Msg | None:
+ """Reply function"""
+ await self.print(
+ Msg(
+ self.name,
+ "123",
+ "user",
+ ),
+ )
+ await self.print(
+ Msg(
+ "user",
+ "456",
+ "user",
+ ),
+ )
+ await self.print(
+ Msg(
+ self.name,
+ "789",
+ "user",
+ ),
+ )
+ return None
+
+ async def observe(self, msg: Msg | list[Msg] | None) -> None:
+ """Observe function"""
+
+ async def handle_interrupt(
+ self,
+ *args: Any,
+ **kwargs: Any,
+ ) -> Msg:
+ """Handle interrupt"""
+
+
class MultAgent(AgentBase):
"""Mult agent class."""
@@ -299,3 +344,35 @@ class PipelineTest(IsolatedAsyncioTestCase):
self.assertEqual(len(res), 2)
self.assertIsNone(res[0])
self.assertIsNone(res[1])
+
+ async def test_stream_printing_messages(self) -> None:
+ """Test stream_printing_messages function"""
+
+ agent = StreamAgent()
+
+ i = 0
+ async for msg, last in stream_printing_messages(
+ [agent],
+ agent(),
+ ):
+ self.assertTrue(last)
+
+ if i == 0:
+ self.assertEqual(
+ msg.content,
+ "123",
+ )
+
+ elif i == 1:
+ self.assertEqual(
+ msg.content,
+ "456",
+ )
+
+ elif i == 2:
+ self.assertEqual(
+ msg.content,
+ "789",
+ )
+
+ i += 1