From c9ac210abfe8da841f337ea3d87b3ca141033450 Mon Sep 17 00:00:00 2001 From: David Zhuang Date: Tue, 21 Nov 2023 00:00:12 -0500 Subject: [PATCH 1/2] fix: MongoDB Vector Search does not support integer as input --- langchain/src/vectorstores/mongodb_atlas.ts | 24 +++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/langchain/src/vectorstores/mongodb_atlas.ts b/langchain/src/vectorstores/mongodb_atlas.ts index 608972781326..6e566f2303c1 100755 --- a/langchain/src/vectorstores/mongodb_atlas.ts +++ b/langchain/src/vectorstores/mongodb_atlas.ts @@ -122,7 +122,7 @@ export class MongoDBAtlasVectorSearch extends VectorStore { const pipeline: MongoDBDocument[] = [ { $vectorSearch: { - queryVector: query, + queryVector: MongoDBAtlasVectorSearch.fixArrayPrecision(query), index: this.indexName, path: this.embeddingKey, limit: k, @@ -182,7 +182,7 @@ export class MongoDBAtlasVectorSearch extends VectorStore { }; const resultDocs = await this.similaritySearchVectorWithScore( - queryEmbedding, + MongoDBAtlasVectorSearch.fixArrayPrecision(queryEmbedding), fetchK, includeEmbeddingsFilter ); @@ -255,4 +255,24 @@ export class MongoDBAtlasVectorSearch extends VectorStore { await instance.addDocuments(docs); return instance; } + /** + * Static method to fix the precision of the array that ensures that + * every number in this array is always float when casted to other types. + * This is needed since MongoDB Atlas Vector Search does not cast integer + * inside vector search to float automatically. + * This method shall introduce a hint of error but should be safe to use + * since introduced error is very small, only applies to integer numbers + * returned by embeddings, and most embeddings shall not have precision + * as high as 15 decimal places. + * @param array Array of number to be fixed. + * @returns + */ + static fixArrayPrecision(array: number[]) { + for (let i = 0; i < array.length; i++) { + if (Number.isInteger(array[i])) { + array[i] += 0.000000000000001; + }; + } + return array; + } } From ebcf2a3eb9bbb8b5420db8369fae20d069996d35 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Wed, 22 Nov 2023 07:53:05 -0800 Subject: [PATCH 2/2] Fix lint + format --- langchain/src/vectorstores/mongodb_atlas.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/langchain/src/vectorstores/mongodb_atlas.ts b/langchain/src/vectorstores/mongodb_atlas.ts index 6e566f2303c1..4815330bddb4 100755 --- a/langchain/src/vectorstores/mongodb_atlas.ts +++ b/langchain/src/vectorstores/mongodb_atlas.ts @@ -255,6 +255,7 @@ export class MongoDBAtlasVectorSearch extends VectorStore { await instance.addDocuments(docs); return instance; } + /** * Static method to fix the precision of the array that ensures that * every number in this array is always float when casted to other types. @@ -264,15 +265,15 @@ export class MongoDBAtlasVectorSearch extends VectorStore { * since introduced error is very small, only applies to integer numbers * returned by embeddings, and most embeddings shall not have precision * as high as 15 decimal places. - * @param array Array of number to be fixed. - * @returns + * @param array Array of number to be fixed. + * @returns */ static fixArrayPrecision(array: number[]) { - for (let i = 0; i < array.length; i++) { - if (Number.isInteger(array[i])) { - array[i] += 0.000000000000001; - }; - } - return array; + return array.map((value) => { + if (Number.isInteger(value)) { + return value + 0.000000000000001; + } + return value; + }); } }