How do you implement RAG with knowledge graphs for enhanced context?
Knowledge graph RAG (GraphRAG) combines structured knowledge relationships with vector retrieval for richer context. Architecture: build a knowledge graph from documents (entities as nodes, relationships as edges), store entity descriptions and relationship descriptions as text, embed these descriptions for vector retrieval, and traverse the graph to gather connected context. Graph construction: extract entities and relationships using NLP (NER + relation extraction) or LLM-based extraction, resolve entity coreferences (different mentions of the same entity), and build a property graph with typed edges. Microsoft's GraphRAG approach: segment documents into text chunks, extract entities and relationships using an LLM, build community structures using Leiden algorithm for hierarchical clustering, generate community summaries at multiple levels, and use these summaries for global queries that need broad knowledge synthesis. Retrieval: for specific factual queries, start at the most relevant entity and traverse local neighbors for context (local search). For broad thematic queries, use community summaries to synthesize knowledge across the entire corpus (global search). Implementation: store the knowledge graph in a graph database (Neo4j, or as tables in PostgreSQL), maintain vector indexes on entity/relationship descriptions, implement graph traversal algorithms for context expansion, and combine graph-retrieved context with traditional chunk retrieval. Benefits: better handling of questions requiring multi-hop reasoning (A is related to B, B causes C, what does A cause?), improved entity disambiguation, and structured representation enables precise relationship queries. Trade-offs: graph construction is expensive (LLM calls for extraction), maintenance is complex (keeping graph synchronized with source documents), and construction quality depends heavily on the extraction model.