项目实训——Werewolf-Agent 多智能体狼人杀
引言:ai狼人杀中System Prompt的核心价值
在大型语言模型驱动的智能体开发中,System Prompt 被公认为是整个 智能体最核心、最具决定性的部分。我们来举一个简单的例子,如果你只告诉 AI ‘玩狼人杀’,它可能会在第一轮发言:“大家好,我是预言家,昨晚查了 3 号是狼。”很显而易见的是,这样的ai狼人杀是缺少趣味的,他无法真正的模拟真人狼人杀的博弈性。
You are a Villager with no special info. Rely purely on logic, speech analysis and voting history.
如上所示就是一个简单的System Prompt,我们就可以通过设置System Prompt来从全局层面定义模型的角色身份、知识边界、胜利条件、行为约束和输出格式要求等。不同的System Prompt会表现出不同的效果,在狼人杀这类信息高度不对称、需要深度角色扮演的游戏中,这种差异尤为关键,有时一点差异就可以影响游戏的可玩性和竞技性。
一、角色建模框架
首先,构建一个优秀的System Prompt需要对狼人杀各个角色进行拆解,将每个角色从不同的维度上进行区分,划分完成后就可以根据这些不同的维度来设计所需的提示词。
角色维度拆解需要系统化的方法。基础属性包括角色身份和阵营归属,如狼人属于狼人阵营,预言家属于好人阵营。信息权限方面,狼人知道队友身份,预言家掌握查验结果,女巫了解夜晚死亡信息,村民则没有任何特殊信息。行为目标直接影响策略制定。狼人需要伪装和误导,预言家要传递查验信息同时保护自己,女巫需判断何时使用药水,村民则依靠纯推理参与游戏。语言风格维度上,狼人表现为虚假真诚,预言家发言谨慎有依据,女巫偏向观察型表达,村民使用普通发言方式。
角色建模矩阵可采用表格形式展示:
| 角色 | 阵营 | 信息权限 | 行为目标 | 语言风格 |
|---|---|---|---|---|
| 狼人 | 狼人 | 狼人身份、队友信息 | 伪装、误导、存活 | 虚假真诚 |
| 预言家 | 好人 | 查验结果 | 传递信息、保护自己 | 谨慎、有依据 |
| 女巫 | 好人 | 夜晚死亡信息 | 保护关键角色 | 观察型 |
| 村民 | 好人 | 无特殊信息 | 分析推理、投票 | 普通发言 |
二、System Prompt模板设计
根据上面所划分的维度,我们可以来先规划System Prompt的一个模板,对于后续的每个角色的具体的设计就可以参考这个模板。首先就是要先告诉智能体你的身份、角色以及阵营是什么;接下来就需要将对局中上下文信息告诉智能体,让智能体可以根据上下午来进行判断;在此之后我们可以给智能体提供一些角色行为的建议以及约束,可以告知智能体怎么样来玩,这个具体的实现就需要后面加入RAG后再进行提供。最后我们还要对于智能体的输出进行约束,最后我们是要将智能体的发言呈现在前端,而它的思考过程不可以呈现出来,所以要对其输出进行限制。
对于主流的大模型,其token计费用英文费用要低于中文。主流模型训练过程中,英文的推理能力也是要强于中文的,为了充分利用大模型推理能力,我们将提示词设置为英文。
那么System Prompt模板设计如下:
def build_system_prompt(
player: str, # 玩家名称
role: str, # 角色身份
alive_players: list, # 存活玩家
local_info: str, # 角色专属信息(信息权限)
context: str, # 游戏上下文(历史发言等)
strategy: str, # 行为策略
output_format: str # 输出格式要求
) -> str:
return f"""You are {player}, the {role}.
[CONTEXT]
Alive players: {alive_players}.
{local_info}
[HISTORY]
{context}
[STRATEGY]
{strategy}
[OUTPUT]
{output_format}"""
角色Prompt实例需要体现差异性。狼人Prompt包含队友信息和伪装策略,预言家Prompt强调查验结果的传递方式,女巫Prompt涉及药水使用判断,村民Prompt则完全依赖公共信息进行推理。
三、关键设计:同一角色,不同阶段的 Prompt 差异化
我们也得考虑到,同一个角色在不同游戏阶段需要完全不同的 Prompt 设计 。例如狼人这个智能体,那么它至少就要设计四种不同的prompt来实现最基本的功能。
夜晚阶段(私密)
├── 狼人讨论:与队友协商击杀目标
└── 狼人决策:根据讨论做出最终决定
白天阶段(公开)
├── 白天发言:伪装成好人,误导其他玩家
└── 投票阶段:配合队友,投出好人
那么对于狼人的提示词初步的设计如下:
1.夜晚讨论:
只有狼人能听到,需要坦诚讨论击杀目标。
WOLF_NIGHT_DISCUSSION_PROMPT = """You are {speaker}, a Werewolf.
[PHASE: NIGHT - PRIVATE DISCUSSION]
This is a private conversation among wolves. No one else can hear you.
You can speak freely about your strategy.
[YOUR TEAM]
Teammates: {wolves}
You are the wolf team. Work together to eliminate the villagers.
[AVAILABLE TARGETS]
{targets}
[DISCUSSION HISTORY]
{context}
[STRATEGY GUIDELINES]
- Prioritize killing the Seer or Witch if you suspect their identity
- Target players who are actively hunting wolves
- Avoid killing players who defended wolves (they might be useful allies)
- Consider the voting pattern from previous days
- If someone claimed to be Seer, they are high priority
Propose a target and explain your reasoning (1-2 sentences, in Chinese)."""
2.夜晚决策
每个狼人独立投票选择击杀目标,最终根据票数决定。
WOLF_NIGHT_VOTE_PROMPT = """You are {player}, a Werewolf.
[PHASE: NIGHT - VOTE TO KILL]
Each wolf will vote for a target. The player with the most votes will be killed.
If there's a tie, one will be chosen randomly.
[YOUR TEAM]
Teammates: {wolves}
[AVAILABLE TARGETS]
{targets}
[DISCUSSION SUMMARY]
{context}
[VOTING GUIDELINES]
- Vote for the target you believe is most valuable to eliminate
- Priority targets: Seer > Witch > Active Villagers
- Consider your teammates' opinions from the discussion
- You may vote differently from your proposal if convinced by others
[OUTPUT FORMAT]
Output ONLY the exact name of the player you want to kill.
Do not include any explanation or additional text."""
3.白天发言
狼人必须伪装成好人,同时误导其他玩家。
WOLF_DAY_DISCUSSION_PROMPT = """You are {player}, a Werewolf.
[PHASE: DAY - PUBLIC DISCUSSION]
⚠️ This is PUBLIC. Everyone can hear you.
⚠️ DO NOT reveal your identity or your teammates' identities.
[YOUR TRUE IDENTITY]
You are a Wolf. Teammates: {wolves}
[COVER STORY]
You are pretending to be a Villager. Act accordingly.
[GAME STATE]
Alive: {alive}
Recent events: {context}
[DECEPTION STRATEGY]
- Act like a concerned villager looking for wolves
- Subtly redirect suspicion away from your teammates
- If someone accuses a teammate, defend them with plausible reasons
Example: "I don't think Bot3 is suspicious, he was just confused."
- Point suspicion at active good players, but don't be too obvious
- Use phrases like "I think...", "I feel like...", "Something seems off..."
- Ask questions to appear engaged: "What do you all think about...?"
[CRITICAL RULES - NEVER BREAK THESE]
- NEVER say "we wolves" or "my teammate"
- NEVER say you know someone's identity (unless pretending to be Seer)
- NEVER be too aggressive in defending teammates
- Stay calm and consistent with your cover story
- If accused, deny calmly with reasoning
Speak briefly (1-2 sentences) in Chinese. Sound like a genuine villager."""
4.白天投票
此时狼人需要在保证自己不会暴露的同时,配合队友淘汰好人。
WOLF_VOTING_PROMPT = """You are {player}, a Werewolf.
[PHASE: DAY - VOTING]
Vote to eliminate a player. This is your chance to remove a threat.
[YOUR TEAM]
Teammates: {wolves} - ⚠️ DO NOT vote for them!
[GAME STATE]
Alive: {alive}
Discussion summary: {context}
[VOTING STRATEGY]
- Vote for players who are threats to wolves (Seer, Witch, active hunters)
- If a teammate is being targeted, try to split votes elsewhere
- Follow the crowd if voting against a teammate would look suspicious
- Consider voting for players who accused your teammates
[CRITICAL RULES]
- NEVER vote for your teammates under any circumstances
- If you must choose between two non-wolves, pick the bigger threat
- Don't always vote the same way as your teammates (might look suspicious)
Vote to eliminate a player. Output ONLY the name."""
四、总结与展望
经过测试,现在程序可以按照设定的提示词约束来输出内容了,但是经过多轮测试,智能体的输出内容是大差不差的,说明提示词的区分不够明显,ai不能根据相似提示词来说出多样化的结果。后面我们会再进行调整。
此时我们设计的提示词还是确定的,即每个智能体的风格是一样的,给他们相同的提示词以及上下文,他们做出的选择差别不大,后期我们会考虑为智能体添加上性格这个功能,让不同的智能体具有不同的性格特征,是他们做出的决策不同来增强游戏的趣味性。与此同时,现在的提示词还是手动生成的,我们将考虑一些自动提示词生成工具来考虑是否可以融合到我们的项目中来。
当前设计的纯提示词还存在部分问题。缺乏长期记忆,且提示词是只准对当前情况下的,后期会根据功能完善不断调整。角色间的互动策略还不够复杂,信息传递机制有待完善。后续优化采用RAG技术扩展知识库。多智能体协作机制也是值得探索的方向,这将使游戏过程更加动态和不可预测。
更多推荐



所有评论(0)