【LangChain系列 12】Prompt模版——序列化

本文速读:

  • PromptTemplate

  • FewShotPromptTemplate

通常prompt以文件形式存储比python代码更好,一方面可以更容易共享、存储。本文将介绍在LangChain中如何对prompt以不同的方式序列化。

一般来说,对于序列化有以下两个设计原则:

1. 支持JSON和YAML格式。对于prompt,我们希望序列化后是可读的,使用这两种格式,我们可以直接打开文件就可以查看里面的内容;对于其它内容,比如示例可以采用其它序列化方式。

2. 可以将指定内容序列化在一个文件中,比如说将prompt序列化在一个文件,将示例序列化在另一个文件;当然有时候将所有内容序列在同一个文件更合理,所以LangChain对于这两种方式都支持。

01 PromptTemplate


下面介绍三种方式序列化的PromptTemplate是如何加载的:

  • YAML

  • JSON

YAML文件

1. 查看文件内容

cat simple_prompt.yaml

输出内容是:

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

2. 加载该文件

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

执行代码,输出结果:

Tell me a funny joke about chickens.

Json文件

1. 查看文件内容

cat simple_prompt.json

输出内容是:

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

2. 加载文件

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

执行代码,输出结果:

Tell me a funny joke about chickens.

带输出解析器的prompt模版

上面两个示例是简单的PromptTemplate,只包含3个最基本的属性;对于某个prompt模版,它可能还需要一些其它属性,比如说输出解析器。LangChain对这种情况也是支持的。

1. 查看包含输出解析器的配置文件

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"
}

​​​2. 加载文件

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'}

prompt模版和配置文件在不同文件

上述两种方式的prompt模版和配置属性都是在同一个文件中,同时也支持两者在不同的文件的情况,模版单独存在一个文件中,配置文件是另一个文件,然后在配置文件中引用它。

1. 查看模版文件内容

cat simple_template.txt

输出内容为:

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

2. 查看配置文件

cat simple_prompt_with_template_file.json

输出内容为:

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

此时配置文件通过template_path指定模版路径。

3. 加载文件​​​​​​​

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

执行代码,输出结果:

Tell me a funny joke about chickens.

02 FewShotPromptTemplate


对于FewShotPromptTemplate同样也是可以序列化的 ,它也支持两种文件格式:

  • YAML

  • JSON

下面先准备两种文件格式的样例:

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配置文件在一个文件中,样例在另一个文件中(examples.json)。

1. 查看配置文件

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:"
} 

2. 加载文件​​​​​​​

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:

同样样例文件也可以是examples.yaml。

1. 查看配置文件,样例在另一个文件中(examples.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:"

2. 加载文件​​​​​​​

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配置文件在一个文件中,样例在另一个文件中(examples.json)。 

1. 查看配置文件

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:"
} 

3. 加载文件​​​​​​​

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:

同样地,样例文件也可以是examples.yarml。

样例prompt在单独文件中

上面的例子,样例prompt (example_prompt属性)直接写在配置文件中,但是有时候 样例prompt 可能在单独的文件中,对于这种情况,LangChain也是支持的,只需要把example_prompt换成example_prompt_path即可。

1. 查看样例prompt

cat example_prompt.json

输出内容:​​​​​​​

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

2. 查看配置文件

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:"
}

3. 加载文件​​​​​​​

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:

样例和配置文件在同一个文件

上述介绍的方式中,样例都是单独的一个文件,和配置文件是分开的。LangChain同时也支持样例和配置在同一个文件中。

1. 查看文件

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:"
  }

2. 加载文件​​​​​​​

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和FewShotPromptTempalte两种模版的序列化,它们都支持JSON、YAML两种格式;同时对于 示例和示例prompt,既可以包含在配置文件中,也可以在独立的一个文件中。

公众号:大白爱爬山

  • 23
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值