Prompt Composition with LangChain’s PipelinePromptTemplate

https://python.langchain.com.cn/docs/modules/model_io/prompts/prompt_templates/prompt_composition

Learning Guide: Prompt Composition with LangChain's PipelinePromptTemplate

This guide simplifies how to combine multiple prompts for reuse (using LangChain's PipelinePromptTemplate), while keeping all original code, examples, and key points exactly as they appear in the link.

1. What is PipelinePromptTemplate?

It's a LangChain tool to reuse parts of prompts. It has two key parts:

  • Final Prompt : The last, complete prompt you get after combining all parts. It uses placeholders (like {introduction}, {example}) to "hold space" for other small prompts.
  • Pipeline Prompts: A list of "name + small prompt" pairs. Each small prompt is formatted first, then put into the final prompt using its name (to match the placeholder).

2. Step-by-Step Code (Exact as Original)

We'll follow the original code step by step. Each code block is unchanged, and we'll explain what it does simply.

Step 1: Import Needed Tools

First, we get the two tools we need from LangChain:

python 复制代码
from langchain.prompts.pipeline import PipelinePromptTemplate
from langchain.prompts.prompt import PromptTemplate
  • PipelinePromptTemplate: Helps combine multiple prompts.
  • PromptTemplate: Makes single, reusable prompt templates.

Step 2: Make the Final Prompt Template

This is the "big" prompt that will hold all the small parts. It uses 3 placeholders:

python 复制代码
full_template = """{introduction}
{example}
{start}"""
full_prompt = PromptTemplate.from_template(full_template)
  • full_template: The structure of the final prompt (with placeholders).
  • PromptTemplate.from_template(): Turns the text structure into a LangChain "prompt object" (so we can use it later).

Step 3: Make Small Reusable Prompts

We create 3 small prompts (each is a reusable part). Each has its own variables:

1. Introduction Prompt (sets who to impersonate)
python 复制代码
introduction_template = """You are impersonating {person}."""
introduction_prompt = PromptTemplate.from_template(introduction_template)
  • Uses {person}: We'll fill this in later (e.g., "Elon Musk").
2. Example Prompt (gives a sample interaction)
python 复制代码
example_template = """Here's an example of an interaction:
Q: {example_q}
A: {example_a}"""
example_prompt = PromptTemplate.from_template(example_template)
  • Uses {example_q} (sample question) and {example_a} (sample answer).
3. Start Prompt (asks for a real response)
python 复制代码
start_template = """Now, do this for real!
Q: {input}
A:"""
start_prompt = PromptTemplate.from_template(start_template)
  • Uses {input}: The real question we want to ask later.

We make a list to connect each small prompt to its placeholder in the final prompt:

python 复制代码
input_prompts = [
    ("introduction", introduction_prompt),  # "introduction" → matches {introduction}
    ("example", example_prompt),            # "example" → matches {example}
    ("start", start_prompt)                 # "start" → matches {start}
]

Step 5: Create the PipelinePromptTemplate

We put the final prompt and the small prompt list together:

python 复制代码
pipeline_prompt = PipelinePromptTemplate(final_prompt=full_prompt, pipeline_prompts=input_prompts)

Step 6: Check Required Variables

To use the pipeline, we need to know all variables we must fill in. The original code shows these variables:

python 复制代码
pipeline_prompt.input_variables
# Output: ['example_a', 'person', 'example_q', 'input']
  • These come from the small prompts: person (from introduction), example_q/example_a (from example), input (from start).

3. Generate the Final Prompt

We fill in all required variables and print the result. The code and output are exactly as in the original:

Code to Format the Prompt

python 复制代码
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?"
))

Final Output

复制代码
You are impersonating Elon Musk.
    Here's an example of an interaction: 
    
    Q: What's your favorite car?
    A: Telsa
    Now, do this for real!
    
    Q: What's your favorite social media site?
    A:

Key Takeaway (No Extra Info)

PipelinePromptTemplate helps you reuse prompt parts (like the "impersonate" or "example" sections) so you don't rewrite code. All parts combine to make one final prompt, and you only need to fill in the required variables.

相关推荐
Boilermaker19927 小时前
[Java 并发编程] Synchronized 锁升级
java·开发语言
Cherry的跨界思维7 小时前
28、AI测试环境搭建与全栈工具实战:从本地到云平台的完整指南
java·人工智能·vue3·ai测试·ai全栈·测试全栈·ai测试全栈
alonewolf_997 小时前
JDK17新特性全面解析:从语法革新到模块化革命
java·开发语言·jvm·jdk
一嘴一个橘子7 小时前
spring-aop 的 基础使用(啥是增强类、切点、切面)- 2
java
sheji34167 小时前
【开题答辩全过程】以 中医药文化科普系统为例,包含答辩的问题和答案
java
恋爱绝缘体18 小时前
2020重学C++重构你的C++知识体系
java·开发语言·c++·算法·junit
wszy18098 小时前
新文章标签:让用户一眼发现最新内容
java·python·harmonyos
wszy18099 小时前
顶部标题栏的设计与实现:让用户知道自己在哪
java·python·react native·harmonyos
程序员小假9 小时前
我们来说一下无锁队列 Disruptor 的原理
java·后端
资生算法程序员_畅想家_剑魔10 小时前
Kotlin常见技术分享-02-相对于Java 的核心优势-协程
java·开发语言·kotlin