@mastra/chroma 0.1.0-alpha.28
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 +260 -0
- package/LICENSE +44 -0
- package/README.md +86 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +190 -0
- package/package.json +32 -0
- package/src/index.ts +1 -0
- package/src/vector/filter.test.ts +413 -0
- package/src/vector/filter.ts +114 -0
- package/src/vector/index.test.ts +889 -0
- package/src/vector/index.ts +153 -0
- package/tsconfig.json +10 -0
- package/vitest.config.ts +8 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# @mastra/chroma
|
|
2
|
+
|
|
3
|
+
## 0.1.0-alpha.28
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [5285356]
|
|
8
|
+
- @mastra/core@0.2.0-alpha.93
|
|
9
|
+
|
|
10
|
+
## 0.1.0-alpha.27
|
|
11
|
+
|
|
12
|
+
### Minor Changes
|
|
13
|
+
|
|
14
|
+
- 684d5d1: Move Chroma package from `@mastra/vector-chroma` to `@mastra/chroma`.
|
|
15
|
+
|
|
16
|
+
- Move package to `stores/chroma`
|
|
17
|
+
- Reorganize source files into `src/vector`
|
|
18
|
+
- Add deprecation notice to old package
|
|
19
|
+
- Update documentation and examples
|
|
20
|
+
- No breaking changes in functionality
|
|
21
|
+
|
|
22
|
+
## 0.1.0-alpha.1
|
|
23
|
+
|
|
24
|
+
### Major Changes
|
|
25
|
+
|
|
26
|
+
- Package renamed from @mastra/vector-chroma to @mastra/chroma
|
|
27
|
+
- Moved package to stores/ directory for better organization
|
|
28
|
+
- All functionality remains the same
|
|
29
|
+
|
|
30
|
+
### Migration
|
|
31
|
+
|
|
32
|
+
To migrate from @mastra/vector-chroma:
|
|
33
|
+
|
|
34
|
+
1. Remove @mastra/vector-chroma from dependencies
|
|
35
|
+
2. Install @mastra/chroma
|
|
36
|
+
3. Update imports from '@mastra/vector-chroma' to '@mastra/chroma'
|
|
37
|
+
|
|
38
|
+
### Previous History
|
|
39
|
+
|
|
40
|
+
This package was previously published as @mastra/vector-chroma. Key changes from that history:
|
|
41
|
+
|
|
42
|
+
- Added Chroma Filter translator for improved query filtering
|
|
43
|
+
- Updated operator validation and handling
|
|
44
|
+
- Implemented new filtering for vectorQueryTool
|
|
45
|
+
- Improved vector dimension validation
|
|
46
|
+
- Added collection-based organization
|
|
47
|
+
- Various dependency updates and bug fixes
|
|
48
|
+
|
|
49
|
+
## 0.1.0-alpha.26
|
|
50
|
+
|
|
51
|
+
### Patch Changes
|
|
52
|
+
|
|
53
|
+
- Updated dependencies [4d4f6b6]
|
|
54
|
+
- @mastra/core@0.2.0-alpha.92
|
|
55
|
+
|
|
56
|
+
## 0.1.0-alpha.25
|
|
57
|
+
|
|
58
|
+
### Patch Changes
|
|
59
|
+
|
|
60
|
+
- a10b7a3: Implemented new filtering for vectorQueryTool and updated docs
|
|
61
|
+
- Updated dependencies [d7d465a]
|
|
62
|
+
- Updated dependencies [d7d465a]
|
|
63
|
+
- Updated dependencies [2017553]
|
|
64
|
+
- Updated dependencies [a10b7a3]
|
|
65
|
+
- Updated dependencies [16e5b04]
|
|
66
|
+
- @mastra/core@0.2.0-alpha.91
|
|
67
|
+
|
|
68
|
+
## 0.1.0-alpha.24
|
|
69
|
+
|
|
70
|
+
### Patch Changes
|
|
71
|
+
|
|
72
|
+
- Updated dependencies [8151f44]
|
|
73
|
+
- Updated dependencies [e897f1c]
|
|
74
|
+
- Updated dependencies [3700be1]
|
|
75
|
+
- @mastra/core@0.2.0-alpha.90
|
|
76
|
+
|
|
77
|
+
## 0.1.0-alpha.23
|
|
78
|
+
|
|
79
|
+
### Patch Changes
|
|
80
|
+
|
|
81
|
+
- Updated dependencies [27275c9]
|
|
82
|
+
- @mastra/core@0.2.0-alpha.89
|
|
83
|
+
|
|
84
|
+
## 0.1.0-alpha.22
|
|
85
|
+
|
|
86
|
+
### Patch Changes
|
|
87
|
+
|
|
88
|
+
- ccbc581: Updated operator validation and handling for all vector stores
|
|
89
|
+
- Updated dependencies [ccbc581]
|
|
90
|
+
- @mastra/core@0.2.0-alpha.88
|
|
91
|
+
|
|
92
|
+
## 0.1.0-alpha.21
|
|
93
|
+
|
|
94
|
+
### Patch Changes
|
|
95
|
+
|
|
96
|
+
- Updated dependencies [7365b6c]
|
|
97
|
+
- @mastra/core@0.2.0-alpha.87
|
|
98
|
+
|
|
99
|
+
## 0.1.0-alpha.20
|
|
100
|
+
|
|
101
|
+
### Minor Changes
|
|
102
|
+
|
|
103
|
+
- 5916f9d: Update deps from fixed to ^
|
|
104
|
+
|
|
105
|
+
### Patch Changes
|
|
106
|
+
|
|
107
|
+
- 7f24c29: Add Chroma Filter translator and updated vector store tests
|
|
108
|
+
- Updated dependencies [6fa4bd2]
|
|
109
|
+
- Updated dependencies [e2e76de]
|
|
110
|
+
- Updated dependencies [7f24c29]
|
|
111
|
+
- Updated dependencies [67637ba]
|
|
112
|
+
- Updated dependencies [04f3171]
|
|
113
|
+
- @mastra/core@0.2.0-alpha.86
|
|
114
|
+
|
|
115
|
+
## 0.0.1-alpha.19
|
|
116
|
+
|
|
117
|
+
### Patch Changes
|
|
118
|
+
|
|
119
|
+
- Updated dependencies [e9d1b47]
|
|
120
|
+
- @mastra/core@0.2.0-alpha.85
|
|
121
|
+
|
|
122
|
+
## 0.0.1-alpha.18
|
|
123
|
+
|
|
124
|
+
### Patch Changes
|
|
125
|
+
|
|
126
|
+
- Updated dependencies [2f17a5f]
|
|
127
|
+
- Updated dependencies [cb290ee]
|
|
128
|
+
- Updated dependencies [b4d7416]
|
|
129
|
+
- Updated dependencies [38b7f66]
|
|
130
|
+
- @mastra/core@0.2.0-alpha.84
|
|
131
|
+
|
|
132
|
+
## 0.0.1-alpha.17
|
|
133
|
+
|
|
134
|
+
### Patch Changes
|
|
135
|
+
|
|
136
|
+
- 9625602: Use mastra core splitted bundles in other packages
|
|
137
|
+
- Updated dependencies [30322ce]
|
|
138
|
+
- Updated dependencies [78eec7c]
|
|
139
|
+
- Updated dependencies [9625602]
|
|
140
|
+
- Updated dependencies [8769a62]
|
|
141
|
+
- @mastra/core@0.2.0-alpha.83
|
|
142
|
+
|
|
143
|
+
## 0.0.1-alpha.16
|
|
144
|
+
|
|
145
|
+
### Patch Changes
|
|
146
|
+
|
|
147
|
+
- Updated dependencies [73d112c]
|
|
148
|
+
- @mastra/core@0.1.27-alpha.82
|
|
149
|
+
|
|
150
|
+
## 0.0.1-alpha.15
|
|
151
|
+
|
|
152
|
+
### Patch Changes
|
|
153
|
+
|
|
154
|
+
- Updated dependencies [9fb3039]
|
|
155
|
+
- @mastra/core@0.1.27-alpha.81
|
|
156
|
+
|
|
157
|
+
## 0.0.1-alpha.14
|
|
158
|
+
|
|
159
|
+
### Patch Changes
|
|
160
|
+
|
|
161
|
+
- b422ed3: Bundle vector provider packages with tsup
|
|
162
|
+
|
|
163
|
+
## 0.0.1-alpha.13
|
|
164
|
+
|
|
165
|
+
### Patch Changes
|
|
166
|
+
|
|
167
|
+
- Updated dependencies [327ece7]
|
|
168
|
+
- @mastra/core@0.1.27-alpha.80
|
|
169
|
+
|
|
170
|
+
## 0.0.1-alpha.12
|
|
171
|
+
|
|
172
|
+
### Patch Changes
|
|
173
|
+
|
|
174
|
+
- Updated dependencies [21fe536]
|
|
175
|
+
- @mastra/core@0.1.27-alpha.79
|
|
176
|
+
|
|
177
|
+
## 0.0.1-alpha.11
|
|
178
|
+
|
|
179
|
+
### Patch Changes
|
|
180
|
+
|
|
181
|
+
- Updated dependencies [685108a]
|
|
182
|
+
- Updated dependencies [685108a]
|
|
183
|
+
- @mastra/core@0.1.27-alpha.78
|
|
184
|
+
|
|
185
|
+
## 0.0.1-alpha.10
|
|
186
|
+
|
|
187
|
+
### Patch Changes
|
|
188
|
+
|
|
189
|
+
- Updated dependencies [8105fae]
|
|
190
|
+
- @mastra/core@0.1.27-alpha.77
|
|
191
|
+
|
|
192
|
+
## 0.0.1-alpha.9
|
|
193
|
+
|
|
194
|
+
### Patch Changes
|
|
195
|
+
|
|
196
|
+
- 7a469e7: Bump vectors
|
|
197
|
+
|
|
198
|
+
## 0.0.1-alpha.8
|
|
199
|
+
|
|
200
|
+
### Patch Changes
|
|
201
|
+
|
|
202
|
+
- Updated dependencies [ae7bf94]
|
|
203
|
+
- Updated dependencies [ae7bf94]
|
|
204
|
+
- @mastra/core@0.1.27-alpha.76
|
|
205
|
+
|
|
206
|
+
## 0.0.1-alpha.7
|
|
207
|
+
|
|
208
|
+
### Patch Changes
|
|
209
|
+
|
|
210
|
+
- Updated dependencies [23dcb23]
|
|
211
|
+
- @mastra/core@0.1.27-alpha.75
|
|
212
|
+
|
|
213
|
+
## 0.0.1-alpha.6
|
|
214
|
+
|
|
215
|
+
### Patch Changes
|
|
216
|
+
|
|
217
|
+
- Updated dependencies [7b87567]
|
|
218
|
+
- @mastra/core@0.1.27-alpha.74
|
|
219
|
+
|
|
220
|
+
## 0.0.1-alpha.5
|
|
221
|
+
|
|
222
|
+
### Patch Changes
|
|
223
|
+
|
|
224
|
+
- Updated dependencies [3427b95]
|
|
225
|
+
- @mastra/core@0.1.27-alpha.73
|
|
226
|
+
|
|
227
|
+
## 0.0.1-alpha.4
|
|
228
|
+
|
|
229
|
+
### Patch Changes
|
|
230
|
+
|
|
231
|
+
- Updated dependencies [e4d4ede]
|
|
232
|
+
- Updated dependencies [06b2c0a]
|
|
233
|
+
- @mastra/core@0.1.27-alpha.72
|
|
234
|
+
|
|
235
|
+
## 0.0.1-alpha.3
|
|
236
|
+
|
|
237
|
+
### Patch Changes
|
|
238
|
+
|
|
239
|
+
- Updated dependencies [d9c8dd0]
|
|
240
|
+
- @mastra/core@0.1.27-alpha.71
|
|
241
|
+
|
|
242
|
+
## 0.0.1-alpha.2
|
|
243
|
+
|
|
244
|
+
### Patch Changes
|
|
245
|
+
|
|
246
|
+
- bdaf834: publish packages
|
|
247
|
+
|
|
248
|
+
## 0.0.1-alpha.1
|
|
249
|
+
|
|
250
|
+
### Patch Changes
|
|
251
|
+
|
|
252
|
+
- Updated dependencies [dd6d87f]
|
|
253
|
+
- Updated dependencies [04434b6]
|
|
254
|
+
- @mastra/core@0.1.27-alpha.70
|
|
255
|
+
|
|
256
|
+
## 0.0.1-alpha.0
|
|
257
|
+
|
|
258
|
+
### Patch Changes
|
|
259
|
+
|
|
260
|
+
- 0d5a03d: Vector store modules
|
package/LICENSE
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
Elastic License 2.0 (ELv2)
|
|
2
|
+
|
|
3
|
+
**Acceptance**
|
|
4
|
+
By using the software, you agree to all of the terms and conditions below.
|
|
5
|
+
|
|
6
|
+
**Copyright License**
|
|
7
|
+
The licensor grants you a non-exclusive, royalty-free, worldwide, non-sublicensable, non-transferable license to use, copy, distribute, make available, and prepare derivative works of the software, in each case subject to the limitations and conditions below
|
|
8
|
+
|
|
9
|
+
**Limitations**
|
|
10
|
+
You may not provide the software to third parties as a hosted or managed service, where the service provides users with access to any substantial set of the features or functionality of the software.
|
|
11
|
+
|
|
12
|
+
You may not move, change, disable, or circumvent the license key functionality in the software, and you may not remove or obscure any functionality in the software that is protected by the license key.
|
|
13
|
+
|
|
14
|
+
You may not alter, remove, or obscure any licensing, copyright, or other notices of the licensor in the software. Any use of the licensor’s trademarks is subject to applicable law.
|
|
15
|
+
|
|
16
|
+
**Patents**
|
|
17
|
+
The licensor grants you a license, under any patent claims the licensor can license, or becomes able to license, to make, have made, use, sell, offer for sale, import and have imported the software, in each case subject to the limitations and conditions in this license. This license does not cover any patent claims that you cause to be infringed by modifications or additions to the software. If you or your company make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
|
|
18
|
+
|
|
19
|
+
**Notices**
|
|
20
|
+
You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms.
|
|
21
|
+
|
|
22
|
+
If you modify the software, you must include in any modified copies of the software prominent notices stating that you have modified the software.
|
|
23
|
+
|
|
24
|
+
**No Other Rights**
|
|
25
|
+
These terms do not imply any licenses other than those expressly granted in these terms.
|
|
26
|
+
|
|
27
|
+
**Termination**
|
|
28
|
+
If you use the software in violation of these terms, such use is not licensed, and your licenses will automatically terminate. If the licensor provides you with a notice of your violation, and you cease all violation of this license no later than 30 days after you receive that notice, your licenses will be reinstated retroactively. However, if you violate these terms after such reinstatement, any additional violation of these terms will cause your licenses to terminate automatically and permanently.
|
|
29
|
+
|
|
30
|
+
**No Liability**
|
|
31
|
+
As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.
|
|
32
|
+
|
|
33
|
+
**Definitions**
|
|
34
|
+
The _licensor_ is the entity offering these terms, and the _software_ is the software the licensor makes available under these terms, including any portion of it.
|
|
35
|
+
|
|
36
|
+
_you_ refers to the individual or entity agreeing to these terms.
|
|
37
|
+
|
|
38
|
+
_your company_ is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. _control_ means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
|
|
39
|
+
|
|
40
|
+
_your licenses_ are all the licenses granted to you for the software under these terms.
|
|
41
|
+
|
|
42
|
+
_use_ means anything you do with the software requiring one of your licenses.
|
|
43
|
+
|
|
44
|
+
_trademark_ means trademarks, service marks, and similar rights.
|
package/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# @mastra/chroma
|
|
2
|
+
|
|
3
|
+
Vector store implementation for ChromaDB using the official chromadb client with added dimension validation and collection management.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @mastra/chroma
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { ChromaVector } from '@mastra/chroma';
|
|
15
|
+
|
|
16
|
+
const vectorStore = new ChromaVector({
|
|
17
|
+
path: 'http://localhost:8000', // ChromaDB server URL
|
|
18
|
+
auth: { // Optional authentication
|
|
19
|
+
provider: 'token',
|
|
20
|
+
credentials: 'your-token'
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// Create a new collection
|
|
25
|
+
await vectorStore.createIndex('my-collection', 1536, 'cosine');
|
|
26
|
+
|
|
27
|
+
// Add vectors
|
|
28
|
+
const vectors = [[0.1, 0.2, ...], [0.3, 0.4, ...]];
|
|
29
|
+
const metadata = [{ text: 'doc1' }, { text: 'doc2' }];
|
|
30
|
+
const ids = await vectorStore.upsert('my-collection', vectors, metadata);
|
|
31
|
+
|
|
32
|
+
// Query vectors
|
|
33
|
+
const results = await vectorStore.query(
|
|
34
|
+
'my-collection',
|
|
35
|
+
[0.1, 0.2, ...],
|
|
36
|
+
10, // topK
|
|
37
|
+
{ text: { $eq: 'doc1' } }, // optional filter
|
|
38
|
+
false // includeVector
|
|
39
|
+
);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Configuration
|
|
43
|
+
|
|
44
|
+
Required:
|
|
45
|
+
|
|
46
|
+
- `path`: URL of your ChromaDB server
|
|
47
|
+
|
|
48
|
+
Optional:
|
|
49
|
+
|
|
50
|
+
- `auth`: Authentication configuration
|
|
51
|
+
- `provider`: Authentication provider
|
|
52
|
+
- `credentials`: Authentication credentials
|
|
53
|
+
|
|
54
|
+
## Features
|
|
55
|
+
|
|
56
|
+
- Vector similarity search with cosine, euclidean, and dot product metrics
|
|
57
|
+
- Strict vector dimension validation
|
|
58
|
+
- Collection-based organization
|
|
59
|
+
- Metadata filtering support
|
|
60
|
+
- Optional vector inclusion in query results
|
|
61
|
+
- Automatic UUID generation for vectors
|
|
62
|
+
- Built-in collection caching for performance
|
|
63
|
+
- Built on top of chromadb client
|
|
64
|
+
|
|
65
|
+
## Methods
|
|
66
|
+
|
|
67
|
+
- `createIndex(indexName, dimension, metric?)`: Create a new collection
|
|
68
|
+
- `upsert(indexName, vectors, metadata?, ids?)`: Add or update vectors
|
|
69
|
+
- `query(indexName, queryVector, topK?, filter?, includeVector?)`: Search for similar vectors
|
|
70
|
+
- `listIndexes()`: List all collections
|
|
71
|
+
- `describeIndex(indexName)`: Get collection statistics
|
|
72
|
+
- `deleteIndex(indexName)`: Delete a collection
|
|
73
|
+
|
|
74
|
+
## Query Response Format
|
|
75
|
+
|
|
76
|
+
Query results include:
|
|
77
|
+
|
|
78
|
+
- `id`: Vector ID
|
|
79
|
+
- `score`: Distance/similarity score
|
|
80
|
+
- `metadata`: Associated metadata
|
|
81
|
+
- `vector`: Original vector (if includeVector is true)
|
|
82
|
+
|
|
83
|
+
## Related Links
|
|
84
|
+
|
|
85
|
+
- [ChromaDB Documentation](https://docs.trychroma.com/)
|
|
86
|
+
- [ChromaDB API Reference](https://docs.trychroma.com/api/client)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Filter } from '@mastra/core/filter';
|
|
2
|
+
import { MastraVector, QueryResult, IndexStats } from '@mastra/core/vector';
|
|
3
|
+
|
|
4
|
+
declare class ChromaVector extends MastraVector {
|
|
5
|
+
private client;
|
|
6
|
+
private collections;
|
|
7
|
+
constructor({ path, auth, }: {
|
|
8
|
+
path: string;
|
|
9
|
+
auth?: {
|
|
10
|
+
provider: string;
|
|
11
|
+
credentials: string;
|
|
12
|
+
};
|
|
13
|
+
});
|
|
14
|
+
private getCollection;
|
|
15
|
+
private validateVectorDimensions;
|
|
16
|
+
upsert(indexName: string, vectors: number[][], metadata?: Record<string, any>[], ids?: string[]): Promise<string[]>;
|
|
17
|
+
createIndex(indexName: string, dimension: number, metric?: 'cosine' | 'euclidean' | 'dotproduct'): Promise<void>;
|
|
18
|
+
transformFilter(filter?: Filter): Filter | undefined;
|
|
19
|
+
query(indexName: string, queryVector: number[], topK?: number, filter?: Filter, includeVector?: boolean): Promise<QueryResult[]>;
|
|
20
|
+
listIndexes(): Promise<string[]>;
|
|
21
|
+
describeIndex(indexName: string): Promise<IndexStats>;
|
|
22
|
+
deleteIndex(indexName: string): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { ChromaVector };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { MastraVector } from '@mastra/core/vector';
|
|
2
|
+
import { ChromaClient } from 'chromadb';
|
|
3
|
+
import { BaseFilterTranslator } from '@mastra/core/filter';
|
|
4
|
+
|
|
5
|
+
// src/vector/index.ts
|
|
6
|
+
var ChromaFilterTranslator = class extends BaseFilterTranslator {
|
|
7
|
+
getSupportedOperators() {
|
|
8
|
+
return {
|
|
9
|
+
...BaseFilterTranslator.DEFAULT_OPERATORS,
|
|
10
|
+
logical: ["$and", "$or"],
|
|
11
|
+
array: ["$in", "$nin"],
|
|
12
|
+
element: [],
|
|
13
|
+
regex: [],
|
|
14
|
+
custom: []
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
translate(filter) {
|
|
18
|
+
if (this.isEmpty(filter)) return filter;
|
|
19
|
+
this.validateFilter(filter);
|
|
20
|
+
return this.translateNode(filter);
|
|
21
|
+
}
|
|
22
|
+
translateNode(node, currentPath = "") {
|
|
23
|
+
if (this.isRegex(node)) {
|
|
24
|
+
throw new Error("Regex is not supported in Chroma");
|
|
25
|
+
}
|
|
26
|
+
if (this.isPrimitive(node)) return this.normalizeComparisonValue(node);
|
|
27
|
+
if (Array.isArray(node)) return { $in: this.normalizeArrayValues(node) };
|
|
28
|
+
const entries = Object.entries(node);
|
|
29
|
+
const firstEntry = entries[0];
|
|
30
|
+
if (entries.length === 1 && firstEntry && this.isOperator(firstEntry[0])) {
|
|
31
|
+
const [operator, value] = firstEntry;
|
|
32
|
+
const translated = this.translateOperator(operator, value);
|
|
33
|
+
return this.isLogicalOperator(operator) ? { [operator]: translated } : translated;
|
|
34
|
+
}
|
|
35
|
+
const result = {};
|
|
36
|
+
const multiOperatorConditions = [];
|
|
37
|
+
for (const [key, value] of entries) {
|
|
38
|
+
const newPath = currentPath ? `${currentPath}.${key}` : key;
|
|
39
|
+
if (this.isOperator(key)) {
|
|
40
|
+
result[key] = this.translateOperator(key, value);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
44
|
+
const valueEntries = Object.entries(value);
|
|
45
|
+
if (valueEntries.every(([op]) => this.isOperator(op)) && valueEntries.length > 1) {
|
|
46
|
+
valueEntries.forEach(([op, opValue]) => {
|
|
47
|
+
multiOperatorConditions.push({
|
|
48
|
+
[newPath]: { [op]: this.normalizeComparisonValue(opValue) }
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (Object.keys(value).length === 0) {
|
|
54
|
+
result[newPath] = this.translateNode(value);
|
|
55
|
+
} else {
|
|
56
|
+
const hasOperators = Object.keys(value).some((k) => this.isOperator(k));
|
|
57
|
+
if (hasOperators) {
|
|
58
|
+
const normalizedValue = {};
|
|
59
|
+
for (const [op, opValue] of Object.entries(value)) {
|
|
60
|
+
normalizedValue[op] = this.isOperator(op) ? this.translateOperator(op, opValue) : opValue;
|
|
61
|
+
}
|
|
62
|
+
result[newPath] = normalizedValue;
|
|
63
|
+
} else {
|
|
64
|
+
Object.assign(result, this.translateNode(value, newPath));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
result[newPath] = this.translateNode(value);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (multiOperatorConditions.length > 0) {
|
|
72
|
+
return { $and: multiOperatorConditions };
|
|
73
|
+
}
|
|
74
|
+
if (Object.keys(result).length > 1 && !currentPath) {
|
|
75
|
+
return {
|
|
76
|
+
$and: Object.entries(result).map(([key, value]) => ({ [key]: value }))
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
translateOperator(operator, value) {
|
|
82
|
+
if (this.isLogicalOperator(operator)) {
|
|
83
|
+
return Array.isArray(value) ? value.map((item) => this.translateNode(item)) : this.translateNode(value);
|
|
84
|
+
}
|
|
85
|
+
return this.normalizeComparisonValue(value);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// src/vector/index.ts
|
|
90
|
+
var ChromaVector = class extends MastraVector {
|
|
91
|
+
constructor({
|
|
92
|
+
path,
|
|
93
|
+
auth
|
|
94
|
+
}) {
|
|
95
|
+
super();
|
|
96
|
+
this.client = new ChromaClient({
|
|
97
|
+
path,
|
|
98
|
+
auth
|
|
99
|
+
});
|
|
100
|
+
this.collections = /* @__PURE__ */ new Map();
|
|
101
|
+
}
|
|
102
|
+
async getCollection(indexName, throwIfNotExists = true) {
|
|
103
|
+
try {
|
|
104
|
+
const collection = await this.client.getCollection({ name: indexName, embeddingFunction: void 0 });
|
|
105
|
+
this.collections.set(indexName, collection);
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if (throwIfNotExists) {
|
|
108
|
+
throw new Error(`Index ${indexName} does not exist`);
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
return this.collections.get(indexName);
|
|
113
|
+
}
|
|
114
|
+
validateVectorDimensions(vectors, dimension) {
|
|
115
|
+
for (let i = 0; i < vectors.length; i++) {
|
|
116
|
+
if (vectors?.[i]?.length !== dimension) {
|
|
117
|
+
throw new Error(
|
|
118
|
+
`Vector at index ${i} has invalid dimension ${vectors?.[i]?.length}. Expected ${dimension} dimensions.`
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
async upsert(indexName, vectors, metadata, ids) {
|
|
124
|
+
const collection = await this.getCollection(indexName);
|
|
125
|
+
const stats = await this.describeIndex(indexName);
|
|
126
|
+
this.validateVectorDimensions(vectors, stats.dimension);
|
|
127
|
+
const generatedIds = ids || vectors.map(() => crypto.randomUUID());
|
|
128
|
+
const normalizedMetadata = metadata || vectors.map(() => ({}));
|
|
129
|
+
await collection.upsert({
|
|
130
|
+
ids: generatedIds,
|
|
131
|
+
embeddings: vectors,
|
|
132
|
+
metadatas: normalizedMetadata
|
|
133
|
+
});
|
|
134
|
+
return generatedIds;
|
|
135
|
+
}
|
|
136
|
+
async createIndex(indexName, dimension, metric = "cosine") {
|
|
137
|
+
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
138
|
+
throw new Error("Dimension must be a positive integer");
|
|
139
|
+
}
|
|
140
|
+
await this.client.createCollection({
|
|
141
|
+
name: indexName,
|
|
142
|
+
metadata: {
|
|
143
|
+
dimension,
|
|
144
|
+
metric
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
transformFilter(filter) {
|
|
149
|
+
const chromaFilter = new ChromaFilterTranslator();
|
|
150
|
+
const translatedFilter = chromaFilter.translate(filter);
|
|
151
|
+
return translatedFilter;
|
|
152
|
+
}
|
|
153
|
+
async query(indexName, queryVector, topK = 10, filter, includeVector = false) {
|
|
154
|
+
const collection = await this.getCollection(indexName, true);
|
|
155
|
+
const defaultInclude = ["documents", "metadatas", "distances"];
|
|
156
|
+
const translatedFilter = this.transformFilter(filter);
|
|
157
|
+
const results = await collection.query({
|
|
158
|
+
queryEmbeddings: [queryVector],
|
|
159
|
+
nResults: topK,
|
|
160
|
+
where: translatedFilter,
|
|
161
|
+
include: includeVector ? [...defaultInclude, "embeddings"] : defaultInclude
|
|
162
|
+
});
|
|
163
|
+
return (results.ids[0] || []).map((id, index) => ({
|
|
164
|
+
id,
|
|
165
|
+
score: results.distances?.[0]?.[index] || 0,
|
|
166
|
+
metadata: results.metadatas?.[0]?.[index] || {},
|
|
167
|
+
...includeVector && { vector: results.embeddings?.[0]?.[index] || [] }
|
|
168
|
+
}));
|
|
169
|
+
}
|
|
170
|
+
async listIndexes() {
|
|
171
|
+
const collections = await this.client.listCollections();
|
|
172
|
+
return collections.map((collection) => collection);
|
|
173
|
+
}
|
|
174
|
+
async describeIndex(indexName) {
|
|
175
|
+
const collection = await this.getCollection(indexName);
|
|
176
|
+
const count = await collection.count();
|
|
177
|
+
const metadata = collection.metadata;
|
|
178
|
+
return {
|
|
179
|
+
dimension: metadata?.dimension || 0,
|
|
180
|
+
count,
|
|
181
|
+
metric: metadata?.metric
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
async deleteIndex(indexName) {
|
|
185
|
+
await this.client.deleteCollection({ name: indexName });
|
|
186
|
+
this.collections.delete(indexName);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
export { ChromaVector };
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mastra/chroma",
|
|
3
|
+
"version": "0.1.0-alpha.28",
|
|
4
|
+
"description": "Chroma vector store provider for Mastra",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"./package.json": "./package.json"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"chromadb": "^1.9.4",
|
|
19
|
+
"@mastra/core": "^0.2.0-alpha.93"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@tsconfig/recommended": "^1.0.7",
|
|
23
|
+
"@types/node": "^22.9.0",
|
|
24
|
+
"tsup": "^8.0.1",
|
|
25
|
+
"vitest": "^3.0.4"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsup-node src/index.ts --format esm --dts --clean --treeshake",
|
|
29
|
+
"dev": "tsup-node src/index.ts --format esm --dts --clean --treeshake --watch",
|
|
30
|
+
"test": "vitest run"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './vector/index';
|