UNPKG

@langchain/core

Version:
1 lines 8.72 kB
{"version":3,"file":"semantic_similarity.cjs","names":["values: Record<string, T>","BaseExampleSelector","data: SemanticSimilarityExampleSelectorInput<V>","example: Example","Document","inputVariables: Record<string, T>","examples: Record<string, string>[]","embeddings: Embeddings","vectorStoreCls: C","options: {\n k?: number;\n inputKeys?: string[];\n } & Parameters<C[\"fromTexts\"]>[3]"],"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,aAAgBA,QAAgC;AACvD,QAAO,OAAO,KAAK,OAAO,CACvB,MAAM,CACN,IAAI,CAAC,QAAQ,OAAO,KAAK;AAC7B;;;;;;;;;;;;;;;;;;;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