UNPKG

@langchain/core

Version:
1 lines 8.27 kB
{"version":3,"file":"semantic_similarity.cjs","names":["BaseExampleSelector","Document"],"sources":["../../src/example_selectors/semantic_similarity.ts"],"sourcesContent":["import type { Embeddings } from \"../embeddings.js\";\nimport type {\n VectorStoreInterface,\n VectorStoreRetrieverInterface,\n VectorStore,\n} from \"../vectorstores.js\";\nimport type { Example } from \"../prompts/base.js\";\nimport { Document } from \"../documents/document.js\";\nimport { BaseExampleSelector } from \"./base.js\";\n\nfunction sortedValues<T>(values: Record<string, T>): T[] {\n return Object.keys(values)\n .sort()\n .map((key) => values[key]);\n}\n\n/**\n * Interface for the input data of the SemanticSimilarityExampleSelector\n * class.\n */\nexport type SemanticSimilarityExampleSelectorInput<\n V extends VectorStoreInterface = VectorStoreInterface,\n> =\n | {\n vectorStore: V;\n k?: number;\n filter?: V[\"FilterType\"];\n exampleKeys?: string[];\n inputKeys?: string[];\n vectorStoreRetriever?: never;\n }\n | {\n vectorStoreRetriever: VectorStoreRetrieverInterface<V>;\n exampleKeys?: string[];\n inputKeys?: string[];\n vectorStore?: never;\n k?: never;\n filter?: never;\n };\n\n/**\n * Class that selects examples based on semantic similarity. It extends\n * the BaseExampleSelector class.\n * @example\n * ```typescript\n * const exampleSelector = await SemanticSimilarityExampleSelector.fromExamples(\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 * new OpenAIEmbeddings(),\n * HNSWLib,\n * { k: 1 },\n * );\n * const dynamicPrompt = new FewShotPromptTemplate({\n * exampleSelector,\n * examplePrompt: PromptTemplate.fromTemplate(\n * \"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(await dynamicPrompt.format({ adjective: \"rainy\" }));\n * ```\n */\nexport class SemanticSimilarityExampleSelector<\n V extends VectorStoreInterface = VectorStoreInterface,\n> extends BaseExampleSelector {\n vectorStoreRetriever: VectorStoreRetrieverInterface<V>;\n\n exampleKeys?: string[];\n\n inputKeys?: string[];\n\n constructor(data: SemanticSimilarityExampleSelectorInput<V>) {\n super(data);\n this.exampleKeys = data.exampleKeys;\n this.inputKeys = data.inputKeys;\n if (data.vectorStore !== undefined) {\n this.vectorStoreRetriever = data.vectorStore.asRetriever({\n k: data.k ?? 4,\n filter: data.filter,\n });\n } else if (data.vectorStoreRetriever) {\n this.vectorStoreRetriever = data.vectorStoreRetriever;\n } else {\n throw new Error(\n `You must specify one of \"vectorStore\" and \"vectorStoreRetriever\".`\n );\n }\n }\n\n /**\n * Method that adds a new example to the vectorStore. The example is\n * converted to a string and added to the vectorStore as a document.\n * @param example The example to be added to the vectorStore.\n * @returns Promise that resolves when the example has been added to the vectorStore.\n */\n async addExample(example: Example): Promise<void> {\n const inputKeys = this.inputKeys ?? Object.keys(example);\n const stringExample = sortedValues(\n inputKeys.reduce(\n (acc, key) => ({ ...acc, [key]: example[key] }),\n {} as Example\n )\n ).join(\" \");\n\n await this.vectorStoreRetriever.addDocuments([\n new Document({\n pageContent: stringExample,\n metadata: example,\n }),\n ]);\n }\n\n /**\n * Method that selects which examples to use based on semantic similarity.\n * It performs a similarity search in the vectorStore using the input\n * variables and returns the examples with the highest similarity.\n * @param inputVariables The input variables used for the similarity search.\n * @returns Promise that resolves with an array of the selected examples.\n */\n async selectExamples<T>(\n inputVariables: Record<string, T>\n ): Promise<Example[]> {\n const inputKeys = this.inputKeys ?? Object.keys(inputVariables);\n const query = sortedValues(\n inputKeys.reduce(\n (acc, key) => ({ ...acc, [key]: inputVariables[key] }),\n {} as Record<string, T>\n )\n ).join(\" \");\n\n const exampleDocs = await this.vectorStoreRetriever.invoke(query);\n\n const examples = exampleDocs.map((doc) => doc.metadata);\n if (this.exampleKeys) {\n // If example keys are provided, filter examples to those keys.\n return examples.map((example) =>\n (this.exampleKeys as string[]).reduce(\n (acc, key) => ({ ...acc, [key]: example[key] }),\n {}\n )\n );\n }\n return examples;\n }\n\n /**\n * Static method that creates a new instance of\n * SemanticSimilarityExampleSelector. It takes a list of examples, an\n * instance of Embeddings, a VectorStore class, and an options object as\n * parameters. It converts the examples to strings, creates a VectorStore\n * from the strings and the embeddings, and returns a new\n * SemanticSimilarityExampleSelector with the created VectorStore and the\n * options provided.\n * @param examples The list of examples to be used.\n * @param embeddings The instance of Embeddings to be used.\n * @param vectorStoreCls The VectorStore class to be used.\n * @param options The options object for the SemanticSimilarityExampleSelector.\n * @returns Promise that resolves with a new instance of SemanticSimilarityExampleSelector.\n */\n static async fromExamples<C extends typeof VectorStore>(\n examples: Record<string, string>[],\n embeddings: Embeddings,\n vectorStoreCls: C,\n options: {\n k?: number;\n inputKeys?: string[];\n } & Parameters<C[\"fromTexts\"]>[3] = {}\n ): Promise<SemanticSimilarityExampleSelector> {\n const inputKeys = options.inputKeys ?? null;\n const stringExamples = examples.map((example) =>\n sortedValues(\n inputKeys\n ? inputKeys.reduce(\n (acc, key) => ({ ...acc, [key]: example[key] }),\n {} as Record<string, string>\n )\n : example\n ).join(\" \")\n );\n\n const vectorStore = await vectorStoreCls.fromTexts(\n stringExamples,\n examples, // metadatas\n embeddings,\n options\n );\n\n return new SemanticSimilarityExampleSelector({\n vectorStore,\n k: options.k ?? 4,\n exampleKeys: options.exampleKeys,\n inputKeys: options.inputKeys,\n });\n }\n}\n"],"mappings":";;;;AAUA,SAAS,aAAgB,QAAgC;AACvD,QAAO,OAAO,KAAK,OAAO,CACvB,MAAM,CACN,KAAK,QAAQ,OAAO,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwD9B,IAAa,oCAAb,MAAa,0CAEHA,iCAAoB;CAC5B;CAEA;CAEA;CAEA,YAAY,MAAiD;AAC3D,QAAM,KAAK;AACX,OAAK,cAAc,KAAK;AACxB,OAAK,YAAY,KAAK;AACtB,MAAI,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