Skip to main content

序列化 Serialization

通常情况下,将提示存储为文件而不是Python代码更为可取。这样可以方便地共享、存储和版本控制提示。本笔记本介绍了如何在LangChain中实现这一点,详细介绍了各种类型的提示和不同的序列化选项。

从高层次上看,序列化应用了以下设计原则:

  1. 支持JSON和YAML两种格式。我们希望支持在磁盘上可读的序列化方法,而YAML和JSON是其中最流行的两种方法。请注意,此规则适用于提示。对于其他资产,如示例,可能支持不同的序列化方法。

  2. 我们支持在一个文件中指定所有内容,或者将不同的组件(模板、示例等)存储在不同的文件中并进行引用。对于某些情况,将所有内容存储在一个文件中是最合理的,但对于其他情况,将一些资产拆分开来(长模板、大示例、可重用组件)更可取。LangChain同时支持这两种方式。

此外,还有一个单一的入口点可以从磁盘加载提示,使得加载任何类型的提示变得容易。

# 所有提示都通过`load_prompt`函数加载。  
from langchain.prompts import load_prompt

API参考:

PromptTemplate​

本节介绍了加载PromptTemplate的示例。

从YAML加载​

以下是从YAML加载PromptTemplate的示例。

cat simple_prompt.yaml

_type: prompt
input_variables:
- "adjective"
- "content"
template:
Tell me a {adjective} joke about {content}.
prompt = load_prompt("simple_prompt.yaml")
print(prompt.format(adjective="funny", content="chickens"))

输出结果为:

Tell me a funny joke about chickens.

从JSON加载

以下是从JSON加载PromptTemplate的示例。

cat simple_prompt.json

{
"_type": "prompt",
"input_variables": ["adjective", "content"],
"template": "Tell me a {adjective} joke about {content}."
}

prompt = load_prompt("simple_prompt.json")
print(prompt.format(adjective="funny", content="chickens"))

输出结果为:Tell me a funny joke about chickens.

从文件加载模板

以下是将模板存储在单独文件中,并在配置中引用的示例。请注意,键从template更改为template_path

cat simple_template.txt

告诉我一个关于{adjective}的笑话,内容是{content}。

cat simple_prompt_with_template_file.json

{
"_type": "prompt",
"input_variables": ["adjective", "content"],
"template_path": "simple_template.txt"
}

prompt = load_prompt("simple_prompt_with_template_file.json")
print(prompt.format(adjective="有趣的", content="鸡"))

告诉我一个有趣的笑话,内容是鸡。

FewShotPromptTemplate

本节介绍了加载few shot prompt模板的示例。

示例

以下是一个示例,展示了存储为JSON的示例的样子。

cat examples.json

[
{"input": "happy", "output": "sad"},
{"input": "tall", "output": "short"}
]

这是将相同示例存储为YAML的样子。

cat examples.yaml

- input: happy
output: sad
- input: tall
output: short

从YAML加载

以下是从YAML文件中加载few shot示例的示例。

cat few_shot_prompt.yaml

_type: few_shot
input_variables:
- "adjective"
prefix: "为以下单词写出反义词。"
example_prompt:
_type: prompt
input_variables:
- "input"
- "output"
template: "输入:{input}\n输出:{output}"
examples: examples.json
suffix: "输入:{adjective}\n输出:"
prompt = load_prompt("few_shot_prompt.yaml")
print(prompt.format(adjective="funny"))

输出结果:

为以下单词写出反义词。

输入:happy
输出:sad

输入:tall
输出:short

输入:funny
输出:

如果从YAML文件中加载示例,同样的方法也适用。

cat few_shot_prompt_yaml_examples.yaml

_type: few_shot
input_variables:
- "adjective"
prefix: "为以下单词写出反义词。"
example_prompt:
_type: prompt
input_variables:
- "input"
- "output"
template: "输入:{input}\n输出:{output}"
examples: examples.yaml
suffix: "输入:{adjective}\n输出:"
prompt = load_prompt("few_shot_prompt_yaml_examples.yaml")
print(prompt.format(adjective="funny"))

输出结果:

为以下单词写出反义词。

输入:happy
输出:sad

输入:tall
输出:short

输入:funny
输出:

从JSON文件中加载

以下是从JSON加载few shot示例的示例。

cat few_shot_prompt.json

{
"_type": "few_shot",
"input_variables": ["adjective"],
"prefix": "写出以下单词的反义词。",
"example_prompt": {
"_type": "prompt",
"input_variables": ["input", "output"],
"template": "输入:{input}\n输出:{output}"
},
"examples": "examples.json",
"suffix": "输入:{adjective}\n输出:"
}
prompt = load_prompt("few_shot_prompt.json")
print(prompt.format(adjective="funny"))

输出结果:

写出以下单词的反义词。

输入:happy
输出:sad

输入:tall
输出:short

输入:funny
输出:

配置中的示例

以下是直接在配置中引用示例的示例。

cat few_shot_prompt_examples_in.json

{
"_type": "few_shot",
"input_variables": ["adjective"],
"prefix": "写出以下单词的反义词。",
"example_prompt": {
"_type": "prompt",
"input_variables": ["input", "output"],
"template": "输入:{input}\n输出:{output}"
},
"examples": [
{"input": "happy", "output": "sad"},
{"input": "tall", "output": "short"}
],
"suffix": "输入:{adjective}\n输出:"
}
prompt = load_prompt("few_shot_prompt_examples_in.json")
print(prompt.format(adjective="funny"))

输出结果:

写出以下单词的反义词。

输入:happy
输出:sad

输入:tall
输出:short

输入:funny
输出:

从文件中加载示例提示

以下是加载PromptTemplate的示例,用于格式化来自单独文件的示例。请注意,键从example_prompt更改为example_prompt_path

cat example_prompt.json

{
"_type": "prompt",
"input_variables": ["input", "output"],
"template": "Input: {input}\nOutput: {output}"
}
cat few_shot_prompt_example_prompt.json

{
"_type": "few_shot",
"input_variables": ["adjective"],
"prefix": "Write antonyms for the following words.",
"example_prompt_path": "example_prompt.json",
"examples": "examples.json",
"suffix": "Input: {adjective}\nOutput:"
}
prompt = load_prompt("few_shot_prompt_example_prompt.json")  
print(prompt.format(adjective="funny"))

输出结果为:

Write antonyms for the following words.

Input: happy
Output: sad

Input: tall
Output: short

Input: funny
Output:

PromptTempalte with OutputParser

这是从文件中加载Prompt和OutputParser的示例。

cat prompt_with_output_parser.json

{
"input_variables": [
"question",
"student_answer"
],
"output_parser": {
"regex": "(.*?)\\nScore: (.*)",
"output_keys": [
"answer",
"score"
],
"default_output_key": null,
"_type": "regex_parser"
},
"partial_variables": {},
"template": "Given the following question and student answer, provide a correct answer and score the student answer.\nQuestion: {question}\nStudent Answer: {student_answer}\nCorrect Answer:",
"template_format": "f-string",
"validate_template": true,
"_type": "prompt"
}
prompt = load_prompt("prompt_with_output_parser.json")  
prompt.output_parser.parse(
"George Washington was born in 1732 and died in 1799.\nScore: 1/2"
)

输出结果为:

{'answer': 'George Washington was born in 1732 and died in 1799.',  
'score': '1/2'}