Domain graphs

Graph RAG with LangChain and Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

Domain graphs

a knowledge graph diagram showing the lexical graph on the left hand side with a book, chapters and pages and on the right the people and companies in the book along with references from other sources including Wikipedia

Graph RAG with LangChain and Neo4j

Lexical graphs to domain graphs

The lyrics to Don't Stop Believing by Journey

Graph RAG with LangChain and Neo4j

Lexical graphs to domain graphs

The lyrics to Don't Stop Believing by Journey with "Small Town Boy" highlighted as a person

Graph RAG with LangChain and Neo4j

Lexical graphs to domain graphs

The lyrics to Don't Stop Believing by Journey with "South Detroit" highlighted as a location

Graph RAG with LangChain and Neo4j

Lexical graphs to domain graphs

The lyrics to Don't Stop Believing by Journey with "Born"  highlighted as a relationship between nodes for the person "Small Town Boy" and and the location "South Detroit"

Graph RAG with LangChain and Neo4j

Lexical graphs to domain graphs

The lyrics to Don't Stop Believing by Journey with "Raised"  highlighted as a relationship between nodes for the person "Small Town Boy" and and the location "South Detroit"

Graph RAG with LangChain and Neo4j

An example domain graph

An example domain graph model consisting of a person who works at a company, located in an address.  The person holds a credit card, which has an order placed against it at a company in the same location

Graph RAG with LangChain and Neo4j

Structured outputs

from pydantic import BaseModel, Field

from typing import Optional
class Character(BaseModel): """A character from the play."""
id: str = Field("A unique identifier for the character in slug-case",
examples=["montague-romeo", "tybalt"])
name: str = Field("The full name of the character", examples=["Romeo", "Tybalt"])
family: Optional[str] = Field("The name of the family they belong to", examples=["Montague", "Capulet"])
class CharacterOutput(BaseModel): characters: list[Character] = Field("A list of characters")
Graph RAG with LangChain and Neo4j

Using structured outputs

from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini", api_key="...")


# With a structured output character_model = llm.with_structured_output(CharacterOutput)
# Use within a chain chain = extraction_prompt | character_model
# Handle the response output = chain.invoke(...)
CharacterOutput(characters=[Character, Character, Character...])
Graph RAG with LangChain and Neo4j

Saving entities

nodes = [

Node(
type="Character",
id=character.id,
properties={"name": character.name, "family": character.family}
)
for character in output.characters
]
# Build a GraphDocument graph_document = GraphDocument(nodes=nodes, relationships=[])
# Save to the graph graph.add_graph_documents([graph_document])
Graph RAG with LangChain and Neo4j

Let's practice!

Graph RAG with LangChain and Neo4j

Preparing Video For Download...