Explaining LangChain Prompt Templates by Example
Rany ElHousieny, PhD???
Generative AI ENGINEERING MANAGER | ex-Microsoft | AI Solutions Architect | Generative AI & NLP Expert | Proven Leader in AI-Driven Innovation | Former Microsoft Research & Azure AI | Software Engineering Manager
LangChain is a powerful framework for developing applications powered by large language models (LLMs). One of its core features is the prompt template, which helps standardize and manage the prompts you send to the LLMs. Let's explore how LangChain's prompt templates work through a practical example using Python.
What is a Prompt Template?
A prompt template in LangChain is a structured way to create prompts that you send to the language model. It allows you to define a template with placeholders that can be filled with dynamic values at runtime. This helps in reusing prompts and ensures consistency in the way prompts are structured.
Example: Creating a Prompt Template
Let's say we want to create a prompt template for generating summaries of articles. Here’s how we can do it using LangChain.
1. Install LangChain:
First, ensure you have LangChain installed. You can install it using pip if you haven't already:
pip install langchain
2. Define the Prompt Template:
We will create a prompt template with placeholders for the article title and content.
from langchain.prompts import PromptTemplate
# Define the template with placeholders
template = """
Summarize the following article.
Title: {title}
Content: {content}
Summary:
"""
# Create a PromptTemplate object
prompt_template = PromptTemplate(template=template, input_variables=["title", "content"])
3. Use the Prompt Template:
Now, let's use the prompt template to generate a prompt for a specific article.
领英推荐
# Example article details
article_title = "The Future of AI"
article_content = """
Artificial intelligence (AI) is rapidly transforming industries across the globe.
From healthcare to finance, AI is being integrated into various sectors to improve efficiency and outcomes.
Experts predict that AI will continue to evolve and become even more integral to our daily lives in the coming years.
"""
# Fill the template with the article details
prompt = prompt_template.format(title=article_title, content=article_content)
# Print the generated prompt
print(prompt)
The generated prompt will look like this:
Summarize the following article.
Title: The Future of AI
Content:
Artificial intelligence (AI) is rapidly transforming industries across the globe.
From healthcare to finance, AI is being integrated into various sectors to improve efficiency and outcomes.
Experts predict that AI will continue to evolve and become even more integral to our daily lives in the coming years.
Summary:
4. Setup Local LLMs and a Chat function
Follow my previous article to install Ollama and create a function to communicate with different LLMs
At the end of the article, you will have a code similar to this:
from langchain_community.llms import Ollama
def chat_with(llm, prompt):
return llm.invoke(prompt)
phi3_llm = Ollama(model='phi3:3.8b')
response = chat_with(phi3_llm, "tell me a joke")
from pprint import pprint
pprint(response)
5. Send the Prompt to an LLM:
Finally, you can send this prompt to a language model using LangChain's interfaces.
# Get the summary from the language model
summary = chat_with(phi3_llm,prompt)
# Print the summary
print("Generated Summary:")
print(summary)
Generated Summary:
The article titled "The Future of AI" discusses how artificial intelligence (AI) is revolutionizing multiple industries worldwide, including healthcare and finance. By enhancing efficiency and outcomes, AI technologies are being increasingly adopted. With ongoing advancements, experts anticipate that AI will play an even more significant role in our lives as time progresses.
This example illustrates how you can create a reusable prompt template with LangChain, fill it with dynamic content, and use it to interact with a language model. Prompt templates are powerful tools for maintaining consistency and efficiency in your AI-powered applications.