Graphes et RAG

Graph RAG avec LangChain et Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

Faites connaissance avec votre instructeur

 

  • Gestionnaire, formation des développeurs chez Neo4j
  • Formation des développeurs via Neo4j GraphAcademy
  • Plus de 20 ans d'expérience en développement logiciel
  • Plus de 10 ans d'expérience avec Neo4j

 

Photo d'Adam

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

Le R dans RAG

Diagramme d'application où une saisie utilisateur est ajoutée à une invite envoyée à un LLM

Graph RAG avec LangChain et Neo4j

Le R dans RAG

L'invite exige un contexte provenant de documents et de sources de données

Graph RAG avec LangChain et Neo4j

Le R dans RAG

Ces données sont souvent converties en incorporations vectorielles

Graph RAG avec LangChain et Neo4j

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

Les vecteurs sont efficaces pour

  • Questions floues ou ouvertes

Les vecteurs sont peu efficaces pour

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

Que pense Paul Graham de la Generative AI?

Dans combien de startups de Generative AI Paul Graham a-t-il investi?

Graph RAG avec LangChain et Neo4j

Vecteurs vs graphes de connaissances

Texte Incorporation vectorielle
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 diagramme de graphe montrant les entités des paroles de « Don't Stop Believin' ». Un petit gars de ville, avec des relations BORN et RAISED vers South Detroit

Graph RAG avec LangChain et Neo4j

Vecteurs vs graphes de connaissances

Texte Incorporation vectorielle
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 Incorporation vectorielle
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 va du nœud Midnight Train vers le nœud Anywhere

Graph RAG avec LangChain et Neo4j

Graphes de connaissances et Neo4j

 

  • Neo4j est la principale base de données de graphes au monde
  • Souple, schéma facultatif
  • Intégration LangChain langchain-neo4j
  • LCEL : LangChain Expression Language

Les logos de neo4j et langchain

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

Les nœuds représentent des choses

from langchain_neo4j.graphs.graph_document \
  import Node

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

Un nœud Livre dans un diagramme de graphe avec des propriétés pour l'ID et le titre

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 Livre 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 Livre

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 pour 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 diagramme de nœuds incluant le Livre et deux relations WRITTEN_BY vers les auteurs, montrant qu'un nœud peut avoir plusieurs relations.

Graph RAG avec LangChain et Neo4j

Se connecter à 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


# Enveloppe pour les nœuds et relations doc = GraphDocument(
nodes=[book, jesus, jim],
relationships=[jesus_wrote_book, jim_wrote_book]
)
# Enregistrer les nœuds et relations dans la base de données graph.add_graph_documents([graph_document])
Graph RAG avec LangChain et Neo4j

Alors, qu'est-ce que GraphRAG ?

Le diagramme précédent avec le contexte des documents et leurs incorporations

Graph RAG avec LangChain et Neo4j

Alors, qu'est-ce que GraphRAG ?

Le diagramme précédent avec le contexte des documents, et un graphe de connaissances ajouté montrant des nœuds et relations dérivés des documents

Graph RAG avec LangChain et Neo4j

Passons à la pratique !

Graph RAG avec LangChain et Neo4j

Preparing Video For Download...