自然语言处理从入门到应用——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/

相关推荐
昵称是6硬币3 分钟前
YOLOv11: AN OVERVIEW OF THE KEY ARCHITECTURAL ENHANCEMENTS目标检测论文精读(逐段解析)
图像处理·人工智能·深度学习·yolo·目标检测·计算机视觉
平和男人杨争争27 分钟前
机器学习2——贝叶斯理论下
人工智能·机器学习
静心问道28 分钟前
XLSR-Wav2Vec2:用于语音识别的无监督跨语言表示学习
人工智能·学习·语音识别
算家计算33 分钟前
5 秒预览物理世界,2 行代码启动生成——ComfyUI-Cosmos-Predict2 本地部署教程,重塑机器人训练范式!
人工智能·开源
摆烂工程师33 分钟前
国内如何安装和使用 Claude Code 教程 - Windows 用户篇
人工智能·ai编程·claude
云天徽上9 天前
【目标检测】图像处理基础:像素、分辨率与图像格式解析
图像处理·人工智能·目标检测·计算机视觉·数据可视化
Vertira9 天前
PyTorch中的permute, transpose, view, reshape和flatten函数详解(已解决)
人工智能·pytorch·python
heimeiyingwang9 天前
【深度学习加速探秘】Winograd 卷积算法:让计算效率 “飞” 起来
人工智能·深度学习·算法
lsd&xql9 天前
AI大模型(四)openAI应用实战
人工智能
飞哥数智坊9 天前
AI编程实战:使用Cursor,65分钟轻松打造番茄时钟应用
人工智能