Skip to content

Commit 2bc3951

Browse files
authored
feat(werewolf): add commented TTS codes for werewolf game example (#1090)
1 parent 542216a commit 2bc3951

File tree

4 files changed

+84
-5
lines changed

4 files changed

+84
-5
lines changed

examples/game/werewolves/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,51 @@ You can replace one of the agents with a `UserAgent` to play with AI agents.
6565

6666
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.
6767

68+
## Enable Text-to-Speech (TTS)
69+
70+
The game supports Text-to-Speech functionality. To enable TTS:
71+
72+
1. **In `main.py`**:
73+
- Uncomment the import statement:
74+
```python
75+
import random
76+
from agentscope.tts import DashScopeTTSModel
77+
```
78+
- Uncomment the `tts_model` parameter in the `get_official_agents` function:
79+
```python
80+
tts_model=DashScopeTTSModel(
81+
api_key=os.environ.get("DASHSCOPE_API_KEY"),
82+
model_name="qwen3-tts-flash",
83+
voice=random.choice(["Cherry", "Serena", "Ethan", "Chelsie"]),
84+
stream=True,
85+
),
86+
```
87+
88+
2. **In `game.py`** (optional, for moderator TTS):
89+
- Uncomment the import statement:
90+
```python
91+
import random
92+
from agentscope.tts import DashScopeTTSModel
93+
```
94+
- Uncomment the `tts_model` parameter in the `moderator` initialization:
95+
```python
96+
tts_model=DashScopeTTSModel(
97+
api_key=os.environ.get("DASHSCOPE_API_KEY"),
98+
model_name="qwen3-tts-flash",
99+
voice=random.choice(["Cherry", "Serena", "Ethan", "Chelsie"]),
100+
stream=True,
101+
),
102+
```
103+
104+
3. **Set up your API key**:
105+
- Make sure you have set the `DASHSCOPE_API_KEY` environment variable.
106+
107+
After enabling TTS, the game will synthesize speech for player messages and moderator announcements, providing a more immersive audio experience.
108+
68109
## Further Reading
69110

70111
- [Structured Output](https://doc.agentscope.io/tutorial/task_agent.html#structured-output)
71112
- [MsgHub and Pipelines](https://doc.agentscope.io/tutorial/task_pipeline.html)
72113
- [Prompt Formatter](https://doc.agentscope.io/tutorial/task_prompt.html)
73114
- [AgentScope Studio](https://doc.agentscope.io/tutorial/task_studio.html)
115+
- [TTS](https://doc.agentscope.io/tutorial/task_tts.html)

examples/game/werewolves/game.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,20 @@
3232
fanout_pipeline,
3333
)
3434

35-
36-
moderator = EchoAgent()
35+
# from agentscope.tts import DashScopeTTSModel
36+
# import os
37+
# import random
38+
39+
moderator = EchoAgent(
40+
# If you want to use TTS, uncomment the following lines and the
41+
# TTS-related import statement at the beginning of this file.
42+
# tts_model=DashScopeTTSModel(
43+
# api_key=os.environ.get("DASHSCOPE_API_KEY"),
44+
# model_name="qwen3-tts-flash",
45+
# voice=random.choice(["Cherry", "Serena", "Ethan", "Chelsie"]),
46+
# stream=True,
47+
# ),
48+
)
3749

3850

3951
async def hunter_stage(

examples/game/werewolves/main.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
from agentscope.model import DashScopeChatModel
1212
from agentscope.session import JSONSession
1313

14+
# import random
15+
# from agentscope.tts import DashScopeTTSModel
16+
1417

1518
def get_official_agents(name: str) -> ReActAgent:
1619
"""Get the official werewolves game agents."""
@@ -73,6 +76,14 @@ def get_official_agents(name: str) -> ReActAgent:
7376
model_name="qwen3-max",
7477
),
7578
formatter=DashScopeMultiAgentFormatter(),
79+
# If you want to use TTS, uncomment the following lines and the
80+
# TTS-related import statement at the beginning of this file.
81+
# tts_model=DashScopeTTSModel(
82+
# api_key=os.environ.get("DASHSCOPE_API_KEY"),
83+
# model_name="qwen3-tts-flash",
84+
# voice=random.choice(["Cherry", "Serena", "Ethan", "Chelsie"]),
85+
# stream=True,
86+
# ),
7687
)
7788
return agent
7889

examples/game/werewolves/utils.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
from prompt import EnglishPrompts as Prompts
99

10-
from agentscope.message import Msg
10+
from agentscope.message import Msg, AudioBlock
1111
from agentscope.agent import ReActAgent, AgentBase
1212

13+
from agentscope.tts import TTSModelBase
14+
1315
MAX_GAME_ROUND = 30
1416
MAX_DISCUSSION_ROUND = 3
1517

@@ -46,18 +48,30 @@ def names_to_str(agents: list[str] | list[ReActAgent]) -> str:
4648
class EchoAgent(AgentBase):
4749
"""Echo agent that repeats the input message."""
4850

49-
def __init__(self) -> None:
51+
def __init__(self, tts_model: TTSModelBase | None = None) -> None:
52+
"""Initialize the echo agent."""
5053
super().__init__()
5154
self.name = "Moderator"
55+
self.tts_model = tts_model
5256

5357
async def reply(self, content: str) -> Msg:
5458
"""Repeat the input content with its name and role."""
59+
5560
msg = Msg(
5661
self.name,
5762
content,
5863
role="assistant",
5964
)
60-
await self.print(msg)
65+
speech: AudioBlock | list[AudioBlock] | None = None
66+
if self.tts_model:
67+
tts_res = await self.tts_model.synthesize(msg)
68+
if self.tts_model.stream:
69+
async for tts_chunk in tts_res:
70+
speech = tts_chunk.content
71+
await self.print(msg, False, speech=speech)
72+
else:
73+
speech = tts_res.content
74+
await self.print(msg, True, speech=speech)
6175
return msg
6276

6377
async def handle_interrupt(

0 commit comments

Comments
 (0)