9.3 RAG 系统的提示词优化

在 RAG 系统中,Retrieval 负责“找对资料”,而 Prompt Engineering 负责“用好资料”。一个设计低劣的提示词会让高质量的检索结果付诸东流。本节将深入探讨 RAG 专用的提示词设计模式。

1. 上下文注入格式:Context Injection

最直观的问题是:如何将检索到的文档片段喂给模型?

❌ 错误示范: 直接拼接,不加分隔符。

参考资料:
这是文档 1 的内容...这是文档 2 的内容...
用户问题:...

模型很难区分文档的边界,容易造成信息混淆。

✅ 最佳实践: 使用明确的分隔符或 XML 标签(Claude 和 GPT-4 对 XML 标签支持极佳)。

Here are the retrieved documents relevant to the user's question:

<documents>
    <document index="1">
        <source>employee_handbook.pdf</source>
        <content>报销必须在费用发生后的 30 天内提交...</content>
    </document>
    <document index="2">
        <source>travel_policy.docx</source>
        <content>所有超过 5000 元的差旅费需要 VP 审批...</content>
    </document>
</documents>

这种结构化的注入方式有三个好处:

  1. 边界清晰:模型能明确区分不同文档。

  2. 便于引用:每个文档都有 index,方便要求模型引用。

  3. 元数据感知:可以包含 sourcedate 等元数据,帮助模型判断时效性。

2. 引用与溯源

RAG 的核心价值在于“有据可查”。我们需要强制模型在回答中标注来源。

提示词策略

"Answer the user's question using only the information in the <documents>. You must cite the document index for every statement you make, using the format [index]. For example: 'Expenses must be submitted within 30 days [1].'"

验证技巧: 为了防止模型胡乱引用(例如引用了文档 [3] 但其实只给了 2 个文档),可以在后处理阶段编写简单的正则表达式代码,校验模型输出的 [index] 是否在提供的文档范围内。

3. 拒绝幻觉

当检索结果不包含答案时,模型倾向于利用自己的预训练知识去“脑补”一个答案,这对 RAG 系统通常是不可接受的。

防御性提示词

"If the provided documents do not contain the necessary information to answer the question, you must say 'The provided documents do not contain this information.' Do NOT try to answer from your own knowledge. Do NOT make up an answer."

思维链强化: 要求模型在回答前先检查文档覆盖率:

"Thought: First, check if the documents contain the answer to the user's specific question. If yes, extract the info. If no, output the refusal message."

4. 答案优化与合成

综合冲突信息

当不同文档在同一事实上有出入时(如旧文档说 A,新文档说 B):

"If there are conflicting statements in the documents, prioritize the document with the more recent date metadata. If dates are not available, mention the conflict in your answer."

风格统一

RAG 的检索结果往往风格迥异(有的来自法条,有的来自聊天记录)。提示词需要统一下一步生成的语调:

"Synthesize the information into a coherent, professional response suitable for a customer service email. Avoid copying the text verbatim; rephrase it to be more natural."

9.3.1 完整模板示例

完整 API 调用示例

以下示例展示如何将上述 RAG 提示词模板与 API 调用结合,实现一个端到端的 RAG 问答流程:

[!TIP] 在生产环境中,建议将 System Prompt(角色定义和规则)放入 system 参数,将检索文档和用户问题放入 messages,以便利用提示词缓存arrow-up-right降低重复调用的成本。

动手试试

  1. 试着写一条 RAG 系统提示词,要求模型”仅根据以下文档回答,如果文档中找不到答案则明确说明”——然后用一个文档未覆盖的问题测试它。

  2. 在你的 RAG 系统中,模型是否会忽略检索结果而依赖自身知识回答?如果会,你如何在提示词中加强”忠实度”?

最后更新于