開始使用免費開始

Text-to-Cypher 擷取鏈

你現在已經有一個能把自然語言轉成 Cypher 陳述式的鏈。讓我們把它放進更大的鏈裡,實際執行這個由 LLM 產生的 Cypher,並用擷取到的資訊來回答使用者的問題。

本練習屬於課程

使用 LangChain 與 Neo4j 的 Graph RAG

檢視課程

練習說明

  • 更新程式碼,執行由 text_to_cypher_chain 產生的 Cypher 陳述式,並將結果指定給 QA_PROMPT 中使用的 "context" 變數。
  • 將輸入原樣傳遞給 "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)
編輯並執行程式碼