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

公众号:大白爱爬山

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/585541.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

深度学习系列64:数字人wav2lip详解

1. 整体流程 第一步,加载视频/图片和音频/tts。用melspectrogram将wav文件拆分成mel_chunks。 第二步,调用face_detect模型,给出人脸检测结果(可以改造成从文件中读取),包装成4个数组batch:img…

74、堆-数组中的第K个最大元素

思路&#xff1a; 直接排序是可以的&#xff0c;但是时间复杂度不符合。可以使用优先队列&#xff0c;代码如下&#xff1a; class Solution {public int findKthLargest(int[] nums, int k) {if (numsnull||nums.length0||k<0||k>nums.length){return Integer.MAX_VAL…

神之浩劫2测试资格100%获取教程 测试资格获取方法教程

《神之浩劫》是一款基于Unreal 3&#xff08;虚幻3&#xff09;游戏引擎开发的3D团队竞技游戏&#xff0c;由美国Hi-Rez工作室开发、腾讯全球代理。2013年10月31日&#xff0c;游戏开启国服首测&#xff0c;并于2014年3月25日在美国公测。2018年1月20日&#xff0c;国服并入全球…

shell脚本-监控系统内存和磁盘容量

监控内存和磁盘容量除了可以使用zabbix监控工具来监控&#xff0c;还可以通过编写Shell脚本来监控。 #! /bin/bash #此脚本用于监控内存和磁盘容量&#xff0c;内存小于500MB且磁盘容量小于1000MB时报警#提取根分区剩余空间 disk_size$(df / | awk /\//{print $4})#提取内存剩…

Redis(七) zset有序集合类型

文章目录 前言命令ZADDZCARDZCOUNTZRANGEZREVRANGEZRANGEBYSCOREZPOPMAXZPOPMIN两个阻塞版本的POP命令BZPOPMAX BZPOPMINZRANKZREVRANKZSCOREZREMZREMRANGEBYRANKZREMRANGEBYSCOREZINCRBY集合间操作ZINTERSTOREZUNIONSTORE 命令小结 内部编码使用场景 前言 对于有序集合这个名…

Java核心技术.卷I-上-笔记

目录 面向对象程序设计 使用命令行工具简单的编译源码 数据类型 StringBuilder 数组 对象与类 理解方法调用 继承 代理 异常 断言 日志 面向对象程序设计 面向对象的程序是由对象组成的&#xff0c;每个对象包含对用户公开的特定功能部分和隐藏的实现部分从根本上…

高校宿舍管理

在高等教育的迅猛发展浪潮中&#xff0c;大学校园正经历着前所未有的变革。随着招生规模的不断扩大&#xff0c;学生宿舍管理工作变得日益繁重和复杂。传统的管理方法&#xff0c;如使用Word和Excel进行数据记录和整理&#xff0c;已经无法满足现代高效、精准的管理需求。此外&…

关于几个水表术语的理解

GB/T778.1-2018《饮用冷水水表和热水水表 第 1 部分&#xff1a;量值要求和技术要求》、JJG162-2019《饮 用冷水水表检定规程》和 JJF1777-2019《饮用冷 水水表型式评价大纲》不仅规范了水表行业的专业名词解释&#xff0c;而且给出了影响水表性能的主要因素的定义。本文从影响…

Mellanox网卡打流命令ib_write_bw执行遇到Couldn‘t listen to port 18515原因与解决办法?

要点 要点&#xff1a; ib默认使用18515命令 相关命令&#xff1a; netstat -tuln | grep 18515 ib_write_bw --help |grep port# server ib_write_bw --ib-devmlx5_1 --port 88990 # client ib_write_bw --ib-devmlx5_0 1.1.1.1 --port88990现象&#xff1a; 根因&#xf…

Spring Cloud Feign

序言 本文给大家介绍一下 Spring Cloud Feign 的基础概念以及使用方式。 一、远程调用 在传统的单体系统中&#xff0c;我们通常是客户端去请求服务端的接口。但是在分布式的系统中&#xff0c;常常需要一个服务去调用另外一个服务的接口。在服务端如何去调用另外一个服务端…

docker compose mysql主从复制及orchestrator高可用使用

1.orchestrator 功能演示&#xff1a; 1.1 多级级联&#xff1a; 1.2 主从切换&#xff1a; 切换成功后&#xff0c;原来的主库是红色的&#xff0c;需要在主库的配置页面点击“start replication ”&#xff0c;重新连接上新的主库。 1.3 主从故障&#xff0c;从库自动切换新…

pyqt字体选择器

pyqt字体选择器 pyqt字体选择器效果代码 pyqt字体选择器 pyqt中QFontDialog 类是一个预定义的对话框&#xff0c;允许用户选择一个字体并设置其样式、大小等属性。 效果 代码 from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton, QFontD…

信息收集。

信息收集 接着使用cs进行信息收集 发现域内管理员账号。 然后查看pc信息&#xff0c; 查看进程。 发现域为god.org 尝试定位域控。 提权 使用cs的功能进行权限提权 成功获取管理员权限。 hash抓取 接着抓hash 成功抓到管理员账号、密码。 接着进行横向传递 成功获取AD和…

从曝光到安装:App传参安装的关键步骤与数据指标

随着移动互联网的普及&#xff0c;手游市场日益繁荣&#xff0c;手游推广方式也日新月异。在这个竞争激烈的市场中&#xff0c;如何有效地推广手游&#xff0c;吸引更多的用户&#xff0c;成为了开发者和广告主关注的焦点。而Xinstall作为国内专业的App全渠道统计服务商&#x…

Android Widget开发代码示例详细说明

因为AppWidgetProvider扩展自BroadcastReceiver, 所以你不能保证回调函数完成调用后&#xff0c;AppWidgetProvider还在继续运行。 a. AppWidgetProvider 的实现 /*** Copyright(C):教育电子有限公司 * Project Name: NineSync* Filename: SynWidgetProvider.java * Author(S…

ORACLE 11G RAC 访问SQLSERVER

平时都是单机&#xff0c;RAC有点不一样&#xff0c;其实也一样。 目录 1.操作环境信息 2.安装GATEWAY 3.配置实例信息 4.配置监听 5.配置网络别名 6.创建到SQLSERVER的DBLINK 7.测试DBLINK有效性 1.操作环境信息 HIS PACS 数据库版本 ORACLE 11.2.0.4 RAC MS SQLSE…

C++多态(全)

多态 概念 调用函数的多种形态&#xff0c; 多态构成条件 1&#xff09;父子类完成虚函数的重写&#xff08;三同&#xff1a;函数名&#xff0c;参数&#xff0c;返回值相同&#xff09; 2&#xff09;父类的指针或者引用调用虚函数 虚函数 被virtual修饰的类成员函数 …

Llama images - 记录我看到的那些羊驼

来自 &#xff1a; DREAM: Distributed RAG Experimentation Framework

73、栈-柱状图中最大的矩形

思路&#xff1a; 矩形面积&#xff1a;宽度*高度 高度如何确定呢&#xff1f;就是在宽度中最矮的元素。如何确定宽度&#xff0c;就是要确定左右边界。 当我们在处理直方图最大矩形面积问题时&#xff0c;遇到一个比栈顶柱子矮的新柱子时开始计算面积的原因关键在于如何确定…

Hotcoin Research|玩赚WEB3:Seraph零成本赚取技巧

在《Seraph》这款游戏里&#xff0c;要提升自己的游戏技能和体验&#xff0c;了解如何免费赚取游戏货币灵魂晶石并挑战游戏主线是非常重要的。你可以通过卖东西、参加虚空异界地图和混沌秘境来在游戏里赚更多的钱&#xff0c;并更享受游戏的乐趣。最酷的是&#xff0c;得到的灵…
最新文章