| by Arround The Web | No comments

How to Build Prompt Templates in LangChain?

LangChain is the framework containing multiple dependencies and libraries that can be used to build Large Language Models. These models can be used to interact with humans but first, the model must learn how to get/understand the prompt/question asked by the human. For that, the model needs to be trained on the prompt templates and then the user asks the question within the given template.

This guide will illustrate the process of building prompt templates in LangChain.

How to Build Prompt Templates in LangChain?

To build prompt templates in LangChain, simply go through the following guide with multiple steps:

Step 1: Install Modules and Setup Environment

Start the process of building prompt templates in LangChain by installing the LangChain framework:

pip install langchain

Now, install OpenAI modules to access its libraries and set an environment using it:

pip install openai

Set up the OpenAI environment using the os library to access the operating system and provide the OpenAI API key:

import os
import getpass

os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")

Step 2: Using Prompt Template

After installing LangChain, simply import the PromptTemplate library and build a template for the query about a joke with some extra aspects as variables like adjective, content, etc.:

from langchain import PromptTemplate

prompt_template = PromptTemplate.from_template(
    "Tell me a {style} joke about {theme}"
)
prompt_template.format(style="funny", theme="chickens")

The prompt has been set and given to the model with the values of the variable inserted in the command:

The user can customize the prompt template with a simple query asking for a joke:

from langchain import PromptTemplate

prompt_template = PromptTemplate.from_template(
"Tell me a joke"
)
prompt_template.format()

The above method is for a single query and answer but sometimes the user wants to interact with the model in the form of a chat and the next section explains its format.

Step 3: Using Chat Prompt Template

This section explains the template for a chat model which is based on a conversational pattern like two humans interacting with each other:

from langchain.prompts import ChatPromptTemplate

template = ChatPromptTemplate.from_messages([
    ("system", "AI chat bot to assist the user. You are called {name}."),
    ("human", "Hello, how do you do"),
    ("ai", "How do you do"),
    ("human", "{user_input}"),
])

messages = template.format_messages(
    name="John",
    user_input="What should i call you"
)

After setting the template structure, simply write some lines in the text to tell the model what is expected from it and use the llm() function to give a prompt:

from langchain.prompts import ChatPromptTemplate
from langchain.prompts.chat import SystemMessage, HumanMessagePromptTemplate

template = ChatPromptTemplate.from_messages(
    [
        SystemMessage(
            content=(
                "You are here to assist and help user to rewrite the user text more effectively"
            )
        ),
        HumanMessagePromptTemplate.from_template("{text}"),
    ]

)

from langchain.chat_models import ChatOpenAI

llm = ChatOpenAI()
llm(template.format_messages(text='i dont like eating tasty things'))

The SystemMessage() method contains the content of the reply for the query used in the LLM:

That is all about building prompt templates in LangChain.

Conclusion

To build a prompt template in LangChain, simply install LangChain and OpenAI modules to set up an environment using the OpenAI API key. After that, create a prompt template for a single prompt like asking for a joke or a single question about anything. Another method is customizing a template for a chat model based on the process of interaction between two different humans. This post has illustrated the process of building a prompt template in LangChain.

Share Button

Source: linuxhint.com

Leave a Reply