模块化提示部分
清晰地分隔上下文、指令、限制条件和输出格式
模块化提示部分 是 CoddyKit 上的免费 AI Prompt Engineering 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Prompt Engineering 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Prompt Engineering 课程共包含 4 节课。
单体式提示词的问题
单体式提示词把所有内容都塞进一个段落:背景、任务、规则、示例和输出格式全部混在一起。随着提示词变得越来越复杂,单体式结构会导致:
- 模型将规则错误地应用于不相应的部分
- 难以调试究竟是哪一部分导致了错误输出
- 提示词字符串难以维护
- 不同模型版本之间的行为不一致
模块化提示词可以解决所有这些问题。
提示词的五个核心部分
结构良好的提示词包含五个不同的部分,每个部分都有明确的用途:
- 上下文 — 模型理解情境所需的背景
- 任务 — 清晰说明模型必须完成的内容
- 约束 — 对响应的规则和限制
- 输出格式 — 预期响应的结构、长度和模式
- 示例 — 正确行为的示范
并不是每个提示词都需要这五个部分。只使用必要的部分即可。
上下文部分
上下文部分提供背景信息,但不会告诉模型该做什么。良好的上下文包括:
- 最终用户是谁
- 这是用于哪个平台或产品
- 相关领域知识
- 模型应该了解的任何先前状态
context_section = '''
<context>
You are assisting a customer support agent at a B2B SaaS company.
The company sells project management software used by engineering teams.
Customers are typically CTOs, engineering managers, or senior developers.
The support agent is handling a live chat conversation with a customer.
</context>
'''
print(context_section)任务部分
任务部分准确说明模型必须生成什么。它应当具体、以行动为导向,并且不包含无关的背景信息。
task_section = '''
<task>
Draft a response to the customer message below.
The response should:
1. Acknowledge the customer's issue
2. Provide a concrete next step
3. Set a realistic expectation for resolution time
</task>
<customer_message>
Our Gantt chart view stopped loading after the last update.
This is blocking our sprint planning session today.
</customer_message>
'''
print(task_section)约束部分
约束定义了防护边界——模型必须 NOT 执行的内容,以及它必须遵守的明确限制。
constraints_section = '''
<constraints>
- Do not promise a fix by a specific date unless you are certain.
- Do not mention competitor products by name.
- Keep the response under 100 words.
- Use a professional but empathetic tone.
- Do not ask the customer for information already provided in their message.
</constraints>
'''
print(constraints_section)输出格式部分
输出格式部分准确告诉模型应如何组织响应——JSON 模式、Markdown 标题、纯文本、编号列表等。
output_format_section = '''
<output_format>
Respond with a JSON object containing:
{
"subject": "string (email subject line)",
"body": "string (email body, plain text, under 150 words)",
"priority": "high | medium | low",
"escalate": true | false
}
Do not include any text outside the JSON object.
</output_format>
'''
print(output_format_section)示例部分
示例向模型展示正确的输入—输出对。请将示例放在约束之后,以便模型先看到规则,再看到示范。
examples_section = '''
<examples>
<example>
<input>Customer: My invoices are not downloading.</input>
<output>{
"subject": "Invoice Download Issue",
"body": "Thank you for reaching out. We see the issue with invoice downloads and our team is investigating. We expect a fix within 2 hours. We will email you once resolved.",
"priority": "high",
"escalate": true
}</output>
</example>
</examples>
'''
print(examples_section)组合模块化提示词
模块化方法让组合和编辑变得简单直接。每个部分都可以独立修改。
def build_support_prompt(customer_message):
context = '<context>B2B SaaS customer support for project management software.</context>'
task = f'<task>Draft a JSON response to this customer message.</task>\n<customer_message>{customer_message}</customer_message>'
constraints = '<constraints>Under 100 words. Professional tone. No competitor names.</constraints>'
output_fmt = '<output_format>Return JSON: {"subject": str, "body": str, "priority": str, "escalate": bool}</output_format>'
return '\n\n'.join([context, task, constraints, output_fmt])
message = 'The Gantt chart stopped loading after your last update.'
print(build_support_prompt(message))对调试的好处
当模块化提示词产生错误输出时,您可以定位问题所在的部分:
- 执行了错误的任务? — 检查任务部分
- 违反了规则? — 检查约束部分
- 输出结构错误? — 检查输出格式部分
- 质量不佳? — 检查示例部分,或添加更多上下文
您可以一次只修改一个部分,然后重新测试,而不必重写整个提示词。
对维护的好处
以结构化对象或函数形式存储的模块化提示词,比长字符串字面量更容易维护:
- 产品发生变化时,可以更新上下文,而不必修改任务逻辑
- 可以将输出格式从 JSON 切换为 Markdown,而不改变约束
- 可以对不同的示例集进行 A/B 测试,同时保持其他内容不变
- 可以独立对各个部分进行版本控制
class Prompt:
def __init__(self):
self.context = ''
self.task = ''
self.constraints = []
self.output_format = ''
self.examples = []
def build(self):
parts = []
if self.context:
parts.append(f'<context>\n{self.context}\n</context>')
if self.task:
parts.append(f'<task>\n{self.task}\n</task>')
if self.constraints:
c = '\n'.join(f'- {r}' for r in self.constraints)
parts.append(f'<constraints>\n{c}\n</constraints>')
if self.output_format:
parts.append(f'<output_format>\n{self.output_format}\n</output_format>')
return '\n\n'.join(parts)何时可以跳过部分内容
并不是每个提示词都需要这五个部分。请参考以下指南:
- 跳过上下文:任务本身已经足够清楚时(例如,翻译这句话)
- 跳过约束:没有边界情况的简单提取任务
- 跳过示例:任务很直接,或模型在零样本情况下已经能很好地完成任务时
- 始终包含任务和输出格式:这是最低限度可用的部分
对简单提示词规定过多,只会增加无益的干扰。
快速检查
模块化提示词产生了错误的输出结构。您首先应该检查哪个部分?
模块化部分——关键要点
模块化提示词部分是构建易维护、易调试提示词工程的基础:
- 五个核心部分:上下文、任务、约束、输出格式、示例
- 每个部分都有单一职责——将它们混在一起会造成混淆
- 模块化结构支持逐部分调试和 A/B 测试
- 将提示词存储为结构化对象或函数,而不是原始字符串
- 跳过不需要的部分——规定过多只会增加干扰
- 对于每个非简单提示词,至少始终包含任务和输出格式
常见问题解答
「模块化提示部分」课时是免费的吗?
是的 — 「模块化提示部分」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Prompt Engineering 课程的其余内容,请升级到 CoddyKit PRO。 AI Prompt Engineering 课程共包含 4 节课。
「模块化提示部分」这节课中我会学到什么?
清晰地分隔上下文、指令、限制条件和输出格式 你通过在浏览器中直接运行的动手代码来练习 AI Prompt Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AI Prompt Engineering 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AI Prompt Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「模块化提示部分」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AI Prompt Engineering 课中编写并运行代码吗?
能。每节 AI Prompt Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。