LangChain

How to Use MessagePromptTemplate in LangChain?

LangChain is the framework to build Large Language Models or LLMs to ensure that the machine can interact with humans as humanly as possible. The developer needs to build the model by providing and setting the template or structure for the prompt/query from the user. It allows the model to understand the prompt swiftly and extract or generate text according to the prompt or query.

This post will illustrate the process of using MessagePromptTemplates in LangChain.

How to Use MessagePromptTemplate in LangChain?

LangChain offers multiple MessagePromptTemplates like HumanMessagePromptTemplate, SystemMessagePromptTemplate, AIMessagePromptTemplate, and many more. These templates are the most used prompt templates. To learn and use them, simply follow this guide:

Start the process of using the MessagePromptTemplate by installing the LangChain framework:

pip install langchain

Method 1: Using ChatMessagePromptTemplate

To use the MessagePromptTemplate, import the ChatMessgaePromptTemplate library which can be imported from the LangChain. After that, configure the prompt variable to set the query. Then, call the prompt template containing the role to be configured as other templates assume the role themselves:

from langchain.prompts import ChatMessagePromptTemplate

prompt = "May the {subject} be with you"

chat_message_prompt = ChatMessagePromptTemplate.from_template(role="Jedi", template=prompt)
chat_message_prompt.format(subject="force")

The output displays that the chat has been understood successfully by the model and the role has been set:

Method 2: Using Human and AI (MessagePromptTemplate)

The next method uses different MessagePromptTemplates like AI and Human templates by importing them from the LangChain framework. These formats do not allow the specification of the role so we use the MessagesPlaceholder library that gives complete control of which message to be used during formatting:

from langchain.prompts import MessagesPlaceholder
from langchain.schema import HumanMessage
from langchain.schema import AIMessage
from langchain.prompts import ChatPromptTemplate
from langchain.prompts import HumanMessagePromptTemplate

human_prompt = "Summarize our conversation so far in {word_count} words."
human_message_template = HumanMessagePromptTemplate.from_template(human_prompt)

chat_prompt = ChatPromptTemplate.from_messages([MessagesPlaceholder(variable_name="conversation"), human_message_template])

Now, simply set the templates for the query using Human Query and AI-generated text as the reply for the query and set the word_count to restrict the answer to a certain length:

human_message = HumanMessage(content="What is the best way to learn programming")
ai_message = AIMessage(content="""\
1 Learning a programming language requires deciding which one to learn as there are so many

2 Begin with the basics concepts of programming

3 Experience is the best option or method to learn the programming languages as the user needs to keep practicing\
"
"")

chat_prompt.format_prompt(conversation=[human_message, ai_message], word_count="10").to_messages()

That is all about how to use the MessagePromptTemplate in LangChain.

Conclusion

To use the MessagePromptTemplate in LangChain, simply start the process by installing the LangChain framework and then importing libraries from it. There are multiple types of MessagePromptTemplate like Human, AI, Chat, System, and many more to create the template for the model. This post has illustrated the process of using MessagePromptTemplate and its multiple types using 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.