What is LangChain PromptTemplate?

LangChain's PromptTemplate is a class within the LangChain framework, designed to facilitate the creation of structured prompts for language models. The primary purpose of PromptTemplate is to provide a flexible and efficient means of crafting prompts that can effectively guide language models in producing desired outputs. This tool uses a template-based approach, allowing for dynamic content inclusion through placeholders that can be filled with specific values at runtime, making it possible to generate a wide range of prompts from a single template​

For example, a PromptTemplate might be structured as “Write a brief article about {topic} in the style of {author}”. Here, the placeholders {topic} and {author} can be replaced with specific values like “artificial intelligence” and “Edgar Allen Poe”, respectively, to generate a tailored prompt for a language model. This not only saves time but also empowers users to experiment with different combinations of variables, leading to more creative and diverse model interactions.

How prompt templates are created
How prompt templates are created

How these templates work with different models?

One of the defining features of LangChain's PromptTemplate is its model-agnostic nature. This characteristic is crucial because it allows these templates to be used universally across various language models, regardless of their underlying architecture or training data. This interoperability is vital in a landscape where numerous language models exist, each with unique capabilities and specifications. This universality is beneficial for developers and users who work with multiple models, as it streamlines the process of prompt creation and reduces the complexity involved in tailoring prompts to each specific model.

By abstracting away the model-specific intricacies, PromptTemplate allows users to focus on the content and intent of their prompts, rather than the technicalities of model compatibility. This facilitates a more intuitive and user-friendly experience in crafting prompts that are both effective and versatile.

Note: Behind the scenes, LangChain's PromptTemplate works by utilizing Python's str.format syntax to allow users to define templates with placeholders for dynamic content.

An example of using PromptTemplate

Let's explore how we can harness the power of language models to generate articles that are not only engaging but also styled after famous authors with the help of a dynamic prompt.

First, let's create a PromptTemplate that acts like a blueprint for our article generation. Our template goes something like this:

article_prompt = PromptTemplate.from_template("""
Write a brief article about '{topic}' in the style of '{author}'
""")
Prompt template

Here, we're laying down the groundwork, specifying that our article should revolve around a certain topic and be penned in the unique style of a chosen author. These placeholders, '{topic}' and '{author}', are like empty slots waiting to be filled with the user's inputs.

It's time to assign values to these variables. We're intrigued by the possibilities of "artificial intelligence" and wonder how it would sound through the gothic tone of "Edgar Allen Poe." So, we set our variables:

topic = "artificial intelligence"
author = "Edgar Allen Poe"
Topic and author selection

Now, we can introduce the star of our show, our LLM, specifically the gpt-3.5-turbo, ready to bring our template to life:

llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.7)
LLM setup

We're also setting a temperature to adjust the creativity level, ensuring our article has just the right amount of flair. When we adjust the temperature setting for our language model, we're essentially fine-tuning how bold or conservative the model's responses will be. Think of temperature as a dial on creativity and randomness. A lower temperature means the model will stick closer to the most probable responses, making it more predictable and less likely to venture into creative or unexpected territory. This can be great for more straightforward tasks where accuracy and reliability are key

With everything in place, we set the wheels in motion with LangChain's LLMChain, which binds our template, topic, and author together, instructing our model to start writing:

chain = LLMChain(llm=llm, prompt=article_prompt, output_parser=StrOutputParser())
article = chain.run(topic=topic, author=author)
Generating the article

The LLMChain is essentially a pipeline that connects our language model (llm) with our prompt (article_prompt) and specifies how the output should be parsed (StrOutputParser()). It's like setting up a production line where the raw materials (prompt and LLM) are input, and the finished product (the generated article) is what we aim to get out at the end. The chain.run(topic=topic, author=author) part is where the magic happens. By invoking the run method on the chain, we're essentially telling the LLM to start writing based on the article_prompt, filling in the topic and author with the values we've provided earlier. The run method executes the entire pipeline we've set up with LLMChain, and the result is our generated article, which is stored in the variable article.

We can witness what we have generated as we do, with a simple print statement as follows:

print(f"Generated Article:\n\n{article}")
Printing the generated article

At this stage we will see our generated article but to take things one level further and see the true power of PromptTemplate let's change things up. How about if we don't want Edgar Allen Poe's tone for our article but something brighter like Dr.Seuss' tone? We can simply do so by changing the value of our author variable as follows:

author = "Dr. Seuss"
article = chain.run(topic=topic, author=author)
print(f"Generated Article:\n\n{article}")
Changing the author

Now the article on the specified topic will be generated with our new selected author. This process showcases the power of LangChain and language models in automating content creation with the help of PromptTemplate.

Try it yourself

Dive into the Jupyter Notebook below to see the LangChain's PromptTemplate mechanisms in action and discover how they can transform conversational AI applications yourself.

Please note that the notebook cells have been pre-configured to display the outputs
for your convenience and to facilitate an understanding of the concepts covered. 
However, if you possess the key, you are encouraged to actively engage with the material
by changing the variable values. This hands-on approach will allow you to
experiment with the memory techniques discussed, providing a more immersive learning experience.
Try it yourself

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved