Skip to content

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

feat(werewolf): add commented TTS codes for werewolf game example (#1090)

qbc2bc3951a96af
diff --git a/examples/game/werewolves/README.md b/examples/game/werewolves/README.md
index e810a15bf..b35f68ad2 100644
--- a/examples/game/werewolves/README.md
+++ b/examples/game/werewolves/README.md
@@ -65,9 +65,51 @@ You can replace one of the agents with a `UserAgent` to play with AI agents.
Just modify the `model` parameter in `main.py` to try different models. Note you need to change the formatter at the same time to match the model's output format.
+## Enable Text-to-Speech (TTS)
+
+The game supports Text-to-Speech functionality. To enable TTS:
+
+1. **In `main.py`**:
+ - Uncomment the import statement:
+ ```python
+ import random
+ from agentscope.tts import DashScopeTTSModel
+ ```
+ - Uncomment the `tts_model` parameter in the `get_official_agents` function:
+ ```python
+ tts_model=DashScopeTTSModel(
+ api_key=os.environ.get("DASHSCOPE_API_KEY"),
+ model_name="qwen3-tts-flash",
+ voice=random.choice(["Cherry", "Serena", "Ethan", "Chelsie"]),
+ stream=True,
+ ),
+ ```
+
+2. **In `game.py`** (optional, for moderator TTS):
+ - Uncomment the import statement:
+ ```python
+ import random
+ from agentscope.tts import DashScopeTTSModel
+ ```
+ - Uncomment the `tts_model` parameter in the `moderator` initialization:
+ ```python
+ tts_model=DashScopeTTSModel(
+ api_key=os.environ.get("DASHSCOPE_API_KEY"),
+ model_name="qwen3-tts-flash",
+ voice=random.choice(["Cherry", "Serena", "Ethan", "Chelsie"]),
+ stream=True,
+ ),
+ ```
+
+3. **Set up your API key**:
+ - Make sure you have set the `DASHSCOPE_API_KEY` environment variable.
+
+After enabling TTS, the game will synthesize speech for player messages and moderator announcements, providing a more immersive audio experience.
+
## Further Reading
- [Structured Output](https://doc.agentscope.io/tutorial/task_agent.html#structured-output)
- [MsgHub and Pipelines](https://doc.agentscope.io/tutorial/task_pipeline.html)
- [Prompt Formatter](https://doc.agentscope.io/tutorial/task_prompt.html)
- [AgentScope Studio](https://doc.agentscope.io/tutorial/task_studio.html)
+- [TTS](https://doc.agentscope.io/tutorial/task_tts.html)
diff --git a/examples/game/werewolves/game.py b/examples/game/werewolves/game.py
index 4fcbd82c3..c088ad7fc 100644
--- a/examples/game/werewolves/game.py
+++ b/examples/game/werewolves/game.py
@@ -32,8 +32,20 @@ from agentscope.pipeline import (
fanout_pipeline,
)
-
-moderator = EchoAgent()
+# from agentscope.tts import DashScopeTTSModel
+# import os
+# import random
+
+moderator = EchoAgent(
+ # If you want to use TTS, uncomment the following lines and the
+ # TTS-related import statement at the beginning of this file.
+ # tts_model=DashScopeTTSModel(
+ # api_key=os.environ.get("DASHSCOPE_API_KEY"),
+ # model_name="qwen3-tts-flash",
+ # voice=random.choice(["Cherry", "Serena", "Ethan", "Chelsie"]),
+ # stream=True,
+ # ),
+)
async def hunter_stage(
diff --git a/examples/game/werewolves/main.py b/examples/game/werewolves/main.py
index eb6b28cc0..e9835b555 100644
--- a/examples/game/werewolves/main.py
+++ b/examples/game/werewolves/main.py
@@ -11,6 +11,9 @@ from agentscope.formatter import DashScopeMultiAgentFormatter
from agentscope.model import DashScopeChatModel
from agentscope.session import JSONSession
+# import random
+# from agentscope.tts import DashScopeTTSModel
+
def get_official_agents(name: str) -> ReActAgent:
"""Get the official werewolves game agents."""
@@ -73,6 +76,14 @@ Your target is to win the game with your teammates as much as possible.
model_name="qwen3-max",
),
formatter=DashScopeMultiAgentFormatter(),
+ # If you want to use TTS, uncomment the following lines and the
+ # TTS-related import statement at the beginning of this file.
+ # tts_model=DashScopeTTSModel(
+ # api_key=os.environ.get("DASHSCOPE_API_KEY"),
+ # model_name="qwen3-tts-flash",
+ # voice=random.choice(["Cherry", "Serena", "Ethan", "Chelsie"]),
+ # stream=True,
+ # ),
)
return agent
diff --git a/examples/game/werewolves/utils.py b/examples/game/werewolves/utils.py
index 2a557e0a9..6cd6c4d46 100644
--- a/examples/game/werewolves/utils.py
+++ b/examples/game/werewolves/utils.py
@@ -7,9 +7,11 @@ import numpy as np
from prompt import EnglishPrompts as Prompts
-from agentscope.message import Msg
+from agentscope.message import Msg, AudioBlock
from agentscope.agent import ReActAgent, AgentBase
+from agentscope.tts import TTSModelBase
+
MAX_GAME_ROUND = 30
MAX_DISCUSSION_ROUND = 3
@@ -46,18 +48,30 @@ def names_to_str(agents: list[str] | list[ReActAgent]) -> str:
class EchoAgent(AgentBase):
"""Echo agent that repeats the input message."""
- def __init__(self) -> None:
+ def __init__(self, tts_model: TTSModelBase | None = None) -> None:
+ """Initialize the echo agent."""
super().__init__()
self.name = "Moderator"
+ self.tts_model = tts_model
async def reply(self, content: str) -> Msg:
"""Repeat the input content with its name and role."""
+
msg = Msg(
self.name,
content,
role="assistant",
)
- await self.print(msg)
+ speech: AudioBlock | list[AudioBlock] | None = None
+ if self.tts_model:
+ tts_res = await self.tts_model.synthesize(msg)
+ if self.tts_model.stream:
+ async for tts_chunk in tts_res:
+ speech = tts_chunk.content
+ await self.print(msg, False, speech=speech)
+ else:
+ speech = tts_res.content
+ await self.print(msg, True, speech=speech)
return msg
async def handle_interrupt(