@mastra/qdrant 1.0.0-beta.3 → 1.0.0-beta.5

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 CHANGED
@@ -252,18 +252,54 @@ var QdrantVector = class extends vector.MastraVector {
252
252
  super({ id });
253
253
  this.client = new jsClientRest.QdrantClient(qdrantParams);
254
254
  }
255
- async upsert({ indexName, vectors, metadata, ids }) {
255
+ /**
256
+ * Validates that a named vector exists in the collection.
257
+ * @param indexName - The name of the collection to check.
258
+ * @param vectorName - The name of the vector space to validate.
259
+ * @throws Error if the vector name doesn't exist in the collection.
260
+ */
261
+ async validateVectorName(indexName, vectorName) {
262
+ const { config } = await this.client.getCollection(indexName);
263
+ const vectorsConfig = config.params.vectors;
264
+ const isNamedVectors = vectorsConfig && typeof vectorsConfig === "object" && !("size" in vectorsConfig);
265
+ if (!isNamedVectors || !(vectorName in vectorsConfig)) {
266
+ throw new Error(`Vector name "${vectorName}" does not exist in collection "${indexName}"`);
267
+ }
268
+ }
269
+ /**
270
+ * Upserts vectors into the index.
271
+ * @param indexName - The name of the index to upsert into.
272
+ * @param vectors - Array of embedding vectors.
273
+ * @param metadata - Optional metadata for each vector.
274
+ * @param ids - Optional vector IDs (auto-generated if not provided).
275
+ * @param vectorName - Optional name of the vector space when using named vectors.
276
+ */
277
+ async upsert({ indexName, vectors, metadata, ids, vectorName }) {
256
278
  const pointIds = ids || vectors.map(() => crypto.randomUUID());
279
+ if (vectorName) {
280
+ try {
281
+ await this.validateVectorName(indexName, vectorName);
282
+ } catch (validationError) {
283
+ throw new error.MastraError(
284
+ {
285
+ id: storage.createVectorErrorId("QDRANT", "UPSERT", "INVALID_VECTOR_NAME"),
286
+ domain: error.ErrorDomain.STORAGE,
287
+ category: error.ErrorCategory.USER,
288
+ details: { indexName, vectorName }
289
+ },
290
+ validationError
291
+ );
292
+ }
293
+ }
257
294
  const records = vectors.map((vector, i) => ({
258
295
  id: pointIds[i],
259
- vector,
296
+ vector: vectorName ? { [vectorName]: vector } : vector,
260
297
  payload: metadata?.[i] || {}
261
298
  }));
262
299
  try {
263
300
  for (let i = 0; i < records.length; i += BATCH_SIZE) {
264
301
  const batch = records.slice(i, i + BATCH_SIZE);
265
302
  await this.client.upsert(indexName, {
266
- // @ts-expect-error
267
303
  points: batch,
268
304
  wait: true
269
305
  });
@@ -275,19 +311,60 @@ var QdrantVector = class extends vector.MastraVector {
275
311
  id: storage.createVectorErrorId("QDRANT", "UPSERT", "FAILED"),
276
312
  domain: error.ErrorDomain.STORAGE,
277
313
  category: error.ErrorCategory.THIRD_PARTY,
278
- details: { indexName, vectorCount: vectors.length }
314
+ details: { indexName, vectorCount: vectors.length, ...vectorName && { vectorName } }
279
315
  },
280
316
  error$1
281
317
  );
282
318
  }
283
319
  }
284
- async createIndex({ indexName, dimension, metric = "cosine" }) {
320
+ /**
321
+ * Creates a new index (collection) in Qdrant.
322
+ * Supports both single vector and named vector configurations.
323
+ *
324
+ * @param indexName - The name of the collection to create.
325
+ * @param dimension - Vector dimension (required for single vector mode).
326
+ * @param metric - Distance metric (default: 'cosine').
327
+ * @param namedVectors - Optional named vector configurations for multi-vector collections.
328
+ *
329
+ * @example
330
+ * ```ts
331
+ * // Single vector collection
332
+ * await qdrant.createIndex({ indexName: 'docs', dimension: 768, metric: 'cosine' });
333
+ *
334
+ * // Named vectors collection
335
+ * await qdrant.createIndex({
336
+ * indexName: 'multi-modal',
337
+ * dimension: 768, // Used as fallback, can be omitted with namedVectors
338
+ * namedVectors: {
339
+ * text: { size: 768, distance: 'cosine' },
340
+ * image: { size: 512, distance: 'euclidean' },
341
+ * },
342
+ * });
343
+ * ```
344
+ */
345
+ async createIndex({ indexName, dimension, metric = "cosine", namedVectors }) {
285
346
  try {
286
- if (!Number.isInteger(dimension) || dimension <= 0) {
287
- throw new Error("Dimension must be a positive integer");
288
- }
289
- if (!DISTANCE_MAPPING[metric]) {
290
- throw new Error(`Invalid metric: "${metric}". Must be one of: cosine, euclidean, dotproduct`);
347
+ if (namedVectors) {
348
+ if (Object.keys(namedVectors).length === 0) {
349
+ throw new Error("namedVectors must contain at least one named vector configuration");
350
+ }
351
+ for (const [name, config] of Object.entries(namedVectors)) {
352
+ if (!Number.isInteger(config.size) || config.size <= 0) {
353
+ throw new Error(`Named vector "${name}": size must be a positive integer`);
354
+ }
355
+ if (!DISTANCE_MAPPING[config.distance]) {
356
+ throw new Error(
357
+ `Named vector "${name}": invalid distance "${config.distance}". Must be one of: cosine, euclidean, dotproduct`
358
+ );
359
+ }
360
+ }
361
+ } else {
362
+ if (!Number.isInteger(dimension) || dimension <= 0) {
363
+ throw new Error("Dimension must be a positive integer");
364
+ }
365
+ if (!DISTANCE_MAPPING[metric]) {
366
+ throw new Error(`Invalid metric: "${metric}". Must be one of: cosine, euclidean, dotproduct`);
367
+ }
291
368
  }
292
369
  } catch (validationError) {
293
370
  throw new error.MastraError(
@@ -295,22 +372,49 @@ var QdrantVector = class extends vector.MastraVector {
295
372
  id: storage.createVectorErrorId("QDRANT", "CREATE_INDEX", "INVALID_ARGS"),
296
373
  domain: error.ErrorDomain.STORAGE,
297
374
  category: error.ErrorCategory.USER,
298
- details: { indexName, dimension, metric }
375
+ details: {
376
+ indexName,
377
+ dimension,
378
+ metric,
379
+ ...namedVectors && { namedVectorNames: Object.keys(namedVectors).join(", ") }
380
+ }
299
381
  },
300
382
  validationError
301
383
  );
302
384
  }
303
385
  try {
304
- await this.client.createCollection(indexName, {
305
- vectors: {
306
- size: dimension,
307
- distance: DISTANCE_MAPPING[metric]
308
- }
309
- });
386
+ if (namedVectors) {
387
+ const namedVectorsConfig = Object.entries(namedVectors).reduce(
388
+ (acc, [name, config]) => {
389
+ acc[name] = {
390
+ size: config.size,
391
+ distance: DISTANCE_MAPPING[config.distance]
392
+ };
393
+ return acc;
394
+ },
395
+ {}
396
+ );
397
+ await this.client.createCollection(indexName, {
398
+ vectors: namedVectorsConfig
399
+ });
400
+ } else {
401
+ await this.client.createCollection(indexName, {
402
+ vectors: {
403
+ size: dimension,
404
+ distance: DISTANCE_MAPPING[metric]
405
+ }
406
+ });
407
+ }
310
408
  } catch (error$1) {
311
409
  const message = error$1?.message || error$1?.toString();
312
410
  if (error$1?.status === 409 || typeof message === "string" && message.toLowerCase().includes("exists")) {
313
- await this.validateExistingIndex(indexName, dimension, metric);
411
+ if (!namedVectors) {
412
+ await this.validateExistingIndex(indexName, dimension, metric);
413
+ } else {
414
+ this.logger.info(
415
+ `Collection "${indexName}" already exists. Skipping validation for named vectors configuration.`
416
+ );
417
+ }
314
418
  return;
315
419
  }
316
420
  throw new error.MastraError(
@@ -328,12 +432,23 @@ var QdrantVector = class extends vector.MastraVector {
328
432
  const translator = new QdrantFilterTranslator();
329
433
  return translator.translate(filter);
330
434
  }
435
+ /**
436
+ * Queries the index for similar vectors.
437
+ *
438
+ * @param indexName - The name of the index to query.
439
+ * @param queryVector - The query vector to find similar vectors for.
440
+ * @param topK - Number of results to return (default: 10).
441
+ * @param filter - Optional metadata filter.
442
+ * @param includeVector - Whether to include vectors in results (default: false).
443
+ * @param using - Name of the vector space to query when using named vectors.
444
+ */
331
445
  async query({
332
446
  indexName,
333
447
  queryVector,
334
448
  topK = 10,
335
449
  filter,
336
- includeVector = false
450
+ includeVector = false,
451
+ using
337
452
  }) {
338
453
  const translatedFilter = this.transformFilter(filter) ?? {};
339
454
  try {
@@ -342,15 +457,20 @@ var QdrantVector = class extends vector.MastraVector {
342
457
  limit: topK,
343
458
  filter: translatedFilter,
344
459
  with_payload: true,
345
- with_vector: includeVector
460
+ with_vector: includeVector,
461
+ ...using ? { using } : {}
346
462
  })).points;
347
463
  return results.map((match) => {
348
464
  let vector = [];
349
- if (includeVector) {
465
+ if (includeVector && match.vector != null) {
350
466
  if (Array.isArray(match.vector)) {
351
467
  vector = match.vector;
352
468
  } else if (typeof match.vector === "object" && match.vector !== null) {
353
- vector = Object.values(match.vector).filter((v) => typeof v === "number");
469
+ const namedVectors = match.vector;
470
+ const sourceArray = using && Array.isArray(namedVectors[using]) ? namedVectors[using] : Object.values(namedVectors).find((v) => Array.isArray(v));
471
+ if (sourceArray) {
472
+ vector = sourceArray.filter((v) => typeof v === "number");
473
+ }
354
474
  }
355
475
  }
356
476
  return {
@@ -366,7 +486,7 @@ var QdrantVector = class extends vector.MastraVector {
366
486
  id: storage.createVectorErrorId("QDRANT", "QUERY", "FAILED"),
367
487
  domain: error.ErrorDomain.STORAGE,
368
488
  category: error.ErrorCategory.THIRD_PARTY,
369
- details: { indexName, topK }
489
+ details: { indexName, topK, ...using && { using } }
370
490
  },
371
491
  error$1
372
492
  );
@@ -725,6 +845,161 @@ var QdrantVector = class extends vector.MastraVector {
725
845
  );
726
846
  }
727
847
  }
848
+ /**
849
+ * Creates a payload index on a Qdrant collection to enable efficient filtering on metadata fields.
850
+ *
851
+ * This is required for Qdrant Cloud and any Qdrant instance with `strict_mode_config = true`,
852
+ * where metadata (payload) fields must be explicitly indexed before they can be used for filtering.
853
+ *
854
+ * @param params - The parameters for creating the payload index.
855
+ * @param params.indexName - The name of the collection (index) to create the payload index on.
856
+ * @param params.fieldName - The name of the payload field to index.
857
+ * @param params.fieldSchema - The schema type for the field (e.g., 'keyword', 'integer', 'text').
858
+ * @param params.wait - Whether to wait for the operation to complete. Defaults to true.
859
+ * @returns A promise that resolves when the index is created (idempotent if index already exists).
860
+ * @throws Will throw a MastraError if arguments are invalid or if the operation fails.
861
+ *
862
+ * @example
863
+ * ```ts
864
+ * // Create a keyword index for filtering by source
865
+ * await qdrant.createPayloadIndex({
866
+ * indexName: 'my-collection',
867
+ * fieldName: 'source',
868
+ * fieldSchema: 'keyword',
869
+ * });
870
+ *
871
+ * // Create an integer index for numeric filtering
872
+ * await qdrant.createPayloadIndex({
873
+ * indexName: 'my-collection',
874
+ * fieldName: 'price',
875
+ * fieldSchema: 'integer',
876
+ * });
877
+ * ```
878
+ *
879
+ * @see https://qdrant.tech/documentation/concepts/indexing/#payload-index
880
+ */
881
+ async createPayloadIndex({
882
+ indexName,
883
+ fieldName,
884
+ fieldSchema,
885
+ wait = true
886
+ }) {
887
+ const validSchemas = [
888
+ "keyword",
889
+ "integer",
890
+ "float",
891
+ "geo",
892
+ "text",
893
+ "bool",
894
+ "datetime",
895
+ "uuid"
896
+ ];
897
+ if (!indexName || typeof indexName !== "string" || indexName.trim() === "") {
898
+ throw new error.MastraError({
899
+ id: storage.createVectorErrorId("QDRANT", "CREATE_PAYLOAD_INDEX", "INVALID_ARGS"),
900
+ text: "indexName must be a non-empty string",
901
+ domain: error.ErrorDomain.STORAGE,
902
+ category: error.ErrorCategory.USER,
903
+ details: { indexName, fieldName, fieldSchema }
904
+ });
905
+ }
906
+ if (!fieldName || typeof fieldName !== "string" || fieldName.trim() === "") {
907
+ throw new error.MastraError({
908
+ id: storage.createVectorErrorId("QDRANT", "CREATE_PAYLOAD_INDEX", "INVALID_ARGS"),
909
+ text: "fieldName must be a non-empty string",
910
+ domain: error.ErrorDomain.STORAGE,
911
+ category: error.ErrorCategory.USER,
912
+ details: { indexName, fieldName, fieldSchema }
913
+ });
914
+ }
915
+ if (!validSchemas.includes(fieldSchema)) {
916
+ throw new error.MastraError({
917
+ id: storage.createVectorErrorId("QDRANT", "CREATE_PAYLOAD_INDEX", "INVALID_ARGS"),
918
+ text: `fieldSchema must be one of: ${validSchemas.join(", ")}`,
919
+ domain: error.ErrorDomain.STORAGE,
920
+ category: error.ErrorCategory.USER,
921
+ details: { indexName, fieldName, fieldSchema }
922
+ });
923
+ }
924
+ try {
925
+ await this.client.createPayloadIndex(indexName, {
926
+ field_name: fieldName,
927
+ field_schema: fieldSchema,
928
+ wait
929
+ });
930
+ } catch (error$1) {
931
+ const message = error$1?.message || error$1?.toString() || "";
932
+ if (error$1?.status === 409 || message.toLowerCase().includes("exists")) {
933
+ this.logger.info(`Payload index for field "${fieldName}" already exists on collection "${indexName}"`);
934
+ return;
935
+ }
936
+ throw new error.MastraError(
937
+ {
938
+ id: storage.createVectorErrorId("QDRANT", "CREATE_PAYLOAD_INDEX", "FAILED"),
939
+ domain: error.ErrorDomain.STORAGE,
940
+ category: error.ErrorCategory.THIRD_PARTY,
941
+ details: { indexName, fieldName, fieldSchema }
942
+ },
943
+ error$1
944
+ );
945
+ }
946
+ }
947
+ /**
948
+ * Deletes a payload index from a Qdrant collection.
949
+ *
950
+ * @param params - The parameters for deleting the payload index.
951
+ * @param params.indexName - The name of the collection (index) to delete the payload index from.
952
+ * @param params.fieldName - The name of the payload field index to delete.
953
+ * @param params.wait - Whether to wait for the operation to complete. Defaults to true.
954
+ * @returns A promise that resolves when the index is deleted (idempotent if index doesn't exist).
955
+ * @throws Will throw a MastraError if the operation fails.
956
+ *
957
+ * @example
958
+ * ```ts
959
+ * await qdrant.deletePayloadIndex({
960
+ * indexName: 'my-collection',
961
+ * fieldName: 'source',
962
+ * });
963
+ * ```
964
+ */
965
+ async deletePayloadIndex({ indexName, fieldName, wait = true }) {
966
+ if (!indexName || typeof indexName !== "string" || indexName.trim() === "") {
967
+ throw new error.MastraError({
968
+ id: storage.createVectorErrorId("QDRANT", "DELETE_PAYLOAD_INDEX", "INVALID_ARGS"),
969
+ text: "indexName must be a non-empty string",
970
+ domain: error.ErrorDomain.STORAGE,
971
+ category: error.ErrorCategory.USER,
972
+ details: { indexName, fieldName }
973
+ });
974
+ }
975
+ if (!fieldName || typeof fieldName !== "string" || fieldName.trim() === "") {
976
+ throw new error.MastraError({
977
+ id: storage.createVectorErrorId("QDRANT", "DELETE_PAYLOAD_INDEX", "INVALID_ARGS"),
978
+ text: "fieldName must be a non-empty string",
979
+ domain: error.ErrorDomain.STORAGE,
980
+ category: error.ErrorCategory.USER,
981
+ details: { indexName, fieldName }
982
+ });
983
+ }
984
+ try {
985
+ await this.client.deletePayloadIndex(indexName, fieldName, { wait });
986
+ } catch (error$1) {
987
+ const message = error$1?.message || error$1?.toString() || "";
988
+ if (error$1?.status === 404 || message.toLowerCase().includes("not found") || message.toLowerCase().includes("not exist")) {
989
+ this.logger.info(`Payload index for field "${fieldName}" does not exist on collection "${indexName}"`);
990
+ return;
991
+ }
992
+ throw new error.MastraError(
993
+ {
994
+ id: storage.createVectorErrorId("QDRANT", "DELETE_PAYLOAD_INDEX", "FAILED"),
995
+ domain: error.ErrorDomain.STORAGE,
996
+ category: error.ErrorCategory.THIRD_PARTY,
997
+ details: { indexName, fieldName }
998
+ },
999
+ error$1
1000
+ );
1001
+ }
1002
+ }
728
1003
  };
729
1004
 
730
1005
  // src/vector/prompt.ts