@mastra/chroma 0.0.0-storage-20250225005900 → 0.0.0-trigger-playground-ui-package-20250506151043

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/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2025 Mastra AI, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @mastra/chroma
2
2
 
3
- Vector store implementation for ChromaDB using the official chromadb client with added dimension validation and collection management.
3
+ Vector store implementation for ChromaDB using the official chromadb client with added dimension validation, collection management, and document storage capabilities.
4
4
 
5
5
  ## Installation
6
6
 
@@ -22,21 +22,28 @@ const vectorStore = new ChromaVector({
22
22
  });
23
23
 
24
24
  // Create a new collection
25
- await vectorStore.createIndex('my-collection', 1536, 'cosine');
25
+ await vectorStore.createIndex({ indexName: 'myCollection', dimension: 1536, metric: 'cosine' });
26
26
 
27
- // Add vectors
27
+ // Add vectors with documents
28
28
  const vectors = [[0.1, 0.2, ...], [0.3, 0.4, ...]];
29
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
- );
30
+ const documents = ['full text 1', 'full text 2'];
31
+ const ids = await vectorStore.upsert({
32
+ indexName: 'myCollection',
33
+ vectors,
34
+ metadata,
35
+ documents, // store original text
36
+ });
37
+
38
+ // Query vectors with document filtering
39
+ const results = await vectorStore.query({
40
+ indexName: 'myCollection',
41
+ queryVector: [0.1, 0.2, ...],
42
+ topK: 10, // topK
43
+ filter: { text: { $eq: 'doc1' } }, // metadata filter
44
+ includeVector: false, // includeVector
45
+ documentFilter: { $contains: 'specific text' } // document content filter
46
+ });
40
47
  ```
41
48
 
42
49
  ## Configuration
@@ -54,6 +61,8 @@ Optional:
54
61
  ## Features
55
62
 
56
63
  - Vector similarity search with cosine, euclidean, and dot product metrics
64
+ - Document storage and retrieval
65
+ - Document content filtering
57
66
  - Strict vector dimension validation
58
67
  - Collection-based organization
59
68
  - Metadata filtering support
@@ -64,9 +73,9 @@ Optional:
64
73
 
65
74
  ## Methods
66
75
 
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
76
+ - `createIndex({ indexName, dimension, metric? })`: Create a new collection
77
+ - `upsert({ indexName, vectors, metadata?, ids?, documents? })`: Add or update vectors with optional document storage
78
+ - `query({ indexName, queryVector, topK?, filter?, includeVector?, documentFilter? })`: Search for similar vectors with optional document filtering
70
79
  - `listIndexes()`: List all collections
71
80
  - `describeIndex(indexName)`: Get collection statistics
72
81
  - `deleteIndex(indexName)`: Delete a collection
@@ -78,6 +87,7 @@ Query results include:
78
87
  - `id`: Vector ID
79
88
  - `score`: Distance/similarity score
80
89
  - `metadata`: Associated metadata
90
+ - `document`: Original document text (if stored)
81
91
  - `vector`: Original vector (if includeVector is true)
82
92
 
83
93
  ## Related Links
@@ -0,0 +1,75 @@
1
+ import { BaseFilterTranslator } from '@mastra/core/vector/filter';
2
+ import type { CreateIndexParams } from '@mastra/core/vector';
3
+ import type { IndexStats } from '@mastra/core/vector';
4
+ import { MastraVector } from '@mastra/core/vector';
5
+ import type { OperatorSupport } from '@mastra/core/vector/filter';
6
+ import type { ParamsToArgs } from '@mastra/core/vector';
7
+ import type { QueryResult } from '@mastra/core/vector';
8
+ import type { QueryVectorArgs } from '@mastra/core/vector';
9
+ import type { QueryVectorParams } from '@mastra/core/vector';
10
+ import type { UpsertVectorArgs } from '@mastra/core/vector';
11
+ import type { UpsertVectorParams } from '@mastra/core/vector';
12
+ import type { VectorFilter } from '@mastra/core/vector/filter';
13
+
14
+ /**
15
+ * Vector store specific prompt that details supported operators and examples.
16
+ * This prompt helps users construct valid filters for Chroma Vector.
17
+ */
18
+ declare const CHROMA_PROMPT = "When querying Chroma, you can ONLY use the operators listed below. Any other operators will be rejected.\nImportant: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.\nIf 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.\n\nBasic Comparison Operators:\n- $eq: Exact match (default when using field: value)\n Example: { \"category\": \"electronics\" }\n- $ne: Not equal\n Example: { \"category\": { \"$ne\": \"electronics\" } }\n- $gt: Greater than\n Example: { \"price\": { \"$gt\": 100 } }\n- $gte: Greater than or equal\n Example: { \"price\": { \"$gte\": 100 } }\n- $lt: Less than\n Example: { \"price\": { \"$lt\": 100 } }\n- $lte: Less than or equal\n Example: { \"price\": { \"$lte\": 100 } }\n\nArray Operators:\n- $in: Match any value in array\n Example: { \"category\": { \"$in\": [\"electronics\", \"books\"] } }\n- $nin: Does not match any value in array\n Example: { \"category\": { \"$nin\": [\"electronics\", \"books\"] } }\n\nLogical Operators:\n- $and: Logical AND\n Example: { \"$and\": [{ \"price\": { \"$gt\": 100 } }, { \"category\": \"electronics\" }] }\n- $or: Logical OR\n Example: { \"$or\": [{ \"price\": { \"$lt\": 50 } }, { \"category\": \"books\" }] }\n\nRestrictions:\n- Regex patterns are not supported\n- Element operators are not supported\n- Only $and and $or logical operators are supported\n- Nested fields are supported using dot notation\n- Multiple conditions on the same field are supported with both implicit and explicit $and\n- Empty arrays in $in/$nin will return no results\n- If multiple top-level fields exist, they're wrapped in $and\n- Only logical operators ($and, $or) can be used at the top level\n- All other operators must be used within a field condition\n Valid: { \"field\": { \"$gt\": 100 } }\n Valid: { \"$and\": [...] }\n Invalid: { \"$gt\": 100 }\n Invalid: { \"$in\": [...] }\n- Logical operators must contain field conditions, not direct operators\n Valid: { \"$and\": [{ \"field\": { \"$gt\": 100 } }] }\n Invalid: { \"$and\": [{ \"$gt\": 100 }] }\n- Logical operators ($and, $or):\n - Can only be used at top level or nested within other logical operators\n - Can not be used on a field level, or be nested inside a field\n - Can not be used inside an operator\n - Valid: { \"$and\": [{ \"field\": { \"$gt\": 100 } }] }\n - Valid: { \"$or\": [{ \"$and\": [{ \"field\": { \"$gt\": 100 } }] }] }\n - Invalid: { \"field\": { \"$and\": [{ \"$gt\": 100 }] } }\n - Invalid: { \"field\": { \"$or\": [{ \"$gt\": 100 }] } }\n - Invalid: { \"field\": { \"$gt\": { \"$and\": [{...}] } } }\n\nExample Complex Query:\n{\n \"$and\": [\n { \"category\": { \"$in\": [\"electronics\", \"computers\"] } },\n { \"price\": { \"$gte\": 100, \"$lte\": 1000 } },\n { \"$or\": [\n { \"inStock\": true },\n { \"preorder\": true }\n ]}\n ]\n}";
19
+ export { CHROMA_PROMPT }
20
+ export { CHROMA_PROMPT as CHROMA_PROMPT_alias_1 }
21
+
22
+ /**
23
+ * Translator for Chroma filter queries.
24
+ * Maintains MongoDB-compatible syntax while ensuring proper validation
25
+ * and normalization of values.
26
+ */
27
+ export declare class ChromaFilterTranslator extends BaseFilterTranslator {
28
+ protected getSupportedOperators(): OperatorSupport;
29
+ translate(filter?: VectorFilter): VectorFilter;
30
+ private translateNode;
31
+ private translateOperator;
32
+ }
33
+
34
+ declare type ChromaQueryArgs = [...QueryVectorArgs, VectorFilter?];
35
+
36
+ declare interface ChromaQueryVectorParams extends QueryVectorParams {
37
+ documentFilter?: VectorFilter;
38
+ }
39
+
40
+ declare type ChromaUpsertArgs = [...UpsertVectorArgs, string[]?];
41
+
42
+ declare interface ChromaUpsertVectorParams extends UpsertVectorParams {
43
+ documents?: string[];
44
+ }
45
+
46
+ declare class ChromaVector extends MastraVector {
47
+ private client;
48
+ private collections;
49
+ constructor({ path, auth, }: {
50
+ path: string;
51
+ auth?: {
52
+ provider: string;
53
+ credentials: string;
54
+ };
55
+ });
56
+ getCollection(indexName: string, throwIfNotExists?: boolean): Promise<any>;
57
+ private validateVectorDimensions;
58
+ upsert(...args: ParamsToArgs<ChromaUpsertVectorParams> | ChromaUpsertArgs): Promise<string[]>;
59
+ private HnswSpaceMap;
60
+ createIndex(...args: ParamsToArgs<CreateIndexParams>): Promise<void>;
61
+ transformFilter(filter?: VectorFilter): VectorFilter;
62
+ query(...args: ParamsToArgs<ChromaQueryVectorParams> | ChromaQueryArgs): Promise<QueryResult[]>;
63
+ listIndexes(): Promise<string[]>;
64
+ describeIndex(indexName: string): Promise<IndexStats>;
65
+ deleteIndex(indexName: string): Promise<void>;
66
+ updateIndexById(indexName: string, id: string, update: {
67
+ vector?: number[];
68
+ metadata?: Record<string, any>;
69
+ }): Promise<void>;
70
+ deleteIndexById(indexName: string, id: string): Promise<void>;
71
+ }
72
+ export { ChromaVector }
73
+ export { ChromaVector as ChromaVector_alias_1 }
74
+
75
+ export { }
@@ -1,9 +1,23 @@
1
- import { BaseFilterTranslator } from '@mastra/core/filter';
2
- import { Filter } from '@mastra/core/filter';
3
- import { IndexStats } from '@mastra/core/vector';
1
+ import { BaseFilterTranslator } from '@mastra/core/vector/filter';
2
+ import type { CreateIndexParams } from '@mastra/core/vector';
3
+ import type { IndexStats } from '@mastra/core/vector';
4
4
  import { MastraVector } from '@mastra/core/vector';
5
- import { OperatorSupport } from '@mastra/core/filter';
6
- import { QueryResult } from '@mastra/core/vector';
5
+ import type { OperatorSupport } from '@mastra/core/vector/filter';
6
+ import type { ParamsToArgs } from '@mastra/core/vector';
7
+ import type { QueryResult } from '@mastra/core/vector';
8
+ import type { QueryVectorArgs } from '@mastra/core/vector';
9
+ import type { QueryVectorParams } from '@mastra/core/vector';
10
+ import type { UpsertVectorArgs } from '@mastra/core/vector';
11
+ import type { UpsertVectorParams } from '@mastra/core/vector';
12
+ import type { VectorFilter } from '@mastra/core/vector/filter';
13
+
14
+ /**
15
+ * Vector store specific prompt that details supported operators and examples.
16
+ * This prompt helps users construct valid filters for Chroma Vector.
17
+ */
18
+ declare const CHROMA_PROMPT = "When querying Chroma, you can ONLY use the operators listed below. Any other operators will be rejected.\nImportant: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.\nIf 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.\n\nBasic Comparison Operators:\n- $eq: Exact match (default when using field: value)\n Example: { \"category\": \"electronics\" }\n- $ne: Not equal\n Example: { \"category\": { \"$ne\": \"electronics\" } }\n- $gt: Greater than\n Example: { \"price\": { \"$gt\": 100 } }\n- $gte: Greater than or equal\n Example: { \"price\": { \"$gte\": 100 } }\n- $lt: Less than\n Example: { \"price\": { \"$lt\": 100 } }\n- $lte: Less than or equal\n Example: { \"price\": { \"$lte\": 100 } }\n\nArray Operators:\n- $in: Match any value in array\n Example: { \"category\": { \"$in\": [\"electronics\", \"books\"] } }\n- $nin: Does not match any value in array\n Example: { \"category\": { \"$nin\": [\"electronics\", \"books\"] } }\n\nLogical Operators:\n- $and: Logical AND\n Example: { \"$and\": [{ \"price\": { \"$gt\": 100 } }, { \"category\": \"electronics\" }] }\n- $or: Logical OR\n Example: { \"$or\": [{ \"price\": { \"$lt\": 50 } }, { \"category\": \"books\" }] }\n\nRestrictions:\n- Regex patterns are not supported\n- Element operators are not supported\n- Only $and and $or logical operators are supported\n- Nested fields are supported using dot notation\n- Multiple conditions on the same field are supported with both implicit and explicit $and\n- Empty arrays in $in/$nin will return no results\n- If multiple top-level fields exist, they're wrapped in $and\n- Only logical operators ($and, $or) can be used at the top level\n- All other operators must be used within a field condition\n Valid: { \"field\": { \"$gt\": 100 } }\n Valid: { \"$and\": [...] }\n Invalid: { \"$gt\": 100 }\n Invalid: { \"$in\": [...] }\n- Logical operators must contain field conditions, not direct operators\n Valid: { \"$and\": [{ \"field\": { \"$gt\": 100 } }] }\n Invalid: { \"$and\": [{ \"$gt\": 100 }] }\n- Logical operators ($and, $or):\n - Can only be used at top level or nested within other logical operators\n - Can not be used on a field level, or be nested inside a field\n - Can not be used inside an operator\n - Valid: { \"$and\": [{ \"field\": { \"$gt\": 100 } }] }\n - Valid: { \"$or\": [{ \"$and\": [{ \"field\": { \"$gt\": 100 } }] }] }\n - Invalid: { \"field\": { \"$and\": [{ \"$gt\": 100 }] } }\n - Invalid: { \"field\": { \"$or\": [{ \"$gt\": 100 }] } }\n - Invalid: { \"field\": { \"$gt\": { \"$and\": [{...}] } } }\n\nExample Complex Query:\n{\n \"$and\": [\n { \"category\": { \"$in\": [\"electronics\", \"computers\"] } },\n { \"price\": { \"$gte\": 100, \"$lte\": 1000 } },\n { \"$or\": [\n { \"inStock\": true },\n { \"preorder\": true }\n ]}\n ]\n}";
19
+ export { CHROMA_PROMPT }
20
+ export { CHROMA_PROMPT as CHROMA_PROMPT_alias_1 }
7
21
 
8
22
  /**
9
23
  * Translator for Chroma filter queries.
@@ -12,11 +26,23 @@ import { QueryResult } from '@mastra/core/vector';
12
26
  */
13
27
  export declare class ChromaFilterTranslator extends BaseFilterTranslator {
14
28
  protected getSupportedOperators(): OperatorSupport;
15
- translate(filter?: Filter): Filter | undefined;
29
+ translate(filter?: VectorFilter): VectorFilter;
16
30
  private translateNode;
17
31
  private translateOperator;
18
32
  }
19
33
 
34
+ declare type ChromaQueryArgs = [...QueryVectorArgs, VectorFilter?];
35
+
36
+ declare interface ChromaQueryVectorParams extends QueryVectorParams {
37
+ documentFilter?: VectorFilter;
38
+ }
39
+
40
+ declare type ChromaUpsertArgs = [...UpsertVectorArgs, string[]?];
41
+
42
+ declare interface ChromaUpsertVectorParams extends UpsertVectorParams {
43
+ documents?: string[];
44
+ }
45
+
20
46
  declare class ChromaVector extends MastraVector {
21
47
  private client;
22
48
  private collections;
@@ -27,15 +53,21 @@ declare class ChromaVector extends MastraVector {
27
53
  credentials: string;
28
54
  };
29
55
  });
30
- private getCollection;
56
+ getCollection(indexName: string, throwIfNotExists?: boolean): Promise<any>;
31
57
  private validateVectorDimensions;
32
- upsert(indexName: string, vectors: number[][], metadata?: Record<string, any>[], ids?: string[]): Promise<string[]>;
33
- createIndex(indexName: string, dimension: number, metric?: 'cosine' | 'euclidean' | 'dotproduct'): Promise<void>;
34
- transformFilter(filter?: Filter): Filter | undefined;
35
- query(indexName: string, queryVector: number[], topK?: number, filter?: Filter, includeVector?: boolean): Promise<QueryResult[]>;
58
+ upsert(...args: ParamsToArgs<ChromaUpsertVectorParams> | ChromaUpsertArgs): Promise<string[]>;
59
+ private HnswSpaceMap;
60
+ createIndex(...args: ParamsToArgs<CreateIndexParams>): Promise<void>;
61
+ transformFilter(filter?: VectorFilter): VectorFilter;
62
+ query(...args: ParamsToArgs<ChromaQueryVectorParams> | ChromaQueryArgs): Promise<QueryResult[]>;
36
63
  listIndexes(): Promise<string[]>;
37
64
  describeIndex(indexName: string): Promise<IndexStats>;
38
65
  deleteIndex(indexName: string): Promise<void>;
66
+ updateIndexById(indexName: string, id: string, update: {
67
+ vector?: number[];
68
+ metadata?: Record<string, any>;
69
+ }): Promise<void>;
70
+ deleteIndexById(indexName: string, id: string): Promise<void>;
39
71
  }
40
72
  export { ChromaVector }
41
73
  export { ChromaVector as ChromaVector_alias_1 }
package/dist/index.cjs ADDED
@@ -0,0 +1,307 @@
1
+ 'use strict';
2
+
3
+ var vector = require('@mastra/core/vector');
4
+ var chromadb = require('chromadb');
5
+ var filter = require('@mastra/core/vector/filter');
6
+
7
+ // src/vector/index.ts
8
+ var ChromaFilterTranslator = class extends filter.BaseFilterTranslator {
9
+ getSupportedOperators() {
10
+ return {
11
+ ...filter.BaseFilterTranslator.DEFAULT_OPERATORS,
12
+ logical: ["$and", "$or"],
13
+ array: ["$in", "$nin"],
14
+ element: [],
15
+ regex: [],
16
+ custom: []
17
+ };
18
+ }
19
+ translate(filter) {
20
+ if (this.isEmpty(filter)) return filter;
21
+ this.validateFilter(filter);
22
+ return this.translateNode(filter);
23
+ }
24
+ translateNode(node, currentPath = "") {
25
+ if (this.isRegex(node)) {
26
+ throw new Error("Regex is not supported in Chroma");
27
+ }
28
+ if (this.isPrimitive(node)) return this.normalizeComparisonValue(node);
29
+ if (Array.isArray(node)) return { $in: this.normalizeArrayValues(node) };
30
+ const entries = Object.entries(node);
31
+ const firstEntry = entries[0];
32
+ if (entries.length === 1 && firstEntry && this.isOperator(firstEntry[0])) {
33
+ const [operator, value] = firstEntry;
34
+ const translated = this.translateOperator(operator, value);
35
+ return this.isLogicalOperator(operator) ? { [operator]: translated } : translated;
36
+ }
37
+ const result = {};
38
+ const multiOperatorConditions = [];
39
+ for (const [key, value] of entries) {
40
+ const newPath = currentPath ? `${currentPath}.${key}` : key;
41
+ if (this.isOperator(key)) {
42
+ result[key] = this.translateOperator(key, value);
43
+ continue;
44
+ }
45
+ if (typeof value === "object" && value !== null && !Array.isArray(value)) {
46
+ const valueEntries = Object.entries(value);
47
+ if (valueEntries.every(([op]) => this.isOperator(op)) && valueEntries.length > 1) {
48
+ valueEntries.forEach(([op, opValue]) => {
49
+ multiOperatorConditions.push({
50
+ [newPath]: { [op]: this.normalizeComparisonValue(opValue) }
51
+ });
52
+ });
53
+ continue;
54
+ }
55
+ if (Object.keys(value).length === 0) {
56
+ result[newPath] = this.translateNode(value);
57
+ } else {
58
+ const hasOperators = Object.keys(value).some((k) => this.isOperator(k));
59
+ if (hasOperators) {
60
+ const normalizedValue = {};
61
+ for (const [op, opValue] of Object.entries(value)) {
62
+ normalizedValue[op] = this.isOperator(op) ? this.translateOperator(op, opValue) : opValue;
63
+ }
64
+ result[newPath] = normalizedValue;
65
+ } else {
66
+ Object.assign(result, this.translateNode(value, newPath));
67
+ }
68
+ }
69
+ } else {
70
+ result[newPath] = this.translateNode(value);
71
+ }
72
+ }
73
+ if (multiOperatorConditions.length > 0) {
74
+ return { $and: multiOperatorConditions };
75
+ }
76
+ if (Object.keys(result).length > 1 && !currentPath) {
77
+ return {
78
+ $and: Object.entries(result).map(([key, value]) => ({ [key]: value }))
79
+ };
80
+ }
81
+ return result;
82
+ }
83
+ translateOperator(operator, value) {
84
+ if (this.isLogicalOperator(operator)) {
85
+ return Array.isArray(value) ? value.map((item) => this.translateNode(item)) : this.translateNode(value);
86
+ }
87
+ return this.normalizeComparisonValue(value);
88
+ }
89
+ };
90
+
91
+ // src/vector/index.ts
92
+ var ChromaVector = class extends vector.MastraVector {
93
+ client;
94
+ collections;
95
+ constructor({
96
+ path,
97
+ auth
98
+ }) {
99
+ super();
100
+ this.client = new chromadb.ChromaClient({
101
+ path,
102
+ auth
103
+ });
104
+ this.collections = /* @__PURE__ */ new Map();
105
+ }
106
+ async getCollection(indexName, throwIfNotExists = true) {
107
+ try {
108
+ const collection = await this.client.getCollection({ name: indexName, embeddingFunction: void 0 });
109
+ this.collections.set(indexName, collection);
110
+ } catch {
111
+ if (throwIfNotExists) {
112
+ throw new Error(`Index ${indexName} does not exist`);
113
+ }
114
+ return null;
115
+ }
116
+ return this.collections.get(indexName);
117
+ }
118
+ validateVectorDimensions(vectors, dimension) {
119
+ for (let i = 0; i < vectors.length; i++) {
120
+ if (vectors?.[i]?.length !== dimension) {
121
+ throw new Error(
122
+ `Vector at index ${i} has invalid dimension ${vectors?.[i]?.length}. Expected ${dimension} dimensions.`
123
+ );
124
+ }
125
+ }
126
+ }
127
+ async upsert(...args) {
128
+ const params = this.normalizeArgs("upsert", args, ["documents"]);
129
+ const { indexName, vectors, metadata, ids, documents } = params;
130
+ const collection = await this.getCollection(indexName);
131
+ const stats = await this.describeIndex(indexName);
132
+ this.validateVectorDimensions(vectors, stats.dimension);
133
+ const generatedIds = ids || vectors.map(() => crypto.randomUUID());
134
+ const normalizedMetadata = metadata || vectors.map(() => ({}));
135
+ await collection.upsert({
136
+ ids: generatedIds,
137
+ embeddings: vectors,
138
+ metadatas: normalizedMetadata,
139
+ documents
140
+ });
141
+ return generatedIds;
142
+ }
143
+ HnswSpaceMap = {
144
+ cosine: "cosine",
145
+ euclidean: "l2",
146
+ dotproduct: "ip",
147
+ l2: "euclidean",
148
+ ip: "dotproduct"
149
+ };
150
+ async createIndex(...args) {
151
+ const params = this.normalizeArgs("createIndex", args);
152
+ const { indexName, dimension, metric = "cosine" } = params;
153
+ if (!Number.isInteger(dimension) || dimension <= 0) {
154
+ throw new Error("Dimension must be a positive integer");
155
+ }
156
+ const hnswSpace = this.HnswSpaceMap[metric];
157
+ if (!["cosine", "l2", "ip"].includes(hnswSpace)) {
158
+ throw new Error(`Invalid metric: "${metric}". Must be one of: cosine, euclidean, dotproduct`);
159
+ }
160
+ await this.client.createCollection({
161
+ name: indexName,
162
+ metadata: {
163
+ dimension,
164
+ "hnsw:space": this.HnswSpaceMap[metric]
165
+ }
166
+ });
167
+ }
168
+ transformFilter(filter) {
169
+ const translator = new ChromaFilterTranslator();
170
+ return translator.translate(filter);
171
+ }
172
+ async query(...args) {
173
+ const params = this.normalizeArgs("query", args, ["documentFilter"]);
174
+ const { indexName, queryVector, topK = 10, filter, includeVector = false, documentFilter } = params;
175
+ const collection = await this.getCollection(indexName, true);
176
+ const defaultInclude = ["documents", "metadatas", "distances"];
177
+ const translatedFilter = this.transformFilter(filter);
178
+ const results = await collection.query({
179
+ queryEmbeddings: [queryVector],
180
+ nResults: topK,
181
+ where: translatedFilter,
182
+ whereDocument: documentFilter,
183
+ include: includeVector ? [...defaultInclude, "embeddings"] : defaultInclude
184
+ });
185
+ return (results.ids[0] || []).map((id, index) => ({
186
+ id,
187
+ score: results.distances?.[0]?.[index] || 0,
188
+ metadata: results.metadatas?.[0]?.[index] || {},
189
+ document: results.documents?.[0]?.[index],
190
+ ...includeVector && { vector: results.embeddings?.[0]?.[index] || [] }
191
+ }));
192
+ }
193
+ async listIndexes() {
194
+ const collections = await this.client.listCollections();
195
+ return collections.map((collection) => collection);
196
+ }
197
+ async describeIndex(indexName) {
198
+ const collection = await this.getCollection(indexName);
199
+ const count = await collection.count();
200
+ const metadata = collection.metadata;
201
+ const hnswSpace = metadata?.["hnsw:space"];
202
+ return {
203
+ dimension: metadata?.dimension || 0,
204
+ count,
205
+ metric: this.HnswSpaceMap[hnswSpace]
206
+ };
207
+ }
208
+ async deleteIndex(indexName) {
209
+ await this.client.deleteCollection({ name: indexName });
210
+ this.collections.delete(indexName);
211
+ }
212
+ async updateIndexById(indexName, id, update) {
213
+ if (!update.vector && !update.metadata) {
214
+ throw new Error("No updates provided");
215
+ }
216
+ const collection = await this.getCollection(indexName, true);
217
+ const updateOptions = { ids: [id] };
218
+ if (update?.vector) {
219
+ updateOptions.embeddings = [update.vector];
220
+ }
221
+ if (update?.metadata) {
222
+ updateOptions.metadatas = [update.metadata];
223
+ }
224
+ return await collection.update(updateOptions);
225
+ }
226
+ async deleteIndexById(indexName, id) {
227
+ try {
228
+ const collection = await this.getCollection(indexName, true);
229
+ await collection.delete({ ids: [id] });
230
+ } catch (error) {
231
+ throw new Error(`Failed to delete index by id: ${id} for index name: ${indexName}: ${error.message}`);
232
+ }
233
+ }
234
+ };
235
+
236
+ // src/vector/prompt.ts
237
+ var CHROMA_PROMPT = `When querying Chroma, you can ONLY use the operators listed below. Any other operators will be rejected.
238
+ Important: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.
239
+ 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.
240
+
241
+ Basic Comparison Operators:
242
+ - $eq: Exact match (default when using field: value)
243
+ Example: { "category": "electronics" }
244
+ - $ne: Not equal
245
+ Example: { "category": { "$ne": "electronics" } }
246
+ - $gt: Greater than
247
+ Example: { "price": { "$gt": 100 } }
248
+ - $gte: Greater than or equal
249
+ Example: { "price": { "$gte": 100 } }
250
+ - $lt: Less than
251
+ Example: { "price": { "$lt": 100 } }
252
+ - $lte: Less than or equal
253
+ Example: { "price": { "$lte": 100 } }
254
+
255
+ Array Operators:
256
+ - $in: Match any value in array
257
+ Example: { "category": { "$in": ["electronics", "books"] } }
258
+ - $nin: Does not match any value in array
259
+ Example: { "category": { "$nin": ["electronics", "books"] } }
260
+
261
+ Logical Operators:
262
+ - $and: Logical AND
263
+ Example: { "$and": [{ "price": { "$gt": 100 } }, { "category": "electronics" }] }
264
+ - $or: Logical OR
265
+ Example: { "$or": [{ "price": { "$lt": 50 } }, { "category": "books" }] }
266
+
267
+ Restrictions:
268
+ - Regex patterns are not supported
269
+ - Element operators are not supported
270
+ - Only $and and $or logical operators are supported
271
+ - Nested fields are supported using dot notation
272
+ - Multiple conditions on the same field are supported with both implicit and explicit $and
273
+ - Empty arrays in $in/$nin will return no results
274
+ - If multiple top-level fields exist, they're wrapped in $and
275
+ - Only logical operators ($and, $or) can be used at the top level
276
+ - All other operators must be used within a field condition
277
+ Valid: { "field": { "$gt": 100 } }
278
+ Valid: { "$and": [...] }
279
+ Invalid: { "$gt": 100 }
280
+ Invalid: { "$in": [...] }
281
+ - Logical operators must contain field conditions, not direct operators
282
+ Valid: { "$and": [{ "field": { "$gt": 100 } }] }
283
+ Invalid: { "$and": [{ "$gt": 100 }] }
284
+ - Logical operators ($and, $or):
285
+ - Can only be used at top level or nested within other logical operators
286
+ - Can not be used on a field level, or be nested inside a field
287
+ - Can not be used inside an operator
288
+ - Valid: { "$and": [{ "field": { "$gt": 100 } }] }
289
+ - Valid: { "$or": [{ "$and": [{ "field": { "$gt": 100 } }] }] }
290
+ - Invalid: { "field": { "$and": [{ "$gt": 100 }] } }
291
+ - Invalid: { "field": { "$or": [{ "$gt": 100 }] } }
292
+ - Invalid: { "field": { "$gt": { "$and": [{...}] } } }
293
+
294
+ Example Complex Query:
295
+ {
296
+ "$and": [
297
+ { "category": { "$in": ["electronics", "computers"] } },
298
+ { "price": { "$gte": 100, "$lte": 1000 } },
299
+ { "$or": [
300
+ { "inStock": true },
301
+ { "preorder": true }
302
+ ]}
303
+ ]
304
+ }`;
305
+
306
+ exports.CHROMA_PROMPT = CHROMA_PROMPT;
307
+ exports.ChromaVector = ChromaVector;
@@ -0,0 +1,2 @@
1
+ export { CHROMA_PROMPT } from './_tsup-dts-rollup.cjs';
2
+ export { ChromaVector } from './_tsup-dts-rollup.cjs';
package/dist/index.d.ts CHANGED
@@ -1 +1,2 @@
1
+ export { CHROMA_PROMPT } from './_tsup-dts-rollup.js';
1
2
  export { ChromaVector } from './_tsup-dts-rollup.js';