Combinare grafi lessicali con la ricerca vettoriale

Graph RAG con LangChain e Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

Usare le relazioni nella ricerca semantica

Com'è lavorare in Neo4j?

Un diagramma di grafo con un nodo Review e la proprietà di testo "Neo4j is a great place to work"

Graph RAG con LangChain e Neo4j

Usare le relazioni nella ricerca semantica

Com'è lavorare in Neo4j?

La recensione è pubblicata da una persona con lo username @emileifrem

Graph RAG con LangChain e Neo4j

Usare le relazioni nella ricerca semantica

Com'è lavorare in Neo4j?

Emil Eifrem ha una relazione WORKS_AT con un nodo Company chiamato Neo4j

Graph RAG con LangChain e Neo4j

Da proprietà di testo a embedding

 

  • Crea embedding dei nodi dalla proprietà text

una scena con relazioni HAS_CHUNK verso nodi Chunk con una proprietà stringa

Graph RAG con LangChain e Neo4j

Da proprietà di testo a embedding

 

  • Crea embedding dei nodi dalla proprietà text
  • Ricerca vettoriale sugli embedding
  • Usa le relazioni come contesto

Le proprietà di testo dei nodi vengono usate per creare un embedding

Graph RAG con LangChain e Neo4j

Neo4j come vector store

from langchain_neo4j import Neo4jVector

 

  • Neo4jVector.from_documents(): crea nodi e indice dal basso da Document
  • Neo4jVector.from_existing_graph(): crea un indice su combinazioni esistenti di label e proprietà
Graph RAG con LangChain e Neo4j

Chunking di proprietà nodo esistenti

Diagramma di knowledge graph che evidenzia la relazione HAS_SCENE dal nodo Act al nodo Scene

res = graph.query("""

MATCH (s:Scene)
WHERE NOT {(s)-[:HAS_LINE]->()}
RETURN s.id AS id, s.text AS text
""")
for scene in res: # Create chunks from scene['text']
Graph RAG con LangChain e Neo4j

Chunking di proprietà nodo esistenti

  • Dividi text per "\n\n"
  • Le didascalie di scena hanno una riga
  • Le battute iniziano con il nome del personaggio
    • Le righe seguenti contengono le parole pronunciate
 Enter Sampson and Gregory armed with swords..

SAMPSON.
Gregory, on my word...
GREGORY. No, for then we should...
SAMPSON. I mean, if we be in choler, we'll draw...
Graph RAG con LangChain e Neo4j

Chunking di proprietà nodo esistenti

# Split text into lines
lines = [
    line.strip() for line in text.split("\n\n")

if "\n" in line
]
for line in lines: parts = line.split("\n")
character = parts[0]
text = "\n".join(parts[1:])
Graph RAG con LangChain e Neo4j

Chunking di proprietà nodo esistenti

# ...Continuing loop over scenes

line_node = Node(type="Line", id=f"{scene.id}-line-{i}", properties={})
character_node = Node(type="Character", id=character, properties={})


# (:Scene)-[:HAS_LINE]->(:Line) graph_document.relationships.append( Relationship(source=scene, target=line_node, type="HAS_LINE"))
# (:Line)-[:SPOKEN_BY]->(:Character) graph_document.relationships.append(Relationship( source=line_node, target=characters[character], type="SPOKEN_BY"))
Graph RAG con LangChain e Neo4j

Creare un indice vettoriale su un grafo esistente

from langchain.embeddings import init_embeddings
store = Neo4jVector.from_existing_graph(

init_embeddings("openai:text-embedding-3-small"),
url=NEO4J_URI, username=NEO4J_USERNAME, password=NEO4J_PASSWORD,
node_label="Line",
text_node_properties=["text"],
embedding_node_property="embedding",
index_name="lines",
)
Graph RAG con LangChain e Neo4j

Recupero vettoriale con LCEL

# Create retriever
retriever = store.as_retriever()

# Invoke manually retriever.invoke("What does Romeo think of Juliet?") # [ Document, Document, ... ]
Graph RAG con LangChain e Neo4j

Passiamo alla pratica!

Graph RAG con LangChain e Neo4j

Preparing Video For Download...