liveBook · Manning

Template

As explained in AI Agents and Applications

A prompt template is a reusable structure that combines user input, context, and optionally examples into a model request.

A prompt template is a reusable instruction with variable fields. Fill those fields with a user’s input, context, or examples to create the request sent to a language model. Keeping the instruction fixed makes dynamic prompts easier to reuse than rebuilding them for every call. section 1.4.1 section 1.2.1

Start with a summary prompt

Suppose users need summaries in different lengths and tones. A Python function can insert `text`, `num_words`, and `tone` into one instruction. LangChain’s `PromptTemplate.from_template()` expresses the same pattern with `{text}`, `{num_words}`, and `{tone}` placeholders: call `.format()` with their values, then send the formatted prompt to `ChatOpenAI`. section 2.3.1 section 2.3.2

from langchain_core.prompts import PromptTemplate

prompt_template = PromptTemplate.from_template(
"""You are an experienced copywriter. 
Write a {num_words} words summary of the following text, 
using a {tone} tone: {text}""")

The listing shows a reusable summary instruction with three named fields rather than a prompt tied to one input. section 2.3.2

The same structure supports richer workflows:

  • For few-shot prompting, keep examples separate from the instruction and inject them with `FewShotPromptTemplate` instead of hardcoding them into one string. section 2.5.5
  • For research, pass instructions selected from one prompt into another that generates web searches; request JSON when the next processing step needs structured data. section 4.5.1
  • For retrieval-augmented Q&A, combine the question with retrieved context. An instruction to admit when the context lacks an answer can reduce made-up answers. section 7.1.2 section 6.3.2

A template organizes a request; it does not supply missing knowledge or guarantee a correct response. Test the filled prompt and its outputs, then adjust the instructions, examples, or retrieved context for the task. section 2.0 section 1.4.1

How AI Agents and Applications covers it

How deep it goes
Works from a Python function through formatting and invoking LangChain templates, then extends to few-shot and retrieval prompts.
Examples
Uses adjustable aqueduct summaries, number-classification examples, research-query generation, and a retrieved-context Q&A prompt.
What it assumes
Introduces the pattern with a Python function before using LangChain classes; assumes familiarity with basic Python functions and model calls.
What you can do after
Apply it via LangChain’s PromptTemplate to format variable inputs for a model, or FewShotPromptTemplate to manage injected examples.
How it connects
Links prompt engineering to few-shot prompting, retrieval-augmented generation, chains, and structured JSON output.

Also explained in

Compare how 21 books explain template →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents and Applications. The section links point to the parts of the book it draws on.