Skip to content

Commit 11ae926

Browse files
feat: add numCandidates setting to MongoDBAtlasVectorStore for tuning queries (run-llama#893)
Co-authored-by: Marcus Schiesser <[email protected]>
1 parent 174cb3e commit 11ae926

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

.changeset/angry-eagles-dress.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"llamaindex": patch
3+
---
4+
5+
feat: add numCandidates setting to MongoDBAtlasVectorStore for tuning queries

packages/core/src/storage/vectorStore/MongoDBAtlasVectorStore.ts

+75-2
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,74 @@ function toMongoDBFilter(
2424
return filters;
2525
}
2626

27-
// MongoDB Atlas Vector Store class implementing VectorStore
27+
/**
28+
* Vector store that uses MongoDB Atlas for storage and vector search.
29+
* This store uses the $vectorSearch aggregation stage to perform vector similarity search.
30+
*/
2831
export class MongoDBAtlasVectorSearch
2932
extends VectorStoreBase
3033
implements VectorStoreNoEmbedModel
3134
{
3235
storesText: boolean = true;
3336
flatMetadata: boolean = true;
3437

38+
/**
39+
* The used MongoClient. If not given, a new MongoClient is created based on the MONGODB_URI env variable.
40+
*/
3541
mongodbClient: MongoClient;
42+
43+
/**
44+
* Name of the vector index. If invalid, Mongo will silently ignore this issue and return 0 results.
45+
*
46+
* Default: "default"
47+
*/
3648
indexName: string;
49+
50+
/**
51+
* Name of the key containing the embedding vector.
52+
*
53+
* Default: "embedding"
54+
*/
3755
embeddingKey: string;
56+
57+
/**
58+
* Name of the key containing the node id.
59+
*
60+
* Default: "id"
61+
*/
3862
idKey: string;
63+
64+
/**
65+
* Name of the key containing the node text.
66+
*
67+
* Default: "text"
68+
*/
3969
textKey: string;
70+
71+
/**
72+
* Name of the key containing the node metadata.
73+
*
74+
* Default: "metadata"
75+
*/
4076
metadataKey: string;
77+
78+
/**
79+
* Options to pass to the insertMany function when adding nodes.
80+
*/
4181
insertOptions?: BulkWriteOptions;
82+
83+
/**
84+
* Function to determine the number of candidates to retrieve for a given query.
85+
* In case your results are not good, you might tune this value.
86+
*
87+
* {@link https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-stage/|Run Vector Search Queries}
88+
*
89+
* {@link https://arxiv.org/abs/1603.09320|Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs}
90+
*
91+
*
92+
* Default: query.similarityTopK * 10
93+
*/
94+
numCandidates: (query: VectorStoreQuery) => number;
4295
private collection: Collection;
4396

4497
constructor(
@@ -69,9 +122,17 @@ export class MongoDBAtlasVectorSearch
69122
this.idKey = init.idKey ?? "id";
70123
this.textKey = init.textKey ?? "text";
71124
this.metadataKey = init.metadataKey ?? "metadata";
125+
this.numCandidates =
126+
init.numCandidates ?? ((query) => query.similarityTopK * 10);
72127
this.insertOptions = init.insertOptions;
73128
}
74129

130+
/**
131+
* Add nodes to the vector store.
132+
*
133+
* @param nodes Nodes to add to the vector store
134+
* @returns List of node ids that were added
135+
*/
75136
async add(nodes: BaseNode[]): Promise<string[]> {
76137
if (!nodes || nodes.length === 0) {
77138
return [];
@@ -101,6 +162,12 @@ export class MongoDBAtlasVectorSearch
101162
return nodes.map((node) => node.id_);
102163
}
103164

165+
/**
166+
* Delete nodes from the vector store with the given redDocId.
167+
*
168+
* @param refDocId The refDocId of the nodes to delete
169+
* @param deleteOptions Options to pass to the deleteOne function
170+
*/
104171
async delete(refDocId: string, deleteOptions?: any): Promise<void> {
105172
await this.collection.deleteMany(
106173
{
@@ -114,14 +181,20 @@ export class MongoDBAtlasVectorSearch
114181
return this.mongodbClient;
115182
}
116183

184+
/**
185+
* Perform a vector similarity search query.
186+
*
187+
* @param query The query to run
188+
* @returns List of nodes and their similarities
189+
*/
117190
async query(
118191
query: VectorStoreQuery,
119192
options?: any,
120193
): Promise<VectorStoreQueryResult> {
121194
const params: any = {
122195
queryVector: query.queryEmbedding,
123196
path: this.embeddingKey,
124-
numCandidates: query.similarityTopK * 10,
197+
numCandidates: this.numCandidates(query),
125198
limit: query.similarityTopK,
126199
index: this.indexName,
127200
};

0 commit comments

Comments
 (0)