Neo4j के साथ Text-to-Cypher Graph RAG

LangChain और Neo4j के साथ Graph RAG

Adam Cowley

Manager, Developer Education at Neo4j

प्राकृतिक भाषा को Cypher में बदलना

यूज़र इनपुट ऐप में जाता है और LLM को एक प्रॉम्प्ट भेजा जाता है

LangChain और Neo4j के साथ Graph RAG

प्राकृतिक भाषा को Cypher में बदलना

ऐप डायग्राम जहाँ उसे एक Cypher स्टेटमेंट जनरेट करना है

LangChain और Neo4j के साथ Graph RAG

प्राकृतिक भाषा को Cypher में बदलना

Cypher जनरेशन स्टेप ऐप में ग्राफ डेटाबेस स्कीमा का उपयोग करता है

LangChain और Neo4j के साथ Graph RAG

प्राकृतिक भाषा को Cypher में बदलना

ऐप डायग्राम जो Cypher जनरेशन और उसके बाद के execution फेज़ को दिखाता है

LangChain और Neo4j के साथ Graph RAG

प्राकृतिक भाषा को Cypher में बदलना

ऐप Cypher स्टेटमेंट चलाता है और परिणाम वापस लाता है

LangChain और Neo4j के साथ Graph RAG

प्राकृतिक भाषा को Cypher में बदलना

Cypher से प्राप्त परिणाम ऐप को वापस भेजे जाते हैं

LangChain और Neo4j के साथ Graph RAG

प्राकृतिक भाषा को Cypher में बदलना

परिणाम प्रॉम्प्ट में डाले जाते हैं ताकि LLM को भेजे जा सकें

LangChain और Neo4j के साथ Graph RAG

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) ...
LangChain और Neo4j के साथ 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 और Neo4j के साथ 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 और Dr Jim Webber ने लिखा...
LangChain और Neo4j के साथ Graph RAG

अभ्यास करते हैं!

LangChain और Neo4j के साथ Graph RAG

Preparing Video For Download...