CodeAlchemy

Jotting one man's journey through software development, programming, and technology


Project maintained by pablogarciaprado Hosted on GitHub Pages — Theme by mattgraham

Knowledge Graphs

◀️ Home

What is a Knowledge Graph?

A knowledge graph is a database that stores information in nodes and relationships.

Both nodes and relationships can have properties. Nodes can be given labels, which are a way of grouping multiple nodes together. Relationships always have a type and a direction.

How to Build a Knowledge Graph

1. Start with your use case

If you don’t anchor the graph in real questions, it’ll turn into an expensive diagram.

Examples:

This step comes from principles in Knowledge Representation. You’re modeling reality for a purpose, not for completeness.

2. Identify entities and relationships

Entities (nodes): these are the “things”.

Relationships (edges): connect entities:

Example:

Pablo ──works_at──> CompanyX
Pablo ──manages──> Olbap

Keep it simple at first. Start with a Minimum Viable Graph (MVG), then extract, enhance, expand, and repeat to grow the graph. Over-modeling is a common mistake.

3. Define a schema (ontology)

This is your structure:

4. Assign unique identifiers

Every entity should have a unique ID.

5. Ingest your data

This is where most of the real work happens.

6. Store it in a graph database

You need a system designed for relationships.

Popular options that let you query relationships efficiently:

Neo4j is a graph database management system designed to store, query, and analyze data as networks of nodes and relationships rather than in traditional tables.

7. Query the graph

Instead of SQL, you’ll use graph query languages like Cypher (Neo4j).

Example query:

“Find all employees managed by Pablo who work in Sales”

8. Connect it to your AI assistant

Use the graph to answer structured queries directly and/or combine it with RAG.

RAG vs Knowledge Graphs

RAG (Retrieval-Augmented Generation)

Built on vector search, typically with embeddings from Natural Language Processing. It’s best at:

Strengths

Weak spots

Knowledge Graph

Structured data with entities and relationships (nodes + edges), often used in Knowledge Representation. It’s best at:

Strengths

Weak spots

A practical decision framework

1. What does your data look like?

2. What kind of questions will users ask?

3. Do relationships matter deeply?

If your assistant needs to understand chains like:

then knowledge graphs shine.

If it’s mostly:

RAG is enough.

4. How important is correctness vs speed?

Hybrid approach

Often the sweet spot is not having to pick one:

Use RAG for:

Use knowledge graph for:

Let the assistant decide which to query.

Graph → precise relationships RAG → unstructured knowledge

Concrete examples

RAG:

Knowledge graphs:

Interesting Resources