@mastra/chroma 0.11.4 → 0.11.7-alpha.0

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.
@@ -1,418 +0,0 @@
1
- import { MastraError, ErrorDomain, ErrorCategory } from '@mastra/core/error';
2
- import { MastraVector } from '@mastra/core/vector';
3
- import type {
4
- QueryResult,
5
- IndexStats,
6
- CreateIndexParams,
7
- UpsertVectorParams,
8
- QueryVectorParams,
9
- DescribeIndexParams,
10
- DeleteIndexParams,
11
- DeleteVectorParams,
12
- UpdateVectorParams,
13
- } from '@mastra/core/vector';
14
- import { ChromaClient, CloudClient } from 'chromadb';
15
- import type { ChromaClientArgs, RecordSet, Where, WhereDocument, Collection, Metadata } from 'chromadb';
16
- import type { ChromaVectorFilter } from './filter';
17
- import { ChromaFilterTranslator } from './filter';
18
-
19
- interface ChromaUpsertVectorParams extends UpsertVectorParams {
20
- documents?: string[];
21
- }
22
-
23
- interface ChromaQueryVectorParams extends QueryVectorParams<ChromaVectorFilter> {
24
- documentFilter?: WhereDocument | null;
25
- }
26
-
27
- interface ChromaGetRecordsParams {
28
- indexName: string;
29
- ids?: string[];
30
- filter?: ChromaVectorFilter;
31
- documentFilter?: WhereDocument | null;
32
- includeVector?: boolean;
33
- limit?: number;
34
- offset?: number;
35
- }
36
-
37
- type MastraMetadata = {
38
- dimension?: number;
39
- };
40
-
41
- type ChromaVectorArgs = ChromaClientArgs & { apiKey?: string };
42
-
43
- const spaceMappings = {
44
- cosine: 'cosine',
45
- euclidean: 'l2',
46
- dotproduct: 'ip',
47
- l2: 'euclidean',
48
- ip: 'dotproduct',
49
- };
50
-
51
- export class ChromaVector extends MastraVector<ChromaVectorFilter> {
52
- private client: ChromaClient;
53
- private collections: Map<string, Collection>;
54
-
55
- constructor(chromaClientArgs?: ChromaVectorArgs) {
56
- super();
57
- if (chromaClientArgs?.apiKey) {
58
- this.client = new CloudClient({
59
- apiKey: chromaClientArgs.apiKey,
60
- tenant: chromaClientArgs.tenant,
61
- database: chromaClientArgs.database,
62
- });
63
- } else {
64
- this.client = new ChromaClient(chromaClientArgs);
65
- }
66
- this.collections = new Map();
67
- }
68
-
69
- async getCollection({ indexName, forceUpdate = false }: { indexName: string; forceUpdate?: boolean }) {
70
- let collection = this.collections.get(indexName);
71
- if (forceUpdate || !collection) {
72
- try {
73
- collection = await this.client.getCollection({ name: indexName });
74
- this.collections.set(indexName, collection);
75
- return collection;
76
- } catch {
77
- throw new MastraError({
78
- id: 'CHROMA_COLLECTION_GET_FAILED',
79
- domain: ErrorDomain.MASTRA_VECTOR,
80
- category: ErrorCategory.THIRD_PARTY,
81
- details: { indexName },
82
- });
83
- }
84
- }
85
- return collection;
86
- }
87
-
88
- private validateVectorDimensions(vectors: number[][], dimension: number): void {
89
- for (let i = 0; i < vectors.length; i++) {
90
- if (vectors?.[i]?.length !== dimension) {
91
- throw new Error(
92
- `Vector at index ${i} has invalid dimension ${vectors?.[i]?.length}. Expected ${dimension} dimensions.`,
93
- );
94
- }
95
- }
96
- }
97
-
98
- async upsert({ indexName, vectors, metadata, ids, documents }: ChromaUpsertVectorParams): Promise<string[]> {
99
- try {
100
- const collection = await this.getCollection({ indexName });
101
-
102
- const stats = await this.describeIndex({ indexName });
103
- this.validateVectorDimensions(vectors, stats.dimension);
104
- const generatedIds = ids || vectors.map(() => crypto.randomUUID());
105
-
106
- await collection.upsert({
107
- ids: generatedIds,
108
- embeddings: vectors,
109
- metadatas: metadata,
110
- documents: documents,
111
- });
112
-
113
- return generatedIds;
114
- } catch (error: any) {
115
- if (error instanceof MastraError) throw error;
116
- throw new MastraError(
117
- {
118
- id: 'CHROMA_VECTOR_UPSERT_FAILED',
119
- domain: ErrorDomain.MASTRA_VECTOR,
120
- category: ErrorCategory.THIRD_PARTY,
121
- details: { indexName },
122
- },
123
- error,
124
- );
125
- }
126
- }
127
-
128
- async createIndex({ indexName, dimension, metric = 'cosine' }: CreateIndexParams): Promise<void> {
129
- if (!Number.isInteger(dimension) || dimension <= 0) {
130
- throw new MastraError({
131
- id: 'CHROMA_VECTOR_CREATE_INDEX_INVALID_DIMENSION',
132
- text: 'Dimension must be a positive integer',
133
- domain: ErrorDomain.MASTRA_VECTOR,
134
- category: ErrorCategory.USER,
135
- details: { dimension },
136
- });
137
- }
138
-
139
- const hnswSpace = spaceMappings[metric] as 'cosine' | 'l2' | 'ip' | undefined;
140
-
141
- if (!hnswSpace || !['cosine', 'l2', 'ip'].includes(hnswSpace)) {
142
- throw new MastraError({
143
- id: 'CHROMA_VECTOR_CREATE_INDEX_INVALID_METRIC',
144
- text: `Invalid metric: "${metric}". Must be one of: cosine, euclidean, dotproduct`,
145
- domain: ErrorDomain.MASTRA_VECTOR,
146
- category: ErrorCategory.USER,
147
- details: { metric },
148
- });
149
- }
150
-
151
- try {
152
- const collection = await this.client.createCollection({
153
- name: indexName,
154
- metadata: { dimension },
155
- configuration: { hnsw: { space: hnswSpace } },
156
- embeddingFunction: null,
157
- });
158
- this.collections.set(indexName, collection);
159
- } catch (error: any) {
160
- // Check for 'already exists' error
161
- const message = error?.message || error?.toString();
162
- if (message && message.toLowerCase().includes('already exists')) {
163
- // Fetch collection info and check dimension
164
- await this.validateExistingIndex(indexName, dimension, metric);
165
- return;
166
- }
167
- throw new MastraError(
168
- {
169
- id: 'CHROMA_VECTOR_CREATE_INDEX_FAILED',
170
- domain: ErrorDomain.MASTRA_VECTOR,
171
- category: ErrorCategory.THIRD_PARTY,
172
- details: { indexName },
173
- },
174
- error,
175
- );
176
- }
177
- }
178
-
179
- transformFilter(filter?: ChromaVectorFilter) {
180
- const translator = new ChromaFilterTranslator();
181
- const translatedFilter = translator.translate(filter);
182
- return translatedFilter ? (translatedFilter as Where) : undefined;
183
- }
184
-
185
- async query<T extends Metadata = Metadata>({
186
- indexName,
187
- queryVector,
188
- topK = 10,
189
- filter,
190
- includeVector = false,
191
- documentFilter,
192
- }: ChromaQueryVectorParams): Promise<QueryResult[]> {
193
- try {
194
- const collection = await this.getCollection({ indexName });
195
-
196
- const defaultInclude: ['documents', 'metadatas', 'distances'] = ['documents', 'metadatas', 'distances'];
197
-
198
- const translatedFilter = this.transformFilter(filter);
199
- const results = await collection.query<T>({
200
- queryEmbeddings: [queryVector],
201
- nResults: topK,
202
- where: translatedFilter ?? undefined,
203
- whereDocument: documentFilter ?? undefined,
204
- include: includeVector ? [...defaultInclude, 'embeddings'] : defaultInclude,
205
- });
206
-
207
- return (results.ids[0] || []).map((id: string, index: number) => ({
208
- id,
209
- score: results.distances?.[0]?.[index] || 0,
210
- metadata: results.metadatas?.[0]?.[index] || {},
211
- document: results.documents?.[0]?.[index] ?? undefined,
212
- ...(includeVector && { vector: results.embeddings?.[0]?.[index] || [] }),
213
- }));
214
- } catch (error: any) {
215
- if (error instanceof MastraError) throw error;
216
- throw new MastraError(
217
- {
218
- id: 'CHROMA_VECTOR_QUERY_FAILED',
219
- domain: ErrorDomain.MASTRA_VECTOR,
220
- category: ErrorCategory.THIRD_PARTY,
221
- details: { indexName },
222
- },
223
- error,
224
- );
225
- }
226
- }
227
-
228
- async get<T extends Metadata = Metadata>({
229
- indexName,
230
- ids,
231
- filter,
232
- includeVector = false,
233
- documentFilter,
234
- offset,
235
- limit,
236
- }: ChromaGetRecordsParams) {
237
- try {
238
- const collection = await this.getCollection({ indexName });
239
-
240
- const defaultInclude: ['documents', 'metadatas'] = ['documents', 'metadatas'];
241
- const translatedFilter = this.transformFilter(filter);
242
-
243
- const result = await collection.get<T>({
244
- ids,
245
- where: translatedFilter ?? undefined,
246
- whereDocument: documentFilter ?? undefined,
247
- offset,
248
- limit,
249
- include: includeVector ? [...defaultInclude, 'embeddings'] : defaultInclude,
250
- });
251
- return result.rows();
252
- } catch (error: any) {
253
- if (error instanceof MastraError) throw error;
254
- throw new MastraError(
255
- {
256
- id: 'CHROMA_VECTOR_GET_FAILED',
257
- domain: ErrorDomain.MASTRA_VECTOR,
258
- category: ErrorCategory.THIRD_PARTY,
259
- details: { indexName },
260
- },
261
- error,
262
- );
263
- }
264
- }
265
-
266
- async listIndexes(): Promise<string[]> {
267
- try {
268
- const collections = await this.client.listCollections();
269
- return collections.map(collection => collection.name);
270
- } catch (error: any) {
271
- throw new MastraError(
272
- {
273
- id: 'CHROMA_VECTOR_LIST_INDEXES_FAILED',
274
- domain: ErrorDomain.MASTRA_VECTOR,
275
- category: ErrorCategory.THIRD_PARTY,
276
- },
277
- error,
278
- );
279
- }
280
- }
281
-
282
- /**
283
- * Retrieves statistics about a vector index.
284
- *
285
- * @param {string} indexName - The name of the index to describe
286
- * @returns A promise that resolves to the index statistics including dimension, count and metric
287
- */
288
- async describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats> {
289
- try {
290
- const collection = await this.getCollection({ indexName });
291
- const count = await collection.count();
292
- const metadata = collection.metadata as MastraMetadata | undefined;
293
- const space = collection.configuration.hnsw?.space || collection.configuration.spann?.space || undefined;
294
-
295
- return {
296
- dimension: metadata?.dimension || 0,
297
- count,
298
- metric: space ? (spaceMappings[space] as 'cosine' | 'euclidean' | 'dotproduct') : undefined,
299
- };
300
- } catch (error: any) {
301
- if (error instanceof MastraError) throw error;
302
- throw new MastraError(
303
- {
304
- id: 'CHROMA_VECTOR_DESCRIBE_INDEX_FAILED',
305
- domain: ErrorDomain.MASTRA_VECTOR,
306
- category: ErrorCategory.THIRD_PARTY,
307
- details: { indexName },
308
- },
309
- error,
310
- );
311
- }
312
- }
313
-
314
- async deleteIndex({ indexName }: DeleteIndexParams): Promise<void> {
315
- try {
316
- await this.client.deleteCollection({ name: indexName });
317
- this.collections.delete(indexName);
318
- } catch (error: any) {
319
- throw new MastraError(
320
- {
321
- id: 'CHROMA_VECTOR_DELETE_INDEX_FAILED',
322
- domain: ErrorDomain.MASTRA_VECTOR,
323
- category: ErrorCategory.THIRD_PARTY,
324
- details: { indexName },
325
- },
326
- error,
327
- );
328
- }
329
- }
330
-
331
- async forkIndex({ indexName, newIndexName }: { indexName: string; newIndexName: string }): Promise<void> {
332
- try {
333
- const collection = await this.getCollection({ indexName, forceUpdate: true });
334
- const forkedCollection = await collection.fork({ name: newIndexName });
335
- this.collections.set(newIndexName, forkedCollection);
336
- } catch (error: any) {
337
- if (error instanceof MastraError) throw error;
338
- throw new MastraError(
339
- {
340
- id: 'CHROMA_INDEX_FORK_FAILED',
341
- domain: ErrorDomain.MASTRA_VECTOR,
342
- category: ErrorCategory.THIRD_PARTY,
343
- details: { indexName },
344
- },
345
- error,
346
- );
347
- }
348
- }
349
-
350
- /**
351
- * Updates a vector by its ID with the provided vector and/or metadata.
352
- * @param indexName - The name of the index containing the vector.
353
- * @param id - The ID of the vector to update.
354
- * @param update - An object containing the vector and/or metadata to update.
355
- * @param update.vector - An optional array of numbers representing the new vector.
356
- * @param update.metadata - An optional record containing the new metadata.
357
- * @returns A promise that resolves when the update is complete.
358
- * @throws Will throw an error if no updates are provided or if the update operation fails.
359
- */
360
- async updateVector({ indexName, id, update }: UpdateVectorParams): Promise<void> {
361
- if (!update.vector && !update.metadata) {
362
- throw new MastraError({
363
- id: 'CHROMA_VECTOR_UPDATE_NO_PAYLOAD',
364
- text: 'No updates provided for vector',
365
- domain: ErrorDomain.MASTRA_VECTOR,
366
- category: ErrorCategory.USER,
367
- details: { indexName, id },
368
- });
369
- }
370
-
371
- try {
372
- const collection: Collection = await this.getCollection({ indexName });
373
-
374
- const updateRecordSet: RecordSet = { ids: [id] };
375
-
376
- if (update?.vector) {
377
- const stats = await this.describeIndex({ indexName });
378
- this.validateVectorDimensions([update.vector], stats.dimension);
379
- updateRecordSet.embeddings = [update.vector];
380
- }
381
-
382
- if (update?.metadata) {
383
- updateRecordSet.metadatas = [update.metadata];
384
- }
385
-
386
- return await collection.update(updateRecordSet);
387
- } catch (error: any) {
388
- if (error instanceof MastraError) throw error;
389
- throw new MastraError(
390
- {
391
- id: 'CHROMA_VECTOR_UPDATE_FAILED',
392
- domain: ErrorDomain.MASTRA_VECTOR,
393
- category: ErrorCategory.THIRD_PARTY,
394
- details: { indexName, id },
395
- },
396
- error,
397
- );
398
- }
399
- }
400
-
401
- async deleteVector({ indexName, id }: DeleteVectorParams): Promise<void> {
402
- try {
403
- const collection: Collection = await this.getCollection({ indexName });
404
- await collection.delete({ ids: [id] });
405
- } catch (error: any) {
406
- if (error instanceof MastraError) throw error;
407
- throw new MastraError(
408
- {
409
- id: 'CHROMA_VECTOR_DELETE_FAILED',
410
- domain: ErrorDomain.MASTRA_VECTOR,
411
- category: ErrorCategory.THIRD_PARTY,
412
- details: { indexName, id },
413
- },
414
- error,
415
- );
416
- }
417
- }
418
- }
@@ -1,72 +0,0 @@
1
- /**
2
- * Vector store specific prompt that details supported operators and examples.
3
- * This prompt helps users construct valid filters for Chroma Vector.
4
- */
5
- export const CHROMA_PROMPT = `When querying Chroma, you can ONLY use the operators listed below. Any other operators will be rejected.
6
- Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
7
- 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.
8
-
9
- Basic Comparison Operators:
10
- - $eq: Exact match (default when using field: value)
11
- Example: { "category": "electronics" }
12
- - $ne: Not equal
13
- Example: { "category": { "$ne": "electronics" } }
14
- - $gt: Greater than
15
- Example: { "price": { "$gt": 100 } }
16
- - $gte: Greater than or equal
17
- Example: { "price": { "$gte": 100 } }
18
- - $lt: Less than
19
- Example: { "price": { "$lt": 100 } }
20
- - $lte: Less than or equal
21
- Example: { "price": { "$lte": 100 } }
22
-
23
- Array Operators:
24
- - $in: Match any value in array
25
- Example: { "category": { "$in": ["electronics", "books"] } }
26
- - $nin: Does not match any value in array
27
- Example: { "category": { "$nin": ["electronics", "books"] } }
28
-
29
- Logical Operators:
30
- - $and: Logical AND
31
- Example: { "$and": [{ "price": { "$gt": 100 } }, { "category": "electronics" }] }
32
- - $or: Logical OR
33
- Example: { "$or": [{ "price": { "$lt": 50 } }, { "category": "books" }] }
34
-
35
- Restrictions:
36
- - Regex patterns are not supported
37
- - Element operators are not supported
38
- - Only $and and $or logical operators are supported
39
- - Nested fields are supported using dot notation
40
- - Multiple conditions on the same field are supported with both implicit and explicit $and
41
- - Empty arrays in $in/$nin will return no results
42
- - If multiple top-level fields exist, they're wrapped in $and
43
- - Only logical operators ($and, $or) can be used at the top level
44
- - All other operators must be used within a field condition
45
- Valid: { "field": { "$gt": 100 } }
46
- Valid: { "$and": [...] }
47
- Invalid: { "$gt": 100 }
48
- Invalid: { "$in": [...] }
49
- - Logical operators must contain field conditions, not direct operators
50
- Valid: { "$and": [{ "field": { "$gt": 100 } }] }
51
- Invalid: { "$and": [{ "$gt": 100 }] }
52
- - Logical operators ($and, $or):
53
- - Can only be used at top level or nested within other logical operators
54
- - Can not be used on a field level, or be nested inside a field
55
- - Can not be used inside an operator
56
- - Valid: { "$and": [{ "field": { "$gt": 100 } }] }
57
- - Valid: { "$or": [{ "$and": [{ "field": { "$gt": 100 } }] }] }
58
- - Invalid: { "field": { "$and": [{ "$gt": 100 }] } }
59
- - Invalid: { "field": { "$or": [{ "$gt": 100 }] } }
60
- - Invalid: { "field": { "$gt": { "$and": [{...}] } } }
61
-
62
- Example Complex Query:
63
- {
64
- "$and": [
65
- { "category": { "$in": ["electronics", "computers"] } },
66
- { "price": { "$gte": 100, "$lte": 1000 } },
67
- { "$or": [
68
- { "inStock": true },
69
- { "preorder": true }
70
- ]}
71
- ]
72
- }`;
@@ -1,9 +0,0 @@
1
- {
2
- "extends": ["./tsconfig.json", "../../tsconfig.build.json"],
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "rootDir": "./src"
6
- },
7
- "include": ["src/**/*"],
8
- "exclude": ["node_modules", "**/*.test.ts", "src/**/*.mock.ts"]
9
- }
package/tsconfig.json DELETED
@@ -1,5 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.node.json",
3
- "include": ["src/**/*", "tsup.config.ts"],
4
- "exclude": ["node_modules", "**/*.test.ts"]
5
- }
package/tsup.config.ts DELETED
@@ -1,17 +0,0 @@
1
- import { generateTypes } from '@internal/types-builder';
2
- import { defineConfig } from 'tsup';
3
-
4
- export default defineConfig({
5
- entry: ['src/index.ts'],
6
- format: ['esm', 'cjs'],
7
- clean: true,
8
- dts: false,
9
- splitting: true,
10
- treeshake: {
11
- preset: 'smallest',
12
- },
13
- sourcemap: true,
14
- onSuccess: async () => {
15
- await generateTypes(process.cwd());
16
- },
17
- });
package/vitest.config.ts DELETED
@@ -1,8 +0,0 @@
1
- import { defineConfig } from 'vitest/config';
2
-
3
- export default defineConfig({
4
- test: {
5
- environment: 'node',
6
- include: ['src/**/*.test.ts'],
7
- },
8
- });