AI Hints
Home›LangChain Tutorial›Prompt Templates
LangChain · Lesson 13 of 120

Prompt Templates

Fill in the blanks.

⏱ 2 min read▶ Video at 12:58🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

Most prompts in an app follow a pattern, with a few parts that change. Write a description of this book, for this audience. A prompt template is that pattern, with blanks to fill in.

Prompt Templates
From the video: Prompt Templates
  • Prompts follow patterns
  • A few parts change
  • Templates with blanks

A prompt template

Prompt template comes from lang chain core. Curly braces mark the blanks, here book and audience. Invoke fills them in, and gives back a finished prompt. Run it. The template becomes a complete sentence. We can reuse it for any book, and any audience.

template.pypython
from langchain_core.prompts import PromptTemplate

template = PromptTemplate.from_template(
    "Describe the book {book} in one sentence for {audience}."
)

prompt = template.invoke({"book": "The Hobbit", "audience": "young children"})
print(prompt.text)
print("blanks:", template.input_variables)
Output
Describe the book The Hobbit in one sentence for young children.
blanks: ['audience', 'book']

Why templates help

Templates keep prompt wording in one place, so you can improve it without hunting through your code. They make it clear which parts change. They check that every blank is filled. And they plug straight into chains, as we'll see soon.

keyvalue
One placeeasy to improve
Clear inputssee what changes
Checkedno missing blanks
Chainableplug into chains

Summary

A prompt template is a reusable prompt with named blanks in curly braces. Invoke fills them in. Next, templates for chat models, with roles.

Key takeaways
  • Curly-brace blanks
  • template.invoke({...})
  • Reuse everywhere
Next up: Chat Prompt Templates — roles in templates