*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to select examples by length

On this page

How to select examples by length
================================

Prerequisites

This guide assumes familiarity with the following concepts:

*   [Prompt templates](/v0.2/docs/concepts/#prompt-templates)
*   [Example selectors](/v0.2/docs/how_to/example_selectors)

This example selector selects which examples to use based on length. This is useful when you are worried about constructing a prompt that will go over the length of the context window. For longer inputs, it will select fewer examples to include, while for shorter inputs it will select more.

    import { PromptTemplate, FewShotPromptTemplate } from "@langchain/core/prompts";import { LengthBasedExampleSelector } from "@langchain/core/example_selectors";export async function run() {  // Create a prompt template that will be used to format the examples.  const examplePrompt = new PromptTemplate({    inputVariables: ["input", "output"],    template: "Input: {input}\nOutput: {output}",  });  // Create a LengthBasedExampleSelector that will be used to select the examples.  const exampleSelector = await LengthBasedExampleSelector.fromExamples(    [      { input: "happy", output: "sad" },      { input: "tall", output: "short" },      { input: "energetic", output: "lethargic" },      { input: "sunny", output: "gloomy" },      { input: "windy", output: "calm" },    ],    {      examplePrompt,      maxLength: 25,    }  );  // Create a FewShotPromptTemplate that will use the example selector.  const dynamicPrompt = new FewShotPromptTemplate({    // We provide an ExampleSelector instead of examples.    exampleSelector,    examplePrompt,    prefix: "Give the antonym of every input",    suffix: "Input: {adjective}\nOutput:",    inputVariables: ["adjective"],  });  // An example with small input, so it selects all examples.  console.log(await dynamicPrompt.format({ adjective: "big" }));  /*   Give the antonym of every input   Input: happy   Output: sad   Input: tall   Output: short   Input: energetic   Output: lethargic   Input: sunny   Output: gloomy   Input: windy   Output: calm   Input: big   Output:   */  // An example with long input, so it selects only one example.  const longString =    "big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else";  console.log(await dynamicPrompt.format({ adjective: longString }));  /*   Give the antonym of every input   Input: happy   Output: sad   Input: big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else   Output:   */}

#### API Reference:

*   [PromptTemplate](https://v02.api.js.langchain.com/classes/langchain_core_prompts.PromptTemplate.html) from `@langchain/core/prompts`
*   [FewShotPromptTemplate](https://v02.api.js.langchain.com/classes/langchain_core_prompts.FewShotPromptTemplate.html) from `@langchain/core/prompts`
*   [LengthBasedExampleSelector](https://v02.api.js.langchain.com/classes/langchain_core_example_selectors.LengthBasedExampleSelector.html) from `@langchain/core/example_selectors`

Next steps[​](#next-steps "Direct link to Next steps")
------------------------------------------------------

You've now learned a bit about using a length based example selector.

Next, check out this guide on how to use a [similarity based example selector](/v0.2/docs/how_to/example_selectors_similarity).

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchainjs/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

How to combine results from multiple retrievers

](/v0.2/docs/how_to/ensemble_retriever)[

Next

How to select examples by similarity

](/v0.2/docs/how_to/example_selectors_similarity)

*   [Next steps](#next-steps)