Best practices for LLM prompt engineering(大语言模型提示工程的最佳实践)¶
Writing effective prompts - a process known as prompt engineering - is essential for unlocking the full potential of large language models. The goal of prompt engineering is to design inputs that guide LLMs to generate desired outputs. The quality of a prompt directly influences the relevance, accuracy, and coherence of the model's responses. This guide provides some best practices for prompt engineering, along with illustrative examples. While the effectiveness of each strategy might vary depending on the LLM used, the following is a consideration of best practices helping you start engineering useful and accurate prompts.
If you are working with AIP Assist specifically, review our AIP Assist best practices.
Key strategies for effective prompting¶
Effective prompt engineering is a dynamic and iterative process that combines clarity, specificity, and contextual relevance. By following these best practices and incorporating examples, users can maximize the effectiveness of large language models. As AI technology evolves, staying informed about new strategies will further enhance prompt quality and output accuracy.
- Be clear and specific
- Be clear: Use straightforward language to define the task or question.
- Example: Instead of asking "What do you know about coding?", specify "Summarize my framework options for developing a web application."
-
Specify context: Provide context to anchor the model's response.
- Example: "As a software engineer, explain the benefits of abstraction."
-
Refine and iterate
- Test and adjust: Experiment with different prompt structures and refine them based on output quality.
- Example: Start with "List the advantages of web applications." If the response is too broad, refine to "List the maintenance benefits of web applications compared to native applications."
-
Feedback loop: Use model feedback to continuously improve prompt design.
- Example: If the model misunderstands a prompt, adjust the wording and re-test.
-
Use examples
- Demonstrate desired output: Provide examples to set expectations for format and content.
- Example: "Translate the following sentence to French: 'Hello, how are you?' Example: 'Hello' translates to 'Bonjour'."
-
Highlight patterns: Use examples to establish a consistent response pattern.
- Example: "For each fruit, list its color and taste. Example: Apple - Red, Sweet."
-
Manage length and complexity
- Be concise: Provide necessary details without overloading the model.
- Example: Instead of "Can you tell me about the history, current state, and future of robotics?", use "Briefly describe the history of robotics."
-
Avoid overloading: Break complex tasks into simpler parts.
- Example: "First, list the steps in the semiconductor manufacturing process. Then, explain each step in detail."
-
Incorporate constraints
- Set boundaries: Define clear constraints to guide response scope.
- Example: "Summarize the article in no more than three sentences."
-
Limit unwanted outputs: Use negative examples or explicit instructions.
- Example: "Generate a list of pros and cons of remote work, but exclude personal opinions."
-
Provide relevant context
- Align with model capabilities: Tailor prompts to the strengths and limitations of the model.
- Example: For a model trained on medical data, ask, "Explain the symptoms of diabetes," rather than unrelated topics.
-
Maintain relevance: Ensure prompts are relevant to the model's training data.
- Example: "Discuss recent advancements in AI," aligning with the model's knowledge base.
-
Optimize the interaction
- Role-playing: Assign roles to guide the model's tone and depth.
- Example: "As a mechanical engineer, describe the most important sensors to deploy in a heavy manufacturing process."
- Sequential prompting: Use a series of prompts for complex responses.
- Example: "First, describe the semiconductor manufacturing process. Next, list three types of semiconductors and how they are manufactured."
Additional prompt engineering resources¶
To understand more on prompting, consider relevant documentation from the following sources:
- Anthropic: Prompt engineering overview ↗
- Google: Prompt engineering whitepaper ↗
- Microsoft: Prompt engineering techniques ↗
- OpenAI: Best practices for prompt engineering with the OpenAI API ↗
中文翻译¶
大语言模型提示工程的最佳实践¶
编写有效的提示(prompt)——这一过程被称为提示工程(prompt engineering)——对于释放大语言模型(LLM)的全部潜力至关重要。提示工程的目标是设计能够引导大语言模型生成期望输出的输入内容。提示的质量直接影响模型响应的相关性、准确性和连贯性。本指南提供了一些提示工程的最佳实践,并附有说明性示例。虽然每种策略的有效性可能因所用的大语言模型而异,但以下内容是对最佳实践的考量,可帮助您着手设计有用且准确的提示。
如果您正在使用 AIP Assist,请查阅我们的 AIP Assist 最佳实践。
有效提示的关键策略¶
有效的提示工程是一个动态且迭代的过程,融合了清晰性、具体性和上下文相关性。通过遵循这些最佳实践并结合示例,用户可以最大限度地发挥大语言模型的效能。随着人工智能技术的发展,及时了解新策略将进一步提升提示质量和输出准确性。
- 清晰且具体
- 保持清晰: 使用直白的语言定义任务或问题。
- 示例: 不要问"你对编码了解多少?",而应指定"总结我开发网页应用的框架选项。"
-
指定上下文: 提供上下文以锚定模型的响应。
- 示例: "作为一名软件工程师,解释抽象(abstraction)的好处。"
-
优化与迭代
- 测试与调整: 尝试不同的提示结构,并根据输出质量进行优化。
- 示例: 从"列出网页应用的优点"开始。如果响应过于宽泛,则优化为"列出网页应用相比原生应用在维护方面的优点。"
-
反馈循环: 利用模型反馈持续改进提示设计。
- 示例: 如果模型误解了提示,调整措辞并重新测试。
-
使用示例
- 展示期望输出: 提供示例以设定格式和内容的预期。
- 示例: "将以下句子翻译成法语:'Hello, how are you?' 示例:'Hello' 翻译为 'Bonjour'。"
-
突出模式: 使用示例建立一致的响应模式。
- 示例: "对于每种水果,列出其颜色和味道。示例:苹果——红色,甜。"
-
控制长度与复杂度
- 保持简洁: 提供必要细节,但不要使模型过载。
- 示例: 不要问"你能告诉我机器人技术的历史、现状和未来吗?",而应使用"简要描述机器人技术的历史。"
-
避免过载: 将复杂任务拆分为更简单的部分。
- 示例: "首先,列出半导体制造过程中的步骤。然后,详细解释每一步。"
-
融入约束条件
- 设定边界: 定义明确的约束条件以引导响应范围。
- 示例: "用不超过三句话总结这篇文章。"
-
限制不想要的输出: 使用负面示例或明确指令。
- 示例: "生成远程工作的优缺点列表,但排除个人观点。"
-
提供相关上下文
- 与模型能力对齐: 根据模型的优势和局限性定制提示。
- 示例: 对于基于医学数据训练的模型,问"解释糖尿病的症状",而不是不相关的话题。
-
保持相关性: 确保提示与模型的训练数据相关。
- 示例: "讨论人工智能的最新进展",与模型的知识库保持一致。
-
优化交互方式
- 角色扮演: 分配角色以引导模型的语气和深度。
- 示例: "作为一名机械工程师,描述在重型制造过程中需要部署的最重要的传感器。"
- 顺序提示: 使用一系列提示来处理复杂响应。
- 示例: "首先,描述半导体制造过程。接下来,列出三种类型的半导体及其制造方式。"
其他提示工程资源¶
要了解更多关于提示的内容,请参考以下来源的相关文档:
- Anthropic: 提示工程概述 ↗
- Google: 提示工程白皮书 ↗
- Microsoft: 提示工程技术 ↗
- OpenAI: 使用 OpenAI API 进行提示工程的最佳实践 ↗