@langchain/core
Version:
Core LangChain.js abstractions and schemas
1 lines • 5.17 kB
Source Map (JSON)
{"version":3,"file":"embeddings.cjs","names":["Embeddings","params?: SyntheticEmbeddingsParams","documents: string[]","document: string","ret: number[]","ret","params?: EmbeddingsParams","_: string"],"sources":["../../../src/utils/testing/embeddings.ts"],"sourcesContent":["import { Embeddings, EmbeddingsParams } from \"../../embeddings.js\";\n\n/**\n * An interface that defines additional parameters specific to the\n * SyntheticEmbeddings class.\n */\ninterface SyntheticEmbeddingsParams extends EmbeddingsParams {\n vectorSize: number;\n}\n\n/**\n * A class that provides synthetic embeddings by overriding the\n * embedDocuments and embedQuery methods to generate embeddings based on\n * the input documents. The embeddings are generated by converting each\n * document into chunks, calculating a numerical value for each chunk, and\n * returning an array of these values as the embedding.\n */\nexport class SyntheticEmbeddings\n extends Embeddings\n implements SyntheticEmbeddingsParams\n{\n vectorSize: number;\n\n constructor(params?: SyntheticEmbeddingsParams) {\n super(params ?? {});\n this.vectorSize = params?.vectorSize ?? 4;\n }\n\n /**\n * Generates synthetic embeddings for a list of documents.\n * @param documents List of documents to generate embeddings for.\n * @returns A promise that resolves with a list of synthetic embeddings for each document.\n */\n async embedDocuments(documents: string[]): Promise<number[][]> {\n return Promise.all(documents.map((doc) => this.embedQuery(doc)));\n }\n\n /**\n * Generates a synthetic embedding for a document. The document is\n * converted into chunks, a numerical value is calculated for each chunk,\n * and an array of these values is returned as the embedding.\n * @param document The document to generate an embedding for.\n * @returns A promise that resolves with a synthetic embedding for the document.\n */\n async embedQuery(document: string): Promise<number[]> {\n let doc = document;\n\n // Only use the letters (and space) from the document, and make them lower case\n doc = doc.toLowerCase().replaceAll(/[^a-z ]/g, \"\");\n\n // Pad the document to make sure it has a divisible number of chunks\n const padMod = doc.length % this.vectorSize;\n const padGapSize = padMod === 0 ? 0 : this.vectorSize - padMod;\n const padSize = doc.length + padGapSize;\n doc = doc.padEnd(padSize, \" \");\n\n // Break it into chunks\n const chunkSize = doc.length / this.vectorSize;\n const docChunk = [];\n for (let co = 0; co < doc.length; co += chunkSize) {\n docChunk.push(doc.slice(co, co + chunkSize));\n }\n\n // Turn each chunk into a number\n const ret: number[] = docChunk.map((s) => {\n let sum = 0;\n // Get a total value by adding the value of each character in the string\n for (let co = 0; co < s.length; co += 1) {\n sum += s === \" \" ? 0 : s.charCodeAt(co);\n }\n // Reduce this to a number between 0 and 25 inclusive\n // Then get the fractional number by dividing it by 26\n const ret = (sum % 26) / 26;\n return ret;\n });\n\n return ret;\n }\n}\n\n/**\n * A class that provides fake embeddings by overriding the embedDocuments\n * and embedQuery methods to return fixed values.\n */\nexport class FakeEmbeddings extends Embeddings {\n constructor(params?: EmbeddingsParams) {\n super(params ?? {});\n }\n\n /**\n * Generates fixed embeddings for a list of documents.\n * @param documents List of documents to generate embeddings for.\n * @returns A promise that resolves with a list of fixed embeddings for each document.\n */\n embedDocuments(documents: string[]): Promise<number[][]> {\n return Promise.resolve(documents.map(() => [0.1, 0.2, 0.3, 0.4]));\n }\n\n /**\n * Generates a fixed embedding for a query.\n * @param _ The query to generate an embedding for.\n * @returns A promise that resolves with a fixed embedding for the query.\n */\n embedQuery(_: string): Promise<number[]> {\n return Promise.resolve([0.1, 0.2, 0.3, 0.4]);\n }\n}\n"],"mappings":";;;;;;;;;;AAiBA,IAAa,sBAAb,cACUA,8BAEV;CACE;CAEA,YAAYC,QAAoC;EAC9C,MAAM,UAAU,CAAE,EAAC;EACnB,KAAK,aAAa,QAAQ,cAAc;CACzC;;;;;;CAOD,MAAM,eAAeC,WAA0C;AAC7D,SAAO,QAAQ,IAAI,UAAU,IAAI,CAAC,QAAQ,KAAK,WAAW,IAAI,CAAC,CAAC;CACjE;;;;;;;;CASD,MAAM,WAAWC,UAAqC;EACpD,IAAI,MAAM;EAGV,MAAM,IAAI,aAAa,CAAC,WAAW,YAAY,GAAG;EAGlD,MAAM,SAAS,IAAI,SAAS,KAAK;EACjC,MAAM,aAAa,WAAW,IAAI,IAAI,KAAK,aAAa;EACxD,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