Langchain文档 序列化

579 阅读3分钟

LangChain Prompt 序列化示例

在 LangChain 中,我们可以将 Prompt 存储为文件,以便于共享、存储和版本控制。本文档将介绍如何在 LangChain 中进行 Prompt 的序列化,包括各种 Prompt 类型和不同的序列化选项。

在设计上,我们遵循以下原则:

  1. 支持 JSON 和 YAML 两种序列化格式。我们希望支持以人类可读的方式存储在磁盘上,而 YAML 和 JSON 是两种最常用的格式。
  2. 支持将所有组件(模板、示例等)存储在单个文件中,或者将它们分开存储在不同的文件中并进行引用。对于某些情况,将所有内容存储在单个文件中可能更合适,但对于其他情况,将某些组件(例如长模板、大型示例、可重复使用的组件)分开存储可能更好。LangChain 支持两种方式。
  3. 提供了一个统一的入口来从磁盘加载 Prompt,方便加载任何类型的 Prompt。

加载 Prompt

所有的 Prompt 都可以通过 load_prompt 函数进行加载。

from langchain.prompts import load_prompt

prompt = load_prompt("prompt.yaml")
print(prompt.format(...))

PromptTemplate

下面是加载 PromptTemplate 的示例。

从 YAML 文件加载

下面是从 YAML 文件加载 PromptTemplate 的示例:

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

输出:

Tell me a funny joke about chickens.

从 JSON 文件加载

下面是从 JSON 文件加载 PromptTemplate 的示例:

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

输出:

Tell me a funny joke about chickens.

从文件中加载模板

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

模板文件 simple_template.txt

Tell me a {adjective} joke about {content}.

配置文件 prompt_with_template_file.json

{
  "_type": "prompt",
  "input_variables": ["adjective", "content"],
  "template_path": "simple_template.txt"
}
prompt = load_prompt("prompt_with_template_file.json")
print(prompt.format(adjective="funny", content="chickens"))

输出:

Tell me a funny joke about chickens.

FewShotPromptTemplate

下面是加载 FewShotPromptTemplate 的示例。

示例

下面是示例以 JSON 格式存储的示例:

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

同样的示例以 YAML 格式存储如下:

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

从 YAML 文件加载

下面是从 YAML 文件加载 FewShotPromptTemplate 的示例:

_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 文件加载:

_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 文件加载

下面是从 JSON 文件加载 FewShotPromptTemplate 的示例:

{
  "_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:

在配置文件中直接包含示例

下面是直接在配置文件中引用示例的示例:

{
  "_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

示例模板文件 example_prompt.json

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

配置文件 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 with OutputParser

下面是加载带有 OutputParser 的 PromptTemplate 的示例。

配置文件 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'
}

希望这些示例能够帮助你理解如何在 LangChain 中进行 Prompt 序列化。