Transformers 文档

提示工程

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

提示工程

提示工程或提示,使用自然语言来提高大型语言模型 (LLM) 在各种任务上的性能。提示可以引导模型生成期望的输出。在许多情况下,您甚至不需要为任务微调模型。您只需要一个好的提示。

尝试提示 LLM 对一些文本进行分类。当您创建提示时,重要的是提供关于任务以及结果应该是什么样子的非常具体的说明。

from transformers import pipeline
import torch

pipeline = pipeline(task="text-generation", model="mistralai/Mistal-7B-Instruct-v0.1", torch_dtype=torch.bfloat16, device_map="auto")
prompt = """Classify the text into neutral, negative or positive.
Text: This movie is definitely one of my favorite movies of its kind. The interaction between respectable and morally strong characters is an ode to chivalry and the honor code amongst thieves and policemen.
Sentiment:
"""

outputs = pipeline(prompt, max_new_tokens=10)
for output in outputs:
    print(f"Result: {output['generated_text']}")
Result: Classify the text into neutral, negative or positive. 
Text: This movie is definitely one of my favorite movies of its kind. The interaction between respectable and morally strong characters is an ode to chivalry and the honor code amongst thieves and policemen.
Sentiment:
Positive

挑战在于设计能够产生您期望结果的提示,因为语言是如此令人难以置信的细致入微和富有表现力。

本指南涵盖了提示工程的最佳实践、技术和示例,以了解如何解决语言和推理任务。

最佳实践

  1. 尝试选择最新的模型以获得最佳性能。请记住,LLM 可以分为两个变体:基础模型指令调优模型(或聊天模型)。

    基础模型擅长根据初始提示完成文本,但它们不太擅长遵循指令。指令调优模型是在指令或对话数据上专门训练的基础模型版本。这使得指令调优模型更适合提示。

    现代 LLM 通常是仅解码器模型,但也有些编码器-解码器 LLM,例如 Flan-T5BART,可以用于提示。对于编码器-解码器模型,请确保将管道任务标识符设置为 text2text-generation 而不是 text-generation

  2. 从简短而简单的提示开始,并在其基础上迭代以获得更好的结果。

  3. 将指令放在提示的开头或结尾。对于较长的提示,模型可能会应用优化以防止注意力呈二次方扩展,这将更多强调放在提示的开头和结尾。

  4. 将指令与感兴趣的文本明确分开。

  5. 具体且描述性地说明任务和期望的输出,例如,包括其格式、长度、样式和语言。避免使用模棱两可的描述和说明。

  6. 指令应侧重于“做什么”而不是“不做什么”。

  7. 通过写出第一个词甚至第一个句子来引导模型生成正确的输出。

  8. 尝试其他技术,如 少样本思维链,以提高结果。

  9. 使用不同的模型测试您的提示,以评估其稳健性。

  10. 对您的提示性能进行版本控制和跟踪。

技术

单独编写好的提示(也称为零样本提示)可能不足以获得您想要的结果。您可能需要尝试一些提示技术才能获得最佳性能。

本节介绍一些提示技术。

少样本

少样本提示通过包含模型应根据输入生成的内容的具体示例来提高准确性和性能。显式示例使模型更好地理解任务和您正在寻找的输出格式。尝试使用不同数量的示例(2、4、8 等)来查看它如何影响性能。

下面的示例为模型提供了一个示例(单样本)的输出格式(MM/DD/YYYY 格式的日期),模型应返回该格式。

from transformers import pipeline
import torch

pipeline = pipeline(model="mistralai/Mistral-7B-Instruct-v0.1", torch_dtype=torch.bfloat16, device_map="auto")
prompt = """Text: The first human went into space and orbited the Earth on April 12, 1961.
Date: 04/12/1961
Text: The first-ever televised presidential debate in the United States took place on September 28, 1960, between presidential candidates John F. Kennedy and Richard Nixon. 
Date:"""

outputs = pipeline(prompt, max_new_tokens=12, do_sample=True, top_k=10)
for output in outputs:
    print(f"Result: {output['generated_text']}")
Result: Text: The first human went into space and orbited the Earth on April 12, 1961.
Date: 04/12/1961
Text: The first-ever televised presidential debate in the United States took place on September 28, 1960, between presidential candidates John F. Kennedy and Richard Nixon. 
Date: 09/28/1960

少样本提示的缺点是您需要创建更长的提示,这会增加计算量和延迟。提示长度也有限制。最后,模型可能会从您的示例中学习到意外的模式,并且在复杂的推理任务中效果不佳。

思维链

思维链 (CoT) 通过提供一系列提示来帮助模型更彻底地“思考”某个主题,从而有效地生成更连贯和更合理的输出。

下面的示例为模型提供了多个提示,以完成中间推理步骤。

from transformers import pipeline
import torch

pipeline = pipeline(model="mistralai/Mistral-7B-Instruct-v0.1", torch_dtype=torch.bfloat16, device_map="auto")
prompt = """Let's go through this step-by-step:
1. You start with 15 muffins.
2. You eat 2 muffins, leaving you with 13 muffins.
3. You give 5 muffins to your neighbor, leaving you with 8 muffins.
4. Your partner buys 6 more muffins, bringing the total number of muffins to 14.
5. Your partner eats 2 muffins, leaving you with 12 muffins.
If you eat 6 muffins, how many are left?"""

outputs = pipeline(prompt, max_new_tokens=20, do_sample=True, top_k=10)
for output in outputs:
    print(f"Result: {output['generated_text']}")
Result: Let's go through this step-by-step:
1. You start with 15 muffins.
2. You eat 2 muffins, leaving you with 13 muffins.
3. You give 5 muffins to your neighbor, leaving you with 8 muffins.
4. Your partner buys 6 more muffins, bringing the total number of muffins to 14.
5. Your partner eats 2 muffins, leaving you with 12 muffins.
If you eat 6 muffins, how many are left?
Answer: 6

少样本 提示类似,CoT 的缺点是它需要更多的工作来设计一系列提示,以帮助模型推理完成复杂的任务,并且提示长度会增加延迟。

微调

虽然提示是使用 LLM 的强大方法,但在某些情况下,微调模型甚至微调模型效果更好。

以下是一些示例场景,其中微调模型很有意义。

  • 您的领域与 LLM 预训练的领域截然不同,并且广泛的提示未能产生您想要的结果。
  • 您的模型需要在低资源语言中良好运行。
  • 您的模型需要使用具有严格监管要求的敏感数据进行训练。
  • 由于成本、隐私、基础设施或其他限制,您正在使用小型模型。

在所有这些场景中,请确保您有足够大的特定领域数据集来训练您的模型,有足够的时间和资源,并且微调的成本是值得的。否则,您最好尝试优化您的提示。

示例

以下示例演示了针对不同任务提示 LLM。

命名实体识别
翻译
摘要
问答
from transformers import pipeline
import torch

pipeline = pipeline(model="mistralai/Mistral-7B-Instruct-v0.1", torch_dtype=torch.bfloat16, device_map="auto")
prompt = """Return a list of named entities in the text.
Text: The company was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf in New York City, originally as a company that developed a chatbot app targeted at teenagers.
Named entities:
"""

outputs = pipeline(prompt, max_new_tokens=50, return_full_text=False)
for output in outputs:
    print(f"Result: {output['generated_text']}")
Result:  [Clément Delangue, Julien Chaumond, Thomas Wolf, company, New York City, chatbot app, teenagers]
< > 更新 在 GitHub 上