LangChain

How to Use Format Template Output in LangChain?

Artificial Intelligence or AI is the most rapidly growing technology across the globe as systems have been trained to act like humans. The basic application of the AI-based model is learning natural languages like English and other languages like it so humans can interact with them. LangChain is the AI framework that allows the user to build Large Language Models or chatbots so it can communicate with humans.

This post will illustrate the process of using the format template output in LangChain.

How to Use Format Template Output in LangChain?

To make LLMs or chatbots, it is required to train the machines so they can understand the natural languages. These models can be trained by building the templates for the output formats. To learn how to use them, simply follow this guide:

Step 1: Install Modules

Start the process by installing the LangChain framework. It contains all the required dependencies for using the format template output:

pip install langchain

 

Step 2: Importing Libraries

After installing the LangChain, simply import the libraries like FewShotChatMessagePromptTemplate and ChatPromptTemplate from the LangChain:

from langchain.prompts import (
    FewShotChatMessagePromptTemplate,
    ChatPromptTemplate,
)

<img src="https://linuxhint.com/wp-content/uploads/2023/09/How-to-Use-Format-Template-Output-in-LangChain-2.png" alt="" width="628" height="112" class="alignnone size-full wp-image-376359" />

<strong>Step 3: Configure Chat Prompt Template</strong>

After getting all the required libraries, simply configure the prompt template using the query defined in the “<strong>template</strong>” variable and then call the ChatPromptTemplate() method with message prompt parameters:
[cc lang="bash" width="100%" height="100%" escaped="true" theme="blackboard" nowrap="0"]
from langchain.prompts.chat import (
    ChatPromptTemplate,
    SystemMessagePromptTemplate,
    HumanMessagePromptTemplate,
)

template = "You are a helper that translates {input_language} to {output_language}"
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
human_template = "{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)

chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])

chat_prompt.format_messages(input_language="Spanish", output_language="English", text="I love LangChain")

 

The output displayed in the following screenshot suggests that the SystemMessagePromptTemplate and HumanMessagePromptTemplate are configured successfully:

Step 4: Assign Template to Variable

The next step is to define the “output_2” variable with the chat_prompt() method. In this way, assign the template to the “assert output” variable:

output_2 = chat_prompt.format_prompt(input_language="English", output_language="French", text="I love AI").to_string()

assert output == output_2

 

Now, use the chat_prompt() method with multiple parameters like input and output language, along with text such as “I love LangChain”:

chat_prompt.format_prompt(input_language="English", output_language="French", text="I love LangChain").to_messages()

 

Step 5: Using ChatPromptValue

The last step calls the function without any additional format method and displays the output on the screen:

chat_prompt.format_prompt(input_language="English", output_language="French", text="I love AI")

 

That’s all about using the format template output in LangChain.

Conclusion

To use the format template output in LangChain, simply install the LangChain framework to import all the required libraries and dependencies. After that, configure the prompt template and then call the ChatPromptTemplate() method with string and message methods. This post has illustrated the process of using the format template output 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.