赞
踩
分类目录:《大模型从入门到应用》总目录
LangChain系列文章:
将提示信息存储为文件而不是Python代码通常更好。这样可以方便共享、存储和版本控制提示信息。本文介绍了如何在LangChain中进行提示信息的序列化,包括不同类型的提示信息和不同的序列化选项。
在高层次上,序列化遵循以下设计原则:
LangChain还提供了一个单一的入口点,用于从磁盘加载提示信息,从而轻松加载任何类型的提示信息。
# All prompts are loaded through the `load_prompt` function.
from langchain.prompts import load_prompt
本部分介绍了加载PromptTemplate
下面是从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加载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
Tell me a {adjective} joke about {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="funny", content="chickens"))
输出:
Tell me a funny joke about chickens.
本部分介绍加载FewShotPromptTemplate
的示例。下面是SON本地文件的示例:
本地文件:
!cat examples.json
[
{"input": "happy", "output": "sad"},
{"input": "tall", "output": "short"}
]
以下是将相同示例存储为YAML格式的示例:
!cat examples.yaml
- input: happy
output: sad
- input: tall
output: short
这是一个从YAML加载Few-Shot Example的示例。
本地文件:
!cat few_shot_prompt.yaml
_type: few_shot
input_variables:
["adjective"]
prefix:
Write antonyms for the following words.
example_prompt:
_type: prompt
input_variables:
["input", "output"]
template:
"Input: {input}\nOutput: {output}"
examples:
examples.json
suffix:
"Input: {adjective}\nOutput:"
输入:
prompt = load_prompt("few_shot_prompt.yaml")
print(prompt.format(adjective="funny"))
输出:
Write antonyms for the following words.
Input: happy
Output: sad
Input: tall
Output: short
Input: funny
Output:
如果我们从YAML文件中加载示例,同样的方法也适用。
本地文件:
!cat few_shot_prompt_yaml_examples.yaml
_type: few_shot
input_variables:
["adjective"]
prefix:
Write antonyms for the following words.
example_prompt:
_type: prompt
input_variables:
["input", "output"]
template:
"Input: {input}\nOutput: {output}"
examples:
examples.yaml
suffix:
"Input: {adjective}\nOutput:"
输入:
prompt = load_prompt("few_shot_prompt_yaml_examples.yaml")
print(prompt.format(adjective="funny"))
Write antonyms for the following words.
输出:
Input: happy
Output: sad
Input: tall
Output: short
Input: funny
Output:
这是一个从JSON加载Few-Shot Example的示例。
本地文件:
!cat few_shot_prompt.json
{
"_type": "few_shot",
"input_variables": ["adjective"],
"prefix": "Write antonyms for the following words.",
"example_prompt": {
"_type": "prompt",
"input_variables": ["input", "output"],
"template": "Input: {input}\nOutput: {output}"
},
"examples": "examples.json",
"suffix": "Input: {adjective}\nOutput:"
}
输入:
prompt = load_prompt("few_shot_prompt.json")
print(prompt.format(adjective="funny"))
输出:
Write antonyms for the following words.
Input: happy
Output: sad
Input: tall
Output: short
Input: funny
Output:
这是一个直接在配置中引用示例的示例。
本地文件:
!cat few_shot_prompt_examples_in.json
{
"_type": "few_shot",
"input_variables": ["adjective"],
"prefix": "Write antonyms for the following words.",
"example_prompt": {
"_type": "prompt",
"input_variables": ["input", "output"],
"template": "Input: {input}\nOutput: {output}"
},
"examples": [
{"input": "happy", "output": "sad"},
{"input": "tall", "output": "short"}
],
"suffix": "Input: {adjective}\nOutput:"
}
输入:
prompt = load_prompt("few_shot_prompt_examples_in.json")
print(prompt.format(adjective="funny"))
输出:
Write antonyms for the following words.
Input: happy
Output: sad
Input: tall
Output: short
Input: funny
Output:
这是一个从单独的文件加载用于格式化示例的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:
这是一个从文件加载PromptTemplate
和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'}
参考文献:
[1] LangChain官方网站:https://www.langchain.com/
[2] LangChain
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。