{"version":3,"file":"length_based.cjs","names":["text: string","BaseExampleSelector","data: LengthBasedExampleSelectorInput","example: Example","v: number[]","values: LengthBasedExampleSelector","eg: Example","eg: string","inputVariables: Example","examples: Example[]","args: LengthBasedExampleSelectorInput"],"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,eAAeA,MAAsB;AAC5C,QAAO,KAAK,MAAM,OAAO,CAAC;AAC3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqDD,IAAa,6BAAb,MAAa,mCAAmCC,iCAAoB;CAClE,AAAU,WAAsB,CAAE;CAElC;CAEA,gBAA0C;CAE1C,YAAY;CAEZ,qBAA+B,CAAE;CAEjC,YAAYC,MAAuC;EACjD,MAAM,KAAK;EACX,KAAK,gBAAgB,KAAK;EAC1B,KAAK,YAAY,KAAK,aAAa;EACnC,KAAK,gBAAgB,KAAK,iBAAiB;CAC5C;;;;;;CAOD,MAAM,WAAWC,SAAiC;EAChD,KAAK,SAAS,KAAK,QAAQ;EAC3B,MAAM,gBAAgB,MAAM,KAAK,cAAc,OAAO,QAAQ;EAC9D,KAAK,mBAAmB,KAAK,KAAK,cAAc,cAAc,CAAC;CAChE;;;;;;;CAQD,MAAM,4BACJC,GACAC,QACmB;AACnB,MAAI,EAAE,SAAS,EACb,QAAO;EAGT,MAAM,EAAE,UAAU,eAAe,GAAG;EACpC,MAAM,iBAAiB,MAAM,QAAQ,IACnC,SAAS,IAAI,CAACC,OAAgB,cAAc,OAAO,GAAG,CAAC,CACxD;AACD,SAAO,eAAe,IAAI,CAACC,OAAe,KAAK,cAAc,GAAG,CAAC;CAClE;;;;;;;CAQD,MAAM,eAAeC,gBAA6C;EAChE,MAAM,SAAS,OAAO,OAAO,eAAe,CAAC,KAAK,IAAI;EACtD,IAAI,kBAAkB,KA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