liveBook · Manning

ChatPromptTemplate

As explained in Build AI-Enhanced Web Apps

ChatPromptTemplate is a LangChain class that constructs chat prompts from a reusable template and supplied values.

ChatPromptTemplate constructs reusable chat prompts from a template and supplied values, allowing LangChain chains to insert dynamic inputs before invoking a model. section 6.1.1

Use it to:

  • Define placeholders such as {vowelCount} and let the framework validate required values. section 6.1.1
  • Reuse one prompt structure across modules while varying tone, length, content, or other inputs. section 6.1.1
  • Pass a prompt object through runnable pipelines that continue into a language model and output parser. section 6.1.1

Use a template inside a runnable chain

const prompt = ChatPromptTemplate.fromTemplate("Show the number {vowelCount} two times."); const sequence = RunnableSequence.from([ RunnableLambda.from(toUpperCase), RunnableLambda.from(vowelCountFunction), prompt, model, new StringOutputParser(), ]); section 6.1.1

Listing 6.1 Example using runnable chains
import { StringOutputParser } from "@langchain/core/output_parsers";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { RunnableLambda } from "@langchain/core/runnables";
import { ChatOpenAI } from "@langchain/openai";
import "dotenv/config";
const apiKey = process.env.OPENAI_API_KEY;

const toUpperCase = (input) => {    #1
  return {
    uppercased: input.text.toUpperCase(),
  };
};

const vowelCountFunction = (input) => {    #2
  const vowels = input.uppercased.match(/[AEIOU]/gi);
  return {
    vowelCount: vowels ? vowels.length : 0,
  };
};
const model = new ChatOpenAI({ openAIApiKey: apiKey, 
model: "gpt-4o" });    #3
const prompt = ChatPromptTemplate.fromTemplate("Show the number {vowelCount} two times.");    #4
const chain = RunnableLambda.from(toUpperCase).   #5
  .pipe(RunnableLambda.from(vowelCountFunction))
  .pipe(prompt)
  .pipe(model)
  .pipe(new StringOutputParser());

await chain.invoke({text: "hello world"})
.then((output) => {    #6
    console.log(output);
});

Inspect the complete runnable-chain listing, including the template, model, transformations, and output parser. section 6.1.1

How Build AI-Enhanced Web Apps covers it

How deep it goes
Explains factory construction, placeholders, validation, reuse, and pipeline placement, but defers detailed ChatPromptTemplate internals.
Examples
Uses a vowel-counting chain and a {vowelCount} template, alongside a separate FewShotPromptTemplate example.
What it assumes
Assumes familiarity with LangChain runnable pipelines and model calls while demonstrating prompt construction through a concrete chain.
What you can do after
Build reusable JavaScript prompt stages and compose them with transformations, models, and output parsers.
How it connects
Connects ChatPromptTemplate with runnable chains, language models, output parsers, and few-shot prompting.

Also explained in

Compare how 2 books explain ChatPromptTemplate →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.