提示链
提示链引言
为了提高大型语言模型(LLM)的可靠性和性能,一种重要的提示工程技术是将任务分解为子任务。一旦确定了这些子任务,就用一个子任务提示 LLM,然后将其响应作为输入用于另一个提示。这就是所谓的提示链(prompt chaining),即将任务分解为子任务,以创建一系列提示操作。
提示链有助于完成 LLM 在面对非常详细的提示时可能难以处理的复杂任务。在提示链中,链式提示会在生成的响应上执行转换或附加处理,直到达到最终所需状态。
除了获得更好的性能外,提示链还有助于提高 LLM 应用的透明度、可控性和可靠性。这意味着您可以更轻松地调试模型响应问题,并在需要改进的不同阶段分析和提升性能。
提示链在构建由 LLM 支持的对话助手以及提升应用个性化和用户体验方面特别有用。
在我们的新 AI 课程中了解更多关于提示链和高级提示方法的内容。立即加入! (在新标签页中打开) 使用代码 PROMPTING20 额外享受八折优惠。
提示链用例
文档问答的提示链
提示链可用于涉及多种操作或转换的不同场景。例如,LLM 的一个常见用例是回答关于大型文本文档的问题。如果您设计两个不同的提示会很有帮助:第一个提示负责提取相关引用来回答问题,第二个提示将这些引用和原始文档作为输入来回答给定问题。换句话说,您将创建两个不同的提示来完成回答文档中给定问题的任务。
下面的第一个提示从给定问题的文档中提取相关引用。请注意,为简单起见,我们为文档添加了一个占位符 {{document}}
。要测试此提示,您可以复制并粘贴维基百科的一篇文章,例如关于 提示工程 (在新标签页中打开) 的页面。由于此任务使用了较大的上下文,我们使用了 OpenAI 的 gpt-4-1106-preview
模型。您也可以将此提示与其他长上下文 LLM(如 Claude)一起使用。
提示 1
You are a helpful assistant. Your task is to help answer a question given in a document. The first step is to extract quotes relevant to the question from the document, delimited by ####. Please output the list of quotes using <quotes></quotes>. Respond with "No relevant quotes found!" if no relevant quotes were found.
####
{{document}}
####
这里是整个提示的截图,包括使用 user
角色传递的问题。

提示 1 的输出
<quotes>
- Chain-of-thought (CoT) prompting[27]
- Generated knowledge prompting[37]
- Least-to-most prompting[38]
- Self-consistency decoding[39]
- Complexity-based prompting[41]
- Self-refine[42]
- Tree-of-thought prompting[43]
- Maieutic prompting[45]
- Directional-stimulus prompting[46]
- Textual inversion and embeddings[59]
- Using gradient descent to search for prompts[61][62][63][64]
- Prompt injection[65][66][67]
</quotes>
第一个提示返回的引用现在可以用作下面第二个提示的输入。请注意,您可以进一步清理这些引用,即删除引文。这些引文可以在链中的另一个提示中删除或利用,但目前您可以忽略这一点。第二个提示然后使用提示 1 提取的相关引用,并根据文档和提取的引用准备一个有用的问题回答。第二个提示可以是以下内容:
提示 2
Given a set of relevant quotes (delimited by <quotes></quotes>) extracted from a document and the original document (delimited by ####), please compose an answer to the question. Ensure that the answer is accurate, has a friendly tone, and sounds helpful.
####
{{document}}
####
<quotes>
- Chain-of-thought (CoT) prompting[27]
- Generated knowledge prompting[37]
- Least-to-most prompting[38]
- Self-consistency decoding[39]
- Complexity-based prompting[41]
- Self-refine[42]
- Tree-of-thought prompting[43]
- Maieutic prompting[45]
- Directional-stimulus prompting[46]
- Textual inversion and embeddings[59]
- Using gradient descent to search for prompts[61][62][63][64]
- Prompt injection[65][66][67]
</quotes>
提示 2 的输出
The prompting techniques mentioned in the document include:
1. Chain-of-thought (CoT) prompting[27]
2. Generated knowledge prompting[37]
3. Least-to-most prompting[38]
4. Self-consistency decoding[39]
5. Complexity-based prompting[41]
6. Self-refine[42]
7. Tree-of-thought prompting[43]
8. Maieutic prompting[45]
9. Directional-stimulus prompting[46]
10. Textual inversion and embeddings[59]
11. Using gradient descent to search for prompts[61][62][63][64]
12. Prompt injection[65][66][67]
Each of these techniques employs unique strategies to enhance or specify the interactions with large language models to produce the desired outcomes.
如您所见,简化和创建提示链是一种有用的提示方法,其中响应需要经过多个操作或转换。作为一个练习,您可以随意设计一个提示,用于在将响应作为最终结果发送给应用用户之前,从响应中删除引文(例如,[27])。
您还可以在这篇 文档 (在新标签页中打开) 中找到更多利用 Claude LLM 的提示链示例。我们的示例是受到其示例的启发和改编。