We use cookies to understand how the site is used and to display ads. Analytics and advertising only run after you accept. You can change your choice anytime. Privacy policy

Skip to content
devvkit
$devvkit learn --librarie chromadb-&-txtai-guide

ChromaDB & txtai Guide

[vector-db][embeddings][rag][similarity]
AI / LLM Tools
Install
# ChromaDB:
pip install chromadb
# txtai:
pip install txtai
# Both: python -m pip install sentence-transformers

ChromaDB is the simplest vector database. `import chromadb; chromadb.Client()` gives you an in-memory or persistent (`./chroma_data`) vector store. Add documents with `collection.add` and search with `collection.query`. It handles embedding generation or accepts pre-computed vectors.

txtai goes further: it's an all-in-one embeddings database with built-in RAG, semantic search, LLM-powered extraction, and workflow pipelines. A single `pip install txtai` gives you a full semantic search + QA pipeline. The YAML config defines embeddings, indexing, and queries declaratively.

Both support multiple embedding models: `all-MiniLM-L6-v2` (fast), `text-embedding-3-small` (OpenAI), or local models via sentence-transformers. For production, ChromaDB scales with `clickhouse` or `postgres` backends. GUI: Chroma's optional web UI, or FiftyOne for dataset visualization.

ChromaDB Setup

Start ChromaDB· In-memory or persistent.
import chromadb

# In-memory (for testing):
client = chromadb.Client()

# Persistent:
client = chromadb.PersistentClient(path='./chroma_data')

# Start server (multi-process):
# chroma run --path /db --port 8000
client = chromadb.HttpClient(host='localhost', port=8000)
Chroma HTTP server· Run as a service.
# Terminal:
pip install chromadb
chroma run --path ./chroma_data --port 8000 --host 0.0.0.0

# Docker:
docker run -p 8000:8000 chromadb/chroma

# Python client:
client = chromadb.HttpClient(host='localhost', port=8000)
collection = client.get_or_create_collection('production')

ChromaDB CRUD

Create collection· Add documents with embeddings.
collection = client.create_collection('my_docs')

collection.add(
    documents=[
        'Chroma is a vector database',
        'txtai builds AI-powered search',
        'Embeddings capture semantic meaning'
    ],
    metadatas=[
        {'source': 'wiki', 'topic': 'database'},
        {'source': 'wiki', 'topic': 'ai'},
        {'source': 'blog', 'topic': 'machine-learning'}
    ],
    ids=['doc1', 'doc2', 'doc3']
)
Chroma: update/delete· Maintain collections.
# Update document:
collection.update(
    ids=['doc1'],
    documents=['Updated document text'],
    metadatas=[{'source': 'wiki', 'version': 2}]
)

# Delete:
collection.delete(ids=['doc2'])

# List collections:
client.list_collections()

# Delete collection:
client.delete_collection('my_docs')
Chroma: batch ingest· Efficient large-scale add.
# Batch add 10k documents:
ids = [f'doc{i}' for i in range(10000)]
chunks = [f'Document number {i}' for i in range(10000)]

collection.add(
    documents=chunks,
    ids=ids,
    batch_size=100  # Chroma handles batching
)

# With custom embeddings (for speed):
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-MiniLM-L6-v2')
embeddings = model.encode(chunks).tolist()

collection.add(
    embeddings=embeddings,
    documents=chunks,
    ids=ids
)

ChromaDB Search

Query by similarity· Semantic search.
results = collection.query(
    query_texts=['What is a vector database?'],
    n_results=2
)
# Returns: {documents, metadatas, distances, ids}
print(results['documents'][0])

# Filter by metadata:
collection.query(
    query_texts=['AI tools'],
    where={'source': 'wiki'},
    n_results=10
)

txtai

txtai: quick start· Launch txtai embeddings.
from txtai import Embeddings

embeddings = Embeddings(path='sentence-transformers/all-MiniLM-L6-v2')

# Index documents
data = [
    'Chroma is a vector database for AI',
    'txtai provides semantic search and RAG',
    'Vector search finds meaning, not keywords'
]
embeddings.index(data)

# Search
results = embeddings.search('semantic search', limit=2)
for result in results:
    print(f'{result["text"]}: score: {result["score"]}'

RAG Pipeline

txtai RAG pipeline· Full question-answering.
from txtai import Application

# YAML config
app = Application('''
writable: true
embeddings:
  path: sentence-transformers/all-MiniLM-L6-v2

rag:
  path: openai/gpt-4o-mini
  prompt: 'Answer based on context: {context}\nQuestion: {question}'
''')

# Index content
app.add('Chroma is a vector database built for AI')
app.add('txtai provides semantic search and RAG pipelines')
app.index([])

# Ask questions
answer = app.search('What database is used for vector storage?')
print(answer)