LangChain is the framework that is used to build applications or bots that can chat with humans in their language like English. These models can interact with humans and store these chats to get the reference for the next query in the chat to use them as the context. To build these applications, the developers need to train them on the prompts and environment using the text data.
This post illustrates the process of using partial prompt templates in LangChain.
How to Use Partial Prompt Templates in LangChain?
Prompts are the most important aspect of the chatbots and LLMs. The system must generate answers after understanding the prompts. The LangChain supports the use of partial prompts as the user can only pass the subset of desired output and the system would be able to get that value.
To learn the process of using the partial prompt templates in LangChain, simply go through the listed steps:
Step 1: Install LangChain
Start the process of using the partial prompt template by installing the LangChain framework:
Step 2: Partial Prompt With Strings
After installing the LangChain framework, simply get the library to use the prompt template:
The example uses the partial prompt template with two variables “foo” and “baz” and simply use the template to get both values simultaneously:
partial_prompt = prompt.partial(foo="foo");
print(partial_prompt.format(bar="baz"))
Running the partial template returns the “foobaz” output without having to wait for the other:
Step 3: Partial Prompt With Functions
Another method of using the partial function that is supported by the LangChain. It is possible with a variable you always want to return. The following example gets the data and time that will be returned with all the outputs after defining the _get_datetime() method:
def _get_datetime():
now = datetime.now()
return now.strftime("%m/%d/%Y, %H:%M:%S")
Now, simply run the partial prompt template to return the prompt for the model, and the timestamp will also be displayed along with it:
template="What is a {style} joke of the day {date}",
input_variables=["style", "date"]
);
partial_prompt = prompt.partial(date=_get_datetime)
print(partial_prompt.format(style="funny"))
The user can simply initialize the prompt or query with the partial variable to call the function:
template="What is a {style} joke of the day {date}",
input_variables=["style"],
partial_variables={"date": _get_datetime}
);
print(prompt.format(style="funny"))
That is all about using the partial prompt templates in LangChain.
Conclusion
To use the partial prompt template in LangChain, simply install the LangChain framework and import the partial template library. The LangChain framework supports multiple types of partial templates like using strings and functions to get the variable with the prompt. This post has illustrated the process of using the partial prompt templates using both methods through the LangChain framework.