เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

Text-to-Cypher retrieval chain

ตอนนี้มี chain ที่สามารถแปลงภาษาธรรมชาติให้เป็น Cypher statement แล้ว ต่อไปจะนำมันไปใช้ใน chain ที่ใหญ่ขึ้น เพื่อรัน Cypher ที่ LLM สร้างขึ้น และตอบคำถามของผู้ใช้ด้วยข้อมูลที่ดึงมาได้

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Graph RAG ด้วย LangChain และ Neo4j

ดูคอร์ส

คำแนะนำการฝึกหัด

  • แก้ไขโค้ดเพื่อรัน Cypher statement ที่ได้จาก text_to_cypher_chain แล้วกำหนดผลลัพธ์ให้กับตัวแปร "context" ที่ใช้ใน QA_PROMPT
  • ส่ง input ไปยังตัวแปร "question" โดยไม่เปลี่ยนแปลงค่าใด ๆ

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

qa_prompt = ChatPromptTemplate.from_messages([
    SystemMessagePromptTemplate.from_template(QA_PROMPT),
    HumanMessagePromptTemplate.from_template("Question: {question}")
])

qa_chain = {
  	# Use output of text_to_cypher to get database results
    "context": text_to_cypher_chain | ____(lambda cypher: graph.____(cypher)),
    # Pass the user input through to the "question" variable
    "question": ____
} | qa_prompt | llm | StrOutputParser()

res = qa_chain.invoke({"question": "What companies is Harrison Chase connected to?"})
print(res)
แก้ไขและรันโค้ด