@mastra/memory 1.0.0-beta.10 → 1.0.0-beta.11

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.
@@ -0,0 +1,929 @@
1
+ # Vectors API Reference
2
+
3
+ > API reference for vectors - 4 entries
4
+
5
+
6
+ ---
7
+
8
+ ## Reference: libSQL Vector Store
9
+
10
+ > Documentation for the LibSQLVector class in Mastra, which provides vector search using libSQL with vector extensions.
11
+
12
+ The libSQL storage implementation provides a SQLite-compatible vector search [libSQL](https://github.com/tursodatabase/libsql), a fork of SQLite with vector extensions, and [Turso](https://turso.tech/) with vector extensions, offering a lightweight and efficient vector database solution.
13
+ It's part of the `@mastra/libsql` package and offers efficient vector similarity search with metadata filtering.
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @mastra/libsql@beta
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ```typescript
24
+ import { LibSQLVector } from "@mastra/libsql";
25
+
26
+ // Create a new vector store instance
27
+ const store = new LibSQLVector({
28
+ id: 'libsql-vector',
29
+ connectionUrl: process.env.DATABASE_URL,
30
+ // Optional: for Turso cloud databases
31
+ authToken: process.env.DATABASE_AUTH_TOKEN,
32
+ });
33
+
34
+ // Create an index
35
+ await store.createIndex({
36
+ indexName: "myCollection",
37
+ dimension: 1536,
38
+ });
39
+
40
+ // Add vectors with metadata
41
+ const vectors = [[0.1, 0.2, ...], [0.3, 0.4, ...]];
42
+ const metadata = [
43
+ { text: "first document", category: "A" },
44
+ { text: "second document", category: "B" }
45
+ ];
46
+ await store.upsert({
47
+ indexName: "myCollection",
48
+ vectors,
49
+ metadata,
50
+ });
51
+
52
+ // Query similar vectors
53
+ const queryVector = [0.1, 0.2, ...];
54
+ const results = await store.query({
55
+ indexName: "myCollection",
56
+ queryVector,
57
+ topK: 10, // top K results
58
+ filter: { category: "A" } // optional metadata filter
59
+ });
60
+ ```
61
+
62
+ ## Constructor Options
63
+
64
+ ## Methods
65
+
66
+ ### createIndex()
67
+
68
+ Creates a new vector collection. The index name must start with a letter or underscore and can only contain letters, numbers, and underscores. The dimension must be a positive integer.
69
+
70
+ ### upsert()
71
+
72
+ Adds or updates vectors and their metadata in the index. Uses a transaction to ensure all vectors are inserted atomically - if any insert fails, the entire operation is rolled back.
73
+
74
+ ### query()
75
+
76
+ Searches for similar vectors with optional metadata filtering.
77
+
78
+ ### describeIndex()
79
+
80
+ Gets information about an index.
81
+
82
+ Returns:
83
+
84
+ ```typescript
85
+ interface IndexStats {
86
+ dimension: number;
87
+ count: number;
88
+ metric: "cosine" | "euclidean" | "dotproduct";
89
+ }
90
+ ```
91
+
92
+ ### deleteIndex()
93
+
94
+ Deletes an index and all its data.
95
+
96
+ ### listIndexes()
97
+
98
+ Lists all vector indexes in the database.
99
+
100
+ Returns: `Promise<string[]>`
101
+
102
+ ### truncateIndex()
103
+
104
+ Removes all vectors from an index while keeping the index structure.
105
+
106
+ ### updateVector()
107
+
108
+ Update a single vector by ID or by metadata filter. Either `id` or `filter` must be provided, but not both.
109
+
110
+ ### deleteVector()
111
+
112
+ Deletes a specific vector entry from an index by its ID.
113
+
114
+ ### deleteVectors()
115
+
116
+ Delete multiple vectors by IDs or by metadata filter. Either `ids` or `filter` must be provided, but not both.
117
+
118
+ ## Response Types
119
+
120
+ Query results are returned in this format:
121
+
122
+ ```typescript
123
+ interface QueryResult {
124
+ id: string;
125
+ score: number;
126
+ metadata: Record<string, any>;
127
+ vector?: number[]; // Only included if includeVector is true
128
+ }
129
+ ```
130
+
131
+ ## Error Handling
132
+
133
+ The store throws specific errors for different failure cases:
134
+
135
+ ```typescript
136
+ try {
137
+ await store.query({
138
+ indexName: "my-collection",
139
+ queryVector: queryVector,
140
+ });
141
+ } catch (error) {
142
+ // Handle specific error cases
143
+ if (error.message.includes("Invalid index name format")) {
144
+ console.error(
145
+ "Index name must start with a letter/underscore and contain only alphanumeric characters",
146
+ );
147
+ } else if (error.message.includes("Table not found")) {
148
+ console.error("The specified index does not exist");
149
+ } else {
150
+ console.error("Vector store error:", error.message);
151
+ }
152
+ }
153
+ ```
154
+
155
+ Common error cases include:
156
+
157
+ - Invalid index name format
158
+ - Invalid vector dimensions
159
+ - Table/index not found
160
+ - Database connection issues
161
+ - Transaction failures during upsert
162
+
163
+ ## Usage Example
164
+
165
+ ### Local embeddings with fastembed
166
+
167
+ Embeddings are numeric vectors used by memory's `semanticRecall` to retrieve related messages by meaning (not keywords). This setup uses `@mastra/fastembed` to generate vector embeddings.
168
+
169
+ Install `fastembed` to get started:
170
+
171
+ ```bash
172
+ npm install @mastra/fastembed@beta
173
+ ```
174
+
175
+ Add the following to your agent:
176
+
177
+ ```typescript title="src/mastra/agents/example-libsql-agent.ts"
178
+ import { Memory } from "@mastra/memory";
179
+ import { Agent } from "@mastra/core/agent";
180
+ import { LibSQLStore, LibSQLVector } from "@mastra/libsql";
181
+ import { fastembed } from "@mastra/fastembed";
182
+
183
+ export const libsqlAgent = new Agent({
184
+ id: "libsql-agent",
185
+ name: "libSQL Agent",
186
+ instructions:
187
+ "You are an AI agent with the ability to automatically recall memories from previous interactions.",
188
+ model: "openai/gpt-5.1",
189
+ memory: new Memory({
190
+ storage: new LibSQLStore({
191
+ id: 'libsql-agent-storage',
192
+ url: "file:libsql-agent.db",
193
+ }),
194
+ vector: new LibSQLVector({
195
+ id: 'libsql-agent-vector',
196
+ connectionUrl: "file:libsql-agent.db",
197
+ }),
198
+ embedder: fastembed,
199
+ options: {
200
+ lastMessages: 10,
201
+ semanticRecall: {
202
+ topK: 3,
203
+ messageRange: 2,
204
+ },
205
+ threads: {
206
+ generateTitle: true, // Explicitly enable automatic title generation
207
+ },
208
+ },
209
+ }),
210
+ });
211
+ ```
212
+
213
+ ## Related
214
+
215
+ - [Metadata Filters](../rag/metadata-filters)
216
+
217
+ ---
218
+
219
+ ## Reference: MongoDB Vector Store
220
+
221
+ > Documentation for the MongoDBVector class in Mastra, which provides vector search using MongoDB Atlas and Atlas Vector Search.
222
+
223
+ The `MongoDBVector` class provides vector search using [MongoDB Atlas Vector Search](https://www.mongodb.com/docs/atlas/atlas-vector-search/). It enables efficient similarity search and metadata filtering within your MongoDB collections.
224
+
225
+ ## Installation
226
+
227
+ ```bash
228
+ npm install @mastra/mongodb@beta
229
+ ```
230
+
231
+ ## Usage Example
232
+
233
+ ```typescript
234
+ import { MongoDBVector } from "@mastra/mongodb";
235
+
236
+ const store = new MongoDBVector({
237
+ id: 'mongodb-vector',
238
+ url: process.env.MONGODB_URL,
239
+ database: process.env.MONGODB_DATABASE,
240
+ });
241
+ ```
242
+
243
+ ## Constructor Options
244
+
245
+ ## Methods
246
+
247
+ ### createIndex()
248
+
249
+ Creates a new vector index (collection) in MongoDB.
250
+
251
+ ### upsert()
252
+
253
+ Adds or updates vectors and their metadata in the collection.
254
+
255
+ ### query()
256
+
257
+ Searches for similar vectors with optional metadata filtering.
258
+
259
+ ### describeIndex()
260
+
261
+ Returns information about the index (collection).
262
+
263
+ Returns:
264
+
265
+ ```typescript
266
+ interface IndexStats {
267
+ dimension: number;
268
+ count: number;
269
+ metric: "cosine" | "euclidean" | "dotproduct";
270
+ }
271
+ ```
272
+
273
+ ### deleteIndex()
274
+
275
+ Deletes a collection and all its data.
276
+
277
+ ### listIndexes()
278
+
279
+ Lists all vector collections in the MongoDB database.
280
+
281
+ Returns: `Promise<string[]>`
282
+
283
+ ### updateVector()
284
+
285
+ Update a single vector by ID or by metadata filter. Either `id` or `filter` must be provided, but not both.
286
+
287
+ ### deleteVector()
288
+
289
+ Deletes a specific vector entry from an index by its ID.
290
+
291
+ ### deleteVectors()
292
+
293
+ Delete multiple vectors by IDs or by metadata filter. Either `ids` or `filter` must be provided, but not both.
294
+
295
+ ### disconnect()
296
+
297
+ Closes the MongoDB client connection. Should be called when done using the store.
298
+
299
+ ## Response Types
300
+
301
+ Query results are returned in this format:
302
+
303
+ ```typescript
304
+ interface QueryResult {
305
+ id: string;
306
+ score: number;
307
+ metadata: Record<string, any>;
308
+ vector?: number[]; // Only included if includeVector is true
309
+ }
310
+ ```
311
+
312
+ ## Error Handling
313
+
314
+ The store throws typed errors that can be caught:
315
+
316
+ ```typescript
317
+ try {
318
+ await store.query({
319
+ indexName: "my_collection",
320
+ queryVector: queryVector,
321
+ });
322
+ } catch (error) {
323
+ // Handle specific error cases
324
+ if (error.message.includes("Invalid collection name")) {
325
+ console.error(
326
+ "Collection name must start with a letter or underscore and contain only valid characters.",
327
+ );
328
+ } else if (error.message.includes("Collection not found")) {
329
+ console.error("The specified collection does not exist");
330
+ } else {
331
+ console.error("Vector store error:", error.message);
332
+ }
333
+ }
334
+ ```
335
+
336
+ ## Best Practices
337
+
338
+ - Index metadata fields used in filters for optimal query performance.
339
+ - Use consistent field naming in metadata to avoid unexpected query results.
340
+ - Regularly monitor index and collection statistics to ensure efficient search.
341
+
342
+ ## Usage Example
343
+
344
+ ### Vector embeddings with MongoDB
345
+
346
+ Embeddings are numeric vectors used by memory's `semanticRecall` to retrieve related messages by meaning (not keywords).
347
+
348
+ > Note: You must use a deployment hosted on MongoDB Atlas to successfully use the MongoDB Vector database.
349
+
350
+ This setup uses FastEmbed, a local embedding model, to generate vector embeddings.
351
+ To use this, install `@mastra/fastembed`:
352
+
353
+ ```bash
354
+ npm install @mastra/fastembed@beta
355
+ ```
356
+
357
+ Add the following to your agent:
358
+
359
+ ```typescript title="src/mastra/agents/example-mongodb-agent.ts"
360
+ import { Memory } from "@mastra/memory";
361
+ import { Agent } from "@mastra/core/agent";
362
+ import { MongoDBStore, MongoDBVector } from "@mastra/mongodb";
363
+ import { fastembed } from "@mastra/fastembed";
364
+
365
+ export const mongodbAgent = new Agent({
366
+ id: "mongodb-agent",
367
+ name: "mongodb-agent",
368
+ instructions:
369
+ "You are an AI agent with the ability to automatically recall memories from previous interactions.",
370
+ model: "openai/gpt-5.1",
371
+ memory: new Memory({
372
+ storage: new MongoDBStore({
373
+ url: process.env.MONGODB_URI!,
374
+ dbName: process.env.MONGODB_DB_NAME!,
375
+ }),
376
+ vector: new MongoDBVector({
377
+ uri: process.env.MONGODB_URI!,
378
+ dbName: process.env.MONGODB_DB_NAME!,
379
+ }),
380
+ embedder: fastembed,
381
+ options: {
382
+ lastMessages: 10,
383
+ semanticRecall: {
384
+ topK: 3,
385
+ messageRange: 2,
386
+ },
387
+ threads: {
388
+ generateTitle: true, // generates descriptive thread titles automatically
389
+ },
390
+ },
391
+ }),
392
+ });
393
+ ```
394
+
395
+ ## Related
396
+
397
+ - [Metadata Filters](../rag/metadata-filters)
398
+
399
+ ---
400
+
401
+ ## Reference: PG Vector Store
402
+
403
+ > Documentation for the PgVector class in Mastra, which provides vector search using PostgreSQL with pgvector extension.
404
+
405
+ The PgVector class provides vector search using [PostgreSQL](https://www.postgresql.org/) with [pgvector](https://github.com/pgvector/pgvector) extension.
406
+ It provides robust vector similarity search capabilities within your existing PostgreSQL database.
407
+
408
+ ## Constructor Options
409
+
410
+ ## Constructor Examples
411
+
412
+ ### Connection String
413
+
414
+ ```ts
415
+ import { PgVector } from "@mastra/pg";
416
+
417
+ const vectorStore = new PgVector({
418
+ id: 'pg-vector',
419
+ connectionString: "postgresql://user:password@localhost:5432/mydb",
420
+ });
421
+ ```
422
+
423
+ ### Host/Port/Database Configuration
424
+
425
+ ```ts
426
+ const vectorStore = new PgVector({
427
+ id: 'pg-vector',
428
+ host: "localhost",
429
+ port: 5432,
430
+ database: "mydb",
431
+ user: "postgres",
432
+ password: "password",
433
+ });
434
+ ```
435
+
436
+ ### Advanced Configuration
437
+
438
+ ```ts
439
+ const vectorStore = new PgVector({
440
+ id: 'pg-vector',
441
+ connectionString: "postgresql://user:password@localhost:5432/mydb",
442
+ schemaName: "custom_schema",
443
+ max: 30,
444
+ idleTimeoutMillis: 60000,
445
+ pgPoolOptions: {
446
+ connectionTimeoutMillis: 5000,
447
+ allowExitOnIdle: true,
448
+ },
449
+ });
450
+ ```
451
+
452
+ ## Methods
453
+
454
+ ### createIndex()
455
+
456
+ #### IndexConfig
457
+
458
+ #### Memory Requirements
459
+
460
+ HNSW indexes require significant shared memory during construction. For 100K vectors:
461
+
462
+ - Small dimensions (64d): ~60MB with default settings
463
+ - Medium dimensions (256d): ~180MB with default settings
464
+ - Large dimensions (384d+): ~250MB+ with default settings
465
+
466
+ Higher M values or efConstruction values will increase memory requirements significantly. Adjust your system's shared memory limits if needed.
467
+
468
+ ### upsert()
469
+
470
+ ### query()
471
+
472
+ ### listIndexes()
473
+
474
+ Returns an array of index names as strings.
475
+
476
+ ### describeIndex()
477
+
478
+ Returns:
479
+
480
+ ```typescript
481
+ interface PGIndexStats {
482
+ dimension: number;
483
+ count: number;
484
+ metric: "cosine" | "euclidean" | "dotproduct";
485
+ type: "flat" | "hnsw" | "ivfflat";
486
+ config: {
487
+ m?: number;
488
+ efConstruction?: number;
489
+ lists?: number;
490
+ probes?: number;
491
+ };
492
+ }
493
+ ```
494
+
495
+ ### deleteIndex()
496
+
497
+ ### updateVector()
498
+
499
+ Update a single vector by ID or by metadata filter. Either `id` or `filter` must be provided, but not both.
500
+
501
+ Updates an existing vector by ID or filter. At least one of vector or metadata must be provided in the update object.
502
+
503
+ ```typescript
504
+ // Update by ID
505
+ await pgVector.updateVector({
506
+ indexName: "my_vectors",
507
+ id: "vector123",
508
+ update: {
509
+ vector: [0.1, 0.2, 0.3],
510
+ metadata: { label: "updated" },
511
+ },
512
+ });
513
+
514
+ // Update by filter
515
+ await pgVector.updateVector({
516
+ indexName: "my_vectors",
517
+ filter: { category: "product" },
518
+ update: {
519
+ metadata: { status: "reviewed" },
520
+ },
521
+ });
522
+ ```
523
+
524
+ ### deleteVector()
525
+
526
+ Deletes a single vector by ID from the specified index.
527
+
528
+ ```typescript
529
+ await pgVector.deleteVector({ indexName: "my_vectors", id: "vector123" });
530
+ ```
531
+
532
+ ### deleteVectors()
533
+
534
+ Delete multiple vectors by IDs or by metadata filter. Either `ids` or `filter` must be provided, but not both.
535
+
536
+ ### disconnect()
537
+
538
+ Closes the database connection pool. Should be called when done using the store.
539
+
540
+ ### buildIndex()
541
+
542
+ Builds or rebuilds an index with specified metric and configuration. Will drop any existing index before creating the new one.
543
+
544
+ ```typescript
545
+ // Define HNSW index
546
+ await pgVector.buildIndex("my_vectors", "cosine", {
547
+ type: "hnsw",
548
+ hnsw: {
549
+ m: 8,
550
+ efConstruction: 32,
551
+ },
552
+ });
553
+
554
+ // Define IVF index
555
+ await pgVector.buildIndex("my_vectors", "cosine", {
556
+ type: "ivfflat",
557
+ ivf: {
558
+ lists: 100,
559
+ },
560
+ });
561
+
562
+ // Define flat index
563
+ await pgVector.buildIndex("my_vectors", "cosine", {
564
+ type: "flat",
565
+ });
566
+ ```
567
+
568
+ ## Response Types
569
+
570
+ Query results are returned in this format:
571
+
572
+ ```typescript
573
+ interface QueryResult {
574
+ id: string;
575
+ score: number;
576
+ metadata: Record<string, any>;
577
+ vector?: number[]; // Only included if includeVector is true
578
+ }
579
+ ```
580
+
581
+ ## Error Handling
582
+
583
+ The store throws typed errors that can be caught:
584
+
585
+ ```typescript
586
+ try {
587
+ await store.query({
588
+ indexName: "index_name",
589
+ queryVector: queryVector,
590
+ });
591
+ } catch (error) {
592
+ if (error instanceof VectorStoreError) {
593
+ console.log(error.code); // 'connection_failed' | 'invalid_dimension' | etc
594
+ console.log(error.details); // Additional error context
595
+ }
596
+ }
597
+ ```
598
+
599
+ ## Index Configuration Guide
600
+
601
+ ### Performance Optimization
602
+
603
+ #### IVFFlat Tuning
604
+
605
+ - **lists parameter**: Set to `sqrt(n) * 2` where n is the number of vectors
606
+ - More lists = better accuracy but slower build time
607
+ - Fewer lists = faster build but potentially lower accuracy
608
+
609
+ #### HNSW Tuning
610
+
611
+ - **m parameter**:
612
+ - 8-16: Moderate accuracy, lower memory
613
+ - 16-32: High accuracy, moderate memory
614
+ - 32-64: Very high accuracy, high memory
615
+ - **efConstruction**:
616
+ - 32-64: Fast build, good quality
617
+ - 64-128: Slower build, better quality
618
+ - 128-256: Slowest build, best quality
619
+
620
+ ### Index Recreation Behavior
621
+
622
+ The system automatically detects configuration changes and only rebuilds indexes when necessary:
623
+
624
+ - Same configuration: Index is kept (no recreation)
625
+ - Changed configuration: Index is dropped and rebuilt
626
+ - This prevents the performance issues from unnecessary index recreations
627
+
628
+ ## Best Practices
629
+
630
+ - Regularly evaluate your index configuration to ensure optimal performance.
631
+ - Adjust parameters like `lists` and `m` based on dataset size and query requirements.
632
+ - **Monitor index performance** using `describeIndex()` to track usage
633
+ - Rebuild indexes periodically to maintain efficiency, especially after significant data changes
634
+
635
+ ## Direct Pool Access
636
+
637
+ The `PgVector` class exposes its underlying PostgreSQL connection pool as a public field:
638
+
639
+ ```typescript
640
+ pgVector.pool; // instance of pg.Pool
641
+ ```
642
+
643
+ This enables advanced usage such as running direct SQL queries, managing transactions, or monitoring pool state. When using the pool directly:
644
+
645
+ - You are responsible for releasing clients (`client.release()`) after use.
646
+ - The pool remains accessible after calling `disconnect()`, but new queries will fail.
647
+ - Direct access bypasses any validation or transaction logic provided by PgVector methods.
648
+
649
+ This design supports advanced use cases but requires careful resource management by the user.
650
+
651
+ ## Usage Example
652
+
653
+ ### Local embeddings with fastembed
654
+
655
+ Embeddings are numeric vectors used by memory's `semanticRecall` to retrieve related messages by meaning (not keywords). This setup uses `@mastra/fastembed` to generate vector embeddings.
656
+
657
+ Install `fastembed` to get started:
658
+
659
+ ```bash
660
+ npm install @mastra/fastembed@beta
661
+ ```
662
+
663
+ Add the following to your agent:
664
+
665
+ ```typescript title="src/mastra/agents/example-pg-agent.ts"
666
+ import { Memory } from "@mastra/memory";
667
+ import { Agent } from "@mastra/core/agent";
668
+ import { PostgresStore, PgVector } from "@mastra/pg";
669
+ import { fastembed } from "@mastra/fastembed";
670
+
671
+ export const pgAgent = new Agent({
672
+ id: "pg-agent",
673
+ name: "PG Agent",
674
+ instructions:
675
+ "You are an AI agent with the ability to automatically recall memories from previous interactions.",
676
+ model: "openai/gpt-5.1",
677
+ memory: new Memory({
678
+ storage: new PostgresStore({
679
+ id: 'pg-agent-storage',
680
+ connectionString: process.env.DATABASE_URL!,
681
+ }),
682
+ vector: new PgVector({
683
+ id: 'pg-agent-vector',
684
+ connectionString: process.env.DATABASE_URL!,
685
+ }),
686
+ embedder: fastembed,
687
+ options: {
688
+ lastMessages: 10,
689
+ semanticRecall: {
690
+ topK: 3,
691
+ messageRange: 2,
692
+ },
693
+ },
694
+ }),
695
+ });
696
+ ```
697
+
698
+ ## Related
699
+
700
+ - [Metadata Filters](../rag/metadata-filters)
701
+
702
+ ---
703
+
704
+ ## Reference: Upstash Vector Store
705
+
706
+ > Documentation for the UpstashVector class in Mastra, which provides vector search using Upstash Vector.
707
+
708
+ The UpstashVector class provides vector search using [Upstash Vector](https://upstash.com/vector), a serverless vector database service that provides vector similarity search with metadata filtering capabilities and hybrid search support.
709
+
710
+ ## Constructor Options
711
+
712
+ ## Methods
713
+
714
+ ### createIndex()
715
+
716
+ Note: This method is a no-op for Upstash as indexes are created automatically.
717
+
718
+ ### upsert()
719
+
720
+ ### query()
721
+
722
+ ### listIndexes()
723
+
724
+ Returns an array of index names (namespaces) as strings.
725
+
726
+ ### describeIndex()
727
+
728
+ Returns:
729
+
730
+ ```typescript
731
+ interface IndexStats {
732
+ dimension: number;
733
+ count: number;
734
+ metric: "cosine" | "euclidean" | "dotproduct";
735
+ }
736
+ ```
737
+
738
+ ### deleteIndex()
739
+
740
+ ### updateVector()
741
+
742
+ The `update` object can have the following properties:
743
+
744
+ - `vector` (optional): An array of numbers representing the new dense vector.
745
+ - `sparseVector` (optional): A sparse vector object with `indices` and `values` arrays for hybrid indexes.
746
+ - `metadata` (optional): A record of key-value pairs for metadata.
747
+
748
+ ### deleteVector()
749
+
750
+ Attempts to delete an item by its ID from the specified index. Logs an error message if the deletion fails.
751
+
752
+ ## Hybrid Vector Search
753
+
754
+ Upstash Vector supports hybrid search that combines semantic search (dense vectors) with keyword-based search (sparse vectors) for improved relevance and accuracy.
755
+
756
+ ### Basic Hybrid Usage
757
+
758
+ ```typescript
759
+ import { UpstashVector } from "@mastra/upstash";
760
+
761
+ const vectorStore = new UpstashVector({
762
+ id: 'upstash-vector',
763
+ url: process.env.UPSTASH_VECTOR_URL,
764
+ token: process.env.UPSTASH_VECTOR_TOKEN,
765
+ });
766
+
767
+ // Upsert vectors with both dense and sparse components
768
+ const denseVectors = [
769
+ [0.1, 0.2, 0.3],
770
+ [0.4, 0.5, 0.6],
771
+ ];
772
+ const sparseVectors = [
773
+ { indices: [1, 5, 10], values: [0.8, 0.6, 0.4] },
774
+ { indices: [2, 6, 11], values: [0.7, 0.5, 0.3] },
775
+ ];
776
+
777
+ await vectorStore.upsert({
778
+ indexName: "hybrid-index",
779
+ vectors: denseVectors,
780
+ sparseVectors: sparseVectors,
781
+ metadata: [{ title: "Document 1" }, { title: "Document 2" }],
782
+ });
783
+
784
+ // Query with hybrid search
785
+ const results = await vectorStore.query({
786
+ indexName: "hybrid-index",
787
+ queryVector: [0.1, 0.2, 0.3],
788
+ sparseVector: { indices: [1, 5], values: [0.9, 0.7] },
789
+ topK: 10,
790
+ });
791
+ ```
792
+
793
+ ### Advanced Hybrid Search Options
794
+
795
+ ```typescript
796
+ import { FusionAlgorithm, QueryMode } from "@upstash/vector";
797
+
798
+ // Query with specific fusion algorithm
799
+ const fusionResults = await vectorStore.query({
800
+ indexName: "hybrid-index",
801
+ queryVector: [0.1, 0.2, 0.3],
802
+ sparseVector: { indices: [1, 5], values: [0.9, 0.7] },
803
+ fusionAlgorithm: FusionAlgorithm.RRF,
804
+ topK: 10,
805
+ });
806
+
807
+ // Dense-only search
808
+ const denseResults = await vectorStore.query({
809
+ indexName: "hybrid-index",
810
+ queryVector: [0.1, 0.2, 0.3],
811
+ queryMode: QueryMode.DENSE,
812
+ topK: 10,
813
+ });
814
+
815
+ // Sparse-only search
816
+ const sparseResults = await vectorStore.query({
817
+ indexName: "hybrid-index",
818
+ queryVector: [0.1, 0.2, 0.3], // Still required for index structure
819
+ sparseVector: { indices: [1, 5], values: [0.9, 0.7] },
820
+ queryMode: QueryMode.SPARSE,
821
+ topK: 10,
822
+ });
823
+ ```
824
+
825
+ ### Updating Hybrid Vectors
826
+
827
+ ```typescript
828
+ // Update both dense and sparse components
829
+ await vectorStore.updateVector({
830
+ indexName: "hybrid-index",
831
+ id: "vector-id",
832
+ update: {
833
+ vector: [0.2, 0.3, 0.4],
834
+ sparseVector: { indices: [2, 7, 12], values: [0.9, 0.8, 0.6] },
835
+ metadata: { title: "Updated Document" },
836
+ },
837
+ });
838
+ ```
839
+
840
+ ## Response Types
841
+
842
+ Query results are returned in this format:
843
+
844
+ ```typescript
845
+ interface QueryResult {
846
+ id: string;
847
+ score: number;
848
+ metadata: Record<string, any>;
849
+ vector?: number[]; // Only included if includeVector is true
850
+ }
851
+ ```
852
+
853
+ ## Error Handling
854
+
855
+ The store throws typed errors that can be caught:
856
+
857
+ ```typescript
858
+ try {
859
+ await store.query({
860
+ indexName: "index_name",
861
+ queryVector: queryVector,
862
+ });
863
+ } catch (error) {
864
+ if (error instanceof VectorStoreError) {
865
+ console.log(error.code); // 'connection_failed' | 'invalid_dimension' | etc
866
+ console.log(error.details); // Additional error context
867
+ }
868
+ }
869
+ ```
870
+
871
+ ## Environment Variables
872
+
873
+ Required environment variables:
874
+
875
+ - `UPSTASH_VECTOR_URL`: Your Upstash Vector database URL
876
+ - `UPSTASH_VECTOR_TOKEN`: Your Upstash Vector API token
877
+
878
+ ## Usage Example
879
+
880
+ ### Local embeddings with fastembed
881
+
882
+ Embeddings are numeric vectors used by memory's `semanticRecall` to retrieve related messages by meaning (not keywords). This setup uses `@mastra/fastembed` to generate vector embeddings.
883
+
884
+ Install `fastembed` to get started:
885
+
886
+ ```bash
887
+ npm install @mastra/fastembed@beta
888
+ ```
889
+
890
+ Add the following to your agent:
891
+
892
+ ```typescript title="src/mastra/agents/example-upstash-agent.ts"
893
+ import { Memory } from "@mastra/memory";
894
+ import { Agent } from "@mastra/core/agent";
895
+ import { UpstashStore, UpstashVector } from "@mastra/upstash";
896
+ import { fastembed } from "@mastra/fastembed";
897
+
898
+ export const upstashAgent = new Agent({
899
+ id: "upstash-agent",
900
+ name: "Upstash Agent",
901
+ instructions:
902
+ "You are an AI agent with the ability to automatically recall memories from previous interactions.",
903
+ model: "openai/gpt-5.1",
904
+ memory: new Memory({
905
+ storage: new UpstashStore({
906
+ id: 'upstash-agent-storage',
907
+ url: process.env.UPSTASH_REDIS_REST_URL!,
908
+ token: process.env.UPSTASH_REDIS_REST_TOKEN!,
909
+ }),
910
+ vector: new UpstashVector({
911
+ id: 'upstash-agent-vector',
912
+ url: process.env.UPSTASH_VECTOR_REST_URL!,
913
+ token: process.env.UPSTASH_VECTOR_REST_TOKEN!,
914
+ }),
915
+ embedder: fastembed,
916
+ options: {
917
+ lastMessages: 10,
918
+ semanticRecall: {
919
+ topK: 3,
920
+ messageRange: 2,
921
+ },
922
+ },
923
+ }),
924
+ });
925
+ ```
926
+
927
+ ## Related
928
+
929
+ - [Metadata Filters](../rag/metadata-filters)