Skip to main content

组合 Composition

本笔记本介绍了如何将多个提示组合在一起。当您想要重用提示的部分时,这将非常有用。这可以通过PipelinePrompt来实现。PipelinePrompt由两个主要部分组成:

  • 最终提示:这是返回的最终提示
  • 管道提示:这是一个元组列表,包含一个字符串名称和一个提示模板。每个提示模板将被格式化,然后作为具有相同名称的变量传递给未来的提示模板。
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="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: