[LLM][Prompt Engineering]:大语言模型提示工程(Prompt Engineering)

Prompt Engineering

大语言模型(LLM)的微调(Fine-tune)代价较高,基于自然语言的提示(Prompt)方法已经成为了使用大语言模型解决下游任务的主要途径。而且提示的质量在很大程度上会影响大语言模型在特定任务中的表现。当前的很多工作和应用集中在如何快速应用LLM,使其适配下游任务,所采用的方法统称为Prompt Engineering,其中包括了上下文学习(In-Context Learning,ICL)和思维链提示(Chain-of-Thought,CoT),部分内容来自GPT Prompt Engineering官网LLM Books

上下文学习和思维链相关文章参考链接:

  1. 上下文学习,ICL:https://blog.csdn.net/qq_41897558/article/details/141676968?spm=1001.2014.3001.5501
  2. 思维链,CoT:https://blog.csdn.net/qq_41897558/article/details/141676968?spm=1001.2014.3001.5501

Prompt Engineering(提示工程) 是啥?

Prompt是什么?

在大语言模型(如GPT-4)的应用中,Prompt 是指用户输入给模型的一段文字或问题,用于引导模型生成相应的回答或内容。简单来说,Prompt 就是你给模型的指令或提示。

例如,如果你想让模型生成一段关于人工智能的介绍,你可以输入一个 Prompt 如:“请介绍一下人工智能的基本概念。” 模型会根据这个提示生成相应的内容。

Prompt 的设计和编写在大语言模型的应用中非常重要,因为一个好的 Prompt 可以帮助模型更准确地理解用户的需求,从而生成更符合预期的回答。

Why Prompt Engineering?

Prompt engineering 是指设计和优化输入提示(Prompt)以便大语言模型(如GPT-4)能够生成更准确和有用的输出。这一过程涉及到理解模型的工作原理用户需求以及**如何通过不同的提示结构来引导模型生成所需的内容。**通过提示工程,可以:

  1. 提高准确性:通过精心设计的Prompt,可以帮助模型更好地理解用户的意图,从而生成更准确的回答
  2. 优化输出质量:不同的Prompt结构可能会导致模型生成不同质量的输出。通过Prompt engineering,可以找到最优的提示结构,提升输出的质量。
  3. 节省时间和资源:一个好的Prompt可以减少模型生成不相关或错误内容的概率,从而节省用户的时间和计算资源。
  4. 增强模型的适应性:Prompt engineering可以使模型更好地适应不同的应用场景和需求,提高其通用性和灵活性。

怎样进行提示工程

一般而言,针对大语言模型的提示设计需要考虑四个关键要素,即==任务描述输入数据上下文信息提示策略==。下面将对这四个关键要素进行具体介绍。

  1. 任务描述: 用户应该使用清晰的、具体的表述来描述任务目标。对于一些特殊任务,还要求对输入或输出的格式进行更详细的说明,可以使用关键词或者特殊符号来强调特殊设置以指导大语言模型更好地完成任务。

  2. 输入数据: 通常情况下,用户可以直接使用自然语言描述输入数据的内容。对于特殊形式的输入数据,则需要采用合适的方法使其能够被大语言模型读取与理解(比如,表格、代码等)。

  3. 上下文信息(上下文学习):任务示例数据也有助于提升大语言模型处理复杂任务的能力,模型可以通过示例数据学习任务目标输出格式以及输入和输出之间的映射关系。

  4. 提示策略:针对不同的大语言模型设计合适的提示策略对于激发模型解决特定任务的能力非常重要。在某些情况下,添加特定的前缀或后缀有助于引导大语言模型解决复杂任务。例如,使用前缀“让我们一步一步地思考”可以激发大语言模型的逐步推理能力,而使用前缀“你是这项任务(或这个领域)的专家”可以提高大语言模型在某些特定任务(或领域)中的表现。此外,对于对话式的大语言模型(例如ChatGPT),由于其使用了大量对话数据进行训练,因此更合适的做法是将提示拆分为多个子任务提示,以多轮对话的方法逐步输入给大语言模型

针对上述这些关键要素,有哪些实用的建议和例子呢?

  1. 清晰地表达任务目标
  • 表达越详细越好。Make your prompt as detailed as possible, e.g., “Summarize the article into a shortparagraph within 50 words. The major storyline and conclusion should be included,and the unimportant details can be omitted.”
  • 角色扮演,让LLM扮演一个该领域的专家
    • It is helpful to let the LLM know that it is an expert with a prefixed prompt, e.g.,“You are a sophisticated expert in the domain of compute science.”
    • Before the prompt, assigning a role for the LLM is useful to help it better fulfill thefollowing task instruction, e.g., “I want you to act as a lawyer”.
  • 告诉LLM应该做啥。Tell the model more what it should do, but not what it should not do.
  • 需要些简短凝练的回答时,给予一些关键字。To avoid the LLM to generate too long output, you can just use the prompt: “Question:Short Answer: ”. Besides, you can also use the following suffixes, “in a or a few words”,“in one of two sentences”.
  • 分类or打分任务,提示详尽的打分标准。If you want LLMs to provide the score for a text, it is necessary to provide a detailed description about the scoring standard with examples as reference.
  • For few-shot chain-of-thought prompting, you can also use the prompt “Let’s think step-by-step”, and the few-shot examples should be separated by “\n” instead of full stop.
  • 少用代词。The prompt should be self-contained, and better not include pronouns (e.g., it and they)in the context.
  • 比较问题,顺序很关键。When using LLMs for comparing two or more examples, the order affects the performancea lot.
  • 多选择的问题,限制输出空间,For multi-choice questions, it is useful to constrain the output space of the LLM. You can use a more detailed explanation or just imposing constraints on the logits.
  • 推荐任务的排序问题,用选项指代要分类or排序的内容。For sorting based tasks (e.g., recommendation), instead of directly outputting the complete text of each item after sorting, one can assign indicators (e.g., ABCD) to the unsorted items and instruct the LLMs to directly output the sorted indicators.
  1. 将复杂任务分解为简单且详细的子任务(CoT,ToT)。
  • **思维链,CoT,**For complex tasks, you can clearly describe the required intermediate steps to accomplishit, e.g., “Please answer the question step by step as: Step 1 - Decompose thequestion into several sub-questions, · · · ”
  • 让LLM解释为什么生成这个答案,When LLMs generate text according to some context (e.g., making recommendations according to purchase history), instructing them with the explanation about the generated result conditioned on context is helpful to improve the quality of the generated text.
  • 通过自然语言的形式实现ToT,An approach similar to tree-of-thoughts but can be done in one prompt: e.g., Imagine three different experts are answering this question. All experts will write down one step of their thinking, then share it with the group of experts. Then all experts will go on to the next step, etc. If any expert realizes they’re wrong at any point then they leave. The question is
  • As a symbol sequence can typically be divided into multiple segments (e.g., 𝑖1, 𝑖2, 𝑖3−→𝑖1, 𝑖2 and 𝑖2, 𝑖3), the preceding ones can be used as in-context exemplars to guide LLMs to predict the subsequent ones, meanwhile providing historical information.
  • **输出前让LLM先检查一下。**Let the LLM check its outputs before draw the conclusion, e.g., “Check whether the above solution is correct or not.”
  1. 提供少量样本,few-shot-CIL。
  • 示例要用良好的格式,Well-formatted in-context exemplars are very useful, especially for producing theoutputs with complex formats.
  • You can also retrieve similar examples in context to supply the useful task-specifi cknowledge for LLMs. To retrieve more relevant examples, it is useful to first obtain the answer of the question, and then concatenate it with the question for retrieval.
  • 示例的多样性和代表性,The diversity of the in-context exemplars within the prompt is also useful. If it is not easy to obtain diverse questions, you can also seek to keep the diversity of the solutions for the questions.
  • 示例的顺序很重要,Order matters for in-context exemplars and prompts components. For very long inputdata, the position of the question (first or last) may also affect the performance.
  • 实用LLM生成一个示例,If you can not obtain the in-context exemplars from existing datasets, an alternative way is to use the zero-shot generated ones from the LLM itself.
  1. 采用模型友好(训练时出现的)的提示格式。

大语言模型采用专门构建的数据集进行预训练,因此可以从数据集中学习到大量的语言表达模式发现并利用这些语言表达模式可以帮助我们更有效地使用大语言模型完成特定任务。

  • 对于提示中需要重点强调的部分,OpenAI 官方文档中建议用户可以使用特殊符号(例如♯♯♯、三引号“““和”””、XML 标签等)进行分隔,从而让大语言模型更好地理解相关内容。
  • 此外,大多数现有的大语言模型主要在英语文本上进行训练,理解英语指令的能力更强,因此在执行任务时使用英语指令可能会获得更好的执行效果。对于非英语用户来说,通过机器翻译工具将非英语任务指令转换为英语指令再输入给大语言模型,可能会是一个更有效的策略。
  • 20
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值