LangChain:Prompt Templates介绍及应用

news/2024/4/29 12:38:17/文章来源:https://blog.csdn.net/qq_41667743/article/details/129678577

❤️觉得内容不错的话,欢迎点赞收藏加关注😊😊😊,后续会继续输入更多优质内容❤️

👉有问题欢迎大家加关注私戳或者评论(包括但不限于NLP算法相关,linux学习相关,读研读博相关......)👈

Prompt Templates

(封面图由文心一格生成)

LangChain:Prompt Templates介绍及应用

在自然语言生成任务中,生成高质量的文本是非常困难的,尤其是当需要针对不同的主题、情境、问题或任务进行文本生成时,需要花费大量的时间和精力去设计、调试和优化模型,而这种方式并不是高效的解决方案。因此,Prompt Templates技术应运而生,可以大大降低模型设计、调试和优化的成本。

Prompt Templates是一种可复制的生成Prompt的方式,它包含一个文本字符串,可以接受来自终端用户的一组参数并生成Prompt。Prompt Templates可以包含指令、少量示例和一个向语言模型提出的问题。我们可以使用Prompt Templates技术来指导语言模型生成更高质量的文本,从而更好地完成我们的任务。

在这篇博客中,我们将学习:

什么是Prompt Templates以及为什么需要它
如何创建Prompt Templates
如何传递few-shot examples给Prompt Templates
如何为Prompt Templates选择examples

什么是Prompt Templates?

Prompt Templates是一种可复制的生成Prompt的方式,它包含一个文本字符串,可以接受来自终端用户的一组参数并生成Prompt。Prompt Templates可以包含指令、少量示例和一个向语言模型提出的问题。Prompt Templates可以帮助我们指导语言模型生成更高质量的文本,从而更好地完成我们的任务。

Prompt Templates可以包含以下内容:

Prompt Templates可能包含:

  • 对语言模型的指令
  • 一组few-shot examples,以帮助语言模型生成更好的响应
  • 对语言模型的问题

下面的代码段包含Prompt Template的一个示例:

from langchain import PromptTemplate
from langchain import PromptTemplatetemplate = """
I want you to act as a naming consultant for new companies.Here are some examples of good company names:- search engine, Google
- social media, Facebook
- video sharing, YouTubeThe name should be short, catchy and easy to remember.What is a good name for a company that makes {product}?
"""prompt = PromptTemplate(input_variables=["product"],template=template,
)

创建Prompt Templates

你可以使用PromptTemplate类创建简单的硬编码提示。Prompt Templates可以采用任何数量的输入变量,并且可以进行格式化以生成提示。

from langchain import PromptTemplate# 没有输入变量的示例prompt
no_input_prompt = PromptTemplate(input_variables=[], template="给我讲个笑话。")
no_input_prompt.format()
# -> "给我讲个笑话。"# 一个有一个输入变量的示例prompt
one_input_prompt = PromptTemplate(input_variables=["adjective"], template="告诉我一个{adjective}笑话。")
one_input_prompt.format(adjective="好笑的")
# -> "告诉我一个好笑的笑话。"# 一个有多个输入变量的示例prompt
multiple_input_prompt = PromptTemplate(input_variables=["adjective", "content"], template="告诉我一个{adjective}关于{content}的笑话。"
)
multiple_input_prompt.format(adjective="好笑的", content="小鸡")
# -> "告诉我一个好笑的关于小鸡的笑话。"

从LangChainHub加载Prompt Templates

LangChainHub包含了许多可以通过LangChain直接加载的Prompt Templates。

from langchain.prompts import load_promptprompt = load_prompt("lc://prompts/conversation/prompt.json")
prompt.format(history="", input="What is 1 + 1?")

传递few-shot examples给Prompt Templates

Few-shot examples是一组可用于帮助语言模型生成更好响应的示例。

要生成具有few-shot examples的prompt,可以使用FewShotPromptTemplate。该类接受一个PromptTemplate和一组few-shot examples。然后,它使用这些few-shot examples格式化prompt模板。

在这个示例中,我们将创建一个用于生成单词反义词的提示。

from langchain import PromptTemplate, FewShotPromptTemplate# First, create the list of few shot examples.
examples = [{"word": "happy", "antonym": "sad"},{"word": "tall", "antonym": "short"},
]# Next, we specify the template to format the examples we have provided.
# We use the `PromptTemplate` class for this.
example_formatter_template = """
Word: {word}
Antonym: {antonym}\n
"""
example_prompt = PromptTemplate(input_variables=["word", "antonym"],template=example_formatter_template,
)# Finally, we create the `FewShotPromptTemplate` object.
few_shot_prompt = FewShotPromptTemplate(# These are the examples we want to insert into the prompt.examples=examples,# This is how we want to format the examples when we insert them into the prompt.example_prompt=example_prompt,# The prefix is some text that goes before the examples in the prompt.# Usually, this consists of intructions.prefix="Give the antonym of every input",# The suffix is some text that goes after the examples in the prompt.# Usually, this is where the user input will gosuffix="Word: {input}\nAntonym:",# The input variables are the variables that the overall prompt expects.input_variables=["input"],# The example_separator is the string we will use to join the prefix, examples, and suffix together with.example_separator="\n\n",
)# We can now generate a prompt using the `format` method.
print(few_shot_prompt.format(input="big"))
# -> Give the antonym of every input
# -> 
# -> Word: happy
# -> Antonym: sad
# ->
# -> Word: tall
# -> Antonym: short
# ->
# -> Word: big
# -> Antonym:

为Prompt Templates选择examples

如果你有大量的示例,则可以使用ExampleSelector来选择最有信息量的一些示例,以帮助你生成更可能产生良好响应的提示。

接下来,我们将使用LengthBasedExampleSelector,根据输入的长度选择示例。当你担心构造的提示将超过上下文窗口的长度时,此方法非常有用。对于较长的输入,它会选择包含较少示例的提示,而对于较短的输入,它会选择包含更多示例。

from langchain.prompts.example_selector import LengthBasedExampleSelector# These are a lot of examples of a pretend task of creating antonyms.
examples = [{"word": "happy", "antonym": "sad"},{"word": "tall", "antonym": "short"},{"word": "energetic", "antonym": "lethargic"},{"word": "sunny", "antonym": "gloomy"},{"word": "windy", "antonym": "calm"},
]# We'll use the `LengthBasedExampleSelector` to select the examples.
example_selector = LengthBasedExampleSelector(# These are the examples is has available to choose from.examples=examples, # This is the PromptTemplate being used to format the examples.example_prompt=example_prompt, # This is the maximum length that the formatted examples should be.# Length is measured by the get_text_length function below.max_length=25,
)# We can now use the `example_selector` to create a `FewShotPromptTemplate`.
dynamic_prompt = FewShotPromptTemplate(# We provide an ExampleSelector instead of examples.example_selector=example_selector,example_prompt=example_prompt,prefix="Give the antonym of every input",suffix="Word: {input}\nAntonym:",input_variables=["input"],example_separator="\n\n",
)# We can now generate a prompt using the `format` method.
print(dynamic_prompt.format(input="big"))
# -> Give the antonym of every input
# ->
# -> Word: happy
# -> Antonym: sad
# ->
# -> Word: tall
# -> Antonym: short
# ->
# -> Word: energetic
# -> Antonym: lethargic
# ->
# -> Word: sunny
# -> Antonym: gloomy
# ->
# -> Word: windy
# -> Antonym: calm
# ->
# -> Word: big
# -> Antonym:

相比之下,如果我们提供了一个非常长的输入,则LengthBasedExampleSelector将选择较少的示例包含在提示中。

long_string = "big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else"
print(dynamic_prompt.format(input=long_string))
# -> Give the antonym of every input# -> Word: happy
# -> Antonym: sad
# ->
# -> Word: big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else
# -> Antonym:

❤️觉得内容不错的话,欢迎点赞收藏加关注😊😊😊,后续会继续输入更多优质内容❤️

👉有问题欢迎大家加关注私戳或者评论(包括但不限于NLP算法相关,linux学习相关,读研读博相关......)👈

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

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

相关文章

WPF+WebView2+react/vue/angular

创建WPF项目 安装WbeView2 Nuget包 在窗体中添加命名空间 xmlns:wv2"clr-namespace:Microsoft.Web.WebView2.Wpf;assemblyMicrosoft.Web.WebView2.Wpf"使用控件 <wv2:WebView2 x:Name"webview"/>在MainWindow中初始化 public MainWindow(){Initia…

什么是语法糖?Java中有哪些语法糖?

本文从 Java 编译原理角度&#xff0c;深入字节码及 class 文件&#xff0c;抽丝剥茧&#xff0c;了解 Java 中的语法糖原理及用法&#xff0c;帮助大家在学会如何使用 Java 语法糖的同时&#xff0c;了解这些语法糖背后的原理1 语法糖语法糖&#xff08;Syntactic Sugar&#…

Linux syslog 日志服务

文章目录Syslog 概述syslog 协议标准syslog APIsyslog 日志文件日志文件介绍日志配置产生本地日志参考文章Syslog 概述 syslog 常被称为系统日志或系统记录&#xff0c;系统日志通过 syslog 进程记录系统的有关事件&#xff0c;也可以记录应用程序运作事件。通过适当配置&…

Python批量删除或移动指定图像

Python批量删除或移动指定图像前言一、批量删除指定名称的图像二、批量移动指定名称的图像前言 笔者的研究方向为计算机视觉&#xff0c;因此经常和大量图像打交道&#xff0c;有时需要批量删除一些图像&#xff0c;有时需要批量移动一些图像&#xff0c;因此编写了下述代码。下…

flink 读取文件数据写入ElasticSearch

前言 es是大数据存储的必备中间件之一,通过flink可以读取来自日志文件,kafka等外部数据源的数据,然后写入到es中,本篇将通过实例演示下完整的操作过程; 一、前置准备 1、提前搭建并开启es服务(本文使用docker搭建的es7.6的服务); 2、提前搭建并开启kibana服务(便于操…

【Java 】Java NIO 底层原理

文章目录1、 Java IO读写原理1.1 内核缓冲与进程缓冲区1.2 java IO读写的底层流程2、 四种主要的IO模型3、 同步阻塞IO&#xff08;Blocking IO&#xff09;4、 同步非阻塞NIO&#xff08;None Blocking IO&#xff09;5、 IO多路复用模型(I/O multiplexing&#xff09;6、 异步…

Cursor编程初体验,搭载GPT-4大模型,你的AI助手,自然语言编程来了

背景 这两天体验了下最新生产力工具Cursor&#xff0c;基于最新的 GPT-4 大模型&#xff0c;目前免费&#xff0c;国内可访问&#xff0c;不限次数&#xff0c;跨平台&#xff0c;你确定不来体验一把&#xff1f;官方的 Slogan &#xff1a; Build Software. Fast. Write, edi…

差速巡线机器人设计-良好(80+)的报告-2023

如何提分&#xff1f;将一篇报告提升20分以上呢&#xff1f;差速巡线机器人设计-及格&#xff08;60&#xff09;的报告-2023_zhangrelay的博客-CSDN博客姓名&#xff1a; 学号&#xff1a; 实践项目1名称&#xff1a;差速巡线机器人设计 60分&#xff1a;缺乏思考、没有对比、…

攻防世界-first

题目下载&#xff1a;下载 IDA载入 __int64 __fastcall main(int a1, char **a2, char **a3) {__useconds_t *v3; // rbpunsigned int v4; // eaxint *v5; // rcxint v6; // edxunsigned int v7; // eaxsigned __int64 v8; // rcx__int64 v9; // raxchar v10; // blchar v11;…

为知笔记私有化部署

前言 原来一直买的为知笔记vip&#xff0c;但是随着内容越来越&#xff0c;并且不好整理。同时还不能一键全部导出&#xff0c;最后决定将数据迁移到自己服务器上。为止笔记提供了docker镜像&#xff0c;这也方便了部署&#xff08;其实吧&#xff0c;从产品层面&#xff0c;可…

C++ Lambda表达式的常见用法

⭐️我叫忆_恒心&#xff0c;一名喜欢书写博客的在读研究生&#x1f468;‍&#x1f393;。 如果觉得本文能帮到您&#xff0c;麻烦点个赞&#x1f44d;呗&#xff01; 近期会不断在专栏里进行更新讲解博客~~~ 有什么问题的小伙伴 欢迎留言提问欧&#xff0c;喜欢的小伙伴给个三…

【Django 网页Web开发】05. 数据库操作,实战用户管理(保姆级图文)

目录1. 安装第三方模块2. ORM2.1 自己手动创建数据库2.2 django连接数据库2.3 建表语句写在哪里&#xff1f;2.4 建表语句写好后如何运行生效&#xff1f;3. 操作表3.1 创建数据表3.2 修改数据表4. 操作数据4.1 插入数据4.2 删除数据4.3 修改数据4.4 查询数据5. 实战&#xff1…

pytest学习和使用22-allure特性 丨总览中的Environment、Categories设置以及Flaky test使用

22-allure特性 丨总览中的Environment和Categories设置1 Environment设置1.1 设置方法1.2 创建文件2 Categories设置2.1 设置方式2.2 创建文件3 关于Flaky test3.1 Flaky test介绍3.2 产生Flaky Tests的原因3.3 Flaky安装3.4 Flaky使用3.5 小结小结1小结2如下图&#xff0c;我们…

开始学习HTML5

HTML5 简介 HTML5是HTML最新的修订版本&#xff0c;2014年10月由万维网联盟&#xff08;W3C&#xff09;完成标准制定。 HTML5的设计目的是为了在移动设备上支持多媒体。 HTML5简单易学。 什么是 HTML5? HTML5 是下一代 HTML 标准。 HTML , HTML 4.01的上一个版本诞生于 1…

如何将3张图片横向拼在一起

如何将3张图片横向拼在一起&#xff1f;遇到这个情况你可能马上就会说出很多图片处理的app&#xff0c;比如用某秀秀来操作&#xff0c;但是也有很多时候某秀秀也处理不了的。当我们的图片非常大&#xff0c;图片数量很多&#xff0c;图片的格式不是jpg那种通用的格式&#xff…

如何监控和诊断JVM堆内和堆外内存使用?

第26讲 | 如何监控和诊断JVM堆内和堆外内存使用&#xff1f; 上一讲我介绍了 JVM 内存区域的划分&#xff0c;总结了相关的一些概念&#xff0c;今天我将结合 JVM 参数、工具等方面&#xff0c;进一步分析 JVM 内存结构&#xff0c;包括外部资料相对较少的堆外部分。 今天我要…

Java栈和队列·下

Java栈和队列下2. 队列(Queue)2.1 概念2.2 实现2.3 相似方法的区别2.4 循环队列3. 双端队列 (Deque)3.1 概念4.java中的栈和队列5. 栈和队列面试题大家好&#xff0c;我是晓星航。今天为大家带来的是 Java栈和队列下 的讲解&#xff01;&#x1f600; 继上一个讲完的栈后&…

视听场景理解经典任务

文章目录1. 视听场景理解简介2. 主要任务2.1 Audio-visual Event Localization (AVE) 2.2 Audio-visual Video Parsing &#xff08;AVVP&#xff09;2.3 Audio-visual Question Answering &#xff08;AVQA&#xff09;2.4 Audio-visual Segmentation &#xff08;AVS&#xf…

STM32中systick中断的优先级

1、systick中断的优先级 systick为内核外设中断&#xff0c;与普通外设中断的优先级有些区别&#xff0c;并没有抢占优先级和子优先级的说法。 对于M3来说内核外设的中断优先级由内核SCB这个外设的寄存器&#xff1a;SHPRx&#xff08;x1.2.3&#xff09;来配置。 内核外设的中…

佳明安夺(Garmin Enduro)续航简单测试

文章目录&#xff08;一&#xff09;结论&#xff08;二&#xff09;测试条件&#xff08;2.1&#xff09;Garmin Connect APP 日历&#xff08;2.2&#xff09;具体运动记录&#xff08;2.3&#xff09;步数情况&#xff08;三&#xff09;补充和探讨&#xff08;3.1&#xff…