A True Context Graph implementation for intelligent document understanding β capturing not just what entities exist, but how decisions were made and why.
π― Learning Project - Exploring modern AI engineering: context graphs, decision tracing, causal reasoning, and LLM orchestration.
π° 100% Free - Runs entirely on local models (Ollama) with no API costs.
| Aspect | Knowledge Graph | Context Graph |
|---|---|---|
| Focus | "What is" β static entities & relationships | "How & Why" β decision traces, reasoning paths |
| Structure | Entities β Relationships | Entities + Decisions + States + Evidence |
| Time | Optional timestamps | Core feature β captures world state at each decision |
| Purpose | Answer factual queries | Trace reasoning, replay decisions, learn from precedents |
| Graph Type | General graph | DAG (Directed Acyclic Graph) β causal flow |
A knowledge graph answers: "Who is the CEO of Company X?"
A context graph answers: "Why did the board choose Alice as CEO, what evidence was considered, who else was evaluated, and what was the company's state at that time?"
CONTEXT GRAPH (DAG)
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β βββββββββββββββ β
β β SOURCE β β Document/Evidence that triggered the graph β
β β DOCUMENT β β
β ββββββββ¬βββββββ β
β β extracted_from β
β βΌ β
β βββββββββββββββ based_on βββββββββββββββ β
β β STATE βββββββββββββββββββ EVIDENCE β β
β β (t=2023) β β (facts) β β
β β β βββββββββββββββ β
β β β’ revenue β β
β β β’ headcount β β
β β β’ market β β
β ββββββββ¬βββββββ β
β β led_to β
β βΌ β
β βββββββββββββββ considered βββββββββββββββ β
β β DECISION ββββββββββββββββββββ ALTERNATIVE β β
β β β β (rejected) β β
β β "Acquire β βββββββββββββββ β
β β Startup B" β β
β β ββββββββββββββββββββββββββ β
β ββββββββ¬βββββββ β β
β β resulted_in β rationale β
β βΌ βΌ β
β βββββββββββββββ βββββββββββββββ β
β β ACTION β β REASONING β β
β β β β β β
β β "Signed β β "Strategic β β
β β agreement" β β fit for β β
β ββββββββ¬βββββββ β AI market" β β
β β caused βββββββββββββββ β
β βΌ β
β βββββββββββββββ β
β β OUTCOME β β
β β (t=2024) β β New state after the decision β
β βββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Node Types:
βββ π SOURCE - Document/input that provides information
βββ π STATE - Snapshot of world/context at a point in time
βββ π EVIDENCE - Facts, data, observations supporting decisions
βββ π€ DECISION - A choice point with alternatives considered
βββ β ALTERNATIVE - Options that were NOT chosen (important!)
βββ π REASONING - The "why" behind a decision
βββ β‘ ACTION - What was done as result of decision
βββ π― OUTCOME - Resulting state after action
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β CONTEXTGRAPH PIPELINE β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β 1. INGESTION β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£ β
β β ββββββββββββ ββββββββββββββββ ββββββββββββββββββββββββββββββββββββ β
β β β Document ββββΆβ Chunker ββββΆβ Decision/State Extractor ββ β
β β β Loader β β (Semantic) β β (LLM identifies decision pts) ββ β
β β ββββββββββββ ββββββββββββββββ ββββββββββββββββββββββββββββββββββββ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β 2. GRAPH CONSTRUCTION β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£ β
β β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββββββ β β
β β β DAG Builder ββββΆβ Causal Linker ββββΆβ Temporal Annotator β β β
β β β (nodes/edges) β β (causeβeffect) β β (timestamps/order) β β β
β β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β 3. STORAGE β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£ β
β β βββββββββββββββββββββββ βββββββββββββββββββββββββββββββ β β
β β β NetworkX DAG β β ChromaDB Vectors β β β
β β β (graph structure) β β (semantic search on nodes) β β β
β β βββββββββββββββββββββββ βββββββββββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β 4. QUERY ENGINE β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£ β
β β ββββββββββββββββββ ββββββββββββββββββ ββββββββββββββββββββββββββ β β
β β β Query Analyzer ββββΆβ Path Finder ββββΆβ Reasoning Assembler β β β
β β β(what/why/how?) β β(trace through β β(build explanation from β β β
β β β β β DAG causally) β β decision trace) β β β
β β ββββββββββββββββββ ββββββββββββββββββ ββββββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β 5. RESPONSE GENERATION β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£ β
β β βββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β Ollama (Local LLM) β β β
β β β Llama 3.2 / Mistral / Gemma 2 β β β
β β β β β β
β β β Input: Query + Decision Trace from DAG β β β
β β β Output: Answer with reasoning provenance β β β
β β βββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- π Document Ingestion - PDF, Markdown, TXT files
- π Decision Point Extraction - LLM identifies decisions, states, and reasoning
- π³ DAG Construction - Builds causal graph with proper temporal ordering
- βͺ Decision Replay - Trace back through any decision to understand "why"
- οΏ½ Causal Reasoning - Follow causeβeffect chains through the graph
- οΏ½ Reasoning Provenance - Every answer cites the decision trace that supports it
- π Incremental Updates - Add new documents, graph merges intelligently
- οΏ½ State Diffing - Compare world state before/after decisions
- π― Counterfactual Queries - "What if alternative X was chosen instead?"
- π Decision Confidence - Track extraction confidence and evidence strength
| Component | Technology | Cost |
|---|---|---|
| Language | Python 3.11+ | Free |
| LLM | Ollama (Llama 3.2 / Mistral / Gemma 2) | Free (local) |
| Embeddings | Ollama embeddings or sentence-transformers | Free |
| Graph | NetworkX (DAG) | Free |
| Persistence | SQLite + JSON serialization | Free |
| Vector Store | ChromaDB | Free |
| CLI | Typer | Free |
| Visualization | Pyvis / Graphviz | Free |
| Web UI | Streamlit (optional) | Free |
- Runs completely locally on your machine
- Supports multiple models: Llama 3.2 (8B), Mistral (7B), Gemma 2 (9B)
- No API keys, no usage limits, no costs
- GPU acceleration if available, CPU fallback
ContextGraph/
βββ src/
β βββ ingestion/ # Document processing
β β βββ loader.py # Multi-format document loading
β β βββ chunker.py # Semantic chunking
β β βββ decision_extractor.py # Extract decisions, states, reasoning
β β
β βββ graph/ # DAG operations
β β βββ dag_builder.py # Construct the context DAG
β β βββ causal_linker.py # Link cause β effect relationships
β β βββ temporal.py # Temporal ordering and state tracking
β β βββ node_types.py # Decision, State, Evidence, Action, etc.
β β
β βββ retrieval/ # Query processing
β β βββ query_analyzer.py # Classify: what/why/how queries
β β βββ path_finder.py # Trace causal paths through DAG
β β βββ reasoning_assembler.py # Build explanation from trace
β β
β βββ storage/ # Persistence
β β βββ graph_store.py # NetworkX β SQLite/JSON
β β βββ vector_store.py # ChromaDB for semantic search
β β
β βββ generation/ # Response generation
β β βββ generator.py # Ollama-powered response with provenance
β β
β βββ interfaces/ # User interfaces
β βββ cli.py # Typer CLI
β βββ web.py # Streamlit dashboard (optional)
β
βββ config/
β βββ settings.yaml # Configuration (model selection, etc.)
β
βββ tests/ # Test suite
βββ examples/ # Example documents & queries
βββ README.md
# Initialize (downloads Ollama model if needed)
contextgraph init --model llama3.2
# Ingest a document (builds/updates context graph)
contextgraph ingest ./documents/company_report.pdf
# Query with reasoning trace
contextgraph query "Why did the company acquire Startup B?"
# Visualize the decision graph
contextgraph visualize --output decision_graph.html
# Trace a specific decision
contextgraph trace --decision "acquisition of Startup B"from contextgraph import ContextGraph
# Initialize with local Ollama
cg = ContextGraph(model="llama3.2")
# Ingest documents
cg.ingest("./documents/board_meeting.pdf")
# Query with decision trace
response = cg.query("Why was Alice chosen as CEO?")
print(response.answer) # Natural language answer
print(response.decision_trace) # DAG path: Evidence β State β Decision β Reasoning
print(response.confidence) # How confident based on evidence
print(response.alternatives) # What other options were consideredUser Query: "Why did the company decide to enter the AI market?"
1. Query Analyzer β Detected: "WHY" query about a DECISION
2. Path Finder β Located decision node "Enter AI Market"
β Traced backward through DAG
3. Reasoning Assembler β Built trace:
EVIDENCE: "Competitor X launched AI product (2023-Q2)"
β observed
STATE: "Market share declining 5% YoY"
β led_to
DECISION: "Enter AI market via acquisition"
β rationale
REASONING: "Build vs Buy analysis favored acquisition
due to time-to-market pressure"
β considered
ALTERNATIVES: ["Build in-house team", "Partner with AI startup"]
4. Generator β Produces answer with full provenance
This project covers cutting-edge AI engineering concepts:
- Context Graphs - Beyond knowledge graphs: decision traces, state tracking
- DAG Algorithms - Topological sort, causal path finding, cycle detection
- Temporal Reasoning - State snapshots, event ordering, time-aware queries
- Causal Inference - Modeling causeβeffect in structured graphs
- Reasoning Provenance - Explainable AI with traceable decision paths
- Local LLM Engineering - Ollama setup, prompt design, structured extraction
- Hybrid Retrieval - Combining graph traversal + vector similarity
- Project setup with dependencies
- Ollama integration and model management
- Basic document loader (PDF, TXT, MD)
- Node type definitions (Decision, State, Evidence, etc.)
- Simple CLI with Typer
- Decision/State extraction with LLM
- NetworkX DAG builder
- Causal relationship linking
- Temporal ordering enforcement
- Graph persistence (SQLite/JSON)
- Query type classification (what/why/how)
- Causal path finding algorithms
- ChromaDB integration for semantic search
- Reasoning assembler (build explanations from traces)
- Response generation with Ollama
- Graph visualization (Pyvis)
- Incremental graph updates
- State diffing (before/after comparisons)
- Counterfactual reasoning ("what if X instead?")
- Streamlit web dashboard
- Confidence scoring
- Multi-document temporal alignment
- Graph export formats
- Comprehensive test suite
- Documentation
- Context Graphs: The Foundation of Enterprise AI
- Decision Traces in AI Systems
- DAG-based Reasoning for LLMs
- LangGraph: State Management for Agents
- Ollama Documentation
- Python 3.11+
- Ollama - Install from ollama.ai
# After installing Ollama, pull a model ollama pull llama3.2 - ~8GB RAM minimum for running local LLMs
MIT License - Built for learning. Modify freely!