Graph RAG da testo a Cypher con Neo4j

Graph RAG con LangChain e Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

Dal linguaggio naturale a Cypher

Flusso: input utente nell'app e prompt inviato all'LLM

Graph RAG con LangChain e Neo4j

Dal linguaggio naturale a Cypher

Diagramma dell'app incaricata di generare un'istruzione Cypher

Graph RAG con LangChain e Neo4j

Dal linguaggio naturale a Cypher

La generazione Cypher nell'app usa lo schema del grafo

Graph RAG con LangChain e Neo4j

Dal linguaggio naturale a Cypher

Diagramma: generazione di Cypher seguita dall'esecuzione

Graph RAG con LangChain e Neo4j

Dal linguaggio naturale a Cypher

L'istruzione Cypher viene eseguita dall'app e i risultati tornano all'app

Graph RAG con LangChain e Neo4j

Dal linguaggio naturale a Cypher

I risultati recuperati dall'istruzione Cypher vengono inviati all'applicazione

Graph RAG con LangChain e Neo4j

Dal linguaggio naturale a Cypher

I risultati vengono inseriti nel prompt pronti per essere inviati all'LLM

Graph RAG con LangChain e Neo4j

Creare una catena text-to-Cypher

prompt = SystemMessagePromptTemplate.from_template(""" 
You are an expert Neo4j developer. Use the following database schema to write 
a Cypher statement to answer the user's question...

Schema: {schema}
Question: {question}
""", partial_variables={"schema": graph.schema})


# Compile the chain cypher_chain = cypher_prompt | llm | StrOutputParser()
# Invoke the chain cypher_chain.invoke({"question": "Who wrote Building Knowledge Graphs?"})
MATCH (b:Book {title: "Building Knowledge Graphs"})-[:WRITTEN_BY]->(a:Author) ...
Graph RAG con LangChain e Neo4j
QA_PROMPT = "You are a helpful assistant. Use the data retrieved from the graph to answer the
user's question. Data: {context}"

answer_prompt = ChatPromptTemplate.from_messages([
    SystemMessagePromptTemplate.from_template(QA_PROMPT),
    HumanMessagePromptTemplate.from_template("Question: {question}")
])
Graph RAG con LangChain e Neo4j
cypher_qa_chain = (
  # Generate and execute the Cypher to get results
  {

"question": RunnablePassthrough()
"context": text_to_cypher_chain | RunnableLambda(lambda cypher: graph.query(cypher)),
}
# Format prompt and pass to LLM | answer_prompt | answer_llm | StrOutputParser()
)
res = cypher_qa_chain.invoke({"question": "Who wrote Building Knowledge Graphs?"})
Building Knowledge Graphs è stato scritto dal Dr Jesus Barrasa e dal Dr Jim Webber...
Graph RAG con LangChain e Neo4j

Passons à la pratique !

Graph RAG con LangChain e Neo4j

Preparing Video For Download...