# Why Your Agent’s Brain Needs a Playbook: Practical Wins from Using Ontologies

Jesús Barrasa, Neo4j

AI Engineer World's Fair 2025

[Talk page](https://ai.engineer/talks/CbiR9xS2skQ-why-your-agents-brain-needs-playbook-practical) · [Video](https://www.youtube.com/watch?v=CbiR9xS2skQ)

## Why Your Agent’s Brain Needs a Playbook: Practical Wins from Using Ontologies

GraphRAG needs more than connected data: it needs a model that guides construction and tells retrievers which connections should become context.

Basic familiarity with RAG, embeddings and Python will help; graph concepts and the role of Cypher traversal are explained as they appear.

### What should retrieval add to an LLM’s context?

[0:26](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=26s)

A user sends a prompt to an AI application. Before asking the LLM to respond, the application retrieves relevant information from a controlled, curated knowledge base. The immediate engineering question is what that retrieval can return: only semantically similar text, or also the connections that make the text useful? Jesús Barrasa, Neo4j’s AI Field CTO, introduces ontologies through this problem of building and retrieving from a knowledge graph.

In Barrasa’s simplified GraphRAG architecture, a knowledge graph on a graph database takes the place of the vector database. The surrounding flow remains familiar: retrieve information, put it into the model’s context window, and generate a grounded response. This is one architecture, rather than a requirement to eliminate vector storage; graph retrieval can incorporate vector indexes and external vector stores.

![GraphRAG diagram connecting a user prompt, GenAI application, LLM and response, with retrieval arrows between the application and Neo4j’s knowledge graph.](https://ai.engineer/api/talk-essay-media/f06c6c2b4f6ec5345eec3d7533714e9139b37c58c043b91bb7a71610beeda47e)

GraphRAG retrieves knowledge graph context before generating a response.

The graph expands the available retrieval strategies:

- **Vector search** finds text with similar meaning.
- **Graph contextualization** follows connections around those matches.
- **Generated structured queries** retrieve information through the graph’s explicit model.

Barrasa describes improvements in completeness, relevance, precision and faithfulness, but supplies no benchmark measurements. The mechanism to examine is the richer retrieval process itself.

### Connect entities to the text that mentions them

[2:43](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=163s)

Neo4j’s **property graph** has two main primitives: nodes and directed relationships. Nodes represent entities—people, objects, locations, events or documents—and key-value properties characterize them with names, descriptions, dates of birth and other values. Together, these entities and their connections form the **domain graph**.

When the source is unstructured text, a second layer records the documents from which the domain graph was extracted. This **lexical graph** might represent a document as a sequential list of chunks. A contract may need a richer tree containing sections, subsections, definitions and clauses. Entity extraction then connects a chunk to the entities it mentions, preserving where a fact or reference appeared so retrieval can use that connection later.

![Two-layer graph showing a person linked to a place above document and chunk nodes, with mention links connecting the layers.](https://ai.engineer/api/talk-essay-media/1be4d945be47ea69865d6bc92e888f6a8abb2e8b01e7d2f8725cf926e8bd1137)

Domain entities connect to document chunks in the lexical layer.

A chunk node can retain both its raw text and an embedding. Vectors are properties alongside strings and numbers, and they can be indexed. That puts semantic search and connected document structure in the same platform: the vector locates a useful chunk, while the graph retains its document context and entity links.

### Two ingestion paths, one schema problem

[4:42](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=282s)

Before any of this retrieval is possible, the graph has to be built. With structured data, the pipeline starts from database tables or files and a description of the target graph. Records are mapped to node types; other records or fields define relationships. Neo4j’s visual interface lets a developer define that model and connect it to source data, with an underlying API supporting programmatic construction. The target schema is an input, not something the mapping can avoid.

The unstructured pipeline in Neo4j’s GraphRAG package follows a different sequence. Barrasa describes the LangChain and LlamaIndex integrations as broadly similar:

1. Process and split the document.
2. Embed its chunks.
3. Extract entities using an injected schema.
4. Write the resulting information to the graph.

The extraction schema tells the system what matters in this domain. Medical documents might call for patients and symptoms; financial documents might call for account holders, accounts and money transfers.

Both paths need a model, but their interfaces may express it differently.

| Schema representation | What it describes |
| --- | --- |
| JSON | Entity and relationship definitions |
| Pydantic | Typed descriptions of entities to extract |
| Natural-language prompt | Instructions about relevant entities |

Maintaining those descriptions independently introduces divergence. The desired shared artifact is a domain model that does not belong exclusively to one ingestion tool or prompting interface.

### Use an ontology as the shared domain model

[6:56](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=416s)

An **ontology** supplies that shared, formal, implementation-agnostic description of a domain. Standards, including W3C standards, provide ways to define it independently of a particular graph-loading interface. Serialization is a separate choice: Barrasa shows XML, jokes about using it in 2025, and points out that JSON-based and other representations are also available.

His example comes from the public [Financial Industry Business Ontology (FIBO)](https://spec.edmcouncil.org/fibo/index.html). The fragment contains classes for a privately held company and a stock corporation, connected through a subclass relationship. It also describes a different kind of connection: a stock corporation is governed by a board agreement. These are the kinds of entity categories and relationships needed to guide graph construction. The example is historical; today’s [Corporations ontology source](https://github.com/edmcouncil/fibo/blob/master/BE/Corporations/Corporations.rdf) deprecates some of those identifiers in favor of equivalents in CorporateBodies.

**The same domain model can guide both structured mapping and unstructured extraction.** Barrasa advocates model-driven construction not just as data-engineering practice, but because a well-described graph remains useful during retrieval. A text-to-structured-query system has a clearer model from which to generate queries, and vector search has an explicit structure to use for contextualization. He points to an ontology-guided ingestion example without walking through it, then turns to the retrieval mechanism.

### Use vector matches as entry points into the graph

[9:08](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=548s)

Embedding the graph’s text-bearing nodes creates another search space. A query is embedded into a point in that space, and nearby vectors identify semantic matches. But the matched vectors are still properties of graph nodes. The retriever can therefore dereference each match back to its node and continue through the graph.

That return to the graph is where contextualization happens. The retriever can navigate relationships, enrich a match with connected information, filter results or aggregate them before passing context to the LLM. **Vector proximity selects a starting point; graph traversal determines what accompanies it.** The completed visual shows both stages: the search region in vector space and exploration among the connected nodes above it.

![A graph suspended above a vector-space plane, with green highlights connecting a search region to nodes and relationships above.](https://ai.engineer/api/talk-essay-media/be01c5ba47fc47a833bc8db2a08bb3ad758d4f3cf24d391678d565e5e233c16d)

Vector matches lead back to graph nodes and related context.

### Retrieve a movie, then add its actors

[10:06](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=606s)

The Python demonstration makes the two stages concrete. Movie plots have been embedded and placed in a vector index. The search describes a hacker who discovers that the world he knows is a simulated reality controlled by machines—the recognizable premise of *The Matrix*. Barrasa requests the top three results; three is his chosen retrieval count, not an accuracy result or an optimum established by evaluation.

The retriever wraps the vector index and accepts a retrieval query. That query supplies the exploration logic to run for each vector match. The following Python expresses the pattern using the documented [`VectorCypherRetriever`](https://neo4j.com/docs/neo4j-graphrag-python/current/user_guide_rag.html) interface, whose retrieval query receives `node` and `score`; it is an implementation illustration, not a reconstruction of the historical slide’s exact package version. Here, `moviePlots` names the plot index, and `ACTED_IN` connects a person to a movie.

```python
from neo4j_graphrag.retrievers import VectorCypherRetriever


def search_movies(driver, embedder):
    retriever = VectorCypherRetriever(
        driver=driver,
        index_name="moviePlots",
        embedder=embedder,
        retrieval_query="""
        OPTIONAL MATCH (actor:Person)-[:ACTED_IN]->(node)
        RETURN node.title AS title,
               node.plot AS plot,
               collect(DISTINCT actor.name) AS actors,
               score
        """,
    )
    return retriever.search(
        query_text=(
            "A hacker discovers that the world he knows is a "
            "simulated reality controlled by machines."
        ),
        top_k=3,
    )
```

The vector search chooses movies by plot similarity. The Cypher query adds actors to each selected movie’s context.

This is powerful, but the author of the retriever must already know the graph model: actors act in movies, and movies have plot properties. In the code, that knowledge appears directly in `ACTED_IN`, `Person`, `title` and `plot`. Part of the contextualization policy is now hard-coded into the retriever. Changing which relationships should contribute context requires addressing that embedded traversal logic.

### Move traversal policy into the stored ontology

[11:40](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=700s)

The next step is to persist the ontology in the graph alongside the movie data. The database then contains both the domain records and a description of their schema. A dynamic query can consult that description before choosing which relationships to traverse.

Barrasa’s query looks for relationship definitions annotated as contextualizing relationships. The classification uses a subproperty relationship: a particular domain relationship belongs under the broader contextualizing property. In [RDF Schema](https://www.w3.org/TR/rdf-schema/), `subPropertyOf` describes a property hierarchy; the application’s query explicitly consumes that classification to implement retrieval policy. The standard does not perform the traversal for it.

The retrieval operation now has two connected parts:

1. Query the ontology for relationship types marked as contextualizing.
2. Use those types to navigate the data graph from each retrieved node.

For the *Matrix* example, the selected relationship is acting in a movie. The retriever follows those connections while excluding producing and directing connections. Those other relationships may exist in the graph; they simply are not selected as context by this policy.

The important change is where the decision lives.

| Decision | Fixed traversal | Ontology-driven traversal |
| --- | --- | --- |
| Which relationships add context? | Named in retrieval code | Selected from ontology annotations |
| What does the query consult? | Movie data | Ontology, then movie data |
| How does traversal policy change? | Edit traversal logic | Edit the stored classification |

The ontology creates a level of indirection: the retriever’s general logic stays in place while the stored model determines which connections it follows. Changing the ontology can therefore change retrieval behavior on the fly.

This gives the ontology two distinct jobs across the application’s lifecycle. During construction, it is the implementation-agnostic model used to shape the knowledge graph. During retrieval, that same kind of stored description becomes data the retriever consults to choose its behavior. Barrasa closes on those two roles and invites further questions at the booth.

## Resources

- [Vector search with graph traversal](https://neo4j.com/blog/developer/graph-traversal-graphrag-python-package/) — A Python walkthrough that retrieves movies by plot similarity, then adds their actors through a Cypher traversal.
- [Neo4j GraphRAG retrieval guide](https://neo4j.com/docs/neo4j-graphrag-python/current/user_guide_rag.html) — Current configuration examples for vector, hybrid, Cypher-enhanced and text-to-Cypher retrievers.
- [Financial Industry Business Ontology](https://spec.edmcouncil.org/fibo/index.html) — An OWL ontology defining financial business concepts and their relationships.
- [RDF Schema: classes and property hierarchies](https://www.w3.org/TR/rdf-schema/) — W3C definitions of subclass and subproperty relationships underlying ontology hierarchies.

## Slides

### Why Your Agent’s Brain Needs a Playbook

Jesús Barrasa introduces practical uses of ontologies in GraphRAG, combining knowledge graphs and large language models to build AI applications.

[0:39](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=39s) [Title slide for Jesús Barrasa’s talk on practical wins from using ontologies.](https://assets.ai.engineer/sites/aiecode2025/sha256/b67ad93a73eb83af7e38b50bca9a5352620a281b45c2b09361563e2d73987807.webp)

neo4j

Why Your Agent’s Brain Needs a Playbook

Practical Wins from Using Ontologies

Dr. Jesús Barrasa - AI Field CTO / Neo4j

Interface text:
docs.google.com

### GraphRAG

The application searches Neo4j’s knowledge graph for relevant information, then sends it with the user’s question to the LLM to generate a response.

[0:58](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=58s) [Diagram showing a user prompt, application, knowledge graph retrieval, and response.](https://assets.ai.engineer/sites/aiecode2025/sha256/eb799aa9c24138826a6f8f603466e3492307b6f4655e5beb5193cec0295a7a01.webp)

GraphRAG

?
User Prompt
GenAI Application
LLM Question + Relevant Info
Complete Response

Query or Semantic Search
Relevant Info
Neo4j’s Knowledge Graph

Interface text:
docs.google.com

### What’s in the graph?

The domain layer links Andreas to London. The lexical layer connects document chunks in sequence and records which entities a chunk mentions, preserving their source for retrieval.

[3:20](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=200s) [Domain and lexical layers connect people and places to document chunks.](https://assets.ai.engineer/sites/aiecode2025/sha256/b3bbe61279b279e6d1502d21dc33745b9464e5f5193d52d30cf2de19b9a9a0f8.webp)

What’s in the graph?

Domain Layer
:Person
"Andreas"
LIVES_IN
:Place
"London"

Lexical Layer
:Chunk
:Chunk
:Chunk
:Document
MENTIONS
MENTIONS
NEXT
NEXT

Interface text:
docs.google.com

### Ontologies: What and Why?

An ontology provides a shared, formal model of a domain that is independent of implementation. It can be serialized in multiple formats.

[6:56](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=416s) [An ontology is defined as a shared, explicit, formal domain model.](https://assets.ai.engineer/sites/aiecode2025/sha256/739c70a599b2ec42cff5b53650765799a17ca06d89f6c57fe11c29998a96ffb5.webp)

Ontologies: What? and why?

An ontology is a shared, explicit, formal domain model

All rights reserved 2021
neo4j

Interface text:
docs.google.com

### Ontologies: What? and why?

The FIBO ontology defines a privately held company as a subclass of stock corporation and connects a stock corporation to a board agreement through the “is governed by” relationship.

[7:50](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=470s) [An XML ontology excerpt and diagram show privately held company as a subclass of stock corporation.](https://assets.ai.engineer/sites/aiecode2025/sha256/ddc4dd409fe32c501cb9f61eb8c7595e626468dcb9c1b6a35572c683ec71de87.webp)

Ontologies: What? and why?

<owl:Class rdf:about="&fibo-be-corp-corp;PrivatelyHeldCompany">
    <rdfs:label>privately held company</rdfs:label>
    <fibo-fnd-utl-av:synonym rdf:datatype="&xsd;string">closed corporation</fibo-fnd-utl-av:synonym>
    <fibo-fnd-utl-av:synonym rdf:datatype="&xsd;string">privately held corporation</fibo-fnd-utl-av:synonym>
    <skos:definition rdf:datatype="&xsd;string">A firm whose issued shares are all held by a family or a small
    group of investors and, therefore, cannot be bought by the public.</skos:definition>
    <rdfs:subClassOf rdf:resource="&fibo-be-le-cb;StockCorporation"/>
</owl:Class>

<owl:Class rdf:about="&fibo-be-le-cb;StockCorporation">
    <rdfs:subClassOf>
        <owl:Restriction>
            <owl:onProperty rdf:resource="&fibo-fnd-rel-rel;isGovernedBy"/>
            <owl:someValuesFrom rdf:resource="&fibo-be-corp-corp;BoardAgreement"/>
        </owl:Restriction>
    </rdfs:subClassOf>
    <rdfs:subClassOf>
        <owl:Restriction>
            <owl:onProperty rdf:resource="&fibo-fnd-aap-agt;isIdentifiedBy"/>
            <owl:onClass rdf:resource="&fibo-be-corp-corp;RegistrationIdentifier"/>
            <owl:qualifiedCardinality rdf:datatype="&xsd;nonNegativeInteger">1</owl:qualifiedCardinality>
        </owl:Restriction>
    </rdfs:subClassOf>
    <rdfs:subClassOf>
        <owl:Restriction>
            <owl:onProperty rdf:resource="&fibo-be-corp-corp;hasDateOfIncorporation"/>
            <owl:onClass rdf:resource="&fibo-fnd-dt-fd;Date"/>
            <owl:qualifiedCardinality rdf:datatype="&xsd;nonNegativeInteger">1</owl:qualifiedCardinality>
        </owl:Restriction>
    </rdfs:subClassOf>
    <rdfs:subClassOf>
    [illegible]

Privately Held Company
rdf:SubClassOf
Stock Corporation

https://spec.edmcouncil.org/fibo/
All rights reserved 2021
neo4j

Interface text:
docs.google.com

[7:57](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=477s) [Ontology code alongside a diagram linking a privately held company, stock corporation, and board agreement.](https://assets.ai.engineer/sites/aiecode2025/sha256/4083348b398dbe6848e8f445123ace3e270689fafc26944dbcf030ffbdd7dced.webp)

Ontologies: What? and why?

<owl:Class rdf:about="&fibo-be-corp-corp;PrivatelyHeldCompany">
    <rdfs:label>privately held company</rdfs:label>
    <fibo-fnd-utl-av:synonym rdf:datatype="&xsd;string">closed corporation</fibo-fnd-utl-av:synonym>
    <fibo-fnd-utl-av:synonym rdf:datatype="&xsd;string">privately held corporation</fibo-fnd-utl-av:synonym>
    <skos:definition rdf:datatype="&xsd;string">A firm whose issued shares are all held by a family or a small
    group of investors and, therefore, cannot be bought by the public.</skos:definition>
    <rdfs:subClassOf rdf:resource="&fibo-be-le-cb;StockCorporation"/>
</owl:Class>

<owl:Class rdf:about="&fibo-be-le-cb;StockCorporation">
    <rdfs:subClassOf>
        <owl:Restriction>
            <owl:onProperty rdf:resource="&fibo-fnd-rel-rel;isGovernedBy"/>
            <owl:someValuesFrom rdf:resource="&fibo-be-corp-corp;BoardAgreement"/>
        </owl:Restriction>
    </rdfs:subClassOf>
    <rdfs:subClassOf>
        <owl:Restriction>
            <owl:onProperty rdf:resource="&fibo-fnd-aap-agt;isIdentifiedBy"/>
            <owl:onClass rdf:resource="&fibo-be-corp-corp;RegistrationIdentifier"/>
            <owl:qualifiedCardinality rdf:datatype="&xsd;nonNegativeInteger">1</owl:qualifiedCardinality>
        </owl:Restriction>
    </rdfs:subClassOf>
    <rdfs:subClassOf>
        <owl:Restriction>
            <owl:onProperty rdf:resource="&fibo-be-corp-corp;hasDateOfIncorporation"/>
            <owl:onClass rdf:resource="&fibo-fnd-dt-fd;Date"/>
            <owl:qualifiedCardinality rdf:datatype="&xsd;nonNegativeInteger">1</owl:qualifiedCardinality>
        </owl:Restriction>
    </rdfs:subClassOf>
    <rdfs:subClassOf>
    [illegible]

Privately Held Company
rdf:SubClassOf
Stock Corporation
is Governed By
Board Agreement

https://spec.edmcouncil.org/fibo/
All rights reserved 2021
neo4j

Interface text:
docs.google.com

### Step by step example

The speaker points to this guide as an example of using an ontology to populate a graph from structured and unstructured data. The walkthrough builds a customer and retail analytics agent.

[8:29](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=509s) [A slide showing a Neo4j guide for a customer and retail analytics GraphRAG agent.](https://assets.ai.engineer/sites/aiecode2025/sha256/1eac5e00a9cca96f0a6d72fd93c5c609ad59dd370819300bf8df4e85633b8bb9.webp)

Step by step example

GraphRAG Agent for Customer & Retail Analytics

This is an end-to-end worked example for building a GraphRAG agent to accelerate customer and retail analytics. It covers the entire process from:

1. Quickly constructing a graph from mixed unstructured and structured data sources
2. Resolving and linking entities in the graph along the way
3. Creating diverse graph retrieval tools, including query templates, vector search, dynamic text2Cypher, and graph community detection to answer a broader range of questions
4. Building an agent with Semantic Kernel for conducting analytics and responding to complex user questions

All of this using a central source-of-truth graph schema to govern the process and AI-interactions, ensuring higher data & retrieval quality.

This workflow can be adapted for analytics, reporting, and Q&A across various other business domains — especially when:

• Data sources include a mix of structured and unstructured data.
• AI needs to navigate a non-trivial business domain model for accurate responses.
• The use case requires flexibility and the ability to scale to complex, evolving AI-driven tasks.

Follow the instructions below to try it yourself! 🚀

Contents
Prerequisites
Setup
Create the Graph from Source Data
1) Load Unstructured Data
2) Merge Structured Data
3) Post-Processing Script
Running the Agent
Schema Generation

Nov 6 2025
NODES 2025
The Call for Papers is now open and we want to hear about your graph-related projects. Submit your talks by June 15
Submit your talk

https://neo4j.com/developer/genai-ecosystem/customer-graph-agent/

Interface text:
docs.google.com
neo4j Developer
Docs
Labs
Get Help
GraphAcademy
Get Started Free
Developer Guides
Neo4j Developer Center
GraphRAG Dev Guide
GraphRAG Examples
GraphRAG for Customer Experience
Agentic GraphRAG for Commercial Contract Q&A
GraphRAG Agent for Customer Analytics
GraphRAG with AWS Bedrock - Getting Started
GraphRAG on AWS with Unstructured/Structured/Mixed Data
GraphRAG with Microsoft Azure OpenAI
GraphRAG with Google Vertex AI
Neo4j GenAI Product Integrations
Unstructured Data Import
Model Context Protocol
GraphRAG Ecosystem Tools
GenAI Frameworks
Graph Examples
[Embedded application screenshot text illegible]

### Knowledge graph

The graph connects enterprise entities through relationships. Some nodes carry natural-language text that can be embedded for retrieval.

[9:10](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=550s) [Text describes entities, relationships, and node properties beside an illustration of a graph.](https://assets.ai.engineer/sites/aiecode2025/sha256/ad6b36a0c2a7d76fed775653ecf6de654d946d6aa67fa52546edf0b0c959c863.webp)

A Knowledge Graph captures key enterprise knowledge in the form of entities and relationships between them.

Some nodes in the graph have properties with NL text

[illegible] reserved 2024

Interface text:
docs.google.com

### Embedding graph properties

Text properties on graph nodes are embedded and placed in a vector index, creating a new search space for entering the graph.

[9:23](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=563s) [A graph above a vector index illustrates embedding property values for semantic search.](https://assets.ai.engineer/sites/aiecode2025/sha256/4ea9b626fae7b56a27c2f1a921176a2544999130c3ea203ed7a680f6f51aa85a.webp)

These property values get embedded (transformed into a vector representation) and added to a vector index to enable vector-based semantic search.

[illegible]nts reserved 2024

Interface text:
docs.google.com

### Semantic search

The query is embedded as a point in vector space. Semantic search returns its k approximate nearest neighbours, shown as a highlighted region in the index beneath the graph.

[9:33](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=573s) [A highlighted region on the vector index represents approximate nearest neighbors to a search concept.](https://assets.ai.engineer/sites/aiecode2025/sha256/96f0100aa967f415cbeee3cf722993f1e0d228d8d3a7eea12ad07a7eb73d2193.webp)

A semantic search on the vector index returns the k approximate nearest neighbours to the search concept (word, question, image, etc)

[illegible] reserved 2024

Interface text:
docs.google.com

### Graph contextualization

Vector search results lead back to graph nodes. Graph exploration then finds related elements that enrich the context passed to the LLM.

[9:51](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=591s) [Highlighted graph nodes connected to the vector index illustrate enriching search results through graph exploration.](https://assets.ai.engineer/sites/aiecode2025/sha256/61d5bda07d16e9de2218cb6fa68f79b02c50f22e968f209b6108ee6196af2329.webp)

Each result from the vector search is “dereferenced“ to get the corresponding node in the graph and a subsequent graph exploration finds semantically related elements that enrich and augment the final search result.

[illegible]ts reserved 2024

Interface text:
docs.google.com

### Retrieval: Vector Search + Contextualisation

The retriever searches movie plots for the top three matches, then runs a graph query on each result to return its title, plot and actors. That query is hard-coded and requires knowledge of the graph’s model.

[10:05](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=605s) [Code configures a vector retriever and returns movie titles, plots, and actors.](https://assets.ai.engineer/sites/aiecode2025/sha256/61713f55dfde04bf517d36057e29c320a1b7a8490f3ede4fb19de578273f62c6.webp)

Retrieval: Vector Search + Contextualisation

retrieval_query = """
RETURN  node.title as movieTitle,
        node.plot as moviePlot,
        collect { MATCH (actor:Actor)-[:ACTED_IN]->(node) RETURN actor.name } AS actors
"""
retriever = VectorCypherRetriever(
    driver,
    Index_name = "movieplots",
    embedder = OpenAIEmbeddings(model="text-embedding-3-large"),
    retrieval_query=retrieval_query,
)

query_text = "A computer hacker discovers that the world he knows is a simulated reality controlled by machines"

retriever_result = retriever.search(query_text=query_text, top_k=3)

Interface text:
docs.google.com

### Retrieval: Powerful but Rigid

The retriever hard-codes the ACTED_IN relationship to collect actors alongside movie titles and plots, so creating it requires knowing the graph’s model.

[11:34](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=694s) [The retrieval example highlights the fixed query used to collect actors.](https://assets.ai.engineer/sites/aiecode2025/sha256/79c26120ed3493bf5c96233c466fba142f0185ceadca5be1e6f9df89c42048c9.webp)

Retrieval... powerful 💪 but rigid 😰

retrieval_query = """
RETURN  node.title as movieTitle,
        node.plot as moviePlot,
        collect { MATCH (actor:Actor)-[:ACTED_IN]->(node) RETURN actor.name } AS actors
"""

retriever = VectorCypherRetriever(
    driver,
    Index_name = "movieplots",
    embedder = OpenAIEmbeddings(model="text-embedding-3-large"),
    retrieval_query=retrieval_query,
)

query_text = "A computer hacker discovers that the world he knows is a simulated reality controlled by machines"

retriever_result = retriever.search(query_text=query_text, top_k=3)

Interface text:
docs.google.com

### Retrieval: Dynamic

The retriever consults the ontology for contextual relationships, then uses them in its query. In the Matrix example, ACTED_IN is selected while DIRECTED and PRODUCED are left out.

[11:55](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=715s) [A dynamic query uses an ontology relationship to retrieve context, illustrated with an actor and movie schema and a Matrix graph.](https://assets.ai.engineer/sites/aiecode2025/sha256/1eab8ecbb33dd8bbd2ff7e81cfd924ca8f9f3a7bb5d44e494fb77b905b2bd8f9.webp)

Retrieval... dynamic

retrieval_query = """
MATCH (r)-[:SPO]->(:Relationship { name: "CONTEXT_REL"})
MATCH (node)-[:$(r.name)]->(related)
RETURN properties(related) as context
"""

Ontology
CONTEXT_REL
SPO
Actor
DOMAIN
ACTED_IN
RANGE
Movie

Data
The Matrix
Keanu Reeves — ACTED_IN
Lilly Wach... — DIRECTED
Hugo Weavi... — ACTED_IN
Laurence Fishb... — ACTED_IN
Carrie-A... — ACTED_IN
Emil Eifrem — ACTED_IN
Joel Silver — PRODUCED
Lana Wach... — DIRECTED

Interface text:
docs.google.com

## Full transcript

[0:00](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=0s) [upbeat music] Welcome.

[0:16](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=16s) Thank you for joining my session this morning. I was thinking that I was in some kind of niche space where people would not be interested, but I'm curious to hear, you know, what brought you,

[0:26](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=26s) uh, to, to, to this session. Anyway, I'm, I'm, I'm Jesús, Jesús Barrasa. I'm the, I'm the Field CTO for AI with Neo4j. And, uh, and yeah, for the next, like, uh, fourteen-ish minutes, I'm going to be diving a bit deeper into this, uh, really powerful combination, which is knowledge graphs and, uh, and large language models to build,

[0:44](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=44s) uh, uh, AI applications. And, and in particular, I'm going to be focusing on these interesting, uh, knowledge artifacts that are ontologies and, and how we can use them, uh, to get some, some practical wins in, in our favorite, uh, architecture, which you I'm sure have heard about over the last couple of days, which is GraphRAG.

[1:03](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=63s) I mean, it is part of this, of this track. Um, but, um, anyway, e- even if you attended the, the workshops or, or you didn't, I'm gonna spend a minute just refreshing what, uh, what GraphRAG is.

[1:15](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=75s) But basically, GraphRAG is, is your RAG, where you have replaced your vector database with a knowledge graph that's built on a, on a database, on a graph database. And, uh, and the flow is, is the same, right?

[1:29](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=89s) So your, your AI application will get a prompt from the user, and instead of passing it on to the LLM directly, what it will do is will go out to a, a retrieval, to a controlled, curated, trusted, uh, sort of knowledge base to retrieve potentially relevant information that will be passed to the LLM in the context window

[1:46](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=106s) so that the response or the task is accomplished based on, on more grounded, uh, information rather than purely generated. So now the, the interesting thing, of course, is that, uh, by using a knowledge graph, what you get is a much richer collection of retrieval strategies.

[2:03](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=123s) And we're gonna see that, uh, you have not only the vector semantic search over a vector index, you can also, uh, contextualize results because a graph captures the connections between data points.

[2:13](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=133s) So you're gonna be able-- We're gonna go deeper in, in, into how that happens. Is it al- also possible to, to generate structured queries? So there's a rich collection of retrieval strategies, like I was saying, that augments what you traditionally have with, uh, with you restrict yourself to only vector search.

[2:29](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=149s) And, and the result is that you get, uh, better quality results, uh, more completeness, better completeness, more relevance, better precision, more faithfulness, a lot of metrics that I'm sure have been mentioned over the last couple of days, and I won't spend too much time.

[2:43](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=163s) But, um, but what does the graph look like? Uh, again, uh, especially for those of you who have not, not familiar with this. But the-- we-- Neo4j implements the property graph model, which has two main primitives, is nodes and relationships.

[2:55](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=175s) Pretty simple. So for us, nodes represent entities, that's things, persons, objects, locations, events, documents, anything. And, and they're connected to each other with relationships that are directed. And then these, you can think of them as objects that will have a collection of key value pairs that characterize them, like description, uh, uh, like name, uh, date of birth,

[3:13](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=193s) whatever. And, uh, this is what we call the domain graph. Now, the thing is, when we build the graph out of unstructured data, we often enrich this representation with what we call the, uh, the lexical graph or some kind of form of description of the, of the source, uh, documents, uh, from where we have extracted the graph.

[3:31](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=211s) And, and you can see here that we will have a node representing a document, which maybe has a sequence of chunks, depending on how we partition the document. It can be, in the simplest case, a sequential list of chunks.

[3:42](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=222s) But if it's, think of a legal document like a, a, a contract with sections, subsections, like definitions, clauses, you might have a richer kind of tree description of what that document looks like.

[3:54](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=234s) Uh, and, and that would be represented also as a graph. Now, of course, these chunks then will be connected to the domain, uh, graph by, uh, entity extraction. So we'll find that this person is mentioned in this chunk, and we'll keep track of where that's men- that's mentioned, 'cause we're gonna use that when we do the, the

[4:09](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=249s) retrieval. Right? And, um, and one interesting thing to keep in mind here is that, uh, these properties that we store in a node can also, well, can be numeric, can be string, can be, uh, all sorts of data types, but it can also be vectors.

[4:21](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=261s) So these chunks will typically contain the text, the raw text that you have, uh, uh, in which you have, you have split your document, but you can also have the vectorized representation, and then you can index it.

[4:32](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=272s) So you-- It's, it's pretty compelling, the fact that you can have in the same platform a vector storage with, uh, enriched with, uh, with, um, with the connected representation that the graph brings.

[4:42](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=282s) Now, of course, in order to get to, to GraphRAG, you have to build a graph. There's no GraphRAG without a graph. And depending how, uh, you, where you're getting your data from, whether it's structured or unstructured, you probably will follow different, uh, type of, of pipelines, right?

[4:56](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=296s) So if you look at the more structured approach, you will have some, uh, uh, um, maybe tabular representation in some database or even files. You will have some description of the target schema, the graph that you wanna build, and you would map those, basically saying, in the simplest case, these records will populate these type of nodes.

[5:12](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=312s) These are the records will define the relationships, and you will write that to the graph. What you have here in the URL is, is the, the, the UI, the, the interface that our product offers, and basically it does what I was describing here, uh, in a visual way.

[5:24](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=324s) So you define your graph model, you map to your data sources, you map the, uh, uh, you connect the dots, and, and you get a graph generated. Behind, of course, there's an API base, and you can do the same thing programmatically.

[5:34](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=334s) But important to understand that you have to pass the target schema. We're gonna get to that in a minute. If you're loading, uh, you're building a graph out of unstructured data, and this is the pipeline that our GraphRAG package follows.

[5:45](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=345s) But if you look at our integration with LangChain, with LlamaIndex, it will look pretty much the same. What you're doing is you're processing the document. You will split it in some way, based on what we described before.

[5:54](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=354s) You will take these chunks, and you will embed them, and you will run this kind of entity extraction process that I was mentioning before.

[6:01](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=361s) To which you will have to inject your, your schema, right? So you'll have to tell what kind of entities do I care about? What's the-- my domain of discourse?

[6:08](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=368s) Am I looking at, uh, a medical documents where I'm gonna find information about patients and, and symptoms? Am I looking at, uh, you know, financial information like, uh, account holders, accounts, tran- you know, mon-money transfers, et cetera?

[6:20](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=380s) So that's the kind of information that you find in the schema, and then ultimately you will write it to the graph. But the common bit there is the fact that we have to inject that schema, and that's where things diverge because depending on the path that you follow, maybe, you know, this approach will have a JSON-based

[6:34](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=394s) representation of your entities and the relationships. Maybe here you're using something like a Pydantic description of the kind of entities that you wanna get, or even a natural language one if you're prompting, you know, your, your, your LLM to, to do the extraction.

[6:46](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=406s) Now, this divergence is something that's, uh, you know, not ideal to manage. And what you wanna do is have some kind of agnostic and, and, and general approach to, to representing schemas.

[6:56](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=416s) And that's exactly what ontology is bringing. And, uh, an ontology is just a shared, you know, description of, of a domain. You can think of it a- as a, as a formal schema that's, that's implementation agnostic.

[7:07](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=427s) And what's interesting, I'm looking at ontologies, uh, there's a bunch of, of standards for defining ontologies, and there's, some of them defining by the W3C. They can be serialized in a number of ways.

[7:17](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=437s) I've picked the XML one. Why not? Who's using XML in twenty twenty-five? But anyway, there's JSON-based as well. There's many others, right? The, the important thing is what an ontology describes is it's exactly what we're after.

[7:28](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=448s) It is a domain, right? So it, uh, you will find a definition of a class which actually matches very well what a, what a graph model looks like. So this is a fragment of the FIBO ontology, which is the financial, uh, industry, uh, um, ontology that's a public one.

[7:41](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=461s) And, and you see the definition of, uh, of um, what's it there? Like a privately held company. I don't know if you can read it there. But anyway, there's, uh, another class there that's a stock corporation.

[7:49](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=469s) We are specifying that one is a subcategory of the other, so there's a subclass of this, you know, relationship between the two of them. Then there's even connections between, uh, classes of other nature.

[7:58](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=478s) For example, we say that, uh, a stock corporation is governed by a board agreement, right? So these, all these sort of elements are described in the ontology, and that's a perfect, you know, like I was saying, uh, implementation agnostic approach to, uh, to drive the knowledge graph creation, and that will work for both, uh, pipelines, the structured

[8:15](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=495s) and the unstructured. And my-- but basically, my, my take on this is, is, um, fundamentally be, be, be model driven, right? Because the, the... Not only because it's, uh, data engineering, but good practice, but also because, uh, you will build a better graph in the first place and, uh, and it will pay dividends in, in, in the

[8:37](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=517s) long run, and we're gonna see that in the retrieval phase. 'Cause if you have a good description of your graph, your text to structured query is gonna generate better and more accurate queries.

[8:46](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=526s) But also when we use the, the vector plus contextualization, that's gonna pay off as well. You have there, uh, an example of, of, uh, use of ontology to, to populate a graph with structured and unstructured data.

[8:56](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=536s) I don't have time to go over that today, but you can, you can have a look there. Now, on the retrieval side of things, let me spend a minute explaining how this magic happens, 'cause I said that you have, um, better retrieval strategies, richer ones.

[9:08](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=548s) And, um, and what's happening there, you see, uh, that the graph captures. And I'm gonna use a visual. Bear with me. I hope that's, that's useful. So you have a graph, right?

[9:15](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=555s) In the graph, some of those nodes will contain pieces of text, the, the chunks that we have embedded. So when we embed them and put them in a vector index, we're effectively creating a new search space.

[9:26](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=566s) Like think of this as a new, you know, uh, way of entering the, the graph, right? So a, a, a search in that space would be, uh, you, you embed your query that defines a point in your multidimensional space, and you will find vectors in the vicinity.

[9:39](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=579s) So that's what you do when you do a vector search, right? So the interesting thing is that in the graph, when you find the, the, the, the vectors that are in proximity, because these vectors are properties of a node, we can dereference them and get back to the graph, and from there we can contextualize, we can navigate

[9:56](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=596s) and enrich and filter out, aggregate. So that's where we do the contextualization, and we can bring, uh, uh, additional richness to the, to the, to the context that we pass to the, to the LLM.

[10:06](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=606s) Now, what does that look like in, in code? So... Oh, I don't know if it's gonna be readable. But, um, let's read from the bot- from the bottom up.

[10:12](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=612s) So this is again, a fragment from our, our GraphRAG package, Python package. But if you look at the, the, the integration with, uh, LangChain is gonna be very, very similar.

[10:21](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=621s) So we do a, we do a search. We specify, we pass a, a, a query. This is a, a movie database. What we're looking for, uh, we have embedded and put, uh, in a vector index the plots of the movies, right?

[10:31](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=631s) So we're looking for something. You can imagine the movie that I'm looking for, right? We're looking for computer's, uh, uh, a hacker that discovers that the world he knows is simul- is simulated reality controlled by machines.

[10:40](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=640s) So that's my search, uh, uh, term and, uh, I'm passing that to my retriever and specifying I want the top three results. That's my-- That's for me to decide.

[10:49](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=649s) And basically what I'm doing is I've created this handler that, that's, uh, a handler over the vector index, and all I'm doing is telling it to do this search, but I'm passing a retrieval query.

[11:01](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=661s) So basically what you see up here is this retrieval query, which is basically the logic, the exploration logic that I'm running of- on every single result that I get from my vector index, right?

[11:10](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=670s) So that's, um, that's powerful. That's great. Now, there is a kind of a problem there, and my problem is that, um, in order to create this retriever, I need to know my model.

[11:22](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=682s) I need to be familiar with the fact that my graph contains actors that acted in movies, that these movies have, uh, uh, have plots and things like that. And, and that introduces...

[11:32](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=692s) I mean, that, that's, that's great, but to some extent is a, is a little bit rigid 'cause I have to hard code part of the logic in my retriever.

[11:40](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=700s) So how could I work around that? Well, the great thing is that if we take the ontology that we saw before and we persist it in the graph, now we have the graph of our data, our movie database, and we have an ontology that describes that schema, what I can do is create a dynamic query.

[11:56](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=716s) And I understand that you guys are not, uh, Cypher experts, but, you know, if you bear with me on that, I mean, what we're doing is I'm, I'm querying my ontology where there's a definition of a relationship.

[12:08](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=728s) So I know that actors act in movies, and there's a definition of this relationship. That's what my domain model describes. Now I'm going to query that, and I'm looking for relationships that I have annotated as contextualizing relationships.

[12:21](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=741s) So I'm defining a subcategory in this case, subproperty of. So what I can do is drive the behavior of my retriever by consulting the ontology. So I look at the ontology, I find the relationships that have been specified as contextualizing relationships, and those are the ones that I navigate.

[12:37](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=757s) So these R is the one that I'm using to then find nodes that are connected to other things. So in other words, my ontology is driving the behavior of this query, and it's determining the fact that when I find the movie The Matrix, I'm gonna n- I'm going to be navigating just the acted in relationships and not

[12:53](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=773s) the produced or the directed, right? I understand that, you know, it might be, uh, going a bit too deep into, into the, the Cypher aspect without having you, you know, you train a little bit on it, but I hope, I hope it makes sense.

[13:03](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=783s) I mean, the idea is that some of the way in which ontologies can drive can give us a kind of a, a level of indirection that makes it possible for us to kind of build dynamic retrievers that are driven by data.

[13:14](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=794s) So I can change my ontology, and by changing my ontology, which is a data artifact in my graph, I'm changing the behavior of my retriever, uh, on, on the fly and, uh, and dynamically.

[13:24](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=804s) So that, that's what I wanted to share with you. So the two takeaways, I mean, the first one is ontologies for knowledge graph creation as an, uh, uh, implementation agnostic, uh, um, data model.

[13:34](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=814s) And the second one, storing ontology can help us drive, uh, dynamic behavior in our retrievers. That's what I have for you today and just in time, so thank you very much and happy to take the q- uh, questions or the conversation back to our booth.

[13:46](https://www.youtube.com/watch?v=CbiR9xS2skQ&t=826s) Cheers. [audience applauding] [upbeat music]
