Agentic AI Notebook
RAG Engineering
Phase 3Module 19 of 22

Knowledge Graphs

Vector RAG finds similar paragraphs. It struggles when the answer is a chain: who owns whom, who reports to whom, which clause modifies which contract. A graph is the map of those links.

Chunks are sticky notes. A knowledge graph is the org-chart on the wall — you can see how people and companies connect, not just which notes mention similar words.

Visual Workflows

Start here — scroll inside each diagram frame to explore, then use + / to zoom up to 200% if needed.

Overview

100%
Loading diagram...

Scroll inside the frame to explore · use + / − to zoom up to 200%

100%
Loading diagram...

Scroll inside the frame to explore · use + / − to zoom up to 200%

Build a graph from docs

100%
Loading diagram...

Scroll inside the frame to explore · use + / − to zoom up to 200%

Extract triples, resolve entities, then store.

Ask a graph

100%
Loading diagram...

Scroll inside the frame to explore · use + / − to zoom up to 200%

Traversal answers 'who owns what' better than cosine on a paragraph.

Key Takeaways

  • 1.A knowledge graph stores entities as nodes and relationships as edges — usually triples like Acme —acquired→ Beta — so facts that live across documents can be joined.
  • 2.Extract entities and relations from text (LLM or NER), store them in Neo4j, FalkorDB, or RDF.
  • 3.Give the graph a simple ontology (Person, Company, acquired, reports_to) so extraction stays consistent.
  • 4.Properties sit on nodes and edges (date, source doc).
  • 5.Graphs do not replace embeddings — many systems keep both: vectors for prose, graph for structure.

Real Example

Scenario

Ten press releases mention acquisitions with different spellings. The graph links Acme → Beta → Gamma so 'who did Acme buy in Europe?' can walk instead of hoping one chunk lists the whole chain.

What you would do

In RAG Engineering, apply Knowledge Graphs to this scenario: Ten press releases mention acquisitions with different spellings. Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.

Practice Task

Open the Code Walkthrough below and run it locally. Change one parameter related to Knowledge Graphs (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.

Code Walkthrough

Highlighted lines show where Knowledge Graphs happens in the code.

Knowledge Graphs
1# Knowledge Graphs — minimal example2from openai import OpenAI3
4client = OpenAI()  # create API client5
6# Ask the model to explain this topic7response = client.chat.completions.create(  # core API call for Knowledge Graphs8    model="gpt-4o-mini",9    messages=[10        {"role": "system", "content": "You explain knowledge graphs clearly."},11        {"role": "user", "content": f"What is knowledge graphs?"},12    ],13    temperature=0,14)15print(response.choices[0].message.content)  # show output for debugging

Commands

Commands to Remember

  • Triple: (subject, relation, object) # Acme -acquired-> BetaCorp
  • Graphs shine when the answer hops across documents
  • You still need entity resolution or the graph fragments
  • Many teams keep vectors for text AND a graph for relations

Cheat Sheet

Quick recap

quick ref
  • Nodes = entities, edges = relations
  • Triple: subject, relation, object
  • Ontology keeps types consistent
  • Graph + vectors is the usual mix

Common Mistakes

  • Dumping raw strings as nodes — duplicates everywhere
  • No ontology — every extract invents new relation names
  • Replacing vector RAG entirely when most questions are still paragraph lookup