Semantic Memory and Knowledge Graphs
Duration: 15 min
Semantic Memory and Knowledge Graphs
Duration: 15 min
Overview
Semantic memory stores structured knowledge - facts, relationships, and concepts that an agent knows to be true. Knowledge graphs are the primary data structure: they represent facts as (subject, predicate, object) triples that can be traversed, queried, and reasoned over. This module covers building knowledge graphs for agent memory and querying structured knowledge to enhance reasoning.
Key Concepts & Foundations
- What: Semantic memory stores factual knowledge as structured relationships between entities
- Why: Agents need organized factual knowledge to answer questions and maintain consistent world models
- How: Extract entities and relationships from text, store as graph triples, query with traversal
Knowledge Graph Basics
A knowledge graph stores facts as triples:
(Sarah, works_at, TechCorp)
(Sarah, knows, Python)
(TechCorp, industry, AI/ML)
Why Knowledge Graphs Over Vector Search
| Aspect | Knowledge Graph | Vector Store | |--------|----------------|--------------| | Query type | Structured, exact | Fuzzy, semantic | | Relationships | Explicit, traversable | Implicit | | Reasoning | Multi-hop (A->B->C) | Single similarity | | Updates | Precise (change one fact) | Re-embed entire memory |
Hands-On Implementation
from collections import defaultdict
from dataclasses import dataclass, field
from datetime import datetime
@dataclass
class Triple:
subject: str
predicate: str
obj: str
confidence: float = 1.0
def __str__(self):
return f"({self.subject}, {self.predicate}, {self.obj})"
class KnowledgeGraph:
def __init__(self):
self.triples: list[Triple] = []
self._by_subject: dict[str, list[Triple]] = defaultdict(list)
self._by_object: dict[str, list[Triple]] = defaultdict(list)
def add(self, subject: str, predicate: str, obj: str, confidence=1.0) -> Triple:
for t in self._by_subject.get(subject, []):
if t.predicate == predicate and t.obj == obj:
t.confidence = max(t.confidence, confidence)
return t
triple = Triple(subject, predicate, obj, confidence)
self.triples.append(triple)
self._by_subject[subject].append(triple)
self._by_object[obj].append(triple)
return triple
def query(self, subject=None, predicate=None, obj=None) -> list[Triple]:
results = self.triples
if subject:
results = [t for t in results if t.subject == subject]
if predicate:
results = [t for t in results if t.predicate == predicate]
if obj:
results = [t for t in results if t.obj == obj]
return results
def get_entity_facts(self, entity: str) -> list[Triple]:
return self._by_subject.get(entity, []) + self._by_object.get(entity, [])
def to_context_string(self, entity: str) -> str:
facts = self.get_entity_facts(entity)
if not facts:
return f"No known facts about {entity}."
lines = [f"Known facts about {entity}:"]
for t in facts:
if t.subject == entity:
lines.append(f" - {entity} {t.predicate} {t.obj}")
else:
lines.append(f" - {t.subject} {t.predicate} {entity}")
return "\n".join(lines)
Demo: Build knowledge graph from conversation
kg = KnowledgeGraph()
kg.add("user", "name", "Sarah")
kg.add("user", "role", "ML Engineer")
kg.add("user", "works_at", "DataFlow Inc")
kg.add("user", "knows", "Python")
kg.add("user", "knows", "PyTorch")
kg.add("user", "learning", "Rust")
kg.add("DataFlow Inc", "industry", "healthcare AI")
kg.add("DataFlow Inc", "uses", "AWS")
kg.add("PyTorch", "is_a", "ML framework")print("What does the user know?")
for t in kg.query(subject="user", predicate="knows"):
print(f" {t.obj}")
print("\n" + kg.to_context_string("DataFlow Inc"))
print("\nMulti-hop: user's tech stack categories")
for t in kg.query(subject="user", predicate="knows"):
related = kg.query(subject=t.obj, predicate="is_a")
for r in related:
print(f" {t.obj} -> {r.obj}")
Advanced Techniques
1. LLM-Based Entity Extraction - Use an LLM to extract triples from conversation automatically 2. Conflict Resolution - When new facts contradict old ones, decide by recency or confidence 3. Schema Enforcement - Define allowed predicates and entity types for consistency 4. Graph Embeddings - Combine graph structure with vector embeddings for hybrid retrieval 5. Incremental Updates - Update the graph in real-time as new information appears
Quiz
Q1: What makes knowledge graphs better than flat text for factual agent memory?
- A) They use less disk space
- B) They make relationships explicit and enable multi-hop reasoning ✓
- C) They are easier to implement
- D) They work without any indexing
Q2: What is a knowledge graph triple?
- A) Three separate databases
- B) A (subject, predicate, object) fact representation ✓
- C) A three-layer neural network
- D) A backup of three copies
Q3: When should you prefer a knowledge graph over a vector store?
- A) When you need fuzzy text matching
- B) When you need exact factual queries and multi-hop relationship traversal ✓
- C) When storage space is unlimited
- D) When the agent only handles one conversation