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 |
Alternatively, you can initialize the prompt template with the partialed variables directly:
prompt = PromptTemplate(template="{foo}{bar}", input_variables=["bar"], partial_variables={"foo": "foo"}) |
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 |
And again, you can initialize the prompt template with the partialed variables directly, often a sensible approach in this workflow:
prompt = PromptTemplate( |