Skip to content

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Refract

CI

A hybrid RAG system — BM25 (lexical) and HNSW (vector) retrieval running in parallel, fused with Reciprocal Rank Fusion, with multi-hop agentic decomposition for cross-topic questions. Instead of hiding all of that behind a spinner, the UI visualizes the pipeline live: every retrieval score, every fusion rank, every sub-question, as it happens.

Why this exists

Most RAG portfolio demos are a black-box chatbot — you can't tell if it's doing anything more sophisticated than a single vector search. This one exists to show the actual mechanics: two independent retrievers scored and ranked side by side, a real fusion algorithm (not "pick the vector results"), and a visible decomposition step for questions that genuinely span multiple source documents. It's also a deliberate unification of the from-scratch algorithm work spread across this portfolio — BM25, HNSW, and multi-hop reasoning — into one flagship system.

How it works

src/lib/
  corpus.ts        7 real, accurate reference documents on distributed
                    systems & retrieval (CAP theorem, Raft, Kafka, Cassandra,
                    HNSW, BM25, RRF) — chosen so genuine multi-hop questions exist
  bm25.ts            From-scratch BM25 (real IDF/TF-saturation math)
  hnsw.ts            From-scratch HNSW with the diversity-aware neighbor-selection
                      heuristic (ports the algorithm from hnsw-vector-index)
  embeddings.ts       Real OpenAI embeddings when OPENAI_API_KEY is set; a
                      deterministic character-trigram hash embedding otherwise —
                      honestly labeled in the UI as not a real semantic embedding
  rrf.ts              Reciprocal Rank Fusion (Cormack, Clarke & Buettcher, 2009)
  agentic.ts          Detects genuinely cross-topic questions and decomposes them
                       into one sub-question per topic touched
  retriever.ts        Orchestrates BM25 + HNSW + RRF for one (sub-)question
  synthesize.ts        Extractive demo synthesis, grounded in the real fused chunks
  synthesizeReal.ts     Real streamed gpt-4o-mini synthesis with the same
                        citation contract when a key is present
app/api/refract/route.ts  Streams newline-delimited pipeline events (decompose →
                         retrieve → synthesize) that the UI consumes live
components/               PipelineView, RetrievalCard, AnswerPanel — the
                          visualization layer

Same zero-cost-demo pattern as the rest of this portfolio: the entire pipeline — decomposition, dual retrieval, fusion, citations — runs identically with or without OPENAI_API_KEY. Only the embedding quality (hash-based vs. real) and answer fluency (extractive vs. LLM-paraphrased) change, and the UI says so directly rather than pretending otherwise.

Usage

npm install
npm run dev     # demo mode, no API key needed

Try a single-hop question ("How does Raft elect a leader?") and a genuine multi-hop one ("How does Kafka's replication relate to the CAP theorem, and how does that compare to Cassandra's tunable consistency?") to see the decomposition step actually branch into per-topic retrieval.

Testing

npm test

22 tests: BM25 ranking correctness, HNSW recall against a brute-force baseline on a clustered dataset, RRF fusion ordering, multi-hop decomposition triggers, and end-to-end retrieval/synthesis grounding against the real corpus.

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages