RAG đồ thị Text-to-Cypher với Neo4j

Graph RAG với LangChain và Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

Chuyển ngôn ngữ tự nhiên sang Cypher

Luồng ứng dụng: đầu vào người dùng vào ứng dụng và prompt gửi tới LLM

Graph RAG với LangChain và Neo4j

Chuyển ngôn ngữ tự nhiên sang Cypher

Sơ đồ ứng dụng được giao tạo câu lệnh Cypher

Graph RAG với LangChain và Neo4j

Chuyển ngôn ngữ tự nhiên sang Cypher

Bước tạo Cypher trong ứng dụng dùng lược đồ cơ sở dữ liệu đồ thị

Graph RAG với LangChain và Neo4j

Chuyển ngôn ngữ tự nhiên sang Cypher

Sơ đồ ứng dụng hiển thị bước tạo Cypher rồi đến bước thực thi

Graph RAG với LangChain và Neo4j

Chuyển ngôn ngữ tự nhiên sang Cypher

Ứng dụng thực thi câu lệnh Cypher và trả kết quả về ứng dụng

Graph RAG với LangChain và Neo4j

Chuyển ngôn ngữ tự nhiên sang Cypher

Kết quả do câu lệnh Cypher truy xuất được gửi lại cho ứng dụng

Graph RAG với LangChain và Neo4j

Chuyển ngôn ngữ tự nhiên sang Cypher

Kết quả được chèn vào prompt, sẵn sàng gửi tới LLM

Graph RAG với LangChain và Neo4j

Xây dựng chuỗi 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 với LangChain và 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 với LangChain và 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 do Dr Jesus Barrasa và Dr Jim Webber viết...
Graph RAG với LangChain và Neo4j

Let's practice!

Graph RAG với LangChain và Neo4j

Preparing Video For Download...