《提示工程指南》 | Prompt Engineering Guide

AI应用信息10个月前发布 XIAOT
201 0

目录

prompt engineering 提示词工程

Six strategies for getting better results获得更好结果的六项策略

Write clear instructions 写下清晰的指示

Provide reference text 提供参考文字

Split complex tasks into simpler subtasks将复杂的任务拆分为更简单的子任务

Give the model time to “think”给模型时间“思考”

Use external tools 使用外部工具

Test changes systematically系统地测试变更

Tactics 策略

Strategy: Write clear instructions策略:写下清晰的说明

Tactic: Include detAIls in your query to get more relevant answers策略:在查询中包含详细信息以获得更相关的答案

Tactic: Ask the model to adopt a persona策略:要求模特采用角色

Tactic: Use delimiters to clearly indicate distinct parts of the input策略:使用分隔符清楚地指示输入的不同部分

Tactic: Specify the steps required to complete a task策略:指定完成任务所需的步骤

Tactic: Provide examples 策略:提供例子

Tactic: Specify the desired length of the output策略:指定所需的输出长度

Strategy: Provide reference text策略:提供参考文本

Tactic: Instruct the model to answer using a reference text策略:指导模型使用参考文本回答

Tactic: Instruct the model to answer with citations from a reference text策略:指示模型通过引用参考文本来回答

Strategy: Split complex tasks into simpler subtasks策略:将复杂的任务拆分为更简单的子任务

Tactic: Use intent classification to identify the most relevant instructions for a user query策略:使用意图分类来识别与用户查询最相关的指令

Tactic: For dialogue applications that require very long conversations, summarize or filter previous dialogue策略:对于需要很长对话的对话应用,总结或过滤之前的对话

Tactic: Summarize long documents piecewise and construct a full summary recursively策略:分段总结长文档并递归构建完整摘要

Strategy: Give models time to “think”策略:给模型时间“思考”

Tactic: Instruct the model to work out its own solution before rushing to a conclusion策略:指示模型在急于得出结论之前找出自己的解决方案

Tactic: Use inner monologue or a sequence of queries to hide the model’s reasoning process策略:使用内心独白或一系列查询来隐藏模型的推理过程

Tactic: Ask the model if it missed anything on previous passes策略:询问模型在之前的过程中是否遗漏了任何内容

Strategy: Use external tools策略:使用外部工具

Tactic: Use embeddings-based search to implement efficient knowledge retrieval策略:使用基于嵌入的搜索实现高效的知识检索

Tactic: Use code execution to perform more accurate calculations or call external APIs策略:使用代码执行来进行更准确的计算或调用外部API

Tactic: Give the model access to specific functions策略:让模型访问特定功能

Strategy: Test changes systematically策略:系统地测试变更

Tactic: Evaluate model outputs with reference to gold-standard answers策略:参考黄金标准答案评估模型输出

Other resources 其他资源

提示工程指南

提示工程简介

模型设置

基本概念

基础提示词

提示词格式

提示词要素

设计提示的通用技巧

从简单开始

指令

具体性

避免不精确

做还是不做?

提示词示例

文本概括

信息提取

问答

文本分类

对话

代码生成

推理

提示技术

零样本提示

少样本提示

少样本提示的限制

Chain-of-Thought Prompting

链式思考(CoT)提示

零样本COT提示

自我一致性

生成知识提示

思维树 (ToT)

检索增强生成 (RAG)

自动推理并使用工具 (ART)


Prompt engineering 提示词工程

This guide shares strategies and tactics for getting better results from large language models (sometimes referred to as GPT models) like GPT-4. The methods described here can sometimes be deployed in combination for greater effect. We encourage experimentation to find the methods that work best for you.
本指南分享了从 GPT-4 等大型语言模型(有时称为 GPT 模型)获得更好结果的策略和策略。有时可以组合使用此处描述的方法以获得更好的效果。我们鼓励尝试找到最适合您的方法。

Some of the examples demonstrated here currently work only with our most capable model, gpt-4. In general, if you find that a model fails at a task and a more capable model is available, it’s often worth trying again with the more capable model.
此处演示的一些示例目前仅适用于我们最强大的模型 gpt-4 。一般来说,如果您发现某个模型在某项任务中失败,并且有一个功能更强大的模型可用,那么通常值得使用功能更强大的模型再次尝试。

You can also explore example prompts which showcase what our models are capable of:
您还可以探索示例提示,展示我们的模型的功能:

Prompt examples 提示示例

Explore prompt examples to learn what GPT models can do探索提示示例以了解 GPT 模型的功能

Six strategies for getting better results获得更好结果的六项策略

Write clear instructions 写下清晰的指示

These models can’t read your mind. If outputs are too long, ask for brief replies. If outputs are too simple, ask for expert-level writing. If you dislike the format, demonstrate the format you’d like to see. The less the model has to guess at what you want, the more likely you’ll get it.
这些模型无法读懂你的想法。如果输出太长,请要求简短答复。如果输出太简单,请要求专家级别的写作。如果您不喜欢这种格式,请演示您希望看到的格式。模型猜测你想要什么的次数越少,你得到它的可能性就越大。

Tactics: 策略:

  • Include details in your query to get more relevant answers在您的查询中包含详细信息以获得更相关的答案
  • Ask the model to adopt a persona要求模特采用角色
  • Use delimiters to clearly indicate distinct parts of the input使用分隔符清楚地指示输入的不同部分
  • Specify the steps required to complete a task指定完成任务所需的步骤
  • Provide examples  提供例子
  • Specify the desired length of the output指定所需的输出长度

Provide reference text 提供参考文字

Language models can confidently invent fake answers, especially when asked about esoteric topics or for citations and URLs. In the same way that a sheet of notes can help a student do better on a test, providing reference text to these models can help in answering with fewer fabrications.
语言模型可以自信地发明假答案,特别是当被问及深奥的主题或引文和 URL 时。就像一张笔记可以帮助学生在考试中取得更好的成绩一样,为这些模型提供参考文本可以帮助减少作答次数。

Tactics: 策略:

  • Instruct the model to answer using a reference text指示模型使用参考文本回答
  • Instruct the model to answer with citations from a reference text指示模型通过引用参考文本来回答

Split complex tasks into simpler subtasks将复杂的任务拆分为更简单的子任务

Just as it is good practice in software engineering to decompose a complex system into a set of modular components, the same is true of tasks submitted to a language model. Complex tasks tend to have higher error rates than simpler tasks. Furthermore, complex tasks can often be re-defined as a workflow of simpler tasks in which the outputs of earlier tasks are used to construct the inputs to later tasks.
正如软件工程中将复杂系统分解为一组模块化组件是良好实践一样,提交给语言模型的任务也是如此。复杂的任务往往比简单的任务具有更高的错误率。此外,复杂的任务通常可以被重新定义为更简单任务的工作流程,其中早期任务的输出用于构造后续任务的输入。

Tactics: 策略:

  • Use intent classification to identify the most relevant instructions for a user query使用意图分类来识别与用户查询最相关的指令
  • For dialogue applications that require very long conversations, summarize or filter previous dialogue对于需要很长对话的对话应用,总结或过滤以前的对话
  • Summarize long documents piecewise and construct a full summary recursively分段总结长文档并递归构建完整摘要

Give the model time to “think”给模型时间“思考”

If asked to multiply 17 by 28, you might not know it instantly, but can still work it out with time. Similarly, models make more reasoning errors when trying to answer right away, rather than taking time to work out an answer. Asking for a “chain of thought” before an answer can help the model reason its way toward correct answers more reliably.
如果要求将 17 乘以 28,您可能不会立即知道,但随着时间的推移仍然可以算出来。同样,模型在尝试立即回答而不是花时间找出答案时会犯更多推理错误。在给出答案之前询问“思路链”可以帮助模型更可靠地推理出正确答案。

Tactics: 策略:

  • Instruct the model to work out its own solution before rushing to a conclusion指示模型在急于得出结论之前找出自己的解决方案
  • Use inner monologue or a sequence of queries to hide the model’s reasoning process使用内心独白或一系列查询来隐藏模型的推理过程
  • Ask the model if it missed anything on previous passes询问模型在之前的过程中是否遗漏了任何内容

Use external tools 使用外部工具

Compensate for the weaknesses of the model by feeding it the outputs of other tools. For example, a text retrieval system (sometimes called RAG or retrieval augmented generation) can tell the model about relevant documents. A code execution engine like OpenAI‘s Code Interpreter can help the model do math and run code. If a task can be done more reliably or efficiently by a tool rather than by a language model, offload it to get the best of both.
通过向模型提供其他工具的输出来弥补模型的弱点。例如,文本检索系统(有时称为 RAG 或检索增强生成)可以告诉模型相关文档。像 OpenAI 的代码解释器这样的代码执行引擎可以帮助模型进行数学运算并运行代码。如果一项任务可以通过工具而不是语言模型更可靠或更有效地完成,那么可以卸载它以充分利用两者。

Tactics: 策略:

  • Use embeddings-based search to implement efficient knowledge retrieval使用基于嵌入的搜索实现高效的知识检索
  • Use code execution to perform more accurate calculations or call external APIs使用代码执行来执行更准确的计算或调用外部API
  • Give the model access to specific functions授予模型访问特定功能的权限

Test changes systematically系统地测试变更

Improving performance is eASIer if you can measure it. In some cases a modification to a prompt will achieve better performance on a few isolated examples but lead to worse overall performance on a more representative set of examples. Therefore to be sure that a change is net positive to performance it may be necessary to define a comprehensive test suite (also known an as an “eval”).
如果您可以衡量性能,那么提高性能就会更容易。在某些情况下,对提示的修改将在一些孤立的示例上实现更好的性能,但会导致在一组更具代表性的示例上整体性能变差。因此,为了确保更改对性能产生净积极影响,可能有必要定义一个全面的测试套件(也称为“评估”)。

Tactic: 战术:

  • Evaluate model outputs with reference to gold-standard answers参考黄金标准答案评估模型输出

Tactics 策略

Each of the strategies listed above can be instantiated with specific tactics. These tactics are meant to provide ideas for things to try. They are by no means fully comprehensive, and you should feel free to try creative ideas not represented here.
上面列出的每个策略都可以用特定的策略来实例化。这些策略旨在提供尝试的想法。它们绝不是完全全面的,您应该随意尝试此处未列出的创意。

Strategy: Write clear instructions策略:写下清晰的说明

Tactic: Include details in your query to get more relevant answers策略:在查询中包含详细信息以获得更相关的答案

In order to get a highly relevant response, make sure that requests provide any important details or context. Otherwise you are leaving it up to the model to guess what you mean.
为了获得高度相关的响应,请确保请求提供任何重要的详细信息或上下文。否则,你将让模型来猜测你的意思。

Worse 更差Better 更好的
How do I add numbers in Excel?
如何在 Excel 中添加数字?
How do I add up a row of dollar amounts in Excel? I want to do this automatically for a whole sheet of rows with all the totals ending up on the right in a column called “Total”.
如何在 Excel 中添加一行美元金额?我想对整张行自动执行此操作,所有总计都在右侧名为“总计”的列中结束。
Who’s president? 谁是总统?Who was the president of Mexico in 2021, and how frequently are elections held?
谁是 2021 年墨西哥总统?选举频率如何?
Write code to calculate the Fibonacci sequence.
编写代码来计算斐波那契数列。
Write a TypeScript function to efficiently calculate the Fibonacci sequence. Comment the code liberally to explain what each piece does and why it’s written that way.
编写一个 TypeScript 函数来高效计算斐波那契数列。自由地注释代码以解释每部分的作用以及为什么这样编写。
Summarize the meeting notes.
总结会议记录。
Summarize the meeting notes in a single paragraph. Then write a markdown list of the speakers and each of their key points. Finally, list the next steps or action items suggested by the speakers, if any.
用一个段落总结会议记录。然后写下演讲者的 Markdown 列表以及他们的每个要点。最后,列出发言人建议的后续步骤或行动项目(如果有)。
Tactic: Ask the model to adopt a persona策略:要求模特采用角色

The system message can be used to specify the persona used by the model in its replies.
系统消息可用于指定模型在其回复中使用的角色。

SYSTEM 系统

When I ask for help to write something, you will reply with a document that contains at least one joke or playful comment in every paragraph.
当我请求帮助写一些东西时,你会回复一份文档,其中每段至少包含一个笑话或有趣的评论。

USER 用户

Write a thank you note to my steel bolt vendor for getting the delivery in on time and in short notice. This made it possible for us to deliver an important order.
给我的钢螺栓供应商写一封感谢信,感谢他们在短时间内准时交货。这使我们能够交付一份重要的订单。

    © 版权声明

    相关文章