【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,既可以包含在配置文件中,也可以在独立的一个文件中。

相关推荐
Magic-Yuan4 分钟前
泰勒制的崩塌 - 上
人工智能·管理
咚咚王者5 分钟前
人工智能之提示词工程 第七章 行业场景深度落地案例
人工智能
feasibility.5 分钟前
量化:LLM与CV模型的极致压缩艺术
人工智能·科技·llm·边缘计算·量化·cv·压缩
β添砖java6 分钟前
深度学习(15)卷积层
人工智能·深度学习·计算机视觉
β添砖java7 分钟前
深度学习(14)确认GPU
人工智能·深度学习
浔川python社10 分钟前
浔川社团第一次福利数据公布
人工智能·python·deepseek
薛定e的猫咪12 分钟前
强化学习中的OOD检测:从状态异常到分布偏移
论文阅读·人工智能·深度学习
geneculture16 分钟前
《一种智能通信子母机》(申请日 1993.4.7公开号CN1095341A)专利文件的全文汉英双语对照版本+系统点评
人工智能·数据挖掘·哲学与科学统一性·智能通信
树獭非懒17 分钟前
LangChain 不是框架,而是一把瑞士军刀
人工智能·程序员·llm
三行数学24 分钟前
数学周刊第17期(2026年04月27日-05月03日)12小时攻克42年数学难题:人机深度协作正式步入数学研究
人工智能·数学周刊·三行数学