Skip to main content
Open In ColabOpen on GitHub

如何组合提示词

先决条件

本指南假定您已熟悉以下概念:

LangChain 提供了一个用户友好的界面,用于将不同的 提示词 部分组合在一起。您可以使用字符串提示词或聊天提示词来完成此操作。通过这种方式构建提示词,可以方便地复用组件。

字符串提示组合

在使用字符串提示时,每个模板会被连接在一起。你可以直接使用提示,或者使用字符串(列表中的第一个元素需要是一个提示)。

from langchain_core.prompts import PromptTemplate

prompt = (
PromptTemplate.from_template("Tell me a joke about {topic}")
+ ", make it funny"
+ "\n\nand in {language}"
)

prompt
API Reference:PromptTemplate
PromptTemplate(input_variables=['language', 'topic'], template='Tell me a joke about {topic}, make it funny\n\nand in {language}')
prompt.format(topic="sports", language="spanish")
'Tell me a joke about sports, make it funny\n\nand in spanish'

Chat 提示词组合

Here's how to compose a

聊天提示由消息列表组成。与上面的示例类似,我们可以串联聊天提示模板。每个新元素都是最终提示中的一条新消息。

首先,我们使用 SystemMessage 初始化一个 ChatPromptTemplate

from langchain_core.messages import AIMessage, HumanMessage, SystemMessage

prompt = SystemMessage(content="You are a nice pirate")

之后,你可以轻松地将它与其它消息或消息模板结合起来,创建一个管道。

当没有需要格式化的变量时,使用 Message;当有需要格式化的变量时,使用 MessageTemplate。你也可以仅使用字符串(注意:这会自动推断为 HumanMessagePromptTemplate)。

new_prompt = (
prompt + HumanMessage(content="hi") + AIMessage(content="what?") + "{input}"
)

在底层,这会创建一个 ChatPromptTemplate 类的实例,所以你可以像之前一样使用它!

new_prompt.format_messages(input="i said hi")
[SystemMessage(content='You are a nice pirate'),
HumanMessage(content='hi'),
AIMessage(content='what?'),
HumanMessage(content='i said hi')]

使用 PipelinePrompt

已弃用

PipelinePromptTemplate 已弃用;有关更多信息,请参阅 PipelinePromptTemplate

LangChain 包含一个名为 PipelinePromptTemplate 的类,当您想重用提示的某些部分时,它会很有用。PipelinePrompt 由两部分组成:

  • Final prompt: 返回的最终提示
  • Pipeline prompts: 一个元组列表,由一个字符串名称和一个提示模板组成。每个提示模板都会被格式化,然后作为同名变量传递给后续的提示模板。
from langchain_core.prompts import PipelinePromptTemplate, 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
['person', 'example_a', 'example_q', 'input']
print(
pipeline_prompt.format(
person="Elon Musk",
example_q="What's your favorite car?",
example_a="Tesla",
input="What's your favorite social media site?",
)
)
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:

后续步骤

现在你已经学会了如何组合提示词。

接下来,请查看本节中关于提示词模板的其它操作指南,例如为提示词模板添加少样本示例