Skip to content

角色设定卡:提升模型的情商与专业度

最基础的 Prompt 是: "Act as a Python expert." 这种指令就像对演员说:“演一个专家。” 优秀的 Prompt Engineering 就像导演说戏,需要提供详尽的剧本、性格、背景、语气甚至禁忌。 这种深度角色设定被称为 Persona Card (角色卡)。 在 Character.ai 等应用中,一个高质量的角色卡可能包含数千字的设定。

1. 为什么角色扮演能提升效果?

1.1 激活潜能 (Latent Space Activation)

大模型记住了整个互联网的知识。 当你说 "Act as a senior engineer",你不仅激活了代码知识,还激活了工程思维(如考虑边界条件、写文档、做测试)。 相比 "Write a function","As a senior engineer, write a function" 的代码质量往往更高。

1.2 增强一致性 (Consistency)

如果不设定角色,模型的回答可能一会儿像教授,一会儿像小孩。 设定了 Persona,回答风格(Tone, Style)就会保持一致。

2. 角色卡架构:Persona Template

2.1 核心要素

一个完整的 Persona 包含以下几部分:

  • Name & Title: 身份标识(如 "Dr. Code")。
  • Traits: 性格关键词(如 "严谨", "幽默", "有点强迫症")。
  • Knowledge Base: 擅长的领域(如 "Python, Go, System Design")。
  • Communication Style: 说话方式(如 "喜欢用比喻", "不讲废话", "总是先给结论")。
  • Constraints: 不能做的事(如 "绝不直接给代码,先解释原理")。

2.2 示例:资深代码审查员

markdown
# Role: Senior Code Reviewer (Alex)

## Profile
- **Experience**: 15+ years in distributed systems.
- **Personality**: Strict but constructive. Loves clean code. Hates magic numbers.
- **Language**: Concise, professional English.

## Rules
1. Always point out potential bugs first.
2. Suggest improvements for readability and performance.
3. If the code is good, say "LGTM" but ask a probing question.
4. Do NOT rewrite the entire code block unless asked.

## Examples
User: "def add(a,b): return a+b"
Alex: "This function lacks type hints. What if `a` is a string? Consider adding `a: int, b: int -> int`."

3. 动态角色切换 (Dynamic Persona Switching)

3.1 场景化适配

在复杂的 Agent 流程中,同一个 Agent 可能需要扮演不同角色。

  • Step 1: 扮演 "产品经理",理解需求。
  • Step 2: 扮演 "架构师",设计方案。
  • Step 3: 扮演 "程序员",写代码。
  • Step 4: 扮演 "测试员",找 Bug。

3.2 实现方法

在 Prompt 中动态注入不同的 System Message。

python
personas = {
    "PM": "You are a Product Manager...",
    "Dev": "You are a Senior Developer...",
}

def chat(role, user_input):
    system_prompt = personas[role]
    return llm.generate(system_prompt, user_input)

4. 情感计算与共情 (Empathy)

4.1 识别情绪

让模型先识别用户情绪,再决定回复策略。 User: "我的代码又崩了!烦死了!" Model (Internal Thought): User is frustrated. Need to be supportive. Response: "别急,Bug 总是难免的。我们一起来看看报错信息,一定能解决。"

4.2 调整语气 (Tone Adjustment)

通过 Prompt 控制语气强度。

  • Tone: Professional -> "The error is caused by..."
  • Tone: Friendly -> "Oops! Looks like a small mistake here..."
  • Tone: Sarcastic -> "Oh great, another NullPointerException. Classic."

角色扮演不仅是娱乐,更是生产力工具。 一个好的 Persona 能让模型更懂你,更专业,甚至更有趣。 在设计 AI 应用时,不仅要设计功能,还要设计性格。 因为用户最终是在和“人”对话,而不是和机器对话。