Neo4j ile Metinden Cypher’a Graph RAG

LangChain ve Neo4j ile Graph RAG

Adam Cowley

Manager, Developer Education at Neo4j

Doğal dili Cypher’a dönüştürme

Kullanıcı girdisinin uygulamaya ve bir istemin LLM’e gönderildiği akış

LangChain ve Neo4j ile Graph RAG

Doğal dili Cypher’a dönüştürme

Bir uygulama diyagramı: bir Cypher ifadesi üretme görevi

LangChain ve Neo4j ile Graph RAG

Doğal dili Cypher’a dönüştürme

Uygulamadaki Cypher üretimi, grafik veritabanı şemasını kullanır

LangChain ve Neo4j ile Graph RAG

Doğal dili Cypher’a dönüştürme

Uygulama diyagramı: Cypher üretimini bir yürütme aşaması izler

LangChain ve Neo4j ile Graph RAG

Doğal dili Cypher’a dönüştürme

Cypher ifadesi uygulama tarafından çalıştırılır ve sonuçlar uygulamaya döndürülür

LangChain ve Neo4j ile Graph RAG

Doğal dili Cypher’a dönüştürme

Cypher ifadesiyle alınan sonuçlar uygulamaya geri gönderilir

LangChain ve Neo4j ile Graph RAG

Doğal dili Cypher’a dönüştürme

Sonuçlar, LLM’e gönderilmeye hazır şekilde bağlama enjekte edilir

LangChain ve Neo4j ile Graph RAG

Metinden Cypher zinciri oluşturma

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) ...
LangChain ve Neo4j ile Graph RAG
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}")
])
LangChain ve Neo4j ile Graph RAG
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, Dr Jesus Barrasa ve Dr Jim Webber tarafından yazılmıştır...
LangChain ve Neo4j ile Graph RAG

Hadi pratik yapalım!

LangChain ve Neo4j ile Graph RAG

Preparing Video For Download...