@mastra/upstash 1.0.0-beta.1 → 1.0.0-beta.10
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/CHANGELOG.md +450 -0
- package/README.md +12 -0
- package/dist/docs/README.md +34 -0
- package/dist/docs/SKILL.md +35 -0
- package/dist/docs/SOURCE_MAP.json +6 -0
- package/dist/docs/memory/01-working-memory.md +386 -0
- package/dist/docs/rag/01-vector-databases.md +638 -0
- package/dist/docs/rag/02-retrieval.md +549 -0
- package/dist/docs/storage/01-reference.md +143 -0
- package/dist/docs/vectors/01-reference.md +233 -0
- package/dist/index.cjs +783 -514
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +774 -508
- package/dist/index.js.map +1 -1
- package/dist/storage/db/index.d.ts +47 -0
- package/dist/storage/db/index.d.ts.map +1 -0
- package/dist/storage/domains/memory/index.d.ts +10 -8
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/scores/index.d.ts +6 -9
- package/dist/storage/domains/scores/index.d.ts.map +1 -1
- package/dist/storage/domains/utils.d.ts +0 -2
- package/dist/storage/domains/utils.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +16 -19
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +76 -167
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/vector/index.d.ts +18 -9
- package/dist/vector/index.d.ts.map +1 -1
- package/dist/vector/types.d.ts +15 -3
- package/dist/vector/types.d.ts.map +1 -1
- package/package.json +10 -10
- package/dist/storage/domains/operations/index.d.ts +0 -40
- package/dist/storage/domains/operations/index.d.ts.map +0 -1
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
# Vectors API Reference
|
|
2
|
+
|
|
3
|
+
> API reference for vectors - 1 entries
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Reference: Upstash Vector Store
|
|
9
|
+
|
|
10
|
+
> Documentation for the UpstashVector class in Mastra, which provides vector search using Upstash Vector.
|
|
11
|
+
|
|
12
|
+
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.
|
|
13
|
+
|
|
14
|
+
## Constructor Options
|
|
15
|
+
|
|
16
|
+
## Methods
|
|
17
|
+
|
|
18
|
+
### createIndex()
|
|
19
|
+
|
|
20
|
+
Note: This method is a no-op for Upstash as indexes are created automatically.
|
|
21
|
+
|
|
22
|
+
### upsert()
|
|
23
|
+
|
|
24
|
+
### query()
|
|
25
|
+
|
|
26
|
+
### listIndexes()
|
|
27
|
+
|
|
28
|
+
Returns an array of index names (namespaces) as strings.
|
|
29
|
+
|
|
30
|
+
### describeIndex()
|
|
31
|
+
|
|
32
|
+
Returns:
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
interface IndexStats {
|
|
36
|
+
dimension: number;
|
|
37
|
+
count: number;
|
|
38
|
+
metric: "cosine" | "euclidean" | "dotproduct";
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### deleteIndex()
|
|
43
|
+
|
|
44
|
+
### updateVector()
|
|
45
|
+
|
|
46
|
+
The `update` object can have the following properties:
|
|
47
|
+
|
|
48
|
+
- `vector` (optional): An array of numbers representing the new dense vector.
|
|
49
|
+
- `sparseVector` (optional): A sparse vector object with `indices` and `values` arrays for hybrid indexes.
|
|
50
|
+
- `metadata` (optional): A record of key-value pairs for metadata.
|
|
51
|
+
|
|
52
|
+
### deleteVector()
|
|
53
|
+
|
|
54
|
+
Attempts to delete an item by its ID from the specified index. Logs an error message if the deletion fails.
|
|
55
|
+
|
|
56
|
+
## Hybrid Vector Search
|
|
57
|
+
|
|
58
|
+
Upstash Vector supports hybrid search that combines semantic search (dense vectors) with keyword-based search (sparse vectors) for improved relevance and accuracy.
|
|
59
|
+
|
|
60
|
+
### Basic Hybrid Usage
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import { UpstashVector } from "@mastra/upstash";
|
|
64
|
+
|
|
65
|
+
const vectorStore = new UpstashVector({
|
|
66
|
+
id: 'upstash-vector',
|
|
67
|
+
url: process.env.UPSTASH_VECTOR_URL,
|
|
68
|
+
token: process.env.UPSTASH_VECTOR_TOKEN,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Upsert vectors with both dense and sparse components
|
|
72
|
+
const denseVectors = [
|
|
73
|
+
[0.1, 0.2, 0.3],
|
|
74
|
+
[0.4, 0.5, 0.6],
|
|
75
|
+
];
|
|
76
|
+
const sparseVectors = [
|
|
77
|
+
{ indices: [1, 5, 10], values: [0.8, 0.6, 0.4] },
|
|
78
|
+
{ indices: [2, 6, 11], values: [0.7, 0.5, 0.3] },
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
await vectorStore.upsert({
|
|
82
|
+
indexName: "hybrid-index",
|
|
83
|
+
vectors: denseVectors,
|
|
84
|
+
sparseVectors: sparseVectors,
|
|
85
|
+
metadata: [{ title: "Document 1" }, { title: "Document 2" }],
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Query with hybrid search
|
|
89
|
+
const results = await vectorStore.query({
|
|
90
|
+
indexName: "hybrid-index",
|
|
91
|
+
queryVector: [0.1, 0.2, 0.3],
|
|
92
|
+
sparseVector: { indices: [1, 5], values: [0.9, 0.7] },
|
|
93
|
+
topK: 10,
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Advanced Hybrid Search Options
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
import { FusionAlgorithm, QueryMode } from "@upstash/vector";
|
|
101
|
+
|
|
102
|
+
// Query with specific fusion algorithm
|
|
103
|
+
const fusionResults = await vectorStore.query({
|
|
104
|
+
indexName: "hybrid-index",
|
|
105
|
+
queryVector: [0.1, 0.2, 0.3],
|
|
106
|
+
sparseVector: { indices: [1, 5], values: [0.9, 0.7] },
|
|
107
|
+
fusionAlgorithm: FusionAlgorithm.RRF,
|
|
108
|
+
topK: 10,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// Dense-only search
|
|
112
|
+
const denseResults = await vectorStore.query({
|
|
113
|
+
indexName: "hybrid-index",
|
|
114
|
+
queryVector: [0.1, 0.2, 0.3],
|
|
115
|
+
queryMode: QueryMode.DENSE,
|
|
116
|
+
topK: 10,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// Sparse-only search
|
|
120
|
+
const sparseResults = await vectorStore.query({
|
|
121
|
+
indexName: "hybrid-index",
|
|
122
|
+
queryVector: [0.1, 0.2, 0.3], // Still required for index structure
|
|
123
|
+
sparseVector: { indices: [1, 5], values: [0.9, 0.7] },
|
|
124
|
+
queryMode: QueryMode.SPARSE,
|
|
125
|
+
topK: 10,
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Updating Hybrid Vectors
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
// Update both dense and sparse components
|
|
133
|
+
await vectorStore.updateVector({
|
|
134
|
+
indexName: "hybrid-index",
|
|
135
|
+
id: "vector-id",
|
|
136
|
+
update: {
|
|
137
|
+
vector: [0.2, 0.3, 0.4],
|
|
138
|
+
sparseVector: { indices: [2, 7, 12], values: [0.9, 0.8, 0.6] },
|
|
139
|
+
metadata: { title: "Updated Document" },
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Response Types
|
|
145
|
+
|
|
146
|
+
Query results are returned in this format:
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
interface QueryResult {
|
|
150
|
+
id: string;
|
|
151
|
+
score: number;
|
|
152
|
+
metadata: Record<string, any>;
|
|
153
|
+
vector?: number[]; // Only included if includeVector is true
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Error Handling
|
|
158
|
+
|
|
159
|
+
The store throws typed errors that can be caught:
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
try {
|
|
163
|
+
await store.query({
|
|
164
|
+
indexName: "index_name",
|
|
165
|
+
queryVector: queryVector,
|
|
166
|
+
});
|
|
167
|
+
} catch (error) {
|
|
168
|
+
if (error instanceof VectorStoreError) {
|
|
169
|
+
console.log(error.code); // 'connection_failed' | 'invalid_dimension' | etc
|
|
170
|
+
console.log(error.details); // Additional error context
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## Environment Variables
|
|
176
|
+
|
|
177
|
+
Required environment variables:
|
|
178
|
+
|
|
179
|
+
- `UPSTASH_VECTOR_URL`: Your Upstash Vector database URL
|
|
180
|
+
- `UPSTASH_VECTOR_TOKEN`: Your Upstash Vector API token
|
|
181
|
+
|
|
182
|
+
## Usage Example
|
|
183
|
+
|
|
184
|
+
### Local embeddings with fastembed
|
|
185
|
+
|
|
186
|
+
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.
|
|
187
|
+
|
|
188
|
+
Install `fastembed` to get started:
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
npm install @mastra/fastembed@beta
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
Add the following to your agent:
|
|
195
|
+
|
|
196
|
+
```typescript title="src/mastra/agents/example-upstash-agent.ts"
|
|
197
|
+
import { Memory } from "@mastra/memory";
|
|
198
|
+
import { Agent } from "@mastra/core/agent";
|
|
199
|
+
import { UpstashStore, UpstashVector } from "@mastra/upstash";
|
|
200
|
+
import { fastembed } from "@mastra/fastembed";
|
|
201
|
+
|
|
202
|
+
export const upstashAgent = new Agent({
|
|
203
|
+
id: "upstash-agent",
|
|
204
|
+
name: "Upstash Agent",
|
|
205
|
+
instructions:
|
|
206
|
+
"You are an AI agent with the ability to automatically recall memories from previous interactions.",
|
|
207
|
+
model: "openai/gpt-5.1",
|
|
208
|
+
memory: new Memory({
|
|
209
|
+
storage: new UpstashStore({
|
|
210
|
+
id: 'upstash-agent-storage',
|
|
211
|
+
url: process.env.UPSTASH_REDIS_REST_URL!,
|
|
212
|
+
token: process.env.UPSTASH_REDIS_REST_TOKEN!,
|
|
213
|
+
}),
|
|
214
|
+
vector: new UpstashVector({
|
|
215
|
+
id: 'upstash-agent-vector',
|
|
216
|
+
url: process.env.UPSTASH_VECTOR_REST_URL!,
|
|
217
|
+
token: process.env.UPSTASH_VECTOR_REST_TOKEN!,
|
|
218
|
+
}),
|
|
219
|
+
embedder: fastembed,
|
|
220
|
+
options: {
|
|
221
|
+
lastMessages: 10,
|
|
222
|
+
semanticRecall: {
|
|
223
|
+
topK: 3,
|
|
224
|
+
messageRange: 2,
|
|
225
|
+
},
|
|
226
|
+
},
|
|
227
|
+
}),
|
|
228
|
+
});
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## Related
|
|
232
|
+
|
|
233
|
+
- [Metadata Filters](../rag/metadata-filters)
|