CoolFace
Apppublic

evanderpool/rag-knowledge-base

sourceHugging Facemitupdated 4mo agoView on Hugging Face
0likes
App README

RAG Knowledge Base Builder

Status: Phase 2 — Complete (Streamlit UI built) Started: 2026-05-09 Target MVP: 3 days | Target v1: 7 days (Streamlit UI + HuggingFace Spaces deploy)

Description

A local RAG system that lets anyone upload company documents and ask questions in plain English — getting accurate answers with source citations. Built with HuggingFace embeddings, ChromaDB, and Groq API (all free).

Tech Stack

Python · Groq API (Llama 3.3 70B) · HuggingFace sentence-transformers · ChromaDB · PyMuPDF · Streamlit (Phase 2)

Quick Start

bash
# 1. Install dependencies
pip install --user -r requirements.txt

# 2. Add your Groq API key (free at console.groq.com)
cp .env.example .env   # Windows: copy .env.example .env

# 3a. Web UI (Phase 2)
streamlit run app.py

# 3b. CLI (Phase 1)
python ingest.py company_policy.pdf
python query.py

Key Dates

  • 2026-05-09: Project scoped and brief created
  • 2026-05-09: Phase 1 MVP complete — loader, embedder, query engine, CLI
  • 2026-05-09: Phase 2 complete — Streamlit UI with file upload, chat, source panel

Full Pipeline

Document
  → loader.load_document()
      → PyMuPDF page-by-page extraction     # preserves page numbers for citations
      → _clean()                             # strip artifacts, fix hyphenation
      → _recursive_split() + _merge()        # 1000-char boundary-aware chunks
      → _apply_overlap()                     # 150-char overlap between chunks
      → list[Chunk]                          # text + source + page + file_hash + token_estimate

  → kb.ingest(chunks)                        # KnowledgeBase in embedder.py
      → is_ingested(file_hash)?              # skip if already in store (dedup)
      → SentenceTransformer.encode()         # all-MiniLM-L6-v2, batch=32
      → ChromaDB.add()                       # cosine similarity, persistent local storage

  → engine.ask(question)                     # QueryEngine in query.py
      → kb.retrieve(question, top_k=5)       # embed query → cosine similarity search
      → _apply_token_budget()                # fit chunks within 4000-token context limit
      → Groq(Llama 3.3 70B)                  # structured prompt with context + citation rules
      → QueryResult(answer, sources, ...)    # answer + deduplicated source list

Files

FilePurpose
loader.pyDocument ingestion — extraction, cleaning, chunking
embedder.pyKnowledgeBase — embedding, dedup, retrieval, document management
query.pyQueryEngine — prompt engineering, Groq generation, streaming CLI
ingest.pyCLI: ingest files/folders, list documents, delete documents
requirements.txtAll dependencies
.env.exampleEnvironment variable template
app.py(Phase 2) Streamlit web UI

CLI Reference

bash
# Ingest
python ingest.py policy.pdf                  # single file
python ingest.py doc1.pdf doc2.txt           # multiple files
python ingest.py docs/                       # whole folder
python ingest.py --list                      # show what's in the knowledge base
python ingest.py --delete policy.pdf         # remove a document

# Query
python query.py                              # interactive Q&A with streaming

Key Design Decisions

DecisionReason
Boundary-aware recursive chunkingRaw char splits break mid-sentence → degraded embeddings
Page-by-page extractionJoining pages first loses page numbers permanently — no useful citations
hnsw:space: cosine in ChromaDBDefault L2 measures vector length, not semantic angle — always wrong for text
file_hash dedupSkip already-ingested files without scanning the full collection
Sources from chunks, not model outputModel cannot hallucinate a citation that wasn't in retrieved context
token_estimate on every ChunkEnables prompt token budget control without a tokenizer dependency
Custom exception typesExplicit failure modes: ScannedPDFError, EmptyDocumentError, UnsupportedFileTypeError
Extensible _LOADERS registryAdd .docx / .md support in one line

Project Brief

Saved to Google Drive: 2026-05-09 AI Project Brief — RAG Knowledge Base Builder

Phase 3 — Next (Stretch Goals)

  • Deploy to HuggingFace Spaces for a public live demo URL
  • Multi-turn conversation with chat history injected into the Groq prompt
  • .docx and .md loader support (add one entry to _LOADERS in loader.py)
  • Relevance threshold filter — skip chunks below a minimum score
  • Re-ranking pass on retrieved chunks before sending to Groq