{"version":3,"file":"length_based.cjs","names":["BaseExampleSelector"],"sources":["../../src/example_selectors/length_based.ts"],"sourcesContent":["import { Example } from \"../prompts/base.js\";\nimport { BaseExampleSelector } from \"./base.js\";\nimport { PromptTemplate } from \"../prompts/prompt.js\";\n\n/**\n * Calculates the length of a text based on the number of words and lines.\n */\nfunction getLengthBased(text: string): number {\n  return text.split(/\\n| /).length;\n}\n\n/**\n * Interface for the input parameters of the LengthBasedExampleSelector\n * class.\n */\nexport interface LengthBasedExampleSelectorInput {\n  examplePrompt: PromptTemplate;\n  maxLength?: number;\n  getTextLength?: (text: string) => number;\n}\n\n/**\n * A specialized example selector that selects examples based on their\n * length, ensuring that the total length of the selected examples does\n * not exceed a specified maximum length.\n * @example\n * ```typescript\n * const exampleSelector = new LengthBasedExampleSelector(\n *   [\n *     { input: \"happy\", output: \"sad\" },\n *     { input: \"tall\", output: \"short\" },\n *     { input: \"energetic\", output: \"lethargic\" },\n *     { input: \"sunny\", output: \"gloomy\" },\n *     { input: \"windy\", output: \"calm\" },\n *   ],\n *   {\n *     examplePrompt: new PromptTemplate({\n *       inputVariables: [\"input\", \"output\"],\n *       template: \"Input: {input}\\nOutput: {output}\",\n *     }),\n *     maxLength: 25,\n *   },\n * );\n * const dynamicPrompt = new FewShotPromptTemplate({\n *   exampleSelector,\n *   examplePrompt: new PromptTemplate({\n *     inputVariables: [\"input\", \"output\"],\n *     template: \"Input: {input}\\nOutput: {output}\",\n *   }),\n *   prefix: \"Give the antonym of every input\",\n *   suffix: \"Input: {adjective}\\nOutput:\",\n *   inputVariables: [\"adjective\"],\n * });\n * console.log(dynamicPrompt.format({ adjective: \"big\" }));\n * console.log(\n *   dynamicPrompt.format({\n *     adjective:\n *       \"big and huge and massive and large and gigantic and tall and much much much much much bigger than everything else\",\n *   }),\n * );\n * ```\n */\nexport class LengthBasedExampleSelector extends BaseExampleSelector {\n  protected examples: Example[] = [];\n\n  examplePrompt!: PromptTemplate;\n\n  getTextLength: (text: string) => number = getLengthBased;\n\n  maxLength = 2048;\n\n  exampleTextLengths: number[] = [];\n\n  constructor(data: LengthBasedExampleSelectorInput) {\n    super(data);\n    this.examplePrompt = data.examplePrompt;\n    this.maxLength = data.maxLength ?? 2048;\n    this.getTextLength = data.getTextLength ?? getLengthBased;\n  }\n\n  /**\n   * Adds an example to the list of examples and calculates its length.\n   * @param example The example to be added.\n   * @returns Promise that resolves when the example has been added and its length calculated.\n   */\n  async addExample(example: Example): Promise<void> {\n    this.examples.push(example);\n    const stringExample = await this.examplePrompt.format(example);\n    this.exampleTextLengths.push(this.getTextLength(stringExample));\n  }\n\n  /**\n   * Calculates the lengths of the examples.\n   * @param v Array of lengths of the examples.\n   * @param values Instance of LengthBasedExampleSelector.\n   * @returns Promise that resolves with an array of lengths of the examples.\n   */\n  async calculateExampleTextLengths(\n    v: number[],\n    values: LengthBasedExampleSelector\n  ): Promise<number[]> {\n    if (v.length > 0) {\n      return v;\n    }\n\n    const { examples, examplePrompt } = values;\n    const stringExamples = await Promise.all(\n      examples.map((eg: Example) => examplePrompt.format(eg))\n    );\n    return stringExamples.map((eg: string) => this.getTextLength(eg));\n  }\n\n  /**\n   * Selects examples until the total length of the selected examples\n   * reaches the maxLength.\n   * @param inputVariables The input variables for the examples.\n   * @returns Promise that resolves with an array of selected examples.\n   */\n  async selectExamples(inputVariables: Example): Promise<Example[]> {\n    const inputs = Object.values(inputVariables).join(\" \");\n    let remainingLength = this.maxLength - this.getTextLength(inputs);\n    let i = 0;\n    const examples: Example[] = [];\n\n    while (remainingLength > 0 && i < this.examples.length) {\n      const newLength = remainingLength - this.exampleTextLengths[i];\n      if (newLength < 0) {\n        break;\n      } else {\n        examples.push(this.examples[i]);\n        remainingLength = newLength;\n      }\n      i += 1;\n    }\n\n    return examples;\n  }\n\n  /**\n   * Creates a new instance of LengthBasedExampleSelector and adds a list of\n   * examples to it.\n   * @param examples Array of examples to be added.\n   * @param args Input parameters for the LengthBasedExampleSelector.\n   * @returns Promise that resolves with a new instance of LengthBasedExampleSelector with the examples added.\n   */\n  static async fromExamples(\n    examples: Example[],\n    args: LengthBasedExampleSelectorInput\n  ) {\n    const selector = new LengthBasedExampleSelector(args);\n    await Promise.all(examples.map((eg) => selector.addExample(eg)));\n    return selector;\n  }\n}\n"],"mappings":";;;;;AAOA,SAAS,eAAe,MAAsB;AAC5C,QAAO,KAAK,MAAM,OAAO,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsD5B,IAAa,6BAAb,MAAa,mCAAmCA,aAAAA,oBAAoB;CAClE,WAAgC,EAAE;CAElC;CAEA,gBAA0C;CAE1C,YAAY;CAEZ,qBAA+B,EAAE;CAEjC,YAAY,MAAuC;AACjD,QAAM,KAAK;AACX,OAAK,gBAAgB,KAAK;AAC1B,OAAK,YAAY,KAAK,aAAa;AACnC,OAAK,gBAAgB,KAAK,iBAAiB;;;;;;;CAQ7C,MAAM,WAAW,SAAiC;AAChD,OAAK,SAAS,KAAK,QAAQ;EAC3B,MAAM,gBAAgB,MAAM,KAAK,cAAc,OAAO,QAAQ;AAC9D,OAAK,mBAAmB,KAAK,KAAK,cAAc,cAAc,CAAC;;;;;;;;CASjE,MAAM,4BACJ,GACA,QACmB;AACnB,MAAI,EAAE,SAAS,EACb,QAAO;EAGT,MAAM,EAAE,UAAU,kBAAkB;AAIpC,UAHuB,MAAM,QAAQ,IACnC,SAAS,KAAK,OAAgB,cAAc,OAAO,GAAG,CAAC,CACxD,EACqB,KAAK,OAAe,KAAK,cAAc,GAAG,CAAC;;;;;;;;CASnE,MAAM,eAAe,gBAA6C;EAChE,MAAM,SAAS,OAAO,OAAO,eAAe,CAAC,KAAK,IAAI;EACtD,IAAI,kBAAkB,KAAK,YAAY,KAAK,cAAc,OAAO;EACjE,IAAI,IAAI;EACR,MAAM,WAAsB,EAAE;AAE9B,SAAO,kBAAkB,KAAK,IAAI,KAAK,SAAS,QAAQ;GACtD,MAAM,YAAY,kBAAkB,KAAK,mBAAmB;AAC5D,OAAI,YAAY,EACd;QACK;AACL,aAAS,KAAK,KAAK,SAAS,GAAG;AAC/B,sBAAkB;;AAEpB,QAAK;;AAGP,SAAO;;;;;;;;;CAUT,aAAa,aACX,UACA,MACA;EACA,MAAM,WAAW,IAAI,2BAA2B,KAAK;AACrD,QAAM,QAAQ,IAAI,SAAS,KAAK,OAAO,SAAS,WAAW,GAAG,CAAC,CAAC;AAChE,SAAO"}