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

相关推荐
董厂长3 小时前
langchain :记忆组件混淆概念澄清 & 创建Conversational ReAct后显示指定 记忆组件
人工智能·深度学习·langchain·llm
G皮T6 小时前
【人工智能】ChatGPT、DeepSeek-R1、DeepSeek-V3 辨析
人工智能·chatgpt·llm·大语言模型·deepseek·deepseek-v3·deepseek-r1
九年义务漏网鲨鱼7 小时前
【大模型学习 | MINIGPT-4原理】
人工智能·深度学习·学习·语言模型·多模态
元宇宙时间7 小时前
Playfun即将开启大型Web3线上活动,打造沉浸式GameFi体验生态
人工智能·去中心化·区块链
开发者工具分享7 小时前
文本音频违规识别工具排行榜(12选)
人工智能·音视频
产品经理独孤虾7 小时前
人工智能大模型如何助力电商产品经理打造高效的商品工业属性画像
人工智能·机器学习·ai·大模型·产品经理·商品画像·商品工业属性
老任与码7 小时前
Spring AI Alibaba(1)——基本使用
java·人工智能·后端·springaialibaba
蹦蹦跳跳真可爱5898 小时前
Python----OpenCV(图像増强——高通滤波(索贝尔算子、沙尔算子、拉普拉斯算子),图像浮雕与特效处理)
人工智能·python·opencv·计算机视觉
雷羿 LexChien8 小时前
从 Prompt 管理到人格稳定:探索 Cursor AI 编辑器如何赋能 Prompt 工程与人格风格设计(上)
人工智能·python·llm·编辑器·prompt
堆栈future8 小时前
上下文工程(Context-Engineering): AI应用核心技术剖析
llm·ai编程·mcp