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

相关推荐
yiersansiwu123d22 分钟前
AI伦理治理:在创新与规范之间寻找动态平衡
人工智能
华清远见成都中心41 分钟前
成都理工大学&华清远见成都中心实训,助力电商人才培养
大数据·人工智能·嵌入式
爱好读书1 小时前
AI生成er图/SQL生成er图在线工具
人工智能
CNRio1 小时前
智能影像:AI视频生成技术的战略布局与产业变革
人工智能
六行神算API-天璇1 小时前
架构思考:大模型作为医疗科研的“智能中间件”
人工智能·中间件·架构·数据挖掘·ar
搞科研的小刘选手2 小时前
【ISSN/ISBN双刊号】第三届电力电子与人工智能国际学术会议(PEAI 2026)
图像处理·人工智能·算法·电力电子·学术会议
wumingxiaoyao2 小时前
AI - 使用 Google ADK 创建你的第一个 AI Agent
人工智能·ai·ai agent·google adk
拉姆哥的小屋2 小时前
从混沌到秩序:条件扩散模型在图像转换中的哲学与技术革命
人工智能·算法·机器学习
Sammyyyyy2 小时前
DeepSeek v3.2 正式发布,对标 GPT-5
开发语言·人工智能·gpt·算法·servbay
JoannaJuanCV2 小时前
自动驾驶—CARLA仿真(6)vehicle_gallery demo
人工智能·机器学习·自动驾驶·carla