Skip to content

About

Multi-hop agentic RAG in TypeScript: query decomposition, from-scratch BM25 retrieval, iterative sufficiency loop, cited synthesis.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

agentic-rag

CI Live trace

Live trace → — a real multi-hop run against the sample corpus, regenerated on every push to main.

A multi-hop agentic RAG pipeline in TypeScript: it decomposes a complex question into sub-questions, retrieves evidence for each independently, checks whether it has enough information to answer, optionally issues follow-up retrieval hops, and synthesizes a cited final answer.

Why this exists

Single-shot RAG ("embed the question, retrieve top-k, stuff into a prompt") breaks down on questions that need facts stitched together from multiple, non-adjacent passages — e.g. "what language is the alerting service written in, and how many alerting rules can a free account create?" answers two unrelated facts that no single retrieval call surfaces well. This project implements the standard fix: decompose, retrieve per sub-question, check sufficiency, loop if needed, then synthesize.

Architecture

src/
  retrieval/
    Retriever.ts        Retriever interface
    Bm25Retriever.ts     BM25 ranking implemented from scratch (no vector DB / embedding API required)
  llm/
    Llm.ts               decompose / answerFromContext / checkSufficiency / synthesizeFinalAnswer
    OpenAILlm.ts          real implementation
    FakeLlm.ts            deterministic heuristic implementation for tests + the zero-cost public demo
  agent.ts                the orchestration loop: decompose -> retrieve+answer per sub-question -> sufficiency loop -> synthesize
  report.ts               renders an AgentTrace to Markdown / HTML
  cli.ts                  wires it together

Both Retriever and Llm are interfaces, so agent.ts has zero dependency on OpenAI or any specific retrieval backend — see test/agent.test.ts, which drives the orchestration loop (including the extra-hop and maxHops-cutoff behavior) with hand-written mock Llm implementations and no network calls.

Usage

npm install
cp .env.example .env   # add OPENAI_API_KEY for real reasoning

npm run ask -- --question "What language is the rule-engine service written in and how many alerting rules can a Free tier account create?"
npm run ask:fake       # heuristic run, no API key needed
npm run render:docs    # writes docs/index.html (what the live trace link above serves)

The default question and corpus (data/corpus.json) describe a fictional logging platform, deliberately spread across multiple short documents so that multi-hop questions actually require multiple retrieval hops to answer.

Testing

npm test

Covers BM25 ranking correctness, sub-question decomposition, the extra-hop sufficiency loop, and the maxHops cutoff — all against FakeLlm, so the suite needs no API key and is fully deterministic.

About

Multi-hop agentic RAG in TypeScript: query decomposition, from-scratch BM25 retrieval, iterative sufficiency loop, cited synthesis.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages