LangChain:Prompt Templates介绍及应用


❤️觉得内容不错的话,欢迎点赞收藏加关注,后续会继续输入更多优质内容❤️

有问题欢迎大家加关注私戳或者评论(包括但不限于NLP算法相关,linux学习相关,读研读博相关......)

(封面图由文心一格生成)

LangChain:Prompt Templates介绍及应用

在自然语言生成任务中,生成高质量的文本是非常困难的,尤其是当需要针对不同的主题、情境、问题或任务进行文本生成时,需要花费大量的时间和精力去设计、调试和优化模型,而这种方式并不是高效的解决方案。因此,Prompt Templates技术应运而生,可以大大降低模型设计、调试和优化的成本。

Prompt Templates是一种可复制的生成Prompt的方式,它包含一个文本字符串,可以接受来自终端用户的一组参数并生成Prompt。Prompt Templates可以包含指令、少量示例和一个向语言模型提出的问题。我们可以使用Prompt Templates技术来指导语言模型生成更高质量的文本,从而更好地完成我们的任务。

在这篇博客中,我们将学习:

什么是Prompt Templates以及为什么需要它
如何创建Prompt Templates
如何传递few-shot examples给Prompt Templates
如何为Prompt Templates选择examples

什么是Prompt Templates?

Prompt Templates是一种可复制的生成Prompt的方式,它包含一个文本字符串,可以接受来自终端用户的一组参数并生成Prompt。Prompt Templates可以包含指令、少量示例和一个向语言模型提出的问题。Prompt Templates可以帮助我们指导语言模型生成更高质量的文本,从而更好地完成我们的任务。

Prompt Templates可以包含以下内容:

Prompt Templates可能包含:

  • 对语言模型的指令
  • 一组few-shot examples,以帮助语言模型生成更好的响应
  • 对语言模型的问题

下面的代码段包含Prompt Template的一个示例:

from langchain import PromptTemplate
from langchain import PromptTemplate


template = """
I want you to act as a naming consultant for new companies.

Here are some examples of good company names:

- search engine, Google
- social media, Facebook
- video sharing, YouTube

The name should be short, catchy and easy to remember.

What is a good name for a company that makes {product}?
"""

prompt = PromptTemplate(
    input_variables=["product"],
    template=template,
)

创建Prompt Templates

你可以使用PromptTemplate类创建简单的硬编码提示。Prompt Templates可以采用任何数量的输入变量,并且可以进行格式化以生成提示。

from langchain import PromptTemplate

# 没有输入变量的示例prompt
no_input_prompt = PromptTemplate(input_variables=[], template="给我讲个笑话。")
no_input_prompt.format()
# -> "给我讲个笑话。"

# 一个有一个输入变量的示例prompt
one_input_prompt = PromptTemplate(input_variables=["adjective"], template="告诉我一个{adjective}笑话。")
one_input_prompt.format(adjective="好笑的")
# -> "告诉我一个好笑的笑话。"

# 一个有多个输入变量的示例prompt
multiple_input_prompt = PromptTemplate(
    input_variables=["adjective", "content"], 
    template="告诉我一个{adjective}关于{content}的笑话。"
)
multiple_input_prompt.format(adjective="好笑的", content="小鸡")
# -> "告诉我一个好笑的关于小鸡的笑话。"

从LangChainHub加载Prompt Templates

LangChainHub包含了许多可以通过LangChain直接加载的Prompt Templates。

from langchain.prompts import load_prompt

prompt = load_prompt("lc://prompts/conversation/prompt.json")
prompt.format(history="", input="What is 1 + 1?")

传递few-shot examples给Prompt Templates

Few-shot examples是一组可用于帮助语言模型生成更好响应的示例。

要生成具有few-shot examples的prompt,可以使用FewShotPromptTemplate。该类接受一个PromptTemplate和一组few-shot examples。然后,它使用这些few-shot examples格式化prompt模板。

在这个示例中,我们将创建一个用于生成单词反义词的提示。

from langchain import PromptTemplate, FewShotPromptTemplate


# First, create the list of few shot examples.
examples = [
    {"word": "happy", "antonym": "sad"},
    {"word": "tall", "antonym": "short"},
]

# Next, we specify the template to format the examples we have provided.
# We use the `PromptTemplate` class for this.
example_formatter_template = """
Word: {word}
Antonym: {antonym}\n
"""
example_prompt = PromptTemplate(
    input_variables=["word", "antonym"],
    template=example_formatter_template,
)

# Finally, we create the `FewShotPromptTemplate` object.
few_shot_prompt = FewShotPromptTemplate(
    # These are the examples we want to insert into the prompt.
    examples=examples,
    # This is how we want to format the examples when we insert them into the prompt.
    example_prompt=example_prompt,
    # The prefix is some text that goes before the examples in the prompt.
    # Usually, this consists of intructions.
    prefix="Give the antonym of every input",
    # The suffix is some text that goes after the examples in the prompt.
    # Usually, this is where the user input will go
    suffix="Word: {input}\nAntonym:",
    # The input variables are the variables that the overall prompt expects.
    input_variables=["input"],
    # The example_separator is the string we will use to join the prefix, examples, and suffix together with.
    example_separator="\n\n",
)

# We can now generate a prompt using the `format` method.
print(few_shot_prompt.format(input="big"))
# -> Give the antonym of every input
# -> 
# -> Word: happy
# -> Antonym: sad
# ->
# -> Word: tall
# -> Antonym: short
# ->
# -> Word: big
# -> Antonym:

为Prompt Templates选择examples

如果你有大量的示例,则可以使用ExampleSelector来选择最有信息量的一些示例,以帮助你生成更可能产生良好响应的提示。

接下来,我们将使用LengthBasedExampleSelector,根据输入的长度选择示例。当你担心构造的提示将超过上下文窗口的长度时,此方法非常有用。对于较长的输入,它会选择包含较少示例的提示,而对于较短的输入,它会选择包含更多示例。

from langchain.prompts.example_selector import LengthBasedExampleSelector


# These are a lot of examples of a pretend task of creating antonyms.
examples = [
    {"word": "happy", "antonym": "sad"},
    {"word": "tall", "antonym": "short"},
    {"word": "energetic", "antonym": "lethargic"},
    {"word": "sunny", "antonym": "gloomy"},
    {"word": "windy", "antonym": "calm"},
]

# We'll use the `LengthBasedExampleSelector` to select the examples.
example_selector = LengthBasedExampleSelector(
    # These are the examples is has available to choose from.
    examples=examples, 
    # This is the PromptTemplate being used to format the examples.
    example_prompt=example_prompt, 
    # This is the maximum length that the formatted examples should be.
    # Length is measured by the get_text_length function below.
    max_length=25,
)

# We can now use the `example_selector` to create a `FewShotPromptTemplate`.
dynamic_prompt = FewShotPromptTemplate(
    # We provide an ExampleSelector instead of examples.
    example_selector=example_selector,
    example_prompt=example_prompt,
    prefix="Give the antonym of every input",
    suffix="Word: {input}\nAntonym:",
    input_variables=["input"],
    example_separator="\n\n",
)

# We can now generate a prompt using the `format` method.
print(dynamic_prompt.format(input="big"))
# -> Give the antonym of every input
# ->
# -> Word: happy
# -> Antonym: sad
# ->
# -> Word: tall
# -> Antonym: short
# ->
# -> Word: energetic
# -> Antonym: lethargic
# ->
# -> Word: sunny
# -> Antonym: gloomy
# ->
# -> Word: windy
# -> Antonym: calm
# ->
# -> Word: big
# -> Antonym:

相比之下,如果我们提供了一个非常长的输入,则LengthBasedExampleSelector将选择较少的示例包含在提示中。

long_string = "big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else"
print(dynamic_prompt.format(input=long_string))
# -> Give the antonym of every input

# -> Word: happy
# -> Antonym: sad
# ->
# -> Word: big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else
# -> Antonym:

❤️觉得内容不错的话,欢迎点赞收藏加关注,后续会继续输入更多优质内容❤️

有问题欢迎大家加关注私戳或者评论(包括但不限于NLP算法相关,linux学习相关,读研读博相关......)

你可能感兴趣的:(自然语言处理,人工智能,python,开发语言,chatgpt,transformer)