How hippoley/causalrag works
1. Overall shape
- This is a Python package that extends standard RAG with a causal-reasoning layer.
- The central unit is CausalRAGPipeline, which connects ingestion, indexing, retrieval, reranking, prompt construction, and LLM generation.
- Around that core are thin entry surfaces: a CLI, a FastAPI service, examples, and evaluation scripts.
2. Primary data flow
- Input documents are first indexed.
- Indexing builds two parallel artifacts: a semantic vector index and a causal graph.
- At query time, the system retrieves candidate passages from both semantic and causal signals, reranks them, then feeds the selected context plus causal paths into the generator.
- The final response includes the answer, supporting context, and causal paths.
3. Causal graph subsystem
- Located under causalrag/causal_graph/.
- builder.py is the extraction and graph-construction layer: it turns text into causal triples and stores them as a graph structure.
- retriever.py uses that graph to find causal paths relevant to a query.
- explainer.py is a utility layer for interpreting or visualizing the graph rather than serving the main retrieval path.
4. Semantic and hybrid retrieval
- Located under causalrag/retriever/.
- vector_store.py provides the semantic similarity index, with FAISS called out in the README as the main backend.
- bm25_retriever.py adds keyword-style retrieval.
- hybrid.py combines semantic retrieval with causal signals, making retrieval multi-signal instead of purely embedding-based.
- This boundary matters because retrieval is not a single index lookup; it merges at least semantic and causal evidence.
5. Reranking layer
- Located under causalrag/reranker/.
- base.py defines the reranker interface.
- causal_path.py implements reranking based on whether candidates align with causal paths or causal graph structure.
- In the architecture, reranking sits between candidate retrieval and generation, acting as a relevance/coherence filter.
6. Generation subsystem
- Located under causalrag/generator/.
- prompt_builder.py assembles prompts using templates stored in causalrag/templates/.
- llm_interface.py abstracts the model provider layer; the README indicates OpenAI-compatible APIs are the default integration point, with other providers possible.
- This layer consumes reranked context and causal evidence and produces the answer.
7. Orchestration and public API
- causalrag/pipeline.py is the orchestration boundary for end-to-end use.
- It likely owns the main objects: graph builder, retriever, reranker, prompt builder, and LLM interface.
- causalrag/cli.py exposes that pipeline for indexing, querying, serving, and evaluation from the command line.
- causalrag/interface/api.py provides the server-facing FastAPI wrapper for programmatic access.
8. Evaluation subsystem
- Located under causalrag/evaluation/.
- evaluator.py runs metrics over pipeline outputs and references.
- The bundled dataset in data/evaluation/evaluation_dataset.json shows that evaluation is first-class, not an afterthought.
- The README emphasizes both generic RAG metrics and causal metrics, so evaluation covers answer quality plus causal consistency/completeness.
9. Utilities and support code
- causalrag/utils/io.py handles file and dataset loading.
- causalrag/utils/logging.py supports tracing/timing/diagnostics across pipeline stages.
- These are cross-cutting helpers used by indexing, querying, CLI execution, and evaluation.
10. Repository surfaces and operational modes
- The project can run as a library, a CLI tool, or an API service.
- Examples in examples/ show basic usage, causal extraction, and evaluation flows.
- The scripts/run_evaluation.py script and data/ directory suggest repeatable offline evaluation workflows.
11. Tests and architectural confidence
- tests/test_graph.py, tests/test_reranker.py, and tests/test_pipeline.py map cleanly to the main subsystems.
- That test split mirrors the architecture: graph construction, reranking logic, and end-to-end orchestration.
- For a diagram, those test targets are good indicators of the package’s main boundaries.
12. Key architectural idea to diagram
- The distinctive feature is the two-track knowledge representation: semantic retrieval via vectors and causal reasoning via a graph.
- The pipeline does not replace RAG; it augments it by inserting causal extraction, causal path retrieval, and causal reranking before generation.
- The most important arrows in the diagram are: documents to vector index and causal graph; query to hybrid retrieval; candidates to reranker; reranked context and causal paths to LLM prompt construction; output answer back to API/CLI caller.
Main entry points: pipeline.py CausalRAG · hybrid.py Hybrid retriever · prompt_builder.py Prompt builder · builder.py Graph builder
Components 17
Orchestration
End-to-end pipeline and public entrypoints
- CausalRAG · Coordinates indexing, retrieval, reranking, and generation
causalrag/pipeline.py
Retrieval
Semantic, keyword, and hybrid candidate retrieval
- Vector index · FAISS-backed semantic similarity store
causalrag/retriever/vector_store.py - BM25 · Lexical retrieval for keyword-style matching
causalrag/retriever/bm25_retriever.py - Hybrid retriever · Combines semantic and causal evidence for candidate selection
causalrag/retriever/hybrid.py - Reranker API · Defines the reranking contract
causalrag/reranker/base.py - Causal rerank · Ranks candidates by alignment with causal paths and graph structure
causalrag/reranker/causal_path.py
Causal graph
Causal extraction, path retrieval, and explanation
- Graph builder · Turns text into causal triples and stores the causal graph
causalrag/causal_graph/builder.py - Path retriever · Finds causal paths relevant to a query
causalrag/causal_graph/retriever.py - Explainer · Interprets or visualizes causal graph structure
causalrag/causal_graph/explainer.py
Generation
Prompt assembly and LLM access
- Prompt builder · Builds prompts from templates and retrieved evidence
causalrag/generator/prompt_builder.py - LLM interface · Abstracts provider access, typically OpenAI-compatible
causalrag/generator/llm_interface.py - Templates · Prompt text used by generation
causalrag/templates/causal_prompt.txt
Surfaces
CLI and API exposure
Connections 19
- CLI → CausalRAG: runs · CLI invokes the end-to-end pipeline
- API → CausalRAG: serves · API wraps the same orchestration boundary
- CausalRAG → Graph builder: indexes · Builds causal graph artifacts from documents
- CausalRAG → Vector index: indexes · Builds semantic index for retrieval
- CausalRAG → Hybrid retriever: queries · Uses hybrid candidate retrieval at query time
- Hybrid retriever → Vector index: uses · Pulls semantic candidates
- Hybrid retriever → BM25: uses · Pulls lexical candidates
- Hybrid retriever → Path retriever: uses · Adds causal path evidence
- Graph builder → Path retriever: feeds · Constructed graph supports causal path lookup
- Hybrid retriever → Causal rerank: passes · Candidate set is filtered by causal relevance
- Causal rerank → Reranker API: implements · Concrete reranker follows the interface
- CausalRAG → Prompt builder: assembles · Builds the final prompt from reranked evidence
- Prompt builder → Templates: uses · Prompt text comes from template files
- Prompt builder → LLM interface: calls · Submits prompt to the model adapter
- LLM interface → CausalRAG: returns · LLM output is folded into the pipeline response
- CausalRAG → Evaluator: evaluates · Outputs can be scored offline
- Evaluator → I/O utils: loads · Reads datasets and references
- CausalRAG → Logging: logs · Cross-cutting tracing across stages
- Explainer → Graph builder: inspects · Explainer operates on the causal graph representation