Grafi e RAG

Graph RAG con LangChain e Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

Incontra il tuo istruttore

 

  • Manager, Developer Education in Neo4j
  • Formazione per sviluppatori tramite Neo4j GraphAcademy
  • 20+ anni di esperienza nello sviluppo software
  • 10+ anni di esperienza con Neo4j

 

Foto di Adam

1 https://graphacademy.neo4j.com
Graph RAG con LangChain e Neo4j

La R in RAG

Un diagramma di applicazione con un input utente aggiunto a un prompt inviato a un LLM

Graph RAG con LangChain e Neo4j

La R in RAG

Il prompt richiede contesto da documenti e fonti dati

Graph RAG con LangChain e Neo4j

La R in RAG

Questi dati spesso vengono convertiti in vector embedding

Graph RAG con LangChain e Neo4j

Dove fallisce la ricerca semantica?

I vettori sono adatti a

  • Domande vaghe o aperte

I vettori sono poco efficaci per

  • Domande molto specifiche o fattuali
  • Query numeriche o a corrispondenza esatta
  • Query logiche

Cosa pensa Paul Graham della Generative AI?

In quante startup di Generative AI ha investito Paul Graham?

Graph RAG con LangChain e Neo4j

Vettori vs. knowledge graph

Testo 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 diagramma di grafo con le entità dei testi di Don't Stop Believin': un ragazzo di provincia, con relazioni BORN e RAISED verso South Detroit

Graph RAG con LangChain e Neo4j

Vettori vs. knowledge graph

Testo 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]

I nodi sono: una persona (Small town boy), una località (South Detroit), un treno (Midnight Train) e una località (Anywhere)

Graph RAG con LangChain e Neo4j

Vettori vs. knowledge graph

Testo 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]

Le relazioni BORN e RAISED vanno dalla persona alla località. La relazione GOING va dal nodo Midnight Train al nodo Anywhere

Graph RAG con LangChain e Neo4j

Knowledge graph e Neo4j

 

  • Neo4j è il principale database a grafo al mondo
  • Flessibile, schema-optional
  • Integrazione LangChain langchain-neo4j
  • LCEL: LangChain Expression Language

I loghi di neo4j e langchain

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

I nodi rappresentano cose

from langchain_neo4j.graphs.graph_document \
  import Node

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

Un nodo Book in un grafo con proprietà ID e Title

Graph RAG con LangChain e Neo4j

Le relazioni collegano i nodi

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(





)

Due relazioni WRITTEN_BY che vanno da un nodo Book a due nodi Person che rappresentano gli autori

Graph RAG con LangChain e Neo4j

Le relazioni collegano i nodi

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,



)

Il nodo sorgente della relazione WRITTEN_BY è il nodo Book

Graph RAG con LangChain e Neo4j

Le relazioni collegano i nodi

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,


)

Il nodo di destinazione per una relazione WRITTEN_IN è una Persona

Graph RAG con LangChain e Neo4j

Le relazioni collegano i nodi

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(...)
)

Il diagramma con il Book e due relazioni WRITTEN_BY verso gli autori per mostrare che i nodi possono avere molte relazioni.

Graph RAG con LangChain e Neo4j

Connessione a 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 con LangChain e Neo4j

Salvataggio dei graph document

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 con LangChain e Neo4j

Quindi, cos'è GraphRAG?

Il diagramma precedente con contesto da documenti e loro embedding

Graph RAG con LangChain e Neo4j

Quindi, cos'è GraphRAG?

Il diagramma precedente con contesto dai documenti e un knowledge graph aggiunto con nodi e relazioni derivati dai documenti

Graph RAG con LangChain e Neo4j

Ayo berlatih!

Graph RAG con LangChain e Neo4j

Preparing Video For Download...