Langchain Chat Prompt Templates, A Dive into Input and Partial Variables


In Langchain, when using chat prompt templates there are the two relevant but confustion concepts of inoput variable and partial variable. Let’s understand the difference.

Understanding Input Variables:

Input variables are fundamental placeholders in a Langchain chat prompt template, awaiting specific values to complete the template. When a developer constructs a prompt template, they denote the input variables that will be requisite to formulate a complete prompt. These variables are then supplied at the time of formatting the template, making them a crucial component for dynamic prompt creation.

Embracing Partial Variables:

On the flip side, partial variables come to the forefront when some values required by the prompt template are available beforehand, while others are anticipated later. This scenario unfolds a feature known as “partialing” where a prompt template is pre-filled with known values, creating a new template that awaits only the remaining unknown values. This mechanism allows for early binding of known values and deferred binding of others, proving invaluable in dynamic or staged processing environments.

Illustrative Examples:
Let’s delve into some examples to visualize the utility of input and partial variables in Langchain chat prompt templates.

Example 1: Simple Partialing

Imagine a scenario where a prompt template necessitates two variables, foo and bar. If foo is obtained early on, but bar is acquired later, one can “partial” the prompt template with the foo value, and later use the partialed prompt template when the bar value is available.

from langchain.prompts import PromptTemplate   

prompt = PromptTemplate(template="{foo}{bar}", input_variables=["foo", "bar"])
partial_prompt = prompt.partial(foo="foo");
print(partial_prompt.format(bar="baz")) # Output: foobaz

Alternatively, you can initialize the prompt template with the partialed variables directly:

prompt = PromptTemplate(template="{foo}{bar}", input_variables=["bar"], partial_variables={"foo": "foo"})  
print(prompt.format(bar="baz")) # Output: foobaz

Example 2: Partialing with Functions

In another instance, if a prompt template always necessitates the current date, partial variables can be utilized with functions to cater to this requirement.

from datetime import datetime   

def _get_datetime():
now = datetime.now()
return now.strftime("%m/%d/%Y, %H:%M:%S")

prompt = PromptTemplate(
template="Tell me a {adjective} joke about the day {date}",
input_variables=["adjective", "date"]
)
partial_prompt = prompt.partial(date=_get_datetime)
print(partial_prompt.format(adjective="funny")) # Output: Tell me a funny joke about the day 02/27/2023, 22:15:16

And again, you can initialize the prompt template with the partialed variables directly, often a sensible approach in this workflow:

prompt = PromptTemplate(  
template="Tell me a {adjective} joke about the day {date}",
input_variables=["adjective"],
partial_variables={"date": _get_datetime}
)
print(prompt.format(adjective="funny")) # Output: Tell me a funny joke about the day 02/27/2023, 22:15:16

Author: robot learner
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source robot learner !
  TOC