logo
2

Langchain进阶之prompt template的使用

引言:langchain一个很好的功能就是prompt template,可以帮助我们针对不同情况的同类型问题简化prompt设计。本文将介绍了什么是 prompt template 以及为什么需要使用它,如何创建 prompt template,如何在 prompt template 中使用 few shot examples。

Prompt template 是一种可复制的生成提示的方法。

它包含一个文本字符串(“template”),可以从最终用户处获取一组参数并生成一个提示。
Prompt template 可以包含以下内容:
  • 对语言模型的指令一组 few shot examples,可以帮助语言模型生成更好的响应
  • 下面是一个 prompt template 的示例:
  
  
  
  
  
  
from langchain import PromptTemplate
template = """
I want you to act as a naming consultant for new companies.
What is a good name for a company that makes {product}?
"""
prompt = PromptTemplate(
input_variables=["product"],
template=template,
)
prompt.format(product="colorful socks")
# -> I want you to act as a naming consultant for new companies.
# -> What is a good name for a company that makes colorful socks?
要创建 prompt template,可以使用 PromptTemplate 类创建一个简单的硬编码提示。Prompt template 可以接受任意数量的输入变量,并可以格式化以生成一个 prompt。
  
  
  
  
  
  
from langchain import PromptTemplate
# 没有输入变量的示例 prompt
no_input_prompt = PromptTemplate(input_variables=[], template="Tell me a joke.")
no_input_prompt.format()
# -> "Tell me a joke."
# 有一个输入变量的示例 prompt
one_input_prompt = PromptTemplate(input_variables=["adjective"], template="Tell me a {adjective} joke.")
one_input_prompt.format(adjective="funny")
# -> "Tell me a funny joke."
# 有多个输入变量的示例 prompt
multiple_input_prompt = PromptTemplate(
input_variables=["adjective", "content"],
template="Tell me a {adjective} joke about {content}."
)
multiple_input_prompt.format(adjective="funny", content="chickens")
# -> "Tell me a funny joke about chickens."
如果不想手动指定 input_variables,则还可以使用 from_template 类方法创建 PromptTemplate。LangChain 会根据传递的模板自动推断 input_variables。
  
  
  
  
  
  
template = "Tell me a {adjective} joke about {content}."
prompt_template = PromptTemplate.from_template(template)
prompt_template.input_variables
# -> ['adjective', 'content']
prompt_template.format(adjective="funny", content="chickens")
# -> Tell me a funny joke about chickens.
默认情况下,PromptTemplate 将通过检查 input_variables 是否与模板定义的变量匹配来验证模板字符串。可以将 validate_template 设置为 False 来禁用此行为。
  
  
  
  
  
  
template = "I am learning langchain because {reason}."
prompt_template = PromptTemplate(template=template,
input_variables=["reason", "foo"]) # 报错,因为有额外的变量
prompt_template = PromptTemplate(template=template,
input_variables=["reason", "foo"],
validate_template=False) # 无误

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

要生成带有 few shot examples 的 prompt,您可以使用 FewShotPromptTemplate。该类接受 PromptTemplate 和 few shot examples 列表。然后,它使用 few shot examples 格式化 prompt template。
下面是一个以生成单词反义词为例的 PromptTemplate 的使用示例:
  
  
  
  
  
  
from langchain import PromptTemplate, FewShotPromptTemplate
# 首先,创建 few shot examples 列表。
examples = [
{"word": "happy", "antonym": "sad"},
{"word": "tall", "antonym": "short"},
]
# 接下来,我们指定模板以格式化我们提供的例子。
# 我们使用 PromptTemplate 类进行此操作。
example_formatter_template = """
Word: {word}
Antonym: {antonym}\n
"""
example_prompt = PromptTemplate(
input_variables=["word", "antonym"],
template=example_formatter_template,
)
# 最后,我们创建 FewShotPromptTemplate 对象。
few_shot_prompt = FewShotPromptTemplate(
# 这些是我们要插入 prompt 的例子。
examples=examples,
# 插入 prompt 时如何将它们格式化。
example_prompt=example_prompt,
# 前缀是在 prompt 中出现在例子之前的某个文本。
# 通常,它由说明组成。
prefix="Give the antonym of every input",
# 后缀是同样会出现在 prompt 中例子之后的某个文本。
# 通常,它是用户输入的地方。
suffix="Word: {input}\nAntonym:",
# input_variables 是总提示希望的变量。
input_variables=["input"],
# example_separator 是我们将前缀、示例和后缀连接在一起的字符串。
example_separator="\n\n",
)
# 我们现在可以使用 format 方法来生成 prompt。
print(few_shot_prompt.format(input="big"))
总结 :本文介绍了 Prompt Template 和Few shot examples 的作用和使用方法,通过它们能够让llm生成更好的结果。
本文为稀土掘金博主「张冶国zyg」的原创文章
如有侵权,请联系千帆社区进行删除
评论
用户头像