How nicolasarmientor/tf-rag works
Purpose and entry points
TensorFlow-RAG is a compact retrieval-augmented Q&A system over TensorFlow Guide documentation. Its two operational entry points are the FastAPI server, src/api/main.py, and offline vector-store construction via python -m src.retrieval.vector_store in src/retrieval/vector_store.py. Evaluation runs separately through src/eval/evaluate.py.
Offline ingestion and indexing
The build pipeline loads Markdown and notebook source files from the externally supplied TensorFlow docs tree, extracts content, and creates source-attributed chunks. src/ingest/loader.py owns document loading; src/ingest/chunker.py performs token-aware chunking with overlap and preserves fenced code blocks intact. The resulting chunks and metadata are embedded and persisted by src/retrieval/vector_store.py.
Retrieval boundary
ChromaDB is both the persistent vector store and embedding provider, using its bundled ONNX Runtime all-MiniLM-L6-v2 model rather than a direct Torch/sentence-transformers runtime. Query retrieval is isolated in src/retrieval/retriever.py, which requests top-k candidates but applies a distance threshold. This is the key grounding boundary: irrelevant questions yield no usable context rather than unrelated nearest neighbors.
Generation and safety
src/generation/generator.py converts retrieved chunks into a constrained Claude prompt, calls Anthropic, and returns answers with source information. The generator is expected to refuse when retrieval produces no qualifying context. Because model output is rendered in the browser, generated HTML is sanitized before presentation. Anthropic Claude is also used as an external evaluation judge.
API and BYOK UI
src/api/main.py composes retrieval and generation behind two interfaces: JSON POST /ask, using server-side ANTHROPIC_API_KEY, and form-oriented POST /ask-ui, which renders an HTMX HTML fragment. Request/response contracts reside in src/api/schemas.py. The browser UI is server-rendered from src/api/templates/index.html and src/api/templates/answer.html; for the UI route, a visitor-provided API key is kept client-side/session-scoped rather than persisted by the application.
Evaluation loop
src/eval/testset.py defines a hand-authored set of answerable and negative questions. src/eval/evaluate.py exercises both stages: whether retrieval finds the expected source and whether a second Claude call judges the answer against a reference summary. This makes refusal behavior a first-class quality criterion, not merely prompt guidance.
Deployment and runtime
The Python application runs under FastAPI/Uvicorn. Dockerfile fetches TensorFlow documentation and builds chroma_data/ during image build, producing a runtime container with no source-data volume requirement. It preserves the Chroma/ONNX cache under /app, then runs non-root. The intended hosting target is Render; runtime port selection honors its PORT environment variable. CI is defined in .github/workflows/ci.yml.
Main entry points: main.py FastAPI application · evaluate.py Evaluation runner · retriever.py Threshold retriever · generator.py Grounded generator
Components 16
Offline indexing
- TensorFlow docs tree · Markdown and notebooks supplied during image build.
- Document loader
src/ingest/loader.py - Token-aware chunker · Preserves fenced code and source attribution.
src/ingest/chunker.py - Vector-store builder · Embeds chunks and persists the Chroma collection.
src/retrieval/vector_store.py - ChromaDB + ONNX embeddings · Persistent store using bundled all-MiniLM-L6-v2 embeddings.
- Image build · Fetches docs and builds chroma_data before runtime.
Dockerfile
Serving
- FastAPI application · Provides JSON and HTMX question-answering routes.
src/api/main.py - API contracts
src/api/schemas.py - Threshold retriever · Rejects top-k candidates beyond the distance threshold.
src/retrieval/retriever.py - Grounded generator · Refuses without qualifying context and returns sources.
src/generation/generator.py - Anthropic Claude
- Browser + HTMX · Supplies a session-scoped BYOK key for the UI route.
- Server-rendered templates
src/api/templates/index.html
Evaluation
- Evaluation test set · Includes answerable and negative questions.
src/eval/testset.py - Evaluation runner · Checks retrieval sources and Claude-judged answers.
src/eval/evaluate.py - CI workflow
.github/workflows/ci.yml
Connections 19
- TensorFlow docs tree → Document loader: loads
- Document loader → Token-aware chunker: documents
- Token-aware chunker → Vector-store builder: chunks + metadata
- Vector-store builder → ChromaDB + ONNX embeddings: embeds and persists
- Image build → TensorFlow docs tree: fetches
- Image build → Vector-store builder: builds index
- Browser + HTMX → FastAPI application: POST /ask-ui
- FastAPI application → API contracts: uses
- FastAPI application → Server-rendered templates: renders
- FastAPI application → Threshold retriever: asks
- Threshold retriever → ChromaDB + ONNX embeddings: top-k query
- Threshold retriever → Grounded generator: qualified context
- FastAPI application → Grounded generator: JSON /ask or UI
- Grounded generator → Anthropic Claude: constrained prompt
- Evaluation test set → Evaluation runner: questions and references
- Evaluation runner → Threshold retriever: tests source retrieval
- Evaluation runner → Grounded generator: tests answers
- Evaluation runner → Anthropic Claude: judges answers
- CI workflow → Evaluation runner: runs