πΈοΈ Why Knowledge Graphs + LLMs?
LLMs excel at language understanding but struggle with factual accuracy and structured reasoning. Knowledge graphs provide explicit relationships and verified facts that complement LLM capabilities.
π§ LLM Strengths
- β’ Natural language understanding
- β’ Complex reasoning and inference
- β’ Creative text generation
- β’ Context integration
πΈοΈ Knowledge Graph Strengths
- β’ Structured factual knowledge
- β’ Explicit relationships
- β’ Graph traversal and path finding
- β’ Consistency and verification
The Synergy: KGs provide factual grounding and structured context while LLMs provide natural language interface and complex reasoning over the structured knowledge.
π Knowledge Graph Examples
Corporate Knowledge Graph
Enterprise organizational and operational knowledge
Entities
Key Relationships
- β’ Employee WORKS_IN Department
- β’ Employee HAS_SKILL Technology
- β’ Project REQUIRES_SKILL Technology
Graph Schema
# Corporate Knowledge Graph Schema (Employee)-[:WORKS_IN]->(Department) (Employee)-[:HAS_SKILL]->(Technology) (Employee)-[:MANAGES]->(Project) (Project)-[:REQUIRES_SKILL]->(Technology) (Document)-[:AUTHORED_BY]->(Employee) (Document)-[:BELONGS_TO]->(Project) (Department)-[:OWNS]->(Project)
Use Case: Expert finding, skill gap analysis, project staffing
LLM Integration Example
# LLM + Knowledge Graph Integration
def answer_with_kg(question):
# 1. Extract entities from question
entities = extract_entities(question)
# 2. Query knowledge graph
kg_context = query_knowledge_graph(entities)
# 3. Enhance prompt with KG context
prompt = f"""
Question: {question}
Knowledge Graph Context:
{format_kg_context(kg_context)}
Answer using the provided context:
"""
return llm.invoke(prompt)
π Knowledge Graph Query Types
Entity Queries
Questions about specific entities and their properties
Example Questions
- β’ "Who is John Smith?"
- β’ "What skills does the ML team have?"
- β’ "Which projects use Python?"
Approach
Extract entities β Query direct properties β Format response
π Integration Approaches
Graph-Enhanced RAG
Use KG to improve document retrieval and provide structured context
Steps
- 1. Extract entities from user query
- 2. Query KG for entity relationships and context
- 3. Use KG context to improve document retrieval
- 4. Enhance LLM prompt with both documents and KG context
Benefits
- β’ Better retrieval precision
- β’ Structured context
- β’ Factual grounding
Implementation
class GraphEnhancedRAG:
def __init__(self, kg, vectorstore, llm):
self.kg = kg
self.vectorstore = vectorstore
self.llm = llm
def answer(self, question):
# Extract entities from question
entities = self.extract_entities(question)
# Get KG context
kg_context = self.kg.get_context(entities)
# Enhance retrieval with entities
docs = self.vectorstore.similarity_search(
question,
filter_entities=entities
)
# Create enhanced prompt
prompt = f"""
Question: {question}
Knowledge Graph Context:
{kg_context}
Document Context:
{docs}
Provide a comprehensive answer using both sources:
"""
return self.llm.invoke(prompt)
KG-First Question Answering
Try to answer from KG first, fallback to LLM for complex reasoning
Steps
- 1. Parse question into graph query
- 2. Execute graph traversal/query
- 3. If sufficient, return KG-based answer
- 4. Otherwise, use LLM with KG context
Benefits
- β’ Fast factual answers
- β’ Explainable reasoning
- β’ Cost efficient
Implementation
class KGFirstQA:
def __init__(self, kg, llm):
self.kg = kg
self.llm = llm
def answer(self, question):
# Try to answer from KG first
kg_answer = self.kg.query_direct(question)
if kg_answer.confidence > 0.8:
return format_kg_answer(kg_answer)
# Fallback to LLM with KG context
kg_context = self.kg.get_relevant_context(question)
prompt = f"""
Question: {question}
Knowledge Base Context:
{kg_context}
Reasoning required beyond facts:
"""
return self.llm.invoke(prompt)
Dynamic Graph Construction
Build and update KG from conversations and documents using LLMs
Steps
- 1. Extract entities and relationships from new content
- 2. Use LLM to resolve entity disambiguation
- 3. Update graph with new knowledge
- 4. Validate against existing knowledge
Benefits
- β’ Self-updating knowledge
- β’ Continuous learning
- β’ Personalization
Implementation
class DynamicKGBuilder:
def __init__(self, kg, llm, ner_model):
self.kg = kg
self.llm = llm
self.ner_model = ner_model
def update_from_conversation(self, conversation):
# Extract entities and relationships
entities = self.ner_model.extract(conversation)
relationships = self.extract_relationships(conversation)
# Resolve entities against existing KG
resolved_entities = self.resolve_entities(entities)
# Validate new knowledge
validated = self.validate_knowledge(
resolved_entities, relationships
)
# Update graph
self.kg.add_knowledge(validated)
def extract_relationships(self, text):
prompt = f"""
Extract relationships from this text:
{text}
Format: (Entity1, Relationship, Entity2)
Only extract factual relationships.
"""
return self.llm.invoke(prompt)
π οΈ Tools and Platforms
Neo4j
Graph DatabaseEnterprise-grade graph database with Cypher query language
Strengths
- β’ Mature ecosystem
- β’ ACID compliance
- β’ Rich query language
LLM Integration
Python driver, LangChain integration, vector similarity
Code Example
from neo4j import GraphDatabase
class Neo4jKG:
def __init__(self, uri, user, password):
self.driver = GraphDatabase.driver(uri, auth=(user, password))
def query_entity_context(self, entity_name):
with self.driver.session() as session:
result = session.run("""
MATCH (e:Entity {name: $name})-[r]-(connected)
RETURN e, type(r) as relationship, connected
LIMIT 20
""", name=entity_name)
return [record for record in result]
Amazon Neptune
Cloud Graph DatabaseFully managed graph database supporting both property graphs and RDF
Strengths
- β’ Managed service
- β’ High availability
- β’ Multiple query languages
LLM Integration
Gremlin/SPARQL queries, AWS ecosystem, serverless options
Code Example
from gremlin_python.driver import client
class NeptuneKG:
def __init__(self, endpoint):
self.client = client.Client(
f'wss://{endpoint}:8182/gremlin', 'g'
)
def find_shortest_path(self, start_entity, end_entity):
query = f"""
g.V().has('name', '{start_entity}')
.repeat(both().simplePath())
.until(has('name', '{end_entity}'))
.path()
.limit(1)
"""
return self.client.submit(query).all().result()
LlamaIndex KG
LLM-Native FrameworkKnowledge graph construction and querying optimized for LLMs
Strengths
- β’ LLM integration
- β’ Automatic extraction
- β’ Easy setup
LLM Integration
Built-in LLM support, vector similarity, RAG optimization
Code Example
from llama_index import KnowledgeGraphIndex, SimpleDirectoryReader
# Build KG from documents
documents = SimpleDirectoryReader('docs/').load_data()
# Create knowledge graph index
kg_index = KnowledgeGraphIndex.from_documents(
documents,
max_triplets_per_chunk=2,
include_embeddings=True
)
# Query with natural language
response = kg_index.as_query_engine().query(
"What are the main research areas in our company?"
)
π¨ Knowledge Graph Visualization
π― Key Takeaways
Complementary Strengths: KGs provide factual grounding while LLMs provide natural language understanding and complex reasoning
Query Complexity Matters: Use direct KG queries for simple facts, LLM reasoning for complex analysis
Graph-Enhanced RAG: Combining structured KG context with document retrieval improves accuracy significantly
Dynamic Updates: LLMs can help build and maintain knowledge graphs from conversations and documents
Tool Selection: Choose graph databases based on scale, query complexity, and integration requirements