liveBook · Manning

FewShotPromptTemplate

As explained in Build AI-Enhanced Web Apps

FewShotPromptTemplate is a LangChain prompt-template class that constructs prompts from example inputs and outputs together with a task template.

FewShotPromptTemplate constructs structured prompts by combining reusable input-output examples with instructions and a task-specific input. Few-shot examples help language models follow the intended context, response format, and interaction pattern. section 6.1.1

Configure the prompt from five coordinated parts:

  • examples: reference question-answer pairs. section 6.1.1
  • examplePrompt: a PromptTemplate that formats each example, such as Question: {question} and Answer: {answer}. section 6.1.1
  • prefix: instructions placed before the examples; suffix: the concluding task, commonly Question: {input}. section 6.1.1
  • inputVariables: values supplied when formatting the completed prompt. section 6.1.1

Create and inspect a few-shot prompt

const examplePrompt = PromptTemplate.fromTemplate("Question: {question}\nAnswer: {answer}"); const prompt = new FewShotPromptTemplate({ examples, examplePrompt, prefix, suffix: "Question: {input}", inputVariables: ["input"], }); const formatted = await prompt.format("How should I respond?"); console.log(formatted); section 6.1.1

Figure 6.2 The FewShotPromptTemplate class takes five key parameters: examples (a list of question-answer pairs serving as reference examples), examplePrompt (a template for formatting each example), prefix (introductory text preceding the examples), suffix (concluding text following the examples, typically containing the main task or question), and inputVariables (specifying the variables to be provided when the prompt is used).

Use the parameter overview to verify how examples, formatting, instructions, suffix text, and runtime variables combine. section 6.1.1

How Build AI-Enhanced Web Apps covers it

How deep it goes
Explains parameters and JavaScript construction, then stops short of model invocation and production prompt management.
Examples
Uses question-answer examples, a formatted example prompt, instructional prefix text, and a runtime question input.
What it assumes
Assumes JavaScript template usage and introduces the roles of examples, templates, prefixes, suffixes, and input variables.
What you can do after
Build it from scratch by formatting reusable examples and a new input into a complete JavaScript prompt.
How it connects
Connects prompt templates with few-shot learning, language models, and chained LangChain workflows.

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.