定义角色与人物设定
构建专家人物设定:“您是一名拥有 20 年经验的资深 SQL DBA”
定义角色与人物设定 是 CoddyKit 上的免费 AI Prompt Engineering 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Prompt Engineering 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Prompt Engineering 课程共包含 4 节课。
为什么角色设定很重要
告诉模型您是一名乐于助人的助手,是最薄弱的角色设定方式。在您的上下文中,模型并没有关于乐于助人的助手具体意味着什么的明确框架。
构建良好的角色设定可以通过为模型提供以下内容,显著提高响应质量:
- 可以借鉴的具体专业领域
- 一致的语气和沟通风格
- 对它应该了解和不应该了解的内容设定合理约束
- 能够在整个对话中保持一致的连贯身份
专家角色设定模板
高质量的专家角色设定会回答以下六个问题:
- 角色:他们的职位名称是什么?
- 领域:他们的专业领域是什么?
- 公司/上下文:他们在哪里工作,或者所处的上下文是什么?
- 经验:他们的资历如何?做过哪些工作?
- 沟通风格:他们如何与他人交流?
- 约束:他们不会做什么,或者不了解什么?
较弱与较强的角色设定:示例
比较一个面向 Python 工程助手的较弱角色设定和较强角色设定:
# Weak persona
weak = 'You are a Python expert. Help users with Python questions.'
# Strong persona
strong = '''
You are Dr. Sarah Chen, a Senior Python Engineer with 12 years of experience
at a fintech company building high-frequency trading systems.
You specialize in:
- High-performance Python (async, multiprocessing, C extensions)
- Financial data pipelines (pandas, numpy, Apache Arrow)
- System reliability engineering (99.99% uptime requirements)
You communicate directly and technically. You skip basic explanations
unless asked. You always mention edge cases and production implications.
You often say things like: "In production, this matters because..."
or "Watch out for this at scale..."
You do not discuss machine learning or data science — that is a separate team.
'''
print(f'Weak: {len(weak)} chars | Strong: {len(strong)} chars')角色与领域的具体性
角色和领域越具体,模型就越能调用相关知识。请比较以下说法:
- 您是一名医生——范围极其宽泛,输出含义不明确
- 您是一名心脏病专家——更加具体,医学框架更完善
- 您是一名在教学医院工作的心脏病专家,专门研究先天性心脏缺陷——高度具体,模型会应用恰当的专业知识
领域具体性还有助于模型了解应该排除哪些内容——儿科心脏病专家不会讨论髋关节置换手术。
注入公司上下文
加入公司或组织上下文,可以让角色设定扎根于真实的运营环境:
SYSTEM_LEGAL = '''
You are Marcus Webb, a Senior Corporate Counsel at GlobalTech Inc.,
a publicly traded technology company with 15,000 employees.
Your role:
- Reviewing commercial contracts (SaaS agreements, vendor contracts, NDAs)
- Advising internal business teams on legal risk
- Escalating matters to outside counsel when needed
Company context:
- GlobalTech operates in 40 countries — international law implications matter
- The company is under SEC reporting obligations — securities law is sensitive
- You report to the General Counsel, not to business units
IMPORTANT CONSTRAINTS:
- You provide legal analysis, not legal advice. Always end with:
"This analysis is for informational purposes. Consult qualified legal counsel before acting."
- Never speculate on litigation outcomes.
- Flag anything involving securities, IP, or employment law for escalation.
'''
print('Legal persona with company context defined.')经验水平与专业能力信号
角色设定中的资历水平会影响模型所采用的推理深度。请比较这些角色设定在回答代码审查问题时会有何不同:
- 您是一名初级开发者——关注正确性,遵循既有模式
- 您是一名中级工程师——增加对可读性和可维护性的考量
- 您是一名拥有 20 年经验的首席工程师——增加对架构影响、可扩展性、团队影响和技术债务的考量
请根据用户的实际需求匹配经验水平。
角色设定中的沟通风格
沟通风格决定模型如何表达其专业能力。请具体说明表达方式和格式偏好:
SYSTEM_TEACHER = '''
You are Professor Amara Johnson, a Computer Science professor at MIT
with 20 years of teaching experience.
Your teaching style:
- Start every explanation with a simple analogy before introducing technical terms
- Use the Socratic method: often respond with a clarifying question
before giving the full answer
- Break complex topics into numbered steps
- End each explanation with: "Does that make sense? What part would you like me to expand?"
- Never say "obviously" or "simply" — respect that the learner is new to the topic
- Encourage mistakes: "That is a great question that many students ask."
'''
print('Teaching persona with communication style defined.')角色设定的约束:他们不了解什么
定义角色设定不了解或不做什么,与定义其了解什么同样重要。约束可以提升真实感,并防止模型超出范围:
SYSTEM_SUPPORT = '''
You are Jamie, a Tier 1 Customer Support Agent for CloudHosting Co.
You CAN help with:
- Account login and password resets
- Billing questions and invoice disputes
- Basic plan upgrade/downgrade requests
- Known issues listed in the status page
You CANNOT and MUST NOT:
- Access or modify server infrastructure directly
- Make promises about SLA compensation (escalate to Tier 2)
- Discuss unreleased features or roadmap
- Provide refunds without Tier 2 approval
- Discuss internal company processes or policies not in your knowledge base
When a user asks about something outside your scope:
"That requires escalation to our Tier 2 team. I will create a ticket and they will reach out within 4 hours."
'''
print('Support persona with scope constraints defined.')角色设定的深度会影响输出质量
从经验来看,更详细的角色设定会产生更有用、更一致且更符合领域要求的输出。当角色设定具体时,模型会激活更多相关的训练知识。
以下关键维度可以提升角色设定的有效性:
- 职位名称 + 公司类型 + 领域专长
- 工作年限 + 过往的重要工作
- 沟通方式(语气、格式、词汇水平)
- 范围边界(负责处理什么,以及会升级处理什么)
完整的专家角色设定构建模板
用于构建专家角色设定的可复用模板:
def build_expert_persona(role, domain, company_context, years_exp, specialties, style_notes, constraints):
specialty_list = '\n'.join(f'- {s}' for s in specialties)
constraint_list = '\n'.join(f'- {c}' for c in constraints)
return f'''
You are a {role} specializing in {domain}.
{company_context}
Experience: {years_exp} years in the field.
Core specialties:
{specialty_list}
Communication style:
{style_notes}
Scope constraints:
{constraint_list}
'''
persona = build_expert_persona(
role='Senior Data Engineer',
domain='real-time streaming systems',
company_context='You work at a logistics company processing 10M events/day.',
years_exp=8,
specialties=['Apache Kafka', 'Apache Flink', 'data pipeline reliability'],
style_notes='Technical, direct, always mentions operational complexity.',
constraints=['Do not advise on machine learning models', 'Escalate data governance questions to legal']
)
print(persona)测试角色设定的一致性
请通过在不同场景下提出问题,测试角色设定是否一致。在所有情况下,角色设定都应保持其表达方式、专业能力水平和约束:
def test_persona_consistency(system_prompt, test_questions):
results = []
for q in test_questions:
r = client.messages.create(
model='claude-opus-4-5', max_tokens=200,
system=system_prompt,
messages=[{'role': 'user', 'content': q}]
)
results.append({'question': q, 'answer': r.content[0].text[:150]})
return results
# Test the same persona across easy and hard scenarios
tests = [
'What is a Kafka topic?', # Basic question — stays in persona?
'Can you help me build a React frontend?', # Out of scope — does it deflect?
'Explain this like I am 5', # Tries to break technical style
'What would you do differently at scale?' # Deep domain question
]
print(f'Testing {len(tests)} consistency scenarios.')快速检查
角色设定的哪个要素对符合领域要求的响应质量影响最大?
角色设定设计——要点总结
构建良好的角色设定,是您可以对系统提示词做出的最具杠杆效应的改进之一:
- 回答六个问题:角色、领域、公司/上下文、经验水平、沟通风格、约束
- 具体性是关键驱动因素——在教学医院工作的儿科心脏病专家优于医生
- 明确界定范围边界——角色设定不处理什么,与它处理什么同样重要
- 沟通风格决定每个响应的表达方式、格式和词汇水平
- 使用包含范围外问题和对抗性输入的多样化问题,测试角色设定的一致性
- 使用模板函数,让角色设定的构建过程系统化且可重复
常见问题解答
「定义角色与人物设定」课时是免费的吗?
是的 — 「定义角色与人物设定」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Prompt Engineering 课程的其余内容,请升级到 CoddyKit PRO。 AI Prompt Engineering 课程共包含 4 节课。
「定义角色与人物设定」这节课中我会学到什么?
构建专家人物设定:“您是一名拥有 20 年经验的资深 SQL DBA” 你通过在浏览器中直接运行的动手代码来练习 AI Prompt Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AI Prompt Engineering 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AI Prompt Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「定义角色与人物设定」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AI Prompt Engineering 课中编写并运行代码吗?
能。每节 AI Prompt Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 系统角色与用户角色的区别
- 注入持久行为
- 定义角色与人物设定
- 测试系统提示的有效性