File size: 11,499 Bytes
cb4c15b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | # Architecture Deep Dive β GraphRAG Inference System
## System Overview
```
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β GraphRAG Inference System β
β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β React Dashboard (Port 5173) β β
β β ββββββββββββββββββ ββββββββββββββββββ ββββββββββββββββββββββββββ β β
β β β Query Input β β Token BarChart β β Accuracy RadarChart β β β
β β β Sample Q's β β (Recharts) β β (LLM-as-Judge) β β β
β β βββββββββ¬βββββββββ ββββββββββββββββββ ββββββββββββββββββββββββββ β β
β β β POST /query/compare β β
β ββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β
β ββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β FastAPI Server (Port 8000) β β
β β β β
β β ββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββββββββ β β
β β β Pipeline 1 β β Pipeline 2 β β Pipeline 3 β β β
β β β LLM Only β β Basic RAG β β GraphRAG β β β
β β β β β β β β β β
β β β β’ sys promptβ β β’ embed query β β β’ extract entities β β β
β β β β’ question β β β’ FAISS search β β β’ TG traversal β β β
β β β β’ LLM call β β β’ top-K chunks β β β’ serialize graph β β β
β β β β β β’ LLM call β β β’ LLM call β β β
β β ββββββββ¬ββββββββ βββββββββ¬βββββββββββ ββββββββββββ¬ββββββββββββ β β
β β β β β β β
β β ββββββββββββββββββββ΄βββββββββββββββββββββββββββ β β
β β β β β
β β ββββββββββΌβββββββββ β β
β β β LLM-as-Judge β β β
β β β + Metrics Agg β β β
β β ββββββββββ¬βββββββββ β β
β ββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββ β
β β β
βββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββ΄ββββββββββββββββββ
β β
ββββββββββββΌβββββββββββ ββββββββββββΌβββββββββββ
β FAISS Vector DB β β TigerGraph Savanna β
β (in-memory/pkl) β β Knowledge Graph β
β β β β
β chunks[] β β Entity vertices β
β embeddings[] β β RELATED_TO edges β
β IndexFlatIP β β MENTIONED_IN edges β
βββββββββββββββββββββββ ββββββββββββββββββββββββ
β
ββββββββββββΌβββββββββββ
β OpenAI API β
β (LLM + Embeddings) β
βββββββββββββββββββββββ
```
---
## Component Details
### 1. Ingestion Pipeline (`backend/graph/ingestion.py`)
Converts raw documents into the knowledge graph:
```
Raw Docs (.txt/.md)
β
βΌ chunk_text() β token-aware, 512 tokens + 64 overlap
Document Chunks
β
βΌ extract_entities_and_relations() β GPT-4o-mini JSON extraction
Entities + Relationships
β
βββΆ get_embeddings_batch() β text-embedding-3-small
β β
β βΌ
β Entity embeddings (for future vector-on-graph hybrid)
β
βββΆ tg.upsert_entity() β Entity vertex in TigerGraph
βββΆ tg.upsert_document() β Document vertex in TigerGraph
βββΆ tg.upsert_relationship() β RELATED_TO edge
βββΆ tg.link_entity_to_document() β MENTIONED_IN edge
```
Simultaneously, all raw chunks are added to FAISS for Pipeline 2.
### 2. Basic RAG Pipeline (`backend/rag/basic_rag.py`)
```
User Query
β
βΌ openai.embeddings.create() β 1536-dim vector
Query Vector
β
βΌ faiss.IndexFlatIP.search() β cosine similarity
Top-K Chunk Indices (K=5)
β
βΌ Concatenate chunk texts
Context String (~3,000 tokens)
β
βΌ openai.chat.completions.create()
Answer + Token Usage
```
Token overhead: system prompt (~50) + context (~3,000) + question (~50) = ~3,100 prompt tokens
### 3. GraphRAG Pipeline (`backend/rag/graph_rag.py`)
```
User Query
β
βΌ extract_query_entities() β LLM β ["Entity A", "Entity B"]
Seed Entities
β
βΌ tg.get_entity_subgraph() β GSQL multi-hop traversal
Subgraph {entities[], relationships[], documents[]}
β
βΌ serialize_subgraph() β compact structured text
Graph Context (~400-800 tokens)
β
βΌ openai.chat.completions.create()
Answer + Token Usage
```
Token overhead: system prompt (~60) + graph context (~600) + question (~50) = ~710 prompt tokens
**Token reduction: ~77%**
### 4. LLM-as-a-Judge (`backend/llm/judge.py`)
```
Question + Answer + Ground Truth
β
βΌ GPT-4o-mini with JSON response format
{
"score": 8,
"correctness": 9,
"completeness": 7,
"clarity": 8,
"relevance": 8,
"reasoning": "..."
}
```
Run for both Basic RAG and GraphRAG answers. Used for per-query comparison and aggregate pass rate.
### 5. BERTScore Evaluation (`evaluation/benchmark.py`)
```python
from bert_score import score as bert_score_fn
P, R, F1 = bert_score_fn(
predictions=[graph_answers],
references=[ground_truths],
lang="en",
model_type="distilbert-base-uncased"
)
```
Target: F1 rescaled β₯ 0.55 (raw β₯ 0.88) for bonus points.
---
## Knowledge Graph Schema
### Vertices
| Type | Key Attributes | Purpose |
|------|---------------|---------|
| Entity | name, entity_type, description, embedding | Core knowledge nodes |
| Document | title, content, chunk_index, token_count | Source text chunks |
| Concept | name, category, importance_score | High-level topics |
### Edges
| Type | Direction | Attributes | Purpose |
|------|-----------|-----------|---------|
| RELATED_TO | Entity β Entity | relation_type, confidence, context | Core relationships |
| MENTIONED_IN | Entity β Document | frequency, relevance_score | Entity-source links |
| HAS_CONCEPT | Document β Concept | weight | Topic tagging |
| CO_OCCURS_WITH | Entity β Entity | co_occurrence_count | Statistical co-occurrence |
---
## Subgraph Serialization Format
The key to token efficiency is our compact serialization format:
```
## ENTITIES
β’ [Entity Name] [TYPE]: description text
## RELATIONSHIPS
β’ Entity A β[RELATION_TYPE]β Entity B (conf: 0.95) | context snippet
## SUPPORTING TEXT (top-1 doc, max 300 chars)
[Document Title]: first 300 characters of relevant chunk...
```
This format is:
- Hierarchically structured (LLM can parse with zero instruction)
- Non-redundant (each fact stated once)
- Confidence-weighted (LLM can trust high-confidence facts more)
- Bounded (caps prevent runaway token usage)
---
## API Endpoints
| Method | Path | Description |
|--------|------|-------------|
| GET | /health | Server health + TG connection status |
| POST | /query/compare | Run all 3 pipelines, return full metrics |
| GET | /stats/session | Cumulative session token savings |
| GET | /graph/stats | TigerGraph vertex/edge counts |
| POST | /ingest/text | Quick-ingest a document |
---
## Performance Characteristics
| Operation | Typical Latency |
|-----------|----------------|
| Query embedding (OpenAI) | 150β300ms |
| FAISS search (100K chunks) | <10ms |
| Entity extraction (LLM) | 300β600ms |
| TigerGraph GSQL traversal | 50β200ms |
| LLM generation (gpt-4o-mini) | 800β2000ms |
| LLM Judge evaluation | 300β600ms |
| **Total: Basic RAG** | **~1,500β2,500ms** |
| **Total: GraphRAG** | **~1,000β1,800ms** |
GraphRAG's latency advantage comes from:
1. No FAISS search needed (graph traversal replaces it)
2. Smaller context = faster LLM generation
3. No chunk deduplication step needed
---
## Scaling Considerations (Round 2)
For 50-100M token datasets:
- TigerGraph Savanna handles billions of edges natively β no schema changes needed
- FAISS upgrades to `IndexIVFFlat` or `HNSW` for ANN search at scale
- Ingestion parallelized with async batch processing
- Entity extraction cached β same entities aren't re-extracted across document runs
- Community detection (TigerGraph built-in) adds global context layer for broad queries
|