Skip to content

Latest commit

Β 

History

27 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

RAG Debugger πŸ”

Chrome DevTools for RAG Pipelines β€” Instrument your RAG code with a single decorator, and this dashboard visualizes every stage in real time: embedding, retrieval, re-ranking, answer generation, and grounding attribution.


Why RAG Debugger?

When your RAG chatbot gives a wrong answer, you're left guessing. RAG Debugger makes the entire pipeline transparent:

Question Without RAG Debugger With RAG Debugger
Was the right document retrieved? 🀷 No idea βœ… See all chunks + similarity scores
Was the reranker helpful? 🀷 No idea βœ… Compare cosine vs rerank scores side by side
Did the LLM use the context? 🀷 No idea βœ… Sentence-level grounding highlights (green/red)
Which stage was slow? 🀷 No idea βœ… Stage-by-stage latency timeline
Is quality improving over time? 🀷 No idea βœ… Grounding trends + analytics dashboard

How It Works

Your RAG App                SDK                    Server                 Dashboard
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ embed()  │──emit──▢│ @rag_trace   │──POST─▢│ FastAPI +    │──GET─▢│ Next.js UI   β”‚
β”‚ retrieve β”‚  event  β”‚ non-blocking β”‚ /eventsβ”‚ DuckDB       β”‚ REST  β”‚ D3 charts    β”‚
β”‚ rerank() β”‚         β”‚ auto trace   β”‚        β”‚ grounding    β”‚ + WS  β”‚ live updates β”‚
β”‚ generate β”‚         β”‚ PII scrub    β”‚        β”‚ analytics    β”‚       β”‚ analytics    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

The 3 Components

Component What It Does Why It Exists
SDK (packages/sdk/) @rag_trace("retrieve") β€” one-line decorator on your existing functions Captures inputs, outputs, timing, errors without blocking your pipeline. Scrubs PII before sending.
Server (apps/server/) FastAPI + DuckDB + sentence-transformers Stores events, computes grounding scores (checks if each LLM sentence is supported by retrieved chunks), broadcasts via WebSocket.
Dashboard (apps/dashboard/) Next.js 16 + D3.js + Recharts Pipeline timeline, chunk waterfall chart, grounding highlighter, embedding scatter plot, live analytics.

Real-World Benefits

  1. Debug bad answers β€” drill into a specific trace, see which chunks were retrieved, and whether the LLM actually used them
  2. Optimize latency β€” see that retrieval takes 800ms (add a cache!) or generation takes 3s (use a faster model!)
  3. Detect hallucinations β€” the grounding highlighter shows you, sentence by sentence, what's supported by evidence vs. hallucinated
  4. Track quality over time β€” after changing your chunking strategy or embedding model, see if grounding scores improved
  5. Production monitoring β€” the live WebSocket feed shows every query flowing through your pipeline in real time

Quick Start (3 terminals)

Terminal 1: Start the server

cd apps/server
uv venv && uv pip install -e .
uv run uvicorn main:app --host 0.0.0.0 --port 7777 --reload

Terminal 2: Start the dashboard

cd apps/dashboard
pnpm install && pnpm dev

Terminal 3: Run the test app

cd apps/test-app
uv venv --clear .venv
uv pip install --python .venv/bin/python3 httpx pydantic
.venv/bin/python3 main.py                              # Run all 8 sample queries
.venv/bin/python3 main.py --query "What is RAG?"       # Single query
.venv/bin/python3 main.py --loop                       # Continuous (every 5s)
.venv/bin/python3 main.py --error                      # Simulate errors

Open http://localhost:3000 (or :3001) β€” traces appear as the pipeline runs.


Instrument Your Own RAG Code

from rag_debugger import init, rag_trace

# 1. Initialize once at startup
init(dashboard_url="http://localhost:7777")

# 2. Decorate your pipeline functions β€” that's it
@rag_trace("embed")
async def embed_query(query: str) -> list[float]:
    return await openai_embed(query)

@rag_trace("retrieve")
async def retrieve_chunks(vector: list[float], k: int = 10):
    return await vector_store.query(vector, k)

@rag_trace("rerank")
async def rerank(query: str, chunks: list) -> list:
    return await cohere_rerank(query, chunks)

@rag_trace("generate")
async def generate(query: str, context: str) -> str:
    return await llm.complete(query, context)

The decorator automatically:

  • Generates trace_id and query_id via ContextVar
  • Captures function inputs and outputs
  • Measures duration_ms
  • Emits events asynchronously (non-blocking, never crashes your pipeline)
  • Scrubs PII (emails, API keys, SSNs) before sending

What Happens Under the Hood

When you call your pipeline, here's the event flow:

embed_query("What is RAG?")
  β†’ @rag_trace captures 64-dim vector, 23ms duration   β†’ POST /events βœ“

retrieve_chunks(vector, k=5)
  β†’ @rag_trace captures 5 chunks + cosine scores       β†’ POST /events βœ“

rerank_chunks(query, chunks)
  β†’ @rag_trace captures reranked scores                 β†’ POST /events βœ“

generate_answer(query, context)
  β†’ @rag_trace captures answer text                     β†’ POST /events βœ“
  β†’ auto-emits session_complete summary                 β†’ POST /events βœ“

Server:
  β†’ Stores all 5 events in DuckDB
  β†’ Computes sentence-level grounding (MiniLM model)
  β†’ Broadcasts via WebSocket to dashboard

Dashboard:
  β†’ Pipeline timeline shows stage durations
  β†’ Chunk waterfall compares cosine vs rerank scores
  β†’ Grounding highlighter shows green (grounded) / red (hallucinated) sentences

Framework Adapters

LangChain

from rag_debugger.adapters.langchain import RAGDebuggerCallback

handler = RAGDebuggerCallback()
chain.invoke({"query": "..."}, config={"callbacks": [handler]})

LlamaIndex

from rag_debugger.adapters.llamaindex import RAGDebuggerLlamaIndex
from llama_index.core.callbacks import CallbackManager

handler = RAGDebuggerLlamaIndex()
callback_manager = CallbackManager([handler])
index = VectorStoreIndex.from_documents(docs, callback_manager=callback_manager)

OpenAI

from rag_debugger.adapters.openai import RAGDebuggerOpenAI

client = RAGDebuggerOpenAI()
embedding = client.embed("What is RAG?")          # β†’ "embed" event
response = client.complete(messages=[...])          # β†’ "generate" event

API Endpoints

Method Endpoint Description
POST /events Ingest SDK events
GET /traces List query sessions (paginated, filterable)
GET /traces/{id} Full trace with all events
GET /traces/{id}/chunks Chunk scores across stages
GET /traces/{id}/embeddings Vectors for UMAP projection
GET /traces/{id}/grounding Sentence attribution data
GET /analytics/metrics?days=7 Daily metrics (grounding, latency, errors)
WS /ws/{trace_id} Real-time event stream
POST /playground/query Test query endpoint
GET /health Health check

Docker

docker-compose up --build

Project Structure

rag-debugger/
β”œβ”€β”€ apps/
β”‚   β”œβ”€β”€ server/             # FastAPI backend
β”‚   β”‚   β”œβ”€β”€ routers/        # events, traces, analytics, ws, playground
β”‚   β”‚   β”œβ”€β”€ database.py     # DuckDB connection + queries
β”‚   β”‚   β”œβ”€β”€ grounding.py    # Sentence attribution scorer (MiniLM)
β”‚   β”‚   └── models.py       # Pydantic schemas
β”‚   β”œβ”€β”€ dashboard/          # Next.js 16 frontend
β”‚   β”‚   β”œβ”€β”€ app/            # Pages (Home, Traces, Analytics, Playground)
β”‚   β”‚   β”œβ”€β”€ components/     # D3 charts, timeline, grounding highlighter
β”‚   β”‚   β”œβ”€β”€ hooks/          # WebSocket, UMAP, metrics hooks
β”‚   β”‚   └── lib/            # API client, TypeScript types
β”‚   └── test-app/           # Test application for SDK integration
β”œβ”€β”€ packages/
β”‚   └── sdk/                # Python instrumentation SDK
β”‚       └── rag_debugger/   # Core SDK + framework adapters
β”œβ”€β”€ docs/                   # Detailed docs (SERVER.md, SDK.md, DASHBOARD.md)
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ Makefile
└── README.md

Tech Stack

Layer Technology
Frontend Next.js 16, React 19, Tailwind CSS 4, TypeScript 5.9
Charts D3.js (waterfall), Recharts (metrics)
Embeddings umap-js (browser), Canvas2D
Backend FastAPI, Python 3.10+
Database DuckDB (columnar analytics)
Grounding sentence-transformers (all-MiniLM-L6-v2)
Real-time WebSockets
SDK Python, httpx, ContextVar propagation

License

MIT

About

Instrument your RAG code with a single decorator, and this dashboard visualizes every stage in real time: embedding, retrieval, re-ranking, answer generation, and grounding attribution.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages