code/chapter6/AgentScopeDemo/README.md
本目录包含第六章 AgentScope 框架的完整实战案例,展示了如何使用 AgentScope 构建一个融合中国古典文化元素的多智能体在线游戏。
main_cn.py - 主要游戏逻辑和控制器prompt_cn.py - 中文提示词管理game_roles.py - 游戏角色定义和配置structured_output_cn.py - 结构化输出模型定义utils_cn.py - 游戏工具函数和辅助方法README.md - 本说明文档pip install agentscope
pip install dashscope
pip install pydantic
设置阿里云 DashScope API Key:
# Linux/Mac
export DASHSCOPE_API_KEY="your-api-key-here"
# Windows PowerShell
$env:DASHSCOPE_API_KEY="your-api-key-here"
# Windows CMD
set DASHSCOPE_API_KEY=your-api-key-here
获取 API Key:https://dashscope.console.aliyun.com/apiKey
python main_cn.py
游戏控制层 (ThreeKingdomsWerewolfGame)
├── 游戏状态管理
├── 流程控制
└── 胜负判定
智能体交互层 (MsgHub)
├── 消息路由
├── 并发处理
└── 状态同步
角色建模层 (DialogAgent)
├── 角色提示词
├── 结构化输出
└── 行为约束
1. 消息中心 (MsgHub)
async with MsgHub(
participants=self.werewolves,
enable_auto_broadcast=True
) as hub:
# 狼人夜晚讨论
for wolf in self.werewolves:
await wolf(structured_model=DiscussionModelCN)
2. 结构化输出
class VoteModelCN(BaseModel):
vote: str = Field(description="投票目标玩家姓名")
reason: str = Field(description="投票理由")
confidence: int = Field(ge=1, le=10, description="信心程度")
3. 并发管道
vote_msgs = await fanout_pipeline(
self.alive_players,
msg=vote_announcement,
structured_model=get_vote_model_cn(self.alive_players),
enable_gather=False,
)
# 在 main_cn.py 中修改
await game.setup_game(player_count=8) # 支持 6-12 人
# 在 game_roles.py 中添加
ROLES["守护者"] = {
"description": "守护者",
"ability": "每晚可以守护一名玩家",
"team": "好人阵营"
}
# 在 prompt_cn.py 中修改
def get_role_prompt(role: str, character: str) -> str:
# 自定义角色提示词逻辑
pass
A: 检查以下几点:
A: 可能原因:
A: 建议:
欢迎提交 Issue 和 Pull Request:
本案例是 Hello-Agents 教程第六章的核心实战项目,展示了 AgentScope 框架在构建复杂多智能体应用方面的强大能力。