Text-to-Cypher Graph RAG กับ Neo4j

Graph RAG ด้วย LangChain และ Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

การแปลงภาษาธรรมชาติเป็น Cypher

ขั้นตอนการทำงานของแอปพลิเคชันจากอินพุตของผู้ใช้ไปยังแอปพลิเคชัน และการส่ง prompt ไปยัง LLM

Graph RAG ด้วย LangChain และ Neo4j

การแปลงภาษาธรรมชาติเป็น Cypher

ไดอะแกรมแอปพลิเคชันที่มีหน้าที่สร้างคำสั่ง Cypher

Graph RAG ด้วย LangChain และ Neo4j

การแปลงภาษาธรรมชาติเป็น Cypher

ขั้นตอนการสร้าง Cypher ในแอปพลิเคชันใช้ schema ของฐานข้อมูลกราฟ

Graph RAG ด้วย LangChain และ Neo4j

การแปลงภาษาธรรมชาติเป็น Cypher

ไดอะแกรมแอปพลิเคชันที่แสดงขั้นตอนการสร้าง Cypher ตามด้วยขั้นตอนการประมวลผล

Graph RAG ด้วย LangChain และ Neo4j

การแปลงภาษาธรรมชาติเป็น Cypher

แอปพลิเคชันประมวลผลคำสั่ง Cypher และส่งผลลัพธ์กลับมายังแอปพลิเคชัน

Graph RAG ด้วย LangChain และ Neo4j

การแปลงภาษาธรรมชาติเป็น Cypher

ผลลัพธ์ที่ได้จากคำสั่ง Cypher ถูกส่งกลับไปยังแอปพลิเคชัน

Graph RAG ด้วย LangChain และ Neo4j

การแปลงภาษาธรรมชาติเป็น Cypher

ผลลัพธ์ถูกนำไปใส่ใน prompt เพื่อส่งต่อไปยัง LLM

Graph RAG ด้วย LangChain และ Neo4j

สร้าง text-to-Cypher chain

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 ด้วย LangChain และ 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 ด้วย LangChain และ 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 was written by Dr Jesus Barrasa and Dr Jim Webber...
Graph RAG ด้วย LangChain และ Neo4j

มาฝึกกันเถอะ!

Graph RAG ด้วย LangChain และ Neo4j

Preparing Video For Download...