Knowledge Graphs for LLM Applications
What is a Knowledge Graph? A knowledge graph represents information as entities (nodes) and relationships (edges), enabling structured reasoning and fact storage.
Structure
[Entity: Eiffel Tower]
|
|-- Type: Landmark
|-- Location: Paris
|-- Height: 330m
|-- Built: 1889
|
+-- (located_in) --> [Entity: France]
|
+-- (designed_by) --> [Entity: Gustave Eiffel]
Knowledge Graph + LLM Approaches
RAG with Knowledge Graph
def kg_rag(query: str) -> str:
# Extract entities from query
entities = extract_entities(query)
# Query knowledge graph for related facts
facts = []
for entity in entities:
facts.extend(kg.get_triplets(entity, hops=2))
# Format as context
context = format_triplets(facts)
return llm.generate(f"""
Context from knowledge base:
{context}
Question: {query}
Answer:
""")
Graph-Guided Retrieval Use KG structure to improve retrieval:
def graph_guided_retrieval(query: str) -> list:
# Get relevant entities
entities = kg.search_entities(query)
# Expand via graph relations
expanded = set()
for entity in entities:
expanded.add(entity)
expanded.update(kg.get_neighbors(entity))
# Retrieve documents for all entities
docs = []
for entity in expanded:
docs.extend(doc_store.search(entity.name))
return docs
Building Knowledge Graphs
From Text (LLM Extraction)
def extract_triplets(text: str) -> list:
result = llm.generate(f"""
Extract entity relationships from this text as triplets:
(subject, relation, object)
Text: {text}
Triplets:
""")
return parse_triplets(result)
Schema Example
entities = ["Person", "Organization", "Location", "Product"]
relations = ["works_at", "located_in", "founded_by", "produces"]
Graph Databases
| Database | Type | Features |
|---|---|---|
| Neo4j | Native graph | Cypher query, LLM integrations |
| Amazon Neptune | Managed | SPARQL/Gremlin |
| TigerGraph | Distributed | High performance |
| NebulaGraph | Open source | Scalable |
Use Cases
| Use Case | Benefit |
|---|---|
| Entity-rich domains | Structured fact storage |
| Multi-hop reasoning | Follow relationships |
| Explainability | Trace fact sources |
| Data consistency | Structured updates |
Knowledge graphs complement vector search by providing structured, explainable facts.
knowledge graphkgentityrelation
Related Topics
Explore 500+ Semiconductor & AI Topics
From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.