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

原文地址:【LangChain系列 12】Prompt模版------序列化

本文速读:

  • PromptTemplate

  • FewShotPromptTemplate

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

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

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

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

01 PromptTemplate

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

  • YAML

  • JSON

YAML文件

  1. 查看文件内容
bash 复制代码
cat simple_prompt.yaml

输出内容是:

css 复制代码
    _type: prompt
    input_variables:
        ["adjective", "content"]
    template: 
        Tell me a {adjective} joke about {content}.
  1. 加载该文件
ini 复制代码
prompt = load_prompt("simple_prompt.yaml")
print(prompt.format(adjective="funny", content="chickens"))

执行代码,输出结果:

css 复制代码
Tell me a funny joke about chickens.

Json文件

  1. 查看文件内容
bash 复制代码
cat simple_prompt.json

输出内容是:

json 复制代码
    {
        "_type": "prompt",
        "input_variables": ["adjective", "content"],
        "template": "Tell me a {adjective} joke about {content}."
    }
  1. 加载文件
ini 复制代码
prompt = load_prompt("simple_prompt.json")
print(prompt.format(adjective="funny", content="chickens"))

执行代码,输出结果:

css 复制代码
Tell me a funny joke about chickens.

**

带输出解析器的****prompt模版

**

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

  1. 查看包含输出解析器的配置文件
bash 复制代码
cat prompt_with_output_parser.json

输出内容:

swift 复制代码
    {
        "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"
    }
  1. 加载文件
ini 复制代码
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"
)

解析后的内容:

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

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

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

  1. 查看模版文件内容
bash 复制代码
cat simple_template.txt

输出内容为:

css 复制代码
Tell me a {adjective} joke about {content}.
  1. 查看配置文件
bash 复制代码
cat simple_prompt_with_template_file.json

输出内容为:

json 复制代码
    {
        "_type": "prompt",
        "input_variables": ["adjective", "content"],
        "template_path": "simple_template.txt"
    }

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

  1. 加载文件
ini 复制代码
prompt = load_prompt("simple_prompt_with_template_file.json")
print(prompt.format(adjective="funny", content="chickens"))

执行代码,输出结果:

css 复制代码
Tell me a funny joke about chickens.

02 FewShotPromptTemplate****

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

  • YAML

  • JSON

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

json格式示例:

bash 复制代码
cat examples.json

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

yaml格式示例:

lua 复制代码
cat examples.yaml

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

YAML文件

YAML配置文件在一个文件中,样例在另一个文件中(examples.json)。

  1. 查看配置文件
bash 复制代码
cat few_shot_prompt.yaml

输出内容:

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:"
  1. 加载文件
ini 复制代码
prompt = load_prompt("few_shot_prompt.yaml")
print(prompt.format(adjective="funny"))

执行代码,输出结果:

yaml 复制代码
    Write antonyms for the following words.
    
    Input: happy
    Output: sad
    
    Input: tall
    Output: short
    
    Input: funny
    Output:

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

  1. 查看配置文件,样例在另一个文件中(examples.yaml)
bash 复制代码
cat few_shot_prompt_yaml_examples.yaml

输出内容:

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:"
  1. 加载文件
ini 复制代码
prompt = load_prompt("few_shot_prompt_yaml_examples.yaml")
print(prompt.format(adjective="funny"))

执行代码,输出结果:

yaml 复制代码
    Write antonyms for the following words.
    
    Input: happy
    Output: sad
    
    Input: tall
    Output: short
    
    Input: funny
    Output:

JSON文件

JSON配置文件在一个文件中,样例在另一个文件中(examples.json)。

  1. 查看配置文件
bash 复制代码
cat few_shot_prompt.json

输出内容:

swift 复制代码
    {
        "_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:"
    }   
  1. 加载文件
ini 复制代码
prompt = load_prompt("few_shot_prompt.json")
print(prompt.format(adjective="funny"))

执行代码,输出结果:

yaml 复制代码
    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
bash 复制代码
cat example_prompt.json

输出内容:

swift 复制代码
    {
        "_type": "prompt",
        "input_variables": ["input", "output"],
        "template": "Input: {input}\nOutput: {output}" 
    }
  1. 查看配置文件
bash 复制代码
cat few_shot_prompt_example_prompt.json

输出内容:

swift 复制代码
    {
        "_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:"
    }   
  1. 加载文件
ini 复制代码
prompt = load_prompt("few_shot_prompt_example_prompt.json")
print(prompt.format(adjective="funny"))

执行代码,输出结果:

yaml 复制代码
    Write antonyms for the following words.
    
    Input: happy
    Output: sad
    
    Input: tall
    Output: short
    
    Input: funny
    Output:

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

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

  1. 查看文件
bash 复制代码
cat few_shot_prompt_examples_in.json

输出内容:

css 复制代码
    {
        "_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:"
    }   
  1. 加载文件
ini 复制代码
prompt = load_prompt("few_shot_prompt_examples_in.json")
print(prompt.format(adjective="funny"))

执行代码,输出结果:

yaml 复制代码
    Write antonyms for the following words.
    
    Input: happy
    Output: sad
    
    Input: tall
    Output: short
    
    Input: funny
    Output:

本文小结

本文主要介绍了PromptTemplate和FewShotPromptTempalte两种模版的序列化,它们都支持JSON、YAML两种格式;同时对于 示例和示例prompt,既可以包含在配置文件中,也可以在独立的一个文件中。

相关推荐
eihh233332 分钟前
Prompt管理技巧
人工智能·prompt
kyle~9 分钟前
大模型微调(Fine-tuning)概览
人工智能
小草cys14 分钟前
先理解软件工程,再谈AI辅助研发
人工智能·软件工程
亚马逊云开发者1 小时前
使用 Amazon Bedrock Agents 加速生物标志物的分析和发现
人工智能
Baihai_IDP1 小时前
AI 智能体到底应该如何构建?分享 Github 上收获 4k stars 的 12 条原则
人工智能·后端·llm
Leo Chaw1 小时前
21 - GAM模块
人工智能·pytorch·深度学习
Qforepost1 小时前
量子加速器切入 AI 底层架构!能源焦虑时代,ORCA 正在改写数据中心的计算逻辑
人工智能·量子计算·量子
这张生成的图像能检测吗1 小时前
生成对抗网络(GANs)入门介绍指南:让AI学会“创造“的魔法(二)【深入版】
人工智能·pytorch·深度学习·神经网络·算法·生成对抗网络·计算机视觉
扉间7983 小时前
Transformer 核心概念转化为夏日生活类比
人工智能·transformer
要努力啊啊啊6 小时前
YOLOv1 技术详解:正负样本划分与置信度设计
人工智能·深度学习·yolo·计算机视觉·目标跟踪