自然语言处理从入门到应用——LangChain:提示(Prompts)-[提示模板:部分填充的提示模板和提示合成]

分类目录:《自然语言处理从入门到应用》总目录


部分填充的提示模板

提示模板是一个具有.format方法的类,它接受一个键值映射并返回一个字符串(一个提示),以传递给语言模型。与其他方法一样,将提示模板进行"部分填充"可能是有意义的。例如,传入所需值的子集,以创建一个新的提示模板,只需要剩余的子集值。

LangChain支持两种方式实现这个功能:

  • 使用字符串值
  • 使用返回字符串值的函数

这两种不同的方式支持不同的用例。在下面的文章中,我们将介绍这两种用例的动机以及在LangChain中如何实现它。

使用字符串进行部分填充

部分填充提示模板的一个常见用例是,如果我们先获得某些变量,然后再获得其他变量。例如,假设我们有一个需要两个变量foobaz的提示模板。如果您先获得foo值,但稍后获得 baz值,那么等到两个变量都在同一个位置时再传递给提示模板可能会很麻烦。相反,我们可以使用foo值部分填充提示模板,然后将部分填充的提示模板传递下去并直接使用它。下面是一个示例:

dart 复制代码
from langchain.prompts import PromptTemplate
prompt = PromptTemplate(template="{foo}{bar}", input_variables=["foo", "bar"])
partial_prompt = prompt.partial(foo="foo");
print(partial_prompt.format(bar="baz"))
foobaz

我们也可以使用部分变量初始化提示:

dart 复制代码
prompt = PromptTemplate(template="{foo}{bar}", input_variables=["bar"], partial_variables={"foo": "foo"})
print(prompt.format(bar="baz"))
foobaz
使用函数进行部分填充

另一个常见的用例是使用函数进行部分填充,这种情况下的用例主要针对当我们知道我们总是以相同方式获取某个变量时。一个典型的例子是日期或时间。想象一下,我们有一个提示,且我们总是希望其中包含当前日期。我们不能在提示中硬编码日期,并且将其与其他输入变量一起传递也有些麻烦。在这种情况下,使用一个函数进行部分填充,该函数始终返回当前日期非常方便。

dart 复制代码
from datetime import datetime

def _get_datetime():
    now = datetime.now()
    return now.strftime("%m/%d/%Y, %H:%M:%S")
prompt = PromptTemplate(
    template="告诉我一个{形容词}的关于{日期}的笑话",
    input_variables=["形容词", "日期"]
)
partial_prompt = prompt.partial(date=_get_datetime)
print(partial_prompt.format(形容词="有趣"))
告诉我一个有趣的关于02/27/2023, 22:15:16的笑话

我们还可以直接使用部分填充的变量初始化提示模板,这在这种工作流程中通常更有意义:

dart 复制代码
prompt = PromptTemplate(
    template="告诉我一个{形容词}的关于{日期}的笑话",
    input_variables=["形容词"],
    partial_variables={"日期": _get_datetime}
)
print(prompt.format(形容词="有趣"))
告诉我一个有趣的关于02/27/2023, 22:15:16的笑话

提示合成

下文介绍的是如何将多个提示组合在一起。当我们想要重用提示的部分时,这将非常有用。我们可以使用PipelinePrompt来实现这一点,PipelinePrompt由两个主要部分组成:

  • final_prompt:这是返回的最终提示
  • pipeline_prompts:这是一个包含元组的列表,每个元组都包含一个字符串name和一个Prompt模板。每个Prompt模板将被格式化,然后作为与name相同名称的变量传递给未来的Prompt模板。
dart 复制代码
from langchain.prompts.pipeline import PipelinePromptTemplate
from langchain.prompts.prompt import PromptTemplate
full_template = """{introduction}

{example}

{start}"""
full_prompt = PromptTemplate.from_template(full_template)
introduction_template = """You are impersonating {person}."""
introduction_prompt = PromptTemplate.from_template(introduction_template)
example_template = """Here's an example of an interaction: 

Q: {example_q}
A: {example_a}"""
example_prompt = PromptTemplate.from_template(example_template)
start_template = """Now, do this for real!

Q: {input}
A:"""
start_prompt = PromptTemplate.from_template(start_template)
input_prompts = [
    ("introduction", introduction_prompt),
    ("example", example_prompt),
    ("start", start_prompt)
]
pipeline_prompt = PipelinePromptTemplate(final_prompt=full_prompt, pipeline_prompts=input_prompts)
pipeline_prompt.input_variables
['example_a', 'person', 'example_q', 'input']

print(pipeline_prompt.format(
    person="Elon Musk",
    example_q="What's your favorite car?",
    example_a="Telsa",
    input="What's your favorite social media site?"
))

输出:

dart 复制代码
You are impersonating Elon Musk.
Here's an example of an interaction: 

Q: What's your favorite car?
A: Tesla
Now, do this for real!

Q: What's your favorite social media site?
A:

参考文献:

1\] LangChain 🦜️🔗 中文网,跟着LangChain一起学LLM/GPT开发:https://www.langchain.com.cn/ \[2\] LangChain中文网 - LangChain 是一个用于开发由语言模型驱动的应用程序的框架:http://www.cnlangchain.com/

相关推荐
周末程序猿26 分钟前
机器学习|大模型为什么会出现"幻觉"?
人工智能
JoannaJuanCV40 分钟前
大语言模型基石:Transformer
人工智能·语言模型·transformer
飞哥数智坊43 分钟前
Qoder vs CodeBuddy,刚起步就收费,值吗?
人工智能·ai编程
强盛小灵通专卖员44 分钟前
闪电科创,深度学习辅导
人工智能·sci·小论文·大论文·延毕
通街市密人有1 小时前
IDF: Iterative Dynamic Filtering Networks for Generalizable Image Denoising
人工智能·深度学习·计算机视觉
大千AI助手1 小时前
TruthfulQA:衡量语言模型真实性的基准
人工智能·语言模型·自然语言处理·llm·模型评估·truthfulqa·事实性基准
蚂蚁RichLab前端团队1 小时前
🚀🚀🚀 RichLab - 花呗前端团队招贤纳士 - 【转岗/内推/社招】
前端·javascript·人工智能
智数研析社1 小时前
9120 部 TMDb 高分电影数据集 | 7 列全维度指标 (评分 / 热度 / 剧情)+API 权威源 | 电影趋势分析 / 推荐系统 / NLP 建模用
大数据·人工智能·python·深度学习·数据分析·数据集·数据清洗
救救孩子把1 小时前
2-机器学习与大模型开发数学教程-第0章 预备知识-0-2 数列与级数(收敛性、幂级数)
人工智能·数学·机器学习
yzx9910131 小时前
接口协议全解析:从HTTP到gRPC,如何选择适合你的通信方案?
网络·人工智能·网络协议·flask·pygame