Prompt Engineering 完整指南

Prompt Engineering 完整指南

在这里插入图片描述

Prompt Engineering 是与 AI 模型进行交互的关键技术,通过精心设计的指令(prompt)引导模型生成高质量的输出。本文将详细介绍 prompt 设计的最佳实践、基本原则以及高级技巧,帮助你在实际应用中最大化模型的效用。

1. Prompt 设计的基本原则

1.1 清晰明确

一个好的 prompt 应该避免模糊不清,直接指出期望的结果。清晰的指令可以减少模型产生误解的可能性,从而提高生成内容的质量。例如,与其说“写点东西”,不如更具体地要求“写一篇关于气候变化对极地冰盖影响的500字文章”。

1.2 具体化和分步指引

对于复杂的任务,将任务分解成多个步骤有助于模型理解并准确完成任务。例如,以下示例展示了如何分步进行指引:

1. 用一句话总结下面的文本。
2. 将该摘要翻译成英语。
3. 列出摘要中的所有专有名词。

这种方法不仅可以使模型产生结构化的输出,还能确保每个步骤的完成质量。

1.3 上下文提供

为模型提供足够的背景信息或上下文可以显著提高生成结果的相关性。例如,如果要求模型写一个故事,可以先给出角色背景和故事设定,这样能帮助模型理解任务的细节。

1.4 示例引导

Few-shot 示例是帮助模型理解预期输出风格的有效方法。在 prompt 中加入几个示例,可以引导模型识别模式,从而生成符合预期的输出。以下是一个示例:

Q: 5 + 3 = ?
A: 8
Q: 10 + 7 = ?
A: 17

这种方式可以有效地提高模型在处理类似问题时的准确性。

2. 进阶策略

2.1 使用逻辑框架

在需要复杂推理的场景中,使用特定的思维逻辑或框架可以帮助模型生成更具逻辑性的输出。例如,要求模型使用贝叶斯定理来计算某个事件的概率,可以使结果更加准确和有理有据:

假如你是天气预测器,最近 7 天都没有下雨,请根据贝叶斯定理思考明天下雨的概率。

这种方式让模型不仅仅是简单地预测结果,还能提供合理的推理过程。

2.2 提供推理过程

为了避免模型快速得出错误结论,可以通过 prompt 引导模型进行一系列相关推理。指示模型在给出最终答案前先思考问题,或分步骤进行推理,这样可以提升结果的可靠性。

2.3 角色扮演

在某些情况下,赋予模型一个特定的角色有助于生成更符合预期的内容。例如,要求模型以“科学家”或“历史学家”的身份回答问题,可以使输出更具专业性和权威性。这种方法在编写技术文档、教学材料或角色扮演对话时尤为有效。

3. 常用技巧和最佳实践

3.1 迭代优化

在实际操作中,通过逐步调整 prompt,反复测试和优化,可以找到最佳的 prompt 设计。可以先从简单的指令开始,然后逐步增加复杂性和细节,通过对比不同 prompt 的输出,选择最优的设计。

3.2 思维框架的一致性

引导模型在生成内容时保持思维框架的一致性可以提高输出的逻辑性。例如,要求模型在整个过程中都遵循同一逻辑框架或使用同一分析方法,能有效减少生成结果的逻辑错误。

3.3 控制生成风格

通过控制 prompt 中的语言风格和结构,可以引导模型生成符合特定要求的内容。例如,在要求模型生成具有正式语气的商业报告时,prompt 可以明确规定输出的格式和语气,确保结果的专业性。

4. 实践案例

案例1:贝叶斯推理

要求模型基于贝叶斯定理进行推理,可以得到更具逻辑性和准确性的结果。以下是一个关于天气预测的示例:

假如你是天气预测器,最近 7 天都没有下雨,请根据贝叶斯定理思考明天下雨的概率。

该 prompt 通过指定思维方式,约束模型在一个合理的逻辑框架内进行推理,最终输出的结果更符合预期。

案例2:角色扮演

在一个终身学习者的场景下,赋予模型一个“学习助手”的角色,可以帮助引导用户学习新知识。这一场景下,prompt 设计如下:

你是一个终身学习者,同时也是一个求知欲强的探索者。我们将合作探索和学习新的知识和技能。

通过这样的设计,模型能够更有效地帮助用户制定学习计划,并提供个性化的建议。

5. 总结

Prompt engineering 是与 AI 模型进行高效交互的关键技术。通过遵循上述原则和策略,你可以显著提高模型输出的质量和相关性。无论是在简单任务还是复杂推理中,合理的 prompt 设计都能够为你的工作带来极大的帮助。希望这份指南能够为你在实践中提供有价值的参考

#Prompt 原则示例 Prompt
1如果你更喜欢简洁的答案,直接切入主题,无需使用礼貌用语。Could you kindly describe the structure of a human cell, please? -> Describe the structure of a human cell.
2在 prompt 中加入预期的受众信息,例如受众是该领域的专家。Construct an overview of how smartphones work, intended for seniors who have never used one before.
3在互动对话中,将复杂任务分解为一系列更简单的 prompt。P1: Distribute the negative sign to each term inside the parentheses of the following equation: 2x + 3y - (4x - 5y)
P2: Combine like terms for ‘x’ and ‘y’ separately.
P3: Provide the simplified expression after combining the terms.
4采用肯定性指令,例如“执行”,避免使用否定性语言。How do buildings remain stable during earthquakes?
5当需要对某个主题有更深入的理解时,使用解释性提示。Explain to me like I’m 11 years old: how does encryption work?
6在 prompt 中加入“我将奖励 $xxx 给更好的解决方案”的表述。I’m going to tip $300K for a better solution! Explain the concept of dynamic programming and provide an example use case.
7使用 few-shot 示例进行提示。Example 1: Translate the following English sentence to French: “The sky is blue.” (Response: “Le ciel est bleu.”)
Example 2: Translate the following English sentence to Spanish: “I love books.” (Response: “Amo los libros.”)
8格式化你的 prompt,先使用 ‘###Instruction###’,然后(如适用)使用 ‘###Example###’ 或 ‘###Question###’,然后再提供内容。###Instruction### Translate a given word from English to French.
###Question### What is the French word for “book”?
9在 prompt 中加入 “Your task is” 和 “You MUST”。Your task is to explain the water cycle to your friend. You MUST use simple language.
10在 prompt 中加入 “You will be penalized”。Your task is to explain the water cycle to your friend. You will be penalized if you fail to use simple language.
11在 prompt 中使用 “Answer a question given in a natural, human-like manner”。Write a paragraph about healthy food. Answer a question given in a natural, human-like manner.
12使用诸如“逐步思考”的提示词。Write a Python code to loop through 10 numbers and sum all of them. Let’s think step by step.
13在 prompt 中加入 “Ensure that your answer is unbiased and avoids relying on stereotypes”。How do cultural backgrounds influence the perception of mental health?
Ensure that your answer is unbiased and avoids relying on stereotypes.
14允许模型通过提问来收集足够的细节和要求,以便提供所需的输出。From now on, ask me questions until you have enough information to create a personalized fitness routine.
15使用“教我[定理/主题/规则名]并在最后进行测试,但不要给出答案”来测试理解。Teach me about the KVL law and include a test at the end, and let me know if my answers are correct after I respond, without providing the answers beforehand.
16在 prompt 中赋予 LLM 角色。If you were an expert economist, how would you answer this: What are the key differences between a capitalist and a socialist economic system?
17在 prompt 中使用分隔符。Compose a persuasive essay discussing the importance of ‘renewable energy sources’ in reducing greenhouse gas emissions.
18在 prompt 中多次重复特定词语或短语。Evolution, as a concept, has shaped the development of species. What are the main drivers of evolution, and how has evolution affected modern humans?
19将链式思维与 few-shot 示例结合使用。Example 1: “Divide 10 by 2. First, take 10 and divide it by 2. The result is 5.”
Example 2: “Divide 20 by 4. First, take 20 and divide it by 4. The result is 5.”
Main Question: “Divide 30 by 6. First, take 30 and divide it by 6. The result is…?”
20在 prompt 结尾使用输出引导词。Describe the principle behind Newton’s First Law of Motion. Explanation:
21要求模型写出详细的文本。Write a detailed paragraph for me on the evolution of smartphones in detail by adding all the information necessary.
22在不改变风格的情况下修正或更改特定文本。Try to revise every text sent by users. You should only improve the user’s grammar and vocabulary and make sure it sounds natural. You should maintain the original writing style, ensuring that a formal paragraph remains formal.
Paragraph: Renewable energy is really important for our planet’s future. It comes from natural…
23在 prompt 中处理多文件代码情况。Generate code that spans more than one file, and generate a Python script that can be run to automatically create the specified files for a Django project with two basic apps for different functionalities.
24在 prompt 中利用开头的词句、短语或句子进行引导。I’m providing you with the beginning of a fantasy tale: “The misty mountains held secrets no man knew.” Finish it based on the words provided. Keep the flow consistent.
25明确说明模型需要遵循的要求,包括关键词、规则、提示或指令。Create a packing list for a beach vacation, including the following keywords “sunscreen,” “swimsuit,” and “beach towel” as essential items.
26在 prompt 中包含仿照提供的样本来写作的指令。“The gentle waves whispered tales of old to the silvery sands, each story a fleeting memory of epochs gone by.” Use the same language based on the provided text to portray a mountain’s interaction with the wind.
  • 12
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

problc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值