การสร้าง text-to-Cypher chain
ได้เขียน Cypher query เองแล้ว คราวนี้ลองให้ LLM เป็นคนสร้างแทนกันดูบ้าง! การแปลงคำถามจากภาษาธรรมชาติให้เป็น Cypher query ถือเป็นขั้นตอนสำคัญในกระบวนการทำงานแบบ text-to-Cypher Graph RAG
แบบฝึกหัดนี้ใช้ฐานข้อมูลเดิมจากแบบฝึกหัดก่อนหน้า ซึ่งมี node แทนบุคคลและบริษัท รวมถึง relationship แบบ WORKS_FOR โดยเข้าถึงได้ผ่านตัวแปร graph
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Graph RAG ด้วย LangChain และ Neo4j
คำแนะนำการฝึกหัด
- อัปเดต dictionary
partial_variablesเพื่อแทรก schema ของกราฟลงใน prompt - สร้าง text-to-Cypher chain โดยใช้
prompt,llmและการ parse เป็น string
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
prompt = ChatPromptTemplate.from_messages([
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. Only generate the Cypher statement, no pre-amble. Do not return any Markdown.
Schema:
{schema}
Question: {question}""",
# Update the partial_variables dictionary
partial_variables={"schema": graph.____})
])
# Create the text-to-Cypher chain
text_to_cypher_chain = ____ | ____ | StrOutputParser()
cypher = text_to_cypher_chain.invoke({"question": "Where does Jo Cornelissen work?"})
print(cypher)