@mastra/rag 0.0.0-commonjs-20250227130920

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.
Files changed (40) hide show
  1. package/.turbo/turbo-build.log +23 -0
  2. package/CHANGELOG.md +1151 -0
  3. package/LICENSE +44 -0
  4. package/README.md +26 -0
  5. package/dist/_tsup-dts-rollup.d.cts +620 -0
  6. package/dist/_tsup-dts-rollup.d.ts +620 -0
  7. package/dist/index.cjs +2524 -0
  8. package/dist/index.d.cts +22 -0
  9. package/dist/index.d.ts +22 -0
  10. package/dist/index.js +2505 -0
  11. package/docker-compose.yaml +22 -0
  12. package/eslint.config.js +6 -0
  13. package/package.json +72 -0
  14. package/src/document/document.test.ts +985 -0
  15. package/src/document/document.ts +281 -0
  16. package/src/document/index.ts +2 -0
  17. package/src/document/transformers/character.ts +279 -0
  18. package/src/document/transformers/html.ts +346 -0
  19. package/src/document/transformers/json.ts +265 -0
  20. package/src/document/transformers/latex.ts +19 -0
  21. package/src/document/transformers/markdown.ts +244 -0
  22. package/src/document/transformers/text.ts +134 -0
  23. package/src/document/transformers/token.ts +148 -0
  24. package/src/document/transformers/transformer.ts +5 -0
  25. package/src/document/types.ts +103 -0
  26. package/src/graph-rag/index.test.ts +235 -0
  27. package/src/graph-rag/index.ts +306 -0
  28. package/src/index.ts +6 -0
  29. package/src/rerank/index.test.ts +150 -0
  30. package/src/rerank/index.ts +154 -0
  31. package/src/tools/document-chunker.ts +30 -0
  32. package/src/tools/graph-rag.ts +117 -0
  33. package/src/tools/index.ts +3 -0
  34. package/src/tools/vector-query.ts +90 -0
  35. package/src/utils/default-settings.ts +33 -0
  36. package/src/utils/index.ts +2 -0
  37. package/src/utils/vector-prompts.ts +729 -0
  38. package/src/utils/vector-search.ts +41 -0
  39. package/tsconfig.json +5 -0
  40. package/vitest.config.ts +11 -0
@@ -0,0 +1,729 @@
1
+ /**
2
+ * Vector store specific prompts that detail supported operators and examples.
3
+ * These prompts help users construct valid filters for each vector store.
4
+ */
5
+
6
+ export const ASTRA_PROMPT = `When querying Astra, you can ONLY use the operators listed below. Any other operators will be rejected.
7
+ Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
8
+ If a user tries to give an explicit operator that is not supported, reject the filter entirely and let them know that the operator is not supported.
9
+
10
+ Basic Comparison Operators:
11
+ - $eq: Exact match (default when using field: value)
12
+ Example: { "category": "electronics" }
13
+ - $ne: Not equal
14
+ Example: { "category": { "$ne": "electronics" } }
15
+ - $gt: Greater than
16
+ Example: { "price": { "$gt": 100 } }
17
+ - $gte: Greater than or equal
18
+ Example: { "price": { "$gte": 100 } }
19
+ - $lt: Less than
20
+ Example: { "price": { "$lt": 100 } }
21
+ - $lte: Less than or equal
22
+ Example: { "price": { "$lte": 100 } }
23
+
24
+ Array Operators:
25
+ - $in: Match any value in array
26
+ Example: { "category": { "$in": ["electronics", "books"] } }
27
+ - $nin: Does not match any value in array
28
+ Example: { "category": { "$nin": ["electronics", "books"] } }
29
+ - $all: Match all values in array
30
+ Example: { "tags": { "$all": ["premium", "sale"] } }
31
+
32
+ Logical Operators:
33
+ - $and: Logical AND (can be implicit or explicit)
34
+ Implicit Example: { "price": { "$gt": 100 }, "category": "electronics" }
35
+ Explicit Example: { "$and": [{ "price": { "$gt": 100 } }, { "category": "electronics" }] }
36
+ - $or: Logical OR
37
+ Example: { "$or": [{ "price": { "$lt": 50 } }, { "category": "books" }] }
38
+ - $not: Logical NOT
39
+ Example: { "$not": { "category": "electronics" } }
40
+
41
+ Element Operators:
42
+ - $exists: Check if field exists
43
+ Example: { "rating": { "$exists": true } }
44
+
45
+ Special Operators:
46
+ - $size: Array length check
47
+ Example: { "tags": { "$size": 2 } }
48
+
49
+ Restrictions:
50
+ - Regex patterns are not supported
51
+ - Only $and, $or, and $not logical operators are supported
52
+ - Nested fields are supported using dot notation
53
+ - Multiple conditions on the same field are supported with both implicit and explicit $and
54
+ - Empty arrays in $in/$nin will return no results
55
+ - A non-empty array is required for $all operator
56
+ - Only logical operators ($and, $or, $not) can be used at the top level
57
+ - All other operators must be used within a field condition
58
+ Valid: { "field": { "$gt": 100 } }
59
+ Valid: { "$and": [...] }
60
+ Invalid: { "$gt": 100 }
61
+ - Logical operators must contain field conditions, not direct operators
62
+ Valid: { "$and": [{ "field": { "$gt": 100 } }] }
63
+ Invalid: { "$and": [{ "$gt": 100 }] }
64
+ - $not operator:
65
+ - Must be an object
66
+ - Cannot be empty
67
+ - Can be used at field level or top level
68
+ - Valid: { "$not": { "field": "value" } }
69
+ - Valid: { "field": { "$not": { "$eq": "value" } } }
70
+ - Other logical operators ($and, $or):
71
+ - Can only be used at top level or nested within other logical operators
72
+ - Can not be used on a field level, or be nested inside a field
73
+ - Can not be used inside an operator
74
+ - Valid: { "$and": [{ "field": { "$gt": 100 } }] }
75
+ - Valid: { "$or": [{ "$and": [{ "field": { "$gt": 100 } }] }] }
76
+ - Invalid: { "field": { "$and": [{ "$gt": 100 }] } }
77
+ - Invalid: { "field": { "$or": [{ "$gt": 100 }] } }
78
+ - Invalid: { "field": { "$gt": { "$and": [{...}] } } }
79
+
80
+ Example Complex Query:
81
+ {
82
+ "$and": [
83
+ { "category": { "$in": ["electronics", "computers"] } },
84
+ { "price": { "$gte": 100, "$lte": 1000 } },
85
+ { "tags": { "$all": ["premium"] } },
86
+ { "rating": { "$exists": true, "$gt": 4 } },
87
+ { "$or": [
88
+ { "stock": { "$gt": 0 } },
89
+ { "preorder": true }
90
+ ]}
91
+ ]
92
+ }`;
93
+
94
+ export const CHROMA_PROMPT = `When querying Chroma, you can ONLY use the operators listed below. Any other operators will be rejected.
95
+ Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
96
+ If a user tries to give an explicit operator that is not supported, reject the filter entirely and let them know that the operator is not supported.
97
+
98
+ Basic Comparison Operators:
99
+ - $eq: Exact match (default when using field: value)
100
+ Example: { "category": "electronics" }
101
+ - $ne: Not equal
102
+ Example: { "category": { "$ne": "electronics" } }
103
+ - $gt: Greater than
104
+ Example: { "price": { "$gt": 100 } }
105
+ - $gte: Greater than or equal
106
+ Example: { "price": { "$gte": 100 } }
107
+ - $lt: Less than
108
+ Example: { "price": { "$lt": 100 } }
109
+ - $lte: Less than or equal
110
+ Example: { "price": { "$lte": 100 } }
111
+
112
+ Array Operators:
113
+ - $in: Match any value in array
114
+ Example: { "category": { "$in": ["electronics", "books"] } }
115
+ - $nin: Does not match any value in array
116
+ Example: { "category": { "$nin": ["electronics", "books"] } }
117
+
118
+ Logical Operators:
119
+ - $and: Logical AND
120
+ Example: { "$and": [{ "price": { "$gt": 100 } }, { "category": "electronics" }] }
121
+ - $or: Logical OR
122
+ Example: { "$or": [{ "price": { "$lt": 50 } }, { "category": "books" }] }
123
+
124
+ Restrictions:
125
+ - Regex patterns are not supported
126
+ - Element operators are not supported
127
+ - Only $and and $or logical operators are supported
128
+ - Nested fields are supported using dot notation
129
+ - Multiple conditions on the same field are supported with both implicit and explicit $and
130
+ - Empty arrays in $in/$nin will return no results
131
+ - If multiple top-level fields exist, they're wrapped in $and
132
+ - Only logical operators ($and, $or) can be used at the top level
133
+ - All other operators must be used within a field condition
134
+ Valid: { "field": { "$gt": 100 } }
135
+ Valid: { "$and": [...] }
136
+ Invalid: { "$gt": 100 }
137
+ Invalid: { "$in": [...] }
138
+ - Logical operators must contain field conditions, not direct operators
139
+ Valid: { "$and": [{ "field": { "$gt": 100 } }] }
140
+ Invalid: { "$and": [{ "$gt": 100 }] }
141
+ - Logical operators ($and, $or):
142
+ - Can only be used at top level or nested within other logical operators
143
+ - Can not be used on a field level, or be nested inside a field
144
+ - Can not be used inside an operator
145
+ - Valid: { "$and": [{ "field": { "$gt": 100 } }] }
146
+ - Valid: { "$or": [{ "$and": [{ "field": { "$gt": 100 } }] }] }
147
+ - Invalid: { "field": { "$and": [{ "$gt": 100 }] } }
148
+ - Invalid: { "field": { "$or": [{ "$gt": 100 }] } }
149
+ - Invalid: { "field": { "$gt": { "$and": [{...}] } } }
150
+ Example Complex Query:
151
+ {
152
+ "$and": [
153
+ { "category": { "$in": ["electronics", "computers"] } },
154
+ { "price": { "$gte": 100, "$lte": 1000 } },
155
+ { "$or": [
156
+ { "inStock": true },
157
+ { "preorder": true }
158
+ ]}
159
+ ]
160
+ }`;
161
+
162
+ export const LIBSQL_PROMPT = `When querying LibSQL Vector, you can ONLY use the operators listed below. Any other operators will be rejected.
163
+ Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
164
+ If a user tries to give an explicit operator that is not supported, reject the filter entirely and let them know that the operator is not supported.
165
+
166
+ Basic Comparison Operators:
167
+ - $eq: Exact match (default when using field: value)
168
+ Example: { "category": "electronics" }
169
+ - $ne: Not equal
170
+ Example: { "category": { "$ne": "electronics" } }
171
+ - $gt: Greater than
172
+ Example: { "price": { "$gt": 100 } }
173
+ - $gte: Greater than or equal
174
+ Example: { "price": { "$gte": 100 } }
175
+ - $lt: Less than
176
+ Example: { "price": { "$lt": 100 } }
177
+ - $lte: Less than or equal
178
+ Example: { "price": { "$lte": 100 } }
179
+
180
+ Array Operators:
181
+ - $in: Match any value in array
182
+ Example: { "category": { "$in": ["electronics", "books"] } }
183
+ - $nin: Does not match any value in array
184
+ Example: { "category": { "$nin": ["electronics", "books"] } }
185
+ - $all: Match all values in array
186
+ Example: { "tags": { "$all": ["premium", "sale"] } }
187
+ - $elemMatch: Match array elements that meet all specified conditions
188
+ Example: { "items": { "$elemMatch": { "price": { "$gt": 100 } } } }
189
+ - $contains: Check if array contains value
190
+ Example: { "tags": { "$contains": "premium" } }
191
+
192
+ Logical Operators:
193
+ - $and: Logical AND (implicit when using multiple conditions)
194
+ Example: { "$and": [{ "price": { "$gt": 100 } }, { "category": "electronics" }] }
195
+ - $or: Logical OR
196
+ Example: { "$or": [{ "price": { "$lt": 50 } }, { "category": "books" }] }
197
+ - $not: Logical NOT
198
+ Example: { "$not": { "category": "electronics" } }
199
+ - $nor: Logical NOR
200
+ Example: { "$nor": [{ "price": { "$lt": 50 } }, { "category": "books" }] }
201
+
202
+ Element Operators:
203
+ - $exists: Check if field exists
204
+ Example: { "rating": { "$exists": true } }
205
+
206
+ Special Operators:
207
+ - $size: Array length check
208
+ Example: { "tags": { "$size": 2 } }
209
+
210
+ Restrictions:
211
+ - Regex patterns are not supported
212
+ - Direct RegExp patterns will throw an error
213
+ - Nested fields are supported using dot notation
214
+ - Multiple conditions on the same field are supported with both implicit and explicit $and
215
+ - Array operations work on array fields only
216
+ - Basic operators handle array values as JSON strings
217
+ - Empty arrays in conditions are handled gracefully
218
+ - Only logical operators ($and, $or, $not, $nor) can be used at the top level
219
+ - All other operators must be used within a field condition
220
+ Valid: { "field": { "$gt": 100 } }
221
+ Valid: { "$and": [...] }
222
+ Invalid: { "$gt": 100 }
223
+ Invalid: { "$contains": "value" }
224
+ - Logical operators must contain field conditions, not direct operators
225
+ Valid: { "$and": [{ "field": { "$gt": 100 } }] }
226
+ Invalid: { "$and": [{ "$gt": 100 }] }
227
+ - $not operator:
228
+ - Must be an object
229
+ - Cannot be empty
230
+ - Can be used at field level or top level
231
+ - Valid: { "$not": { "field": "value" } }
232
+ - Valid: { "field": { "$not": { "$eq": "value" } } }
233
+ - Other logical operators ($and, $or, $nor):
234
+ - Can only be used at top level or nested within other logical operators
235
+ - Can not be used on a field level, or be nested inside a field
236
+ - Can not be used inside an operator
237
+ - Valid: { "$and": [{ "field": { "$gt": 100 } }] }
238
+ - Valid: { "$or": [{ "$and": [{ "field": { "$gt": 100 } }] }] }
239
+ - Invalid: { "field": { "$and": [{ "$gt": 100 }] } }
240
+ - Invalid: { "field": { "$or": [{ "$gt": 100 }] } }
241
+ - Invalid: { "field": { "$gt": { "$and": [{...}] } } }
242
+ - $elemMatch requires an object with conditions
243
+ Valid: { "array": { "$elemMatch": { "field": "value" } } }
244
+ Invalid: { "array": { "$elemMatch": "value" } }
245
+
246
+ Example Complex Query:
247
+ {
248
+ "$and": [
249
+ { "category": { "$in": ["electronics", "computers"] } },
250
+ { "price": { "$gte": 100, "$lte": 1000 } },
251
+ { "tags": { "$all": ["premium", "sale"] } },
252
+ { "items": { "$elemMatch": { "price": { "$gt": 50 }, "inStock": true } } },
253
+ { "$or": [
254
+ { "stock": { "$gt": 0 } },
255
+ { "preorder": true }
256
+ ]}
257
+ ]
258
+ }`;
259
+
260
+ export const PGVECTOR_PROMPT = `When querying PG Vector, you can ONLY use the operators listed below. Any other operators will be rejected.
261
+ Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
262
+ If a user tries to give an explicit operator that is not supported, reject the filter entirely and let them know that the operator is not supported.
263
+
264
+ Basic Comparison Operators:
265
+ - $eq: Exact match (default when using field: value)
266
+ Example: { "category": "electronics" }
267
+ - $ne: Not equal
268
+ Example: { "category": { "$ne": "electronics" } }
269
+ - $gt: Greater than
270
+ Example: { "price": { "$gt": 100 } }
271
+ - $gte: Greater than or equal
272
+ Example: { "price": { "$gte": 100 } }
273
+ - $lt: Less than
274
+ Example: { "price": { "$lt": 100 } }
275
+ - $lte: Less than or equal
276
+ Example: { "price": { "$lte": 100 } }
277
+
278
+ Array Operators:
279
+ - $in: Match any value in array
280
+ Example: { "category": { "$in": ["electronics", "books"] } }
281
+ - $nin: Does not match any value in array
282
+ Example: { "category": { "$nin": ["electronics", "books"] } }
283
+ - $all: Match all values in array
284
+ Example: { "tags": { "$all": ["premium", "sale"] } }
285
+ - $elemMatch: Match array elements that meet all specified conditions
286
+ Example: { "items": { "$elemMatch": { "price": { "$gt": 100 } } } }
287
+ - $contains: Check if array contains value
288
+ Example: { "tags": { "$contains": "premium" } }
289
+
290
+ Logical Operators:
291
+ - $and: Logical AND
292
+ Example: { "$and": [{ "price": { "$gt": 100 } }, { "category": "electronics" }] }
293
+ - $or: Logical OR
294
+ Example: { "$or": [{ "price": { "$lt": 50 } }, { "category": "books" }] }
295
+ - $not: Logical NOT
296
+ Example: { "$not": { "category": "electronics" } }
297
+ - $nor: Logical NOR
298
+ Example: { "$nor": [{ "price": { "$lt": 50 } }, { "category": "books" }] }
299
+
300
+ Element Operators:
301
+ - $exists: Check if field exists
302
+ Example: { "rating": { "$exists": true } }
303
+
304
+ Special Operators:
305
+ - $size: Array length check
306
+ Example: { "tags": { "$size": 2 } }
307
+ - $regex: Pattern matching (PostgreSQL regex syntax)
308
+ Example: { "name": { "$regex": "^iphone" } }
309
+ - $options: Regex options (used with $regex)
310
+ Example: { "name": { "$regex": "iphone", "$options": "i" } }
311
+
312
+ Restrictions:
313
+ - Direct RegExp patterns are supported
314
+ - Nested fields are supported using dot notation
315
+ - Multiple conditions on the same field are supported with both implicit and explicit $and
316
+ - Array operations work on array fields only
317
+ - Regex patterns must follow PostgreSQL syntax
318
+ - Empty arrays in conditions are handled gracefully
319
+ - Only logical operators ($and, $or, $not, $nor) can be used at the top level
320
+ - All other operators must be used within a field condition
321
+ Valid: { "field": { "$gt": 100 } }
322
+ Valid: { "$and": [...] }
323
+ Invalid: { "$gt": 100 }
324
+ Invalid: { "$regex": "pattern" }
325
+ - Logical operators must contain field conditions, not direct operators
326
+ Valid: { "$and": [{ "field": { "$gt": 100 } }] }
327
+ Invalid: { "$and": [{ "$gt": 100 }] }
328
+ - $not operator:
329
+ - Must be an object
330
+ - Cannot be empty
331
+ - Can be used at field level or top level
332
+ - Valid: { "$not": { "field": "value" } }
333
+ - Valid: { "field": { "$not": { "$eq": "value" } } }
334
+ - Other logical operators ($and, $or, $nor):
335
+ - Can only be used at top level or nested within other logical operators
336
+ - Can not be used on a field level, or be nested inside a field
337
+ - Can not be used inside an operator
338
+ - Valid: { "$and": [{ "field": { "$gt": 100 } }] }
339
+ - Valid: { "$or": [{ "$and": [{ "field": { "$gt": 100 } }] }] }
340
+ - Invalid: { "field": { "$and": [{ "$gt": 100 }] } }
341
+ - Invalid: { "field": { "$or": [{ "$gt": 100 }] } }
342
+ - Invalid: { "field": { "$gt": { "$and": [{...}] } } }
343
+ - $elemMatch requires an object with conditions
344
+ Valid: { "array": { "$elemMatch": { "field": "value" } } }
345
+ Invalid: { "array": { "$elemMatch": "value" } }
346
+
347
+ Example Complex Query:
348
+ {
349
+ "$and": [
350
+ { "category": { "$in": ["electronics", "computers"] } },
351
+ { "price": { "$gte": 100, "$lte": 1000 } },
352
+ { "tags": { "$all": ["premium", "sale"] } },
353
+ { "items": { "$elemMatch": { "price": { "$gt": 50 }, "inStock": true } } },
354
+ { "$or": [
355
+ { "name": { "$regex": "^iphone", "$options": "i" } },
356
+ { "description": { "$regex": ".*apple.*" } }
357
+ ]}
358
+ ]
359
+ }`;
360
+
361
+ export const PINECONE_PROMPT = `When querying Pinecone, you can ONLY use the operators listed below. Any other operators will be rejected.
362
+ Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
363
+ If a user tries to give an explicit operator that is not supported, reject the filter entirely and let them know that the operator is not supported.
364
+
365
+ Basic Comparison Operators:
366
+ - $eq: Exact match (default when using field: value)
367
+ Example: { "category": "electronics" }
368
+ - $ne: Not equal
369
+ Example: { "category": { "$ne": "electronics" } }
370
+ - $gt: Greater than
371
+ Example: { "price": { "$gt": 100 } }
372
+ - $gte: Greater than or equal
373
+ Example: { "price": { "$gte": 100 } }
374
+ - $lt: Less than
375
+ Example: { "price": { "$lt": 100 } }
376
+ - $lte: Less than or equal
377
+ Example: { "price": { "$lte": 100 } }
378
+
379
+ Array Operators:
380
+ - $in: Match any value in array
381
+ Example: { "category": { "$in": ["electronics", "books"] } }
382
+ - $nin: Does not match any value in array
383
+ Example: { "category": { "$nin": ["electronics", "books"] } }
384
+ - $all: Match all values in array
385
+ Example: { "tags": { "$all": ["premium", "sale"] } }
386
+
387
+ Logical Operators:
388
+ - $and: Logical AND (can be implicit or explicit)
389
+ Implicit Example: { "price": { "$gt": 100 }, "category": "electronics" }
390
+ Explicit Example: { "$and": [{ "price": { "$gt": 100 } }, { "category": "electronics" }] }
391
+ - $or: Logical OR
392
+ Example: { "$or": [{ "price": { "$lt": 50 } }, { "category": "books" }] }
393
+
394
+ Element Operators:
395
+ - $exists: Check if field exists
396
+ Example: { "rating": { "$exists": true } }
397
+
398
+ Restrictions:
399
+ - Regex patterns are not supported
400
+ - Only $and and $or logical operators are supported at the top level
401
+ - Empty arrays in $in/$nin will return no results
402
+ - A non-empty array is required for $all operator
403
+ - Nested fields are supported using dot notation
404
+ - Multiple conditions on the same field are supported with both implicit and explicit $and
405
+ - At least one key-value pair is required in filter object
406
+ - Empty objects and undefined values are treated as no filter
407
+ - Invalid types in comparison operators will throw errors
408
+ - All non-logical operators must be used within a field condition
409
+ Valid: { "field": { "$gt": 100 } }
410
+ Valid: { "$and": [...] }
411
+ Invalid: { "$gt": 100 }
412
+ - Logical operators must contain field conditions, not direct operators
413
+ Valid: { "$and": [{ "field": { "$gt": 100 } }] }
414
+ Invalid: { "$and": [{ "$gt": 100 }] }
415
+ - Logical operators ($and, $or):
416
+ - Can only be used at top level or nested within other logical operators
417
+ - Can not be used on a field level, or be nested inside a field
418
+ - Can not be used inside an operator
419
+ - Valid: { "$and": [{ "field": { "$gt": 100 } }] }
420
+ - Valid: { "$or": [{ "$and": [{ "field": { "$gt": 100 } }] }] }
421
+ - Invalid: { "field": { "$and": [{ "$gt": 100 }] } }
422
+ - Invalid: { "field": { "$or": [{ "$gt": 100 }] } }
423
+ - Invalid: { "field": { "$gt": { "$and": [{...}] } } }
424
+ Example Complex Query:
425
+ {
426
+ "$and": [
427
+ { "category": { "$in": ["electronics", "computers"] } },
428
+ { "price": { "$gte": 100, "$lte": 1000 } },
429
+ { "tags": { "$all": ["premium", "sale"] } },
430
+ { "rating": { "$exists": true, "$gt": 4 } },
431
+ { "$or": [
432
+ { "stock": { "$gt": 0 } },
433
+ { "preorder": true }
434
+ ]}
435
+ ]
436
+ }`;
437
+
438
+ export const QDRANT_PROMPT = `When querying Qdrant, you can ONLY use the operators listed below. Any other operators will be rejected.
439
+ Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
440
+ If a user tries to give an explicit operator that is not supported, reject the filter entirely and let them know that the operator is not supported.
441
+
442
+ Basic Comparison Operators:
443
+ - $eq: Exact match (default when using field: value)
444
+ Example: { "category": "electronics" }
445
+ - $ne: Not equal
446
+ Example: { "category": { "$ne": "electronics" } }
447
+ - $gt: Greater than
448
+ Example: { "price": { "$gt": 100 } }
449
+ - $gte: Greater than or equal
450
+ Example: { "price": { "$gte": 100 } }
451
+ - $lt: Less than
452
+ Example: { "price": { "$lt": 100 } }
453
+ - $lte: Less than or equal
454
+ Example: { "price": { "$lte": 100 } }
455
+
456
+ Array Operators:
457
+ - $in: Match any value in array
458
+ Example: { "category": { "$in": ["electronics", "books"] } }
459
+ - $nin: Does not match any value in array
460
+ Example: { "category": { "$nin": ["electronics", "books"] } }
461
+
462
+ Logical Operators:
463
+ - $and: Logical AND (implicit when using multiple conditions)
464
+ Example: { "$and": [{ "price": { "$gt": 100 } }, { "category": "electronics" }] }
465
+ - $or: Logical OR
466
+ Example: { "$or": [{ "price": { "$lt": 50 } }, { "category": "books" }] }
467
+ - $not: Logical NOT
468
+ Example: { "$not": { "category": "electronics" } }
469
+
470
+ Element Operators:
471
+ - $exists: Check if field exists
472
+ Example: { "rating": { "$exists": true } }
473
+
474
+ Special Operators:
475
+ - $regex: Pattern matching
476
+ Example: { "name": { "$regex": "iphone.*" } }
477
+ - $count: Array length/value count
478
+ Example: { "tags": { "$count": { "$gt": 2 } } }
479
+ - $geo: Geographical filters (supports radius, box, polygon)
480
+ Example: {
481
+ "location": {
482
+ "$geo": {
483
+ "type": "radius",
484
+ "center": { "lat": 52.5, "lon": 13.4 },
485
+ "radius": 10000
486
+ }
487
+ }
488
+ }
489
+ - $hasId: Match specific document IDs
490
+ Example: { "$hasId": ["doc1", "doc2"] }
491
+ - $hasVector: Check vector existence
492
+ Example: { "$hasVector": "" }
493
+ - $datetime: RFC 3339 datetime range
494
+ Example: {
495
+ "created_at": {
496
+ "$datetime": {
497
+ "range": {
498
+ "gt": "2024-01-01T00:00:00Z",
499
+ "lt": "2024-12-31T23:59:59Z"
500
+ }
501
+ }
502
+ }
503
+ }
504
+ - $null: Check for null values
505
+ Example: { "field": { "$null": true } }
506
+ - $empty: Check for empty values
507
+ Example: { "array": { "$empty": true } }
508
+ - $nested: Nested object filters
509
+ Example: {
510
+ "items[]": {
511
+ "$nested": {
512
+ "price": { "$gt": 100 },
513
+ "stock": { "$gt": 0 }
514
+ }
515
+ }
516
+ }
517
+
518
+ Restrictions:
519
+ - Only logical operators ($and, $or, $not) and collection operators ($hasId, $hasVector) can be used at the top level
520
+ - All other operators must be used within a field condition
521
+ Valid: { "field": { "$gt": 100 } }
522
+ Valid: { "$and": [...] }
523
+ Valid: { "$hasId": [...] }
524
+ Invalid: { "$gt": 100 }
525
+ - Nested fields are supported using dot notation
526
+ - Array fields with nested objects use [] suffix: "items[]"
527
+ - Geo filtering requires specific format for radius, box, or polygon
528
+ - Datetime values must be in RFC 3339 format
529
+ - Empty arrays in conditions are handled as empty values
530
+ - Null values are handled with $null operator
531
+ - Empty values are handled with $empty operator
532
+ - $regex uses standard regex syntax
533
+ - $count can only be used with numeric comparison operators
534
+ - $nested requires an object with conditions
535
+ - Logical operators must contain field conditions, not direct operators
536
+ Valid: { "$and": [{ "field": { "$gt": 100 } }] }
537
+ Invalid: { "$and": [{ "$gt": 100 }] }
538
+ - $not operator:
539
+ - Must be an object
540
+ - Cannot be empty
541
+ - Can be used at field level or top level
542
+ - Valid: { "$not": { "field": "value" } }
543
+ - Valid: { "field": { "$not": { "$eq": "value" } } }
544
+ - Other logical operators ($and, $or):
545
+ - Can only be used at top level or nested within other logical operators
546
+ - Can not be used on a field level, or be nested inside a field
547
+ - Can not be used inside an operator
548
+ - Valid: { "$and": [{ "field": { "$gt": 100 } }] }
549
+ - Valid: { "$or": [{ "$and": [{ "field": { "$gt": 100 } }] }] }
550
+ - Invalid: { "field": { "$and": [{ "$gt": 100 }] } }
551
+ - Invalid: { "field": { "$or": [{ "$gt": 100 }] } }
552
+ - Invalid: { "field": { "$gt": { "$and": [{...}] } } }
553
+ Example Complex Query:
554
+ {
555
+ "$and": [
556
+ { "category": { "$in": ["electronics"] } },
557
+ { "price": { "$gt": 100 } },
558
+ { "location": {
559
+ "$geo": {
560
+ "type": "radius",
561
+ "center": { "lat": 52.5, "lon": 13.4 },
562
+ "radius": 5000
563
+ }
564
+ }},
565
+ { "items[]": {
566
+ "$nested": {
567
+ "price": { "$gt": 50 },
568
+ "stock": { "$gt": 0 }
569
+ }
570
+ }},
571
+ { "created_at": {
572
+ "$datetime": {
573
+ "range": {
574
+ "gt": "2024-01-01T00:00:00Z"
575
+ }
576
+ }
577
+ }},
578
+ { "$or": [
579
+ { "status": { "$ne": "discontinued" } },
580
+ { "clearance": true }
581
+ ]}
582
+ ]
583
+ }`;
584
+
585
+ export const UPSTASH_PROMPT = `When querying Upstash Vector, you can ONLY use the operators listed below. Any other operators will be rejected.
586
+ Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
587
+ If a user tries to give an explicit operator that is not supported, reject the filter entirely and let them know that the operator is not supported.
588
+
589
+ Basic Comparison Operators:
590
+ - $eq: Exact match (default when using field: value)
591
+ Example: { "category": "electronics" } or { "category": { "$eq": "electronics" } }
592
+ - $ne: Not equal
593
+ Example: { "category": { "$ne": "electronics" } }
594
+ - $gt: Greater than
595
+ Example: { "price": { "$gt": 100 } }
596
+ - $gte: Greater than or equal
597
+ Example: { "price": { "$gte": 100 } }
598
+ - $lt: Less than
599
+ Example: { "price": { "$lt": 100 } }
600
+ - $lte: Less than or equal
601
+ Example: { "price": { "$lte": 100 } }
602
+
603
+ Array Operators:
604
+ - $in: Match any value in array
605
+ Example: { "category": { "$in": ["electronics", "books"] } }
606
+ - $nin: Does not match any value in array
607
+ Example: { "category": { "$nin": ["electronics", "books"] } }
608
+ - $all: Matches all values in array
609
+ Example: { "tags": { "$all": ["premium", "new"] } }
610
+
611
+ Logical Operators:
612
+ - $and: Logical AND (implicit when using multiple conditions)
613
+ Example: { "$and": [{ "price": { "$gt": 100 } }, { "category": "electronics" }] }
614
+ - $or: Logical OR
615
+ Example: { "$or": [{ "price": { "$lt": 50 } }, { "category": "books" }] }
616
+ - $not: Logical NOT
617
+ Example: { "$not": { "category": "electronics" } }
618
+ - $nor: Logical NOR
619
+ Example: { "$nor": [{ "price": { "$lt": 50 } }, { "category": "books" }] }
620
+
621
+ Element Operators:
622
+ - $exists: Check if field exists
623
+ Example: { "rating": { "$exists": true } }
624
+
625
+ Special Operators:
626
+ - $regex: Pattern matching using glob syntax (only as operator, not direct RegExp)
627
+ Example: { "name": { "$regex": "iphone*" } }
628
+ - $contains: Check if array/string contains value
629
+ Example: { "tags": { "$contains": "premium" } }
630
+
631
+ Restrictions:
632
+ - Null/undefined values are not supported in any operator
633
+ - Empty arrays are only supported in $in/$nin operators
634
+ - Direct RegExp patterns are not supported, use $regex with glob syntax
635
+ - Nested fields are supported using dot notation
636
+ - Multiple conditions on same field are combined with AND
637
+ - String values with quotes are automatically escaped
638
+ - Only logical operators ($and, $or, $not, $nor) can be used at the top level
639
+ - All other operators must be used within a field condition
640
+ Valid: { "field": { "$gt": 100 } }
641
+ Valid: { "$and": [...] }
642
+ Invalid: { "$gt": 100 }
643
+ - $regex uses glob syntax (*, ?) not standard regex patterns
644
+ - $contains works on both arrays and string fields
645
+ - Logical operators must contain field conditions, not direct operators
646
+ Valid: { "$and": [{ "field": { "$gt": 100 } }] }
647
+ Invalid: { "$and": [{ "$gt": 100 }] }
648
+ - $not operator:
649
+ - Must be an object
650
+ - Cannot be empty
651
+ - Can be used at field level or top level
652
+ - Valid: { "$not": { "field": "value" } }
653
+ - Valid: { "field": { "$not": { "$eq": "value" } } }
654
+ - Other logical operators ($and, $or, $nor):
655
+ - Can only be used at top level or nested within other logical operators
656
+ - Can not be used on a field level, or be nested inside a field
657
+ - Can not be used inside an operator
658
+ - Valid: { "$and": [{ "field": { "$gt": 100 } }] }
659
+ - Valid: { "$or": [{ "$and": [{ "field": { "$gt": 100 } }] }] }
660
+ - Invalid: { "field": { "$and": [{ "$gt": 100 }] } }
661
+ - Invalid: { "field": { "$or": [{ "$gt": 100 }] } }
662
+ - Invalid: { "field": { "$gt": { "$and": [{...}] } } }
663
+ Example Complex Query:
664
+ {
665
+ "$and": [
666
+ { "category": { "$in": ["electronics", "computers"] } },
667
+ { "price": { "$gt": 100, "$lt": 1000 } },
668
+ { "tags": { "$all": ["premium", "new"] } },
669
+ { "name": { "$regex": "iphone*" } },
670
+ { "description": { "$contains": "latest" } },
671
+ { "$or": [
672
+ { "brand": "Apple" },
673
+ { "rating": { "$gte": 4.5 } }
674
+ ]}
675
+ ]
676
+ }`;
677
+
678
+ export const VECTORIZE_PROMPT = `When querying Vectorize, you can ONLY use the operators listed below. Any other operators will be rejected.
679
+ Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
680
+ If a user tries to give an explicit operator that is not supported, reject the filter entirely and let them know that the operator is not supported.
681
+
682
+ Basic Comparison Operators:
683
+ - $eq: Exact match (default when using field: value)
684
+ Example: { "category": "electronics" }
685
+ - $ne: Not equal
686
+ Example: { "category": { "$ne": "electronics" } }
687
+ - $gt: Greater than
688
+ Example: { "price": { "$gt": 100 } }
689
+ - $gte: Greater than or equal
690
+ Example: { "price": { "$gte": 100 } }
691
+ - $lt: Less than
692
+ Example: { "price": { "$lt": 100 } }
693
+ - $lte: Less than or equal
694
+ Example: { "price": { "$lte": 100 } }
695
+
696
+ Array Operators:
697
+ - $in: Match any value in array
698
+ Example: { "category": { "$in": ["electronics", "books"] } }
699
+ - $nin: Does not match any value in array
700
+ Example: { "category": { "$nin": ["electronics", "books"] } }
701
+
702
+ Restrictions:
703
+ - Regex patterns are not supported
704
+ - Logical operators are not supported
705
+ - Element operators are not supported
706
+ - Fields must have a flat structure, as nested fields are not supported
707
+ - Multiple conditions on the same field are supported
708
+ - Empty arrays in $in/$nin will return no results
709
+ - Filter keys cannot be longer than 512 characters
710
+ - Filter keys cannot contain invalid characters ($, ", empty)
711
+ - Filter size is limited to prevent oversized queries
712
+ - Invalid types in operators return no results instead of throwing errors
713
+ - Empty objects are accepted in filters
714
+ - Metadata must use flat structure with dot notation (no nested objects)
715
+ - Must explicitly create metadata indexes for filterable fields (limit 10 per index)
716
+ - Can only effectively filter on indexed metadata fields
717
+ - Metadata values can be strings, numbers, booleans, or homogeneous arrays
718
+ - No operators can be used at the top level (no logical operators supported)
719
+ - All operators must be used within a field condition
720
+ Valid: { "field": { "$gt": 100 } }
721
+ Invalid: { "$gt": 100 }
722
+ Invalid: { "$in": [...] }
723
+
724
+ Example Complex Query:
725
+ {
726
+ "category": { "$in": ["electronics", "computers"] },
727
+ "price": { "$gte": 100, "$lte": 1000 },
728
+ "inStock": true
729
+ }`;