@mastra/chroma 0.0.0-vnext-inngest-20250508122351 → 0.0.0-vnext-20251104230439

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,231 +0,0 @@
1
- import { MastraVector } from '@mastra/core/vector';
2
- import type {
3
- QueryResult,
4
- IndexStats,
5
- CreateIndexParams,
6
- UpsertVectorParams,
7
- QueryVectorParams,
8
- ParamsToArgs,
9
- QueryVectorArgs,
10
- UpsertVectorArgs,
11
- } from '@mastra/core/vector';
12
-
13
- import type { VectorFilter } from '@mastra/core/vector/filter';
14
- import { ChromaClient } from 'chromadb';
15
- import type { UpdateRecordsParams, Collection } from 'chromadb';
16
- import { ChromaFilterTranslator } from './filter';
17
-
18
- interface ChromaUpsertVectorParams extends UpsertVectorParams {
19
- documents?: string[];
20
- }
21
-
22
- type ChromaUpsertArgs = [...UpsertVectorArgs, string[]?];
23
-
24
- interface ChromaQueryVectorParams extends QueryVectorParams {
25
- documentFilter?: VectorFilter;
26
- }
27
-
28
- type ChromaQueryArgs = [...QueryVectorArgs, VectorFilter?];
29
-
30
- export class ChromaVector extends MastraVector {
31
- private client: ChromaClient;
32
- private collections: Map<string, any>;
33
-
34
- constructor({
35
- path,
36
- auth,
37
- }: {
38
- path: string;
39
- auth?: {
40
- provider: string;
41
- credentials: string;
42
- };
43
- }) {
44
- super();
45
- this.client = new ChromaClient({
46
- path,
47
- auth,
48
- });
49
- this.collections = new Map();
50
- }
51
-
52
- async getCollection(indexName: string, throwIfNotExists: boolean = true) {
53
- try {
54
- const collection = await this.client.getCollection({ name: indexName, embeddingFunction: undefined as any });
55
- this.collections.set(indexName, collection);
56
- } catch {
57
- if (throwIfNotExists) {
58
- throw new Error(`Index ${indexName} does not exist`);
59
- }
60
- return null;
61
- }
62
- return this.collections.get(indexName);
63
- }
64
-
65
- private validateVectorDimensions(vectors: number[][], dimension: number): void {
66
- for (let i = 0; i < vectors.length; i++) {
67
- if (vectors?.[i]?.length !== dimension) {
68
- throw new Error(
69
- `Vector at index ${i} has invalid dimension ${vectors?.[i]?.length}. Expected ${dimension} dimensions.`,
70
- );
71
- }
72
- }
73
- }
74
-
75
- async upsert(...args: ParamsToArgs<ChromaUpsertVectorParams> | ChromaUpsertArgs): Promise<string[]> {
76
- const params = this.normalizeArgs<ChromaUpsertVectorParams, ChromaUpsertArgs>('upsert', args, ['documents']);
77
-
78
- const { indexName, vectors, metadata, ids, documents } = params;
79
-
80
- const collection = await this.getCollection(indexName);
81
-
82
- // Get index stats to check dimension
83
- const stats = await this.describeIndex(indexName);
84
-
85
- // Validate vector dimensions
86
- this.validateVectorDimensions(vectors, stats.dimension);
87
-
88
- // Generate IDs if not provided
89
- const generatedIds = ids || vectors.map(() => crypto.randomUUID());
90
-
91
- // Ensure metadata exists for each vector
92
- const normalizedMetadata = metadata || vectors.map(() => ({}));
93
-
94
- await collection.upsert({
95
- ids: generatedIds,
96
- embeddings: vectors,
97
- metadatas: normalizedMetadata,
98
- documents: documents,
99
- });
100
-
101
- return generatedIds;
102
- }
103
-
104
- private HnswSpaceMap = {
105
- cosine: 'cosine',
106
- euclidean: 'l2',
107
- dotproduct: 'ip',
108
- l2: 'euclidean',
109
- ip: 'dotproduct',
110
- };
111
-
112
- async createIndex(...args: ParamsToArgs<CreateIndexParams>): Promise<void> {
113
- const params = this.normalizeArgs<CreateIndexParams>('createIndex', args);
114
- const { indexName, dimension, metric = 'cosine' } = params;
115
-
116
- if (!Number.isInteger(dimension) || dimension <= 0) {
117
- throw new Error('Dimension must be a positive integer');
118
- }
119
- const hnswSpace = this.HnswSpaceMap[metric];
120
- if (!['cosine', 'l2', 'ip'].includes(hnswSpace)) {
121
- throw new Error(`Invalid metric: "${metric}". Must be one of: cosine, euclidean, dotproduct`);
122
- }
123
- try {
124
- await this.client.createCollection({
125
- name: indexName,
126
- metadata: {
127
- dimension,
128
- 'hnsw:space': hnswSpace,
129
- },
130
- });
131
- } catch (error: any) {
132
- // Check for 'already exists' error
133
- const message = error?.message || error?.toString();
134
- if (message && message.toLowerCase().includes('already exists')) {
135
- // Fetch collection info and check dimension
136
- await this.validateExistingIndex(indexName, dimension, metric);
137
- return;
138
- }
139
- throw error;
140
- }
141
- }
142
-
143
- transformFilter(filter?: VectorFilter) {
144
- const translator = new ChromaFilterTranslator();
145
- return translator.translate(filter);
146
- }
147
- async query(...args: ParamsToArgs<ChromaQueryVectorParams> | ChromaQueryArgs): Promise<QueryResult[]> {
148
- const params = this.normalizeArgs<ChromaQueryVectorParams, ChromaQueryArgs>('query', args, ['documentFilter']);
149
-
150
- const { indexName, queryVector, topK = 10, filter, includeVector = false, documentFilter } = params;
151
-
152
- const collection = await this.getCollection(indexName, true);
153
-
154
- const defaultInclude = ['documents', 'metadatas', 'distances'];
155
-
156
- const translatedFilter = this.transformFilter(filter);
157
- const results = await collection.query({
158
- queryEmbeddings: [queryVector],
159
- nResults: topK,
160
- where: translatedFilter,
161
- whereDocument: documentFilter,
162
- include: includeVector ? [...defaultInclude, 'embeddings'] : defaultInclude,
163
- });
164
-
165
- // Transform ChromaDB results to QueryResult format
166
- return (results.ids[0] || []).map((id: string, index: number) => ({
167
- id,
168
- score: results.distances?.[0]?.[index] || 0,
169
- metadata: results.metadatas?.[0]?.[index] || {},
170
- document: results.documents?.[0]?.[index],
171
- ...(includeVector && { vector: results.embeddings?.[0]?.[index] || [] }),
172
- }));
173
- }
174
-
175
- async listIndexes(): Promise<string[]> {
176
- const collections = await this.client.listCollections();
177
- return collections.map(collection => collection);
178
- }
179
-
180
- async describeIndex(indexName: string): Promise<IndexStats> {
181
- const collection = await this.getCollection(indexName);
182
- const count = await collection.count();
183
- const metadata = collection.metadata;
184
-
185
- const hnswSpace = metadata?.['hnsw:space'] as 'cosine' | 'l2' | 'ip';
186
-
187
- return {
188
- dimension: metadata?.dimension || 0,
189
- count,
190
- metric: this.HnswSpaceMap[hnswSpace] as 'cosine' | 'euclidean' | 'dotproduct',
191
- };
192
- }
193
-
194
- async deleteIndex(indexName: string): Promise<void> {
195
- await this.client.deleteCollection({ name: indexName });
196
- this.collections.delete(indexName);
197
- }
198
-
199
- async updateIndexById(
200
- indexName: string,
201
- id: string,
202
- update: { vector?: number[]; metadata?: Record<string, any> },
203
- ): Promise<void> {
204
- if (!update.vector && !update.metadata) {
205
- throw new Error('No updates provided');
206
- }
207
-
208
- const collection: Collection = await this.getCollection(indexName, true);
209
-
210
- const updateOptions: UpdateRecordsParams = { ids: [id] };
211
-
212
- if (update?.vector) {
213
- updateOptions.embeddings = [update.vector];
214
- }
215
-
216
- if (update?.metadata) {
217
- updateOptions.metadatas = [update.metadata];
218
- }
219
-
220
- return await collection.update(updateOptions);
221
- }
222
-
223
- async deleteIndexById(indexName: string, id: string): Promise<void> {
224
- try {
225
- const collection: Collection = await this.getCollection(indexName, true);
226
- await collection.delete({ ids: [id] });
227
- } catch (error: any) {
228
- throw new Error(`Failed to delete index by id: ${id} for index name: ${indexName}: ${error.message}`);
229
- }
230
- }
231
- }
@@ -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
- }`;
package/tsconfig.json DELETED
@@ -1,5 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.node.json",
3
- "include": ["src/**/*"],
4
- "exclude": ["node_modules", "**/*.test.ts"]
5
- }
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
- });