liveBook · Manning

Template

As explained in Build AI-Enhanced Web Apps

A prompt template is a reusable specification for constructing prompts by inserting variable inputs and, optionally, example instructions.

A prompt template is a reusable way to construct a model prompt from fixed instructions and variable inputs. Instead of assembling a new string for every request, define placeholders such as {question} or {context}, then supply their values when the prompt is used. In LangChain.js, a template can also be one step in a chain between data preparation and the language model. section 6.1.1 section 7.2.4

Put a template in a chain

A chain uppercases input, counts its vowels, fills “Show the number {vowelCount} two times,” sends the result to ChatOpenAI, and parses the reply as a string. The step order matters: the template needs the vowelCount produced by the preceding function. LangChain.js can express the sequence with .pipe() or RunnableSequence.from(). section 6.1.1

Use a few-shot template when examples should guide the answer format. Format each question–answer example with an examplePrompt; place instructions before the examples in a prefix and the incoming question in a suffix. Declare the inputVariables that must be supplied when formatting. section 6.1.1

Listing 6.2 Creating a few-shot prompt template
const examplePrompt = PromptTemplate.fromTemplate(    #1
  "Question: {question}\nAnswer: {answer}"
);
const prefix = `You are an intelligent assistant designed 
to answer questions accurately and concisely. 
Below are some examples of how to approach different 
types of questions. Pay attention to whether follow-up 
questions are needed and how the final answer is presented. 
After reviewing these examples, please answer the user's 
question in a similar format.

Remember:
1. Determine if follow-up questions are needed.
2. If yes, ask the follow-up and provide an intermediate answer.
3. Always conclude with a final answer.

Here are some examples:`;    #2

const prompt = new FewShotPromptTemplate({    #3
  examples,
  examplePrompt,
  prefix,
  suffix: "Question: {input}",
  inputVariables: ["input"],
});

const formatted = await prompt.format({    #4
  input: "What is the capital of Canada?",
});

The listing shows how examples, examplePrompt, prefix, suffix, and inputVariables fit together in a FewShotPromptTemplate. section 6.1.1

To use that template in a streaming chat application, format it with the user’s input in a server-side conversation handler and pass the formatted result as a user message to streamUI. Keep provider API keys confidential. For retrieval-augmented generation, use a different template that inserts retrieved {context} and the {question}, instructing the model to answer only from that context. section 6.1.2 section 7.2.4

Listing 7.9 Performing augmentation of the response
async performRAG(query) {
    const prompt = ChatPromptTemplate
.fromTemplate(`    #1
        Answer the question based only on the context provided.

        Context: {context}

        Question: {question}`);

    const chain = RunnableSequence.from([    #2
      {
        context: this.retriever.pipe(formatDocs),
        question: new RunnablePassthrough(),
      },
      prompt,
      this.llm,AQ: 
      new StringOutputParser(),
    ]);

    const response = await chain.invoke(query);    #3
    const sourceDocuments = 
await this.retriever.invoke(query);    #4
    return {
      answer: response,
      sourceDocuments,
    };
  }

The RAG listing shows the context-and-question template inside a runnable sequence with a retriever, model, and output parser. section 7.2.4

How Build AI-Enhanced Web Apps covers it

How deep it goes
Works through placeholder substitution, few-shot components, and runnable-chain placement, without descending into model internals.
Examples
Uses a vowel-count chain, a question–answer few-shot template, a streaming chat handler, and a context-grounded RAG prompt.
What it assumes
Assumes JavaScript application setup and provider API keys, while introducing LangChain.js prompt construction through examples.
What you can do after
Apply it via LangChain.js to format user input and retrieved context before passing prompts to a model or streaming handler.
How it connects
Connects few-shot learning and prompt management to runnable chains, conversation history, the Vercel AI SDK, and retrieval-augmented generation.

Also explained in

Compare how 21 books explain template →

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.