@vectorstores/mongodb 0.1.4 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +70 -3
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.edge-light.d.ts +2 -0
- package/dist/index.edge-light.js +71 -4
- package/dist/index.js +71 -4
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -71,7 +71,7 @@ class MongoKVStore extends core.BaseKVStore {
|
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
var version = "0.1.
|
|
74
|
+
var version = "0.1.6";
|
|
75
75
|
var pkg = {
|
|
76
76
|
version: version};
|
|
77
77
|
|
|
@@ -232,6 +232,26 @@ function toMongoDBFilter(filters) {
|
|
|
232
232
|
* @param query The query to run
|
|
233
233
|
* @returns List of nodes and their similarities
|
|
234
234
|
*/ async query(query, options) {
|
|
235
|
+
switch(query.mode){
|
|
236
|
+
case "bm25":
|
|
237
|
+
return this.bm25Search(query);
|
|
238
|
+
case "hybrid":
|
|
239
|
+
{
|
|
240
|
+
// Calculate prefetch limit for sub-searches
|
|
241
|
+
const prefetchK = query.hybridPrefetch ?? query.similarityTopK * core.DEFAULT_HYBRID_PREFETCH_MULTIPLIER;
|
|
242
|
+
const prefetchQuery = {
|
|
243
|
+
...query,
|
|
244
|
+
similarityTopK: prefetchK
|
|
245
|
+
};
|
|
246
|
+
const vectorResult = await this.vectorSearch(prefetchQuery);
|
|
247
|
+
const bm25Result = await this.bm25Search(prefetchQuery);
|
|
248
|
+
return core.combineResults(vectorResult, bm25Result, query.alpha ?? 0.5, query.similarityTopK);
|
|
249
|
+
}
|
|
250
|
+
default:
|
|
251
|
+
return this.vectorSearch(query);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
async vectorSearch(query) {
|
|
235
255
|
const params = {
|
|
236
256
|
queryVector: query.queryEmbedding,
|
|
237
257
|
path: this.embeddingKey,
|
|
@@ -272,12 +292,59 @@ function toMongoDBFilter(filters) {
|
|
|
272
292
|
nodes.push(node);
|
|
273
293
|
similarities.push(score);
|
|
274
294
|
}
|
|
275
|
-
|
|
295
|
+
return {
|
|
296
|
+
nodes,
|
|
297
|
+
similarities,
|
|
298
|
+
ids
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
async bm25Search(query) {
|
|
302
|
+
if (!query.queryStr) {
|
|
303
|
+
throw new Error("queryStr is required for BM25 mode");
|
|
304
|
+
}
|
|
305
|
+
const pipeline = [
|
|
306
|
+
{
|
|
307
|
+
$search: {
|
|
308
|
+
index: this.indexName,
|
|
309
|
+
text: {
|
|
310
|
+
query: query.queryStr,
|
|
311
|
+
path: this.textKey
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
},
|
|
315
|
+
{
|
|
316
|
+
$project: {
|
|
317
|
+
score: {
|
|
318
|
+
$meta: "searchScore"
|
|
319
|
+
},
|
|
320
|
+
[this.embeddingKey]: 0
|
|
321
|
+
}
|
|
322
|
+
},
|
|
323
|
+
{
|
|
324
|
+
$limit: query.similarityTopK
|
|
325
|
+
}
|
|
326
|
+
];
|
|
327
|
+
const collection = await this.ensureCollection();
|
|
328
|
+
const cursor = await collection.aggregate(pipeline);
|
|
329
|
+
const nodes = [];
|
|
330
|
+
const ids = [];
|
|
331
|
+
const similarities = [];
|
|
332
|
+
for await (const res of (await cursor)){
|
|
333
|
+
const text = res[this.textKey];
|
|
334
|
+
const score = res.score;
|
|
335
|
+
const id = res[this.idKey];
|
|
336
|
+
const metadata = res[this.metadataKey];
|
|
337
|
+
const node = core.metadataDictToNode(metadata);
|
|
338
|
+
node.setContent(text);
|
|
339
|
+
ids.push(id);
|
|
340
|
+
nodes.push(node);
|
|
341
|
+
similarities.push(score);
|
|
342
|
+
}
|
|
343
|
+
return {
|
|
276
344
|
nodes,
|
|
277
345
|
similarities,
|
|
278
346
|
ids
|
|
279
347
|
};
|
|
280
|
-
return result;
|
|
281
348
|
}
|
|
282
349
|
async exists(refDocId) {
|
|
283
350
|
const collection = await this.ensureCollection();
|
package/dist/index.d.cts
CHANGED
|
@@ -111,6 +111,8 @@ declare class MongoDBAtlasVectorSearch extends BaseVectorStore {
|
|
|
111
111
|
* @returns List of nodes and their similarities
|
|
112
112
|
*/
|
|
113
113
|
query(query: VectorStoreQuery, options?: object): Promise<VectorStoreQueryResult>;
|
|
114
|
+
private vectorSearch;
|
|
115
|
+
private bm25Search;
|
|
114
116
|
exists(refDocId: string): Promise<boolean>;
|
|
115
117
|
}
|
|
116
118
|
|
package/dist/index.d.ts
CHANGED
|
@@ -111,6 +111,8 @@ declare class MongoDBAtlasVectorSearch extends BaseVectorStore {
|
|
|
111
111
|
* @returns List of nodes and their similarities
|
|
112
112
|
*/
|
|
113
113
|
query(query: VectorStoreQuery, options?: object): Promise<VectorStoreQueryResult>;
|
|
114
|
+
private vectorSearch;
|
|
115
|
+
private bm25Search;
|
|
114
116
|
exists(refDocId: string): Promise<boolean>;
|
|
115
117
|
}
|
|
116
118
|
|
|
@@ -111,6 +111,8 @@ declare class MongoDBAtlasVectorSearch extends BaseVectorStore {
|
|
|
111
111
|
* @returns List of nodes and their similarities
|
|
112
112
|
*/
|
|
113
113
|
query(query: VectorStoreQuery, options?: object): Promise<VectorStoreQueryResult>;
|
|
114
|
+
private vectorSearch;
|
|
115
|
+
private bm25Search;
|
|
114
116
|
exists(refDocId: string): Promise<boolean>;
|
|
115
117
|
}
|
|
116
118
|
|
package/dist/index.edge-light.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BaseKVStore, BaseVectorStore, nodeToMetadata, MetadataMode, metadataDictToNode, FilterCondition, Document } from '@vectorstores/core';
|
|
1
|
+
import { BaseKVStore, BaseVectorStore, nodeToMetadata, MetadataMode, DEFAULT_HYBRID_PREFETCH_MULTIPLIER, combineResults, metadataDictToNode, FilterCondition, Document } from '@vectorstores/core';
|
|
2
2
|
import { getEnv } from '@vectorstores/env';
|
|
3
3
|
import { MongoClient } from 'mongodb';
|
|
4
4
|
|
|
@@ -69,7 +69,7 @@ class MongoKVStore extends BaseKVStore {
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
var version = "0.1.
|
|
72
|
+
var version = "0.1.6";
|
|
73
73
|
var pkg = {
|
|
74
74
|
version: version};
|
|
75
75
|
|
|
@@ -230,6 +230,26 @@ function toMongoDBFilter(filters) {
|
|
|
230
230
|
* @param query The query to run
|
|
231
231
|
* @returns List of nodes and their similarities
|
|
232
232
|
*/ async query(query, options) {
|
|
233
|
+
switch(query.mode){
|
|
234
|
+
case "bm25":
|
|
235
|
+
return this.bm25Search(query);
|
|
236
|
+
case "hybrid":
|
|
237
|
+
{
|
|
238
|
+
// Calculate prefetch limit for sub-searches
|
|
239
|
+
const prefetchK = query.hybridPrefetch ?? query.similarityTopK * DEFAULT_HYBRID_PREFETCH_MULTIPLIER;
|
|
240
|
+
const prefetchQuery = {
|
|
241
|
+
...query,
|
|
242
|
+
similarityTopK: prefetchK
|
|
243
|
+
};
|
|
244
|
+
const vectorResult = await this.vectorSearch(prefetchQuery);
|
|
245
|
+
const bm25Result = await this.bm25Search(prefetchQuery);
|
|
246
|
+
return combineResults(vectorResult, bm25Result, query.alpha ?? 0.5, query.similarityTopK);
|
|
247
|
+
}
|
|
248
|
+
default:
|
|
249
|
+
return this.vectorSearch(query);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
async vectorSearch(query) {
|
|
233
253
|
const params = {
|
|
234
254
|
queryVector: query.queryEmbedding,
|
|
235
255
|
path: this.embeddingKey,
|
|
@@ -270,12 +290,59 @@ function toMongoDBFilter(filters) {
|
|
|
270
290
|
nodes.push(node);
|
|
271
291
|
similarities.push(score);
|
|
272
292
|
}
|
|
273
|
-
|
|
293
|
+
return {
|
|
294
|
+
nodes,
|
|
295
|
+
similarities,
|
|
296
|
+
ids
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
async bm25Search(query) {
|
|
300
|
+
if (!query.queryStr) {
|
|
301
|
+
throw new Error("queryStr is required for BM25 mode");
|
|
302
|
+
}
|
|
303
|
+
const pipeline = [
|
|
304
|
+
{
|
|
305
|
+
$search: {
|
|
306
|
+
index: this.indexName,
|
|
307
|
+
text: {
|
|
308
|
+
query: query.queryStr,
|
|
309
|
+
path: this.textKey
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
{
|
|
314
|
+
$project: {
|
|
315
|
+
score: {
|
|
316
|
+
$meta: "searchScore"
|
|
317
|
+
},
|
|
318
|
+
[this.embeddingKey]: 0
|
|
319
|
+
}
|
|
320
|
+
},
|
|
321
|
+
{
|
|
322
|
+
$limit: query.similarityTopK
|
|
323
|
+
}
|
|
324
|
+
];
|
|
325
|
+
const collection = await this.ensureCollection();
|
|
326
|
+
const cursor = await collection.aggregate(pipeline);
|
|
327
|
+
const nodes = [];
|
|
328
|
+
const ids = [];
|
|
329
|
+
const similarities = [];
|
|
330
|
+
for await (const res of (await cursor)){
|
|
331
|
+
const text = res[this.textKey];
|
|
332
|
+
const score = res.score;
|
|
333
|
+
const id = res[this.idKey];
|
|
334
|
+
const metadata = res[this.metadataKey];
|
|
335
|
+
const node = metadataDictToNode(metadata);
|
|
336
|
+
node.setContent(text);
|
|
337
|
+
ids.push(id);
|
|
338
|
+
nodes.push(node);
|
|
339
|
+
similarities.push(score);
|
|
340
|
+
}
|
|
341
|
+
return {
|
|
274
342
|
nodes,
|
|
275
343
|
similarities,
|
|
276
344
|
ids
|
|
277
345
|
};
|
|
278
|
-
return result;
|
|
279
346
|
}
|
|
280
347
|
async exists(refDocId) {
|
|
281
348
|
const collection = await this.ensureCollection();
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BaseKVStore, BaseVectorStore, nodeToMetadata, MetadataMode, metadataDictToNode, FilterCondition, Document } from '@vectorstores/core';
|
|
1
|
+
import { BaseKVStore, BaseVectorStore, nodeToMetadata, MetadataMode, DEFAULT_HYBRID_PREFETCH_MULTIPLIER, combineResults, metadataDictToNode, FilterCondition, Document } from '@vectorstores/core';
|
|
2
2
|
import { getEnv } from '@vectorstores/env';
|
|
3
3
|
import { MongoClient } from 'mongodb';
|
|
4
4
|
|
|
@@ -69,7 +69,7 @@ class MongoKVStore extends BaseKVStore {
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
var version = "0.1.
|
|
72
|
+
var version = "0.1.6";
|
|
73
73
|
var pkg = {
|
|
74
74
|
version: version};
|
|
75
75
|
|
|
@@ -230,6 +230,26 @@ function toMongoDBFilter(filters) {
|
|
|
230
230
|
* @param query The query to run
|
|
231
231
|
* @returns List of nodes and their similarities
|
|
232
232
|
*/ async query(query, options) {
|
|
233
|
+
switch(query.mode){
|
|
234
|
+
case "bm25":
|
|
235
|
+
return this.bm25Search(query);
|
|
236
|
+
case "hybrid":
|
|
237
|
+
{
|
|
238
|
+
// Calculate prefetch limit for sub-searches
|
|
239
|
+
const prefetchK = query.hybridPrefetch ?? query.similarityTopK * DEFAULT_HYBRID_PREFETCH_MULTIPLIER;
|
|
240
|
+
const prefetchQuery = {
|
|
241
|
+
...query,
|
|
242
|
+
similarityTopK: prefetchK
|
|
243
|
+
};
|
|
244
|
+
const vectorResult = await this.vectorSearch(prefetchQuery);
|
|
245
|
+
const bm25Result = await this.bm25Search(prefetchQuery);
|
|
246
|
+
return combineResults(vectorResult, bm25Result, query.alpha ?? 0.5, query.similarityTopK);
|
|
247
|
+
}
|
|
248
|
+
default:
|
|
249
|
+
return this.vectorSearch(query);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
async vectorSearch(query) {
|
|
233
253
|
const params = {
|
|
234
254
|
queryVector: query.queryEmbedding,
|
|
235
255
|
path: this.embeddingKey,
|
|
@@ -270,12 +290,59 @@ function toMongoDBFilter(filters) {
|
|
|
270
290
|
nodes.push(node);
|
|
271
291
|
similarities.push(score);
|
|
272
292
|
}
|
|
273
|
-
|
|
293
|
+
return {
|
|
294
|
+
nodes,
|
|
295
|
+
similarities,
|
|
296
|
+
ids
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
async bm25Search(query) {
|
|
300
|
+
if (!query.queryStr) {
|
|
301
|
+
throw new Error("queryStr is required for BM25 mode");
|
|
302
|
+
}
|
|
303
|
+
const pipeline = [
|
|
304
|
+
{
|
|
305
|
+
$search: {
|
|
306
|
+
index: this.indexName,
|
|
307
|
+
text: {
|
|
308
|
+
query: query.queryStr,
|
|
309
|
+
path: this.textKey
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
},
|
|
313
|
+
{
|
|
314
|
+
$project: {
|
|
315
|
+
score: {
|
|
316
|
+
$meta: "searchScore"
|
|
317
|
+
},
|
|
318
|
+
[this.embeddingKey]: 0
|
|
319
|
+
}
|
|
320
|
+
},
|
|
321
|
+
{
|
|
322
|
+
$limit: query.similarityTopK
|
|
323
|
+
}
|
|
324
|
+
];
|
|
325
|
+
const collection = await this.ensureCollection();
|
|
326
|
+
const cursor = await collection.aggregate(pipeline);
|
|
327
|
+
const nodes = [];
|
|
328
|
+
const ids = [];
|
|
329
|
+
const similarities = [];
|
|
330
|
+
for await (const res of (await cursor)){
|
|
331
|
+
const text = res[this.textKey];
|
|
332
|
+
const score = res.score;
|
|
333
|
+
const id = res[this.idKey];
|
|
334
|
+
const metadata = res[this.metadataKey];
|
|
335
|
+
const node = metadataDictToNode(metadata);
|
|
336
|
+
node.setContent(text);
|
|
337
|
+
ids.push(id);
|
|
338
|
+
nodes.push(node);
|
|
339
|
+
similarities.push(score);
|
|
340
|
+
}
|
|
341
|
+
return {
|
|
274
342
|
nodes,
|
|
275
343
|
similarities,
|
|
276
344
|
ids
|
|
277
345
|
};
|
|
278
|
-
return result;
|
|
279
346
|
}
|
|
280
347
|
async exists(refDocId) {
|
|
281
348
|
const collection = await this.ensureCollection();
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vectorstores/mongodb",
|
|
3
3
|
"description": "MongoDB Storage for vectorstores",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.6",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
7
7
|
"module": "./dist/index.js",
|
|
@@ -36,11 +36,11 @@
|
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"vitest": "2.1.0",
|
|
38
38
|
"mongodb-memory-server": "^10.1.4",
|
|
39
|
-
"@vectorstores/core": "0.1.
|
|
39
|
+
"@vectorstores/core": "0.1.6",
|
|
40
40
|
"@vectorstores/env": "0.1.0"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"@vectorstores/core": "0.1.
|
|
43
|
+
"@vectorstores/core": "0.1.6",
|
|
44
44
|
"@vectorstores/env": "0.1.0"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|