RAG/graphrag/ — Entity Graphs & Relationships
Entity extraction, relationship modeling, community detection
General GraphRAG (Microsoft), LightRAG, Leiden community detection, entity resolution, graph search.
❓ What is this & why?
The graphrag module builds entity-relationship graphs from extracted entities. Leiden community detection finds natural clusters (e.g., 'neural networks' community). This enables graph-based reasoning: 'if you're interested in X, you might care about Y' (via the knowledge graph).
💡 Real-world analogy
Like a social network. Instead of treating documents as isolated chunks, the graph module builds a web of connections between concepts (entities). When you search for a person, you not only find documents about them but also their friends, collaborators, and communities.
🤔 Why Is It Designed This Way?
{{DESIGN_RATIONALE}} — 150+ words explaining the design choices: what pattern does this follow and why? What constraints shaped the implementation? What trade-offs were made and what was sacrificed?
| Design Choice | What Was Chosen | What Was Rejected | Why |
|---|---|---|---|
| Example: data layout | Array of structs | Struct of arrays | Random access pattern dominates; cache locality for single items matters more than SIMD sweeps |
Contents
Architecture
flowchart LR
subgraph Extract["Entity & Relation Extraction"]
E["LLM Extraction"]
end
subgraph Detect["Community Detection"]
D["Leiden Algorithm"]
end
subgraph Resolve["Entity Resolution"]
R["Dedup & Link"]
end
subgraph Store["Graph Database"]
S["Entity Store"]
end
Extract --> Detect
Detect --> Resolve
Resolve --> Store
Key Classes
| Class / Function | File | Purpose |
|---|---|---|
ClassName | file.h | Brief description |
Data Structures
| Type | Name | Description |
|---|---|---|
| enum | EnumName | Brief description |
Interactions
| Direction | Partner | Interface | Description |
|---|---|---|---|
| → | Other Module | methodName() | Brief description |
Configuration
| Knob / Parameter | Default | Description |
|---|---|---|
param_name | value | What it controls |
Directory Map
Source tree for graphrag
submod_name/
├── file_a.ext ← Role: brief annotation
├── file_b.ext ← Role: brief annotation
├── file_c.ext ← Role: brief annotation
└── index.ext ← Role: entry point
📑 Annotated Code Walkthrough
The most important code in this sub-module, annotated with WHY each piece exists:
{{KEY_FUNCTION_NAME}}
// Replace with actual code from the source
void key_function() {
// ← WHY: Explain the purpose of this line
// ← WHY: What constraint or requirement drives this?
}
Line-by-line:
- Line N: What this does and WHY — what would break if removed
- Line M: The design choice here — why this approach over the obvious alternative
⚠️ Edge Cases & Boundaries
| Edge Case | What Happens | Where in Code | Risk Level |
|---|---|---|---|
| Empty input | How it's handled | function_name:line | 🟢 Low |
| Concurrent access | How it's handled | function_name:line | 🔴 High |
Deep-Dive Highlights
Core mechanisms from this sub-module have been extracted into dedicated focus pages for in-depth exploration. Each page covers one algorithm, protocol, or subsystem with diagrams, code walks, and configuration details.
Deep-Dive Pages
Entity Graph Building
sequenceDiagram
participant Chunks as Chunks
participant LLM as LLM Extractor
participant Leiden as Leiden Detector
participant Graph as Knowledge Graph
Chunks->>LLM: Extract entities
LLM->>Leiden: Entity list + edges
Leiden->>Leiden: Cluster
Leiden->>Graph: Communities
Graph->>Chunks: Graph-based retrieval
Deep-Dive Highlights
Code Examples
Example: Typical Usage
// Replace with actual code example
void example() {
// ...
}