LangChain:Prompt Templates介绍及应用

这篇具有很好参考价值的文章主要介绍了LangChain:Prompt Templates介绍及应用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


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

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

LangChain: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 PromptTemplate


template = """
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, YouTube

The 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_prompt

prompt = 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 go
    suffix="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将选择较少的示例包含在提示中。文章来源地址https://www.toymoban.com/news/detail-464020.html

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学习相关,读研读博相关......)👈

到了这里,关于LangChain:Prompt Templates介绍及应用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ChatGPT | 使用自己Prompt替换LangChain默认Prompt

    某些场景会要求ChatGPT重复处理同一个操作,要么在问题里面加入Prompt,要么用自己Prompt替换LangChain默认Prompt。 直接看看前后对比结果 LangChain默认的Prompt template=\\\"Use the following pieces of context to answer the users question. nIf you don\\\'t know the answer, just say that you don\\\'t know, don\\\'t try to make up

    2024年02月16日
    浏览(29)
  • Langchain框架 prompt injection注入

    Prompt Injection 是一种攻击技术,黑客或恶意攻击者操纵 AI 模型的输入值,以诱导模型返回非预期的结果 LangChain 是一个基于大语言模型进行应用开发的框架。 所谓大语言模型(Large Language Models, LLMs),是指基于海量语料训练、参数动辄数十亿上百亿的语言模型。除了大家熟知

    2024年02月01日
    浏览(34)
  • LangChain(2)提示工程 Prompt Engineering

    提示一般包含如下部分: Instructions:整体结构,模型的人设 Instructions tell the model what to do, how to use external information if provided, what to do with the query, and how to construct the output. External information:额外提供给模型的信息 External information or context(s) act as an additional source of knowledge for t

    2024年02月15日
    浏览(32)
  • LangChain-10 Agents langchainhub 共享的提示词Prompt

    LangChainHub 的思路真的很好,通过Hub的方式将 Prompt 共享起来,大家可以通过很方便的手段,短短的几行代码就可以使用共享的 Prompt 。 我个人非常看好这个项目。 官方推荐使用LangChainHub,但是它在GitHub已经一年没有更新了, 倒是数据还在更新。 为了防止大家不能访问,我这

    2024年04月10日
    浏览(39)
  • 在langchain中使用带简短知识内容的prompt template

    langchain中有个比较有意思的prompt template叫做FewShotPromptTemplate。 他是这句话的简写:“Prompt template that contains few shot examples.” 什么意思呢?就是说在Prompt template带了几个比较简单的例子。然后把这些例子发送给LLM,作为简单的上下文环境,从而为LLM提供额外的一些关键信息。

    2024年02月15日
    浏览(32)
  • 【Java-LangChain:使用 ChatGPT API 搭建系统-6】处理输入-链式 Prompt Chaining Prompts

    在本章中,我们将学习如何通过将复杂任务拆分为一系列简单的子任务来链接多个 Prompt。 您可能会想,为什么要将任务拆分为多个 Prompt,而不是像我们在上一个视频中学习的那样,使用思维链推理一次性完成呢?我们已经证明了语言模型非常擅长遵循复杂的指令,特别是像

    2024年02月07日
    浏览(39)
  • 【ChatGLM_02】LangChain知识库+Lora微调chatglm2-6b模型+提示词Prompt的使用原则

    运行langchain-ChatGLM-master下面的webui.py文件 (1) 配置知识库 新建知识库 向知识库当中添加文件 支持上传的数据格式:word、pdf、excel、csv、txt、文件夹等。但是此处我试了一下 (2) 文档数据测试 word文档测试: (3) 知识库测试模式 知识库测试只会返回输入内容在当前知识库当中的

    2024年02月14日
    浏览(30)
  • AI大模型预先学习笔记二:prompt提问大模型、langchain使用大模型框架、fine tune微调大模型

    1)环境准备 ①安装OpenAI库 附加 安装来源 ②生成API key ③设定本地的环境变量 ④代码的准备工作 ⑤在代码运用prompt(简单提问和返回) 2)交互代码的参数备注 temperature:随机性(从0到2可以调节,回答天马行空变化大可以选2) model:跟什么类型的model互动 role:(定义交互

    2024年01月17日
    浏览(37)
  • Prompt工程师指南[应用篇]:Prompt应用、ChatGPT|Midjouney Prompt Engineering

    主题: 与 ChatGPT 对话 Python 笔记本 Topics: ChatGPT介绍 审查对话任务 与ChatGPT对话 Python笔记本 ChatGPT介绍 ChatGPT是OpenAI训练的一种新型模型,可以进行对话交互。该模型经过训练,可以按照提示中的指令,在对话上下文中提供适当的回应。ChatGPT 可以帮助回答问题、建议菜谱、按

    2024年02月04日
    浏览(44)
  • 提示学习Prompt介绍

    为什么要用提示学习? 下游任务的目标与预训练的目标差距过大导致提升效果不明显,微调过程中依赖大量的监督语料 降低语义差异:预训练任务主要以(MLM)为主,而下游任务则重新引入新的训练参数,因此两个阶段的目标通常有较大差异; 避免过拟合:由于再Fine-tuni

    2023年04月22日
    浏览(46)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包