fastrag.vector_store.chroma#
ChromaVectorStore, via chromadb’s async HTTP client or an embedded store.
A VectorStore backed by Chroma. Chroma has
no BM25 ranking (its document filters are substring/regex matches, not
relevance scores), so this store does not implement
LexicalStore: pair it with a separate
lexical store for hybrid retrieval.
Every write and query passes vectors, so the collection’s default embedding function is never invoked (and its model never downloaded).
Chroma metadata values must be scalars, so a document’s (possibly nested)
metadata is kept as one JSON string; source_id, source_uri and
step_config_hash are stored as their own metadata keys so where
filters can match them.
Module Attributes
Collection name used when the caller does not name one. |
|
Chroma's HNSW distance functions. |
|
Ids per |
Classes
|
Vectors in a Chroma server, or in an embedded on-disk Chroma database. |
- fastrag.vector_store.chroma.DEFAULT_COLLECTION = 'fastrag_documents'#
Collection name used when the caller does not name one.
- fastrag.vector_store.chroma.METRICS = frozenset({'cosine', 'ip', 'l2'})#
Chroma’s HNSW distance functions.
- fastrag.vector_store.chroma.ID_PAGE_SIZE = 10000#
Ids per
getpage inChromaVectorStore.all_ids().
- class fastrag.vector_store.chroma.ChromaVectorStore(*, url=None, path=None, collection='fastrag_documents', headers=None, tenant='default_tenant', database='default_database', metric_type='cosine', policy=None)[source]#
Bases:
VectorStoreVectors in a Chroma server, or in an embedded on-disk Chroma database.
Give
urlto talk to a Chroma server through its async HTTP client, orpathto run Chroma in-process against a local directory (its client is synchronous, so calls run in a worker thread). Construction never touches the network or the disk — the client is built in__aenter__.async with ChromaVectorStore(url="http://localhost:8000") as vector_store: async for document in vector_store.save(embedded_documents): ...
filteris a JSON-encoded Chromawhereobject, e.g.'{"source_uri": "https://example.org/docs"}'.scoreis Chroma’s own distance, so lower is better; results are still best first.- Parameters:
url (str | None) – Server endpoint, e.g.
http://localhost:8000. Exclusive withpath.path (str | None) – Directory of an embedded persistent database. Exclusive with
url.collection (str) – Name of the Chroma collection. Created on demand.
headers (Mapping[str, str] | None) – Extra HTTP headers, e.g. an auth token, sent with
url.tenant (str) – Chroma tenant the collection lives in.
database (str) – Chroma database the collection lives in.
metric_type (str) – HNSW distance for a new collection:
cosine,l2orip. An existing collection must already use it.policy (Policy | None) – Execution policy for the terminal step
save()returns.
- async ensure_ready(dimension)[source]#
Create
collectionif absent, or validate its metric and width.Chroma fixes a collection’s width on its first insert rather than at creation, so an existing collection is checked against one stored vector; an empty one accepts any width.
- Parameters:
dimension (int)
- Return type:
None
- async search(embedding, *, k, filter=None)[source]#
Return at most
kdocuments nearestembedding, best first.scoreis Chroma’s distance for the collection’s metric, where lower means closer;rankis the position in Chroma’s already nearest-first order.
- save(upstream, *, skip_existing=False)#
Return a lazy terminal step that indexes
upstream’s documents.Constructing the returned step performs no I/O — evaluating it (
async for document in vector_store.save(upstream):or.run()) is what upserts. Each document is queued into a batcher that checksexisting_ids()and flushes toupsert()onceself.policy.batch_sizedocuments have queued, or after a short window; every document is yielded back once its batch is resolved, withcachedset to whether the store already held it, andDocumentSavedemitted for it.- Parameters:
upstream (PipelineStep[Document]) – Final upstream step yielding embedded documents.
skip_existing (bool) – For an identity-preserving Embedder, check destination ids before embedding. Indexed items bypass vector retrieval and produce cached DocumentEmbedded and DocumentSaved events. Cached events and results may contain only identity/metadata with empty content and no vectors. Custom content filters disable header projection. Use a separate destination for each embedding configuration.
- Return type:
Either way,
recreate_cache(on the store’s policy or the pipeline’s) skips the existence check and upserts every document.
- sync(upstream, *, skip_existing=False)#
Like
save(), then delete every stored documentupstreamdid not yield.After a clean run the store holds exactly the documents the run produced: documents whose source disappeared or changed (a changed chunk gets a new id) are removed. Deletion happens once the upstream is exhausted, so a run that raises or is stopped early deletes nothing. A run with any
ItemFaileddeletes nothing either: a failed item’s documents are missing from the run, not from the source. Either way aDocumentsPrunedevent reports the outcome.The run must cover everything the store should keep — use one store (collection/index) per synced pipeline.
- Parameters:
upstream (PipelineStep[Document]) – Final upstream step yielding embedded documents.
- Return type: