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

news/2024/9/23 5:20:40/
aidu_pl">

本文速读:

  • 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: happyoutput: sad
- input: talloutput: 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: sadInput: tall
Output: shortInput: funny
Output:

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

1. 查看配置文件,样例在另一个文件中(examples.yaml)

cat few_shot_prompt_yaml_examples.yaml

输出内容:

  _type: few_shotinput_variables:["adjective"]prefix: Write antonyms for the following words.example_prompt:_type: promptinput_variables:["input", "output"]template:"Input: {input}\nOutput: {output}"examples:examples.yamlsuffix:"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: happyOutput: sadInput: tallOutput: shortInput: funnyOutput:

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: happyOutput: sadInput: tallOutput: shortInput: funnyOutput:

同样地,样例文件也可以是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: happyOutput: sadInput: tallOutput: shortInput: funnyOutput:

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

上述介绍的方式中,样例都是单独的一个文件,和配置文件是分开的。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: happyOutput: sadInput: tallOutput: shortInput: funnyOutput:

本文小结

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

公众号:大白爱爬山


http://www.ppmy.cn/news/1445591.html

相关文章

基于SpringBoot+Vue大学生兼职管理系统的设计与实现

目录 一、前言介绍 二、功能需求 三、功能结构设计 四、管理员功能实现 招聘单位管理 用户管理 论坛管理 公告信息管理 五、招聘单位功能实现 职位招聘管理 职位留言管理 简历投递管理 六、用户功能实现 在线论坛 职位招聘信息 简历投递 简历 七、部分核心代码 …

C语言 | Leetcode C语言题解之第51题N皇后

题目&#xff1a; 题解&#xff1a; int solutionsSize;char** generateBoard(int* queens, int n) {char** board (char**)malloc(sizeof(char*) * n);for (int i 0; i < n; i) {board[i] (char*)malloc(sizeof(char) * (n 1));for (int j 0; j < n; j) board[i][…

小程序评分/关键词/UV优化助力小程序登顶

随着小程序市场的日益繁荣&#xff0c;小程序搜索排名优化成为了众多开发者关注的焦点。小程序搜索排名被很多因素影响着&#xff0c;关键词、评分还有uv&#xff08;授权&#xff09;等。在本文小柚和各位老板分享如何有效优化小程序搜索排名的经验。 一、关键词策略 关键词是…

【MySQL | 第八篇】在MySQL中,如何定位慢查询以及对应解决方法?

文章目录 8.在MySQL中&#xff0c;如何定位慢查询以及对应解决方法&#xff1f;8.1MySQL慢查询日志8.1.1开启慢查询&#xff08;1&#xff09;修改配置文件&#xff08;2&#xff09;设置全局变量 8.1.2日志记录在表上&#xff08;实践&#xff09;8.1.3日志记录在文件上&#…

资深项目经理15年心得:管理需求变更5大技巧

高效管理需求变更对项目管理至关重要。通过严格的变更控制&#xff0c;确保所有需求变更都与项目目标和范围保持一致&#xff0c;避免偏离原定计划&#xff0c;有助于项目按既定目标顺利推进。能够及时评估变更对项目的影响&#xff0c;有利于减低项目延期和超支的风险&#xf…

从0开始学习制作一个微信小程序 前端学习部分(7)数据控制操作,修改、判断等

系列文章目录 本系列会从前后端的全面角度讲述制作小程序&#xff0c;从零开始学小程序&#xff0c;跟着本系列就够了&#xff01; 前端学习篇 学习篇第一篇我们讲了编译器下载&#xff0c;项目、环境建立、文件说明与简单操作&#xff1a;第一篇链接 第二、三篇分析了几个重要…

boa交叉编译(移植到arm)

参考&#xff1a;CentOS7 boa服务器的搭建和配置-CSDN博客 以下操作在宿主机/编译平台操作&#xff1a; 1. 先执行[参考]1到3、 4.2、4.3、4.4、4.5 2. 修改MakeFile # 由以下&#xff1a; CC gcc CPP gcc -E # 改为&#xff1a; CC arm-linux-gnueabihf-gcc CPP arm-l…

centos8配置的IPV4失效

centos8在/etc/sysconfig/network-scripts/ifcfg-ens160中配置的IP突然失效&#xff0c;使用ifconfig查看发现没有ens160的网卡。 使用ifconfig up ens160命令后查看ifconfig&#xff0c;发现ens160网卡但是里面没有IPV4只有IPV6 使用ifup ens160报错&#xff1a;No suitable…