LangChain

How to Build Template Formats in LangChain?

The Large Language Models or LLMs are used to create an interactive model that can communicate with humans in natural languages. The user needs to configure the template for the prompts so the model can understand the text and then generate the answer efficiently. To generate the text in natural language, the model needs to be trained on the dataset in the natural language.

This post will illustrate the process of building template formats in LangChain.

How to Build Template Formats in LangChain?

Python is the most effective programming language that uses “jinja2” and “fstring” template formats as the fstring is used by default. To learn how to build a template format in LangChain, simply follow this guide:

Prerequisite: Install LangChain

Firstly, install the LangChain framework that contains the PromptTemplate libraries which can be used to build template formats. LangChain framework installs all the required dependencies to build the structure of the query for the LLMs or chatbots:

pip install langchain

Method 1: Using jinja2 Template

After that, import the PromptTemplate library to use the jinja2 template containing the query with variables that are defined in the prompt.format() method. The jinja2 format is specified as the parameter of the PromptTemplate() method and assigned to the prompt variable:

from langchain.prompts import PromptTemplate

jinja2_template = "Tell me a {{ style }} poem about {{ theme }}"
prompt = PromptTemplate.from_template(jinja2_template, template_format="jinja2")

prompt.format(style="motivational", theme="earth")

The output displays that the model has used the values of the variable in the query correctly after understanding it:

Method 2: Using fstring Template

The second method uses the fstring template format which is used by default as the PromptTemplate by the Python programming language. For instance, the “fstring_template” variable contains the query and then calls the PromptTemplate() method with the variable inside it to build the template format:

from langchain.prompts import PromptTemplate

fstring_template = """Tell me a {style} poem about {theme}"""
prompt = PromptTemplate.from_template(fstring_template)

prompt.format(style="motivational", theme="earth")

That is all about the process of building template formats in LangChain.

Conclusion

To build the template format in LangChain, simply start the process by installing the LangChain framework. It contains all the dependencies for using the PromptTemplate() function. It uses the fstring template format by default for the Python programming languages. The user can also use the jinja2 template using the template_format parameter. This guide has explained both the PromptTemplate formats to build the template in LangChain.

About the author

Talha Mahmood

As a technical author, I am eager to learn about writing and technology. I have a degree in computer science which gives me a deep understanding of technical concepts and the ability to communicate them to a variety of audiences effectively.