Graphes et RAG

Graph RAG avec LangChain et Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

Faites connaissance avec votre instructeur

 

  • Responsable, Developer Education chez Neo4j
  • Formations développeurs via Neo4j GraphAcademy
  • 20+ ans d’expérience en développement logiciel
  • 10+ ans d’expérience Neo4j

 

Photo d’Adam

1 https://graphacademy.neo4j.com
Graph RAG avec LangChain et Neo4j

Le R de RAG

Un schéma d’application où une saisie Utilisateur est ajoutée à une invite envoyée à un LLM

Graph RAG avec LangChain et Neo4j

Le R de RAG

L’invite requiert un contexte issu de documents et de sources de données

Graph RAG avec LangChain et Neo4j

Le R de RAG

Ces données sont souvent converties en embeddings de vecteurs

Graph RAG avec LangChain et Neo4j

Où la recherche sémantique échoue-t-elle ?

Les vecteurs sont adaptés à

  • Questions floues ou ouvertes

Les vecteurs sont peu efficaces pour

  • Questions très spécifiques ou factuelles
  • Requêtes numériques ou à correspondance exacte
  • Requêtes logiques

Que pense Paul Graham de l’IA générative ?

Dans combien de startups d’IA générative Paul Graham a-t-il investi ?

Graph RAG avec LangChain et Neo4j

Vecteurs vs. graphes de connaissances

Texte Vector Embedding
He's just a city boy born [0.12, -0.34, 0.56, 0.78, ..., -0.91]
born and raised in South Detroit [0.22, 0.45, -0.67, 0.11, ..., 0.33]
He took the midnight train [-0.55, 0.89, 0.12, -0.44, ..., 0.67]
going anywhere [0.78, -0.23, 0.45, 0.91, ..., -0.12]

Un graphe montrant les entités citées dans les paroles de Don't Stop Believin'. Un petit gars de province, avec des relations BORN et RAISED vers South Detroit

Graph RAG avec LangChain et Neo4j

Vecteurs vs. graphes de connaissances

Texte Vector Embedding
He's just a city boy born [0.12, -0.34, 0.56, 0.78, ..., -0.91]
born and raised in South Detroit [0.22, 0.45, -0.67, 0.11, ..., 0.33]
He took the midnight train [-0.55, 0.89, 0.12, -0.44, ..., 0.67]
going anywhere [0.78, -0.23, 0.45, 0.91, ..., -0.12]

Les nœuds sont une personne : Small town boy, un lieu : South Detroit, un train : Midnight Train et un lieu : Anywhere

Graph RAG avec LangChain et Neo4j

Vecteurs vs. graphes de connaissances

Texte Vector Embedding
He's just a city boy born [0.12, -0.34, 0.56, 0.78, ..., -0.91]
born and raised in South Detroit [0.22, 0.45, -0.67, 0.11, ..., 0.33]
He took the midnight train [-0.55, 0.89, 0.12, -0.44, ..., 0.67]
going anywhere [0.78, -0.23, 0.45, 0.91, ..., -0.12]

Les relations BORN et RAISED vont de la personne vers le lieu. La relation GOING du train va de Midnight Train vers Anywhere

Graph RAG avec LangChain et Neo4j

Graphes de connaissances et Neo4j

 

  • Neo4j est la base de graphes la plus utilisée au monde
  • Flexible, schéma optionnel
  • Intégration LangChain langchain-neo4j
  • LCEL : LangChain Expression Language

Les logos neo4j et langchain

1 https://db-engines.com/en/ranking/graph%20dbms
Graph RAG avec LangChain et Neo4j

Les nœuds représentent des entités

from langchain_neo4j.graphs.graph_document \
  import Node

book = Node(
type="Book",
id=f"9781098127107",
properties={ "title": "Building Knowledge Graphs" }
)

Un nœud Book dans un graphe avec des propriétés ID et Title

Graph RAG avec LangChain et Neo4j

Les relations relient les nœuds

from langchain_neo4j.graphs.graph_document \
  import Node, Relationship

book = Node(type="Book", id=f"9781098127107")

jesus = Node(type="Person", id="dr-jesus-barrasa")
jim = Node(type="Person", id="chief-scientist")
for author in [jesus, jim]:
    relationship = Relationship(





)

Deux relations WRITTEN_BY allant d’un nœud Book vers deux nœuds Person représentant les auteurs

Graph RAG avec LangChain et Neo4j

Les relations relient les nœuds

from langchain_neo4j.graphs.graph_document \
  import Node, Relationship

book = Node(type="Book", id=f"9781098127107")

jesus = Node(type="Person", id="dr-jesus-barrasa")
jim = Node(type="Person", id="chief-scientist")
for author in [jesus, jim]:
    relationship = Relationship(

source=book,



)

Le nœud source de la relation WRITTEN_BY est le nœud Book

Graph RAG avec LangChain et Neo4j

Les relations relient les nœuds

from langchain_neo4j.graphs.graph_document \
  import Node, Relationship

book = Node(type="Book", id=f"9781098127107")

jesus = Node(type="Person", id="dr-jesus-barrasa")
jim = Node(type="Person", id="chief-scientist")
for author in [jesus, jim]:
    relationship = Relationship(

source=book,
target=author,


)

Le nœud de destination d’une relation WRITTEN_IN est une Person

Graph RAG avec LangChain et Neo4j

Les relations relient les nœuds

from langchain_neo4j.graphs.graph_document \
  import Node, Relationship

book = Node(type="Book", id=f"9781098127107")

jesus = Node(type="Person", id="dr-jesus-barrasa")
jim = Node(type="Person", id="chief-scientist")
for author in [jesus, jim]:
    relationship = Relationship(

source=book,
target=author,
type="WRITTEN_BY"
properties=dict(...)
)

Le schéma avec le nœud Book et deux relations WRITTEN_BY vers les auteurs, montrant qu’un nœud peut avoir plusieurs relations.

Graph RAG avec LangChain et Neo4j

Connexion à Neo4j

from langchain_neo4j import Neo4jGraph


graph = Neo4jGraph( url=NEO4J_URI, username=NEO4J_USERNAME, password=NEO4J_PASSWORD )
1 https://neo4j.com/docs/getting-started/
Graph RAG avec LangChain et Neo4j

Enregistrer des documents de graphe

from langchain_neo4j.graphs.graph_document import GraphDocument


# Wrapper for Nodes and Relationships doc = GraphDocument(
nodes=[book, jesus, jim],
relationships=[jesus_wrote_book, jim_wrote_book]
)
# Save nodes and relationships to the database graph.add_graph_documents([graph_document])
Graph RAG avec LangChain et Neo4j

Alors, qu’est-ce que GraphRAG ?

Le schéma précédent avec le contexte provenant des documents et de leurs embeddings

Graph RAG avec LangChain et Neo4j

Alors, qu’est-ce que GraphRAG ?

Le schéma précédent avec le contexte des documents, plus un graphe de connaissances montrant des nœuds et relations extraits des documents

Graph RAG avec LangChain et Neo4j

Passons à la pratique !

Graph RAG avec LangChain et Neo4j

Preparing Video For Download...