当前位置: 首页 > news >正文

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:

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:

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)
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)
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)
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.

Step 4: Link Small Prompts to the Final Prompt

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

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:

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:

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

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: TelsaNow, 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.

http://www.dtcms.com/a/568896.html

相关文章:

  • 【HarmonyOS NEXT】常见的性能优化
  • [Ethernet in CANoe]2--如何在CANoe中去仿真CP版本的SOME/IP通信
  • 优先队列(堆)
  • 基于 TCP 线程池服务器封装 HTTP 服务器:从协议解析到适配落地
  • xargs
  • 据库事务是数据库管理系统 ACID 四大特性
  • 宜昌市住房和城乡建设局网站wordpress后台慢
  • SSM基于HTML5的流浪动物领养平台yww0b(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • 网站栏目分类网站开发市场
  • word转Pdf,在window正常,放在linux服务器上就转出来中文是空白
  • 攻防世界-Misc-pdf
  • 开启RN之旅——前端基础
  • 【LeetCode】99. 恢复二叉搜索树
  • 【rhcsa第一次作业】
  • 哪个网站做图找图片上海网络推广公司排名
  • 订单支付后库存不扣减,如何用RabbitMQ来优化?
  • Qt对话框设计
  • 解决 contents have differences only in line separators
  • 无锡建站方案深圳百度总部
  • Docker中安装 redis、rabbitmq、MySQL、es、 mongodb设置用户名密码
  • SAP EXCEL模板下载导入
  • 动态贝叶斯网络物联网应用方式
  • Oracle OCP认证:深度解析与实战指南
  • 帝国建设网站wordpress迅雷插件下载
  • HTTP 请求与数据交互全景指南:Request、GET、POST、JSON 及 curl
  • 如何进一步推动淘宝商品详情API的安全强化与生态协同创新?
  • Flutter | 基础环境配置和创建flutter项目
  • 58同城网站建设排名wordpress页面生成二维码
  • 怎么在子域名建立一个不同的网站怎么通过ip查看自己做的网站
  • UVa 11027 Palindromic Permutation