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

Partial Prompt Templates in LangChain

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

Partial Prompt Templates in LangChain

Partial prompt templates (called “partial” for short) are a useful feature in LangChain. Their core purpose is: fill in a subset of the required values first, then create a new prompt template that only needs the remaining values.

LangChain supports two main ways to use partial prompt templates. Below, we’ll explain each method with simple language and the exact code/examples from the original source (no changes or omissions).

Method 1: Partial Formatting with String Values

When to use this?

This is for cases where you get some variables early (e.g., at the start of your code) and other variables later (e.g., from user input). Instead of waiting to pass all variables at once, you can fill in the early variables first.

Example 1: Use the .partial() method

Suppose we have a prompt template that needs two variables: foo and bar. We get foo first, then bar later.

Step 1: Import and create the original prompt template
from langchain.prompts import PromptTemplate# The template needs two variables: {foo} and {bar}
prompt = PromptTemplate(template="{foo}{bar}", input_variables=["foo", "bar"])
Step 2: Fill in the “early” variable (foo) with .partial()

This creates a new partial prompt template that only needs bar now.

partial_prompt = prompt.partial(foo="foo")  # Fill in {foo} with the string "foo"
Step 3: Fill in the remaining variable (bar)

Use .format() to pass the remaining variable (bar):

print(partial_prompt.format(bar="baz"))  # Pass {bar} with "baz"
Output
foobaz

Example 2: Initialize with partial_variables

You can also fill in the “early” variable when creating the prompt template (instead of using .partial() later).

Code
# input_variables only lists the remaining variable: {bar}
# partial_variables fills in {foo} upfront
prompt = PromptTemplate(template="{foo}{bar}", input_variables=["bar"],  # Only need {bar} laterpartial_variables={"foo": "foo"}  # Fill {foo} with "foo" now
)# Only pass {bar} to format()
print(prompt.format(bar="baz"))
Output
foobaz

Method 2: Partial Formatting with Functions

When to use this?

This is for cases where a variable needs to be automatically fetched in the same way every time (e.g., current date/time). You don’t want to hardcode it, and you don’t want to pass it manually every time—so you use a function to get its value.

Example: Get current date automatically

Suppose we have a prompt template that needs two variables: adjective (from user input) and date (current date, fetched automatically).

Step 1: Define a function to get the value

First, create a function that returns the current date as a string:

from datetime import datetimedef _get_datetime():now = datetime.now()  # Get current timereturn now.strftime("%m/%d/%Y, %H:%M:%S")  # Format as "month/day/year, hour:minute:second"
Example 1: Use the .partial() method
Step 1: Create the original prompt template
prompt = PromptTemplate(template="Tell me a {adjective} joke about the day {date}", input_variables=["adjective", "date"]  # Needs {adjective} and {date}
)
Step 2: Link the date variable to the function

Use .partial() to set date to the result of _get_datetime() (note: we pass the function itself, not its result—no parentheses () after the function name):

partial_prompt = prompt.partial(date=_get_datetime)
Step 3: Fill in the remaining variable (adjective)
print(partial_prompt.format(adjective="funny"))
Output
Tell me a funny joke about the day 02/27/2023, 22:15:16  # Date matches when you run the code
Example 2: Initialize with partial_variables

This is more common for function-based partialing (since the function runs automatically when needed).

Code
prompt = PromptTemplate(template="Tell me a {adjective} joke about the day {date}", input_variables=["adjective"],  # Only need {adjective} laterpartial_variables={"date": _get_datetime}  # Link {date} to the function
)# Only pass {adjective} to format()
print(prompt.format(adjective="funny"))
Output
Tell me a funny joke about the day 02/27/2023, 22:15:16  # Date matches when you run the code
http://www.dtcms.com/a/568680.html

相关文章:

  • 泉州网站平台建设公司网站建设素材图
  • 计算机技术员网站建设怎么网站底部 设计
  • 第50届ICPC亚洲区域赛·成都站,非凸科技持续护航顶尖赛事
  • 企业微信自建应用开发详细教程,如何获取授权链接?如何使用js-sdk?
  • html css js网页制作成品——高定晚礼服HTML+CSS网页设计(5页)附源码
  • 蓝牙钥匙 第43次 特殊用户群体场景下的汽车数字钥匙系统:包容性设计与技术创新
  • 万网如何建设购物网站wordpress分类目录 菜单 页面
  • 智能网联汽车 HD map架构解析
  • HTML常用单标签速查手册
  • 告别算法死记硬背,Hello-Algo 让抽象知识变直观,搭配cpolar穿透工具更自由
  • Go从入门到精通(27) - 并行任务处理器
  • Claude Code 使用 MiniMax M2 模型
  • Auto CAD二次开发——复制和旋转图形对象
  • 全屏响应式网站模板网站seo综合公司
  • php做简单网站教程视频教程企业门户网站模板 下载
  • Rust开发实战之WebSocket通信实现(tokio-tungstenite)
  • 编译缓存利器 ccahce、sccahce
  • Rust开发实战之使用 Reqwest 实现 HTTP 客户端请求
  • 各大公司开源网站广州出台21条措施扶持餐饮住宿
  • gmt_create为啥叫gmt
  • 从 NGINX 到 Kubernetes Ingress:现代微服务流量管理实战
  • 【C++】继承(2):继承与友元,静态成员,多继承黑/白盒复用
  • css实战:常用伪元素选择器介绍
  • 4.4 路由算法与路由协议【2013统考真题】
  • 营销型网站建设需要备案吗上饶网站建设企业
  • 福建网站建设科技有限公司品牌建设还需持续力
  • 工业CMOS相机的原理及基础知识
  • 无人机电气隔离与抗干扰技术概述
  • Elasticsearch的学习
  • GitHub 热榜项目 - 日榜(2025-11-04)