stores/chroma/README.md
Vector store implementation for Chroma using the official chromadb client with added dimension validation, collection management, and document storage capabilities.
npm install @mastra/chroma
To run a Chroma server, use the Chroma CLI. It is available to you when you install this package.
chroma run
You will now have a Chroma server running on localhost:8000.
import { ChromaVector } from '@mastra/chroma';
const vectorStore = new ChromaVector();
If you run a Chroma server locally with a different configuration, or deploy a Chroma server yourself, you can configure your ChromaVector instantiation with specific connection details:
import { ChromaVector } from '@mastra/chroma';
const vectorStore = new ChromaVector({
host: 'your-host-address',
port: 8000,
ssl: false,
headers: {}, // any HTTP headers to send,
});
Provide your Chroma Cloud API key, tenant, and database.
You can use the Chroma CLI to set these as environment variables: chroma db connect [DB-NAME] --env-file.
import { ChromaVector } from '@mastra/chroma';
const vectorStore = new ChromaVector({
apiKey: process.env.CHROMA_API_KEY,
tenant: process.env.CHROMA_TENANT,
database: process.env.CHROMA_DATABASE,
});
// Create a new collection
await vectorStore.createIndex({ indexName: 'myCollection', dimension: 1536, metric: 'cosine' });
// Add vectors with documents
const vectors = [[0.1, 0.2, ...], [0.3, 0.4, ...]];
const metadata = [{ text: 'doc1' }, { text: 'doc2' }];
const documents = ['full text 1', 'full text 2'];
const ids = await vectorStore.upsert({
indexName: 'myCollection',
vectors,
metadata,
documents, // store original text
});
// Query vectors with document filtering
const results = await vectorStore.query({
indexName: 'myCollection',
queryVector: [0.1, 0.2, ...],
topK: 10, // topK
filter: { text: { $eq: 'doc1' } }, // metadata filter
includeVector: false, // includeVector
documentFilter: { $contains: 'specific text' } // document content filter
});
createIndex({ indexName, dimension, metric? }): Create a new collectionupsert({ indexName, vectors, metadata?, ids?, documents? }): Add or update vectors with optional document storagequery({ indexName, queryVector, topK?, filter?, includeVector?, documentFilter? }): Search for similar vectors with optional document filteringupdateVector({ indexName, id?, filter?, update }): Update a single vector by ID or metadata filterdeleteVector({ indexName, id }): Delete a single vector by IDdeleteVectors({ indexName, ids?, filter? }): Delete multiple vectors by IDs or metadata filterlistIndexes(): List all collectionsdescribeIndex(indexName): Get collection statisticsdeleteIndex(indexName): Delete a collectionQuery results include:
id: Vector IDscore: Distance/similarity scoremetadata: Associated metadatadocument: Original document text (if stored)vector: Original vector (if includeVector is true)