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




















