clients/new-js/packages/ai-embeddings/sentence-transformer/README.md
This package provides a Sentence Transformers embedding provider for Chroma using transformers.js (@huggingface/transformers). It allows you to run Sentence Transformer models directly in Node.js without requiring a separate server.
npm install @chroma-core/sentence-transformer
import { ChromaClient } from 'chromadb';
import { SentenceTransformersEmbeddingFunction } from '@chroma-core/sentence-transformer';
// Initialize the embedder with the default model (all-MiniLM-L6-v2)
const embedder = new SentenceTransformersEmbeddingFunction();
// Or initialize with a custom model
const customEmbedder = new SentenceTransformersEmbeddingFunction({
modelName: 'Xenova/all-mpnet-base-v2', // Higher quality model
device: 'cpu', // 'cpu' or 'gpu' (default: 'cpu')
normalizeEmbeddings: false, // Whether to normalize embeddings (default: false)
kwargs: { quantized: true }, // Optional: additional arguments like quantized
});
// Create a new ChromaClient
const client = new ChromaClient({
path: 'http://localhost:8000',
});
// Create a collection with the embedder
const collection = await client.createCollection({
name: 'my-collection',
embeddingFunction: embedder,
});
// Add documents
await collection.add({
ids: ["1", "2", "3"],
documents: ["Document 1", "Document 2", "Document 3"],
});
// Query documents
const results = await collection.query({
queryTexts: ["Sample query"],
nResults: 2,
});
"all-MiniLM-L6-v2")
"all-MiniLM-L6-v2" for cross-client compatibility with Python. These are automatically resolved to Xenova/all-MiniLM-L6-v2 for transformers.js.Xenova/all-MiniLM-L6-v2 or sentence-transformers/all-MiniLM-L6-v2 if you need to specify a particular variant.all-MiniLM-L6-v2 (default), all-mpnet-base-v2, bge-small-en-v1.5'cpu' or 'gpu' (default: 'cpu')false){ quantized: true })You can use any Sentence Transformer model that is compatible with transformers.js. Popular models include:
Xenova/all-MiniLM-L6-v2 - Fast, lightweight model (384 dimensions)Xenova/all-mpnet-base-v2 - Higher quality model (768 dimensions)sentence-transformers/all-MiniLM-L6-v2 - Alternative model identifiersentence-transformers/all-mpnet-base-v2 - Alternative model identifierCheck the transformers.js documentation for more available models.
quantized: true in kwargs for faster loading and reduced memory usage