This commit is contained in:
raykkk
2025-10-17 21:40:45 +08:00
commit 7d0451131f
155 changed files with 14873 additions and 0 deletions

View File

@@ -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)

View File

@@ -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())

View File

@@ -0,0 +1 @@
agentscope[full]>=1.0.5

View File

@@ -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_python_code,
execute_shell_command,
view_text_file,
)
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())