開始使用免費開始

建立 text-to-Cypher chain

你已經自己寫過 Cypher 查詢了,現在來試試用 LLM 自動產生吧!從使用者的自然語言詢問產生 Cypher 查詢,是 text-to-Cypher Graph RAG 工作流程的關鍵步驟。

你將使用和上一個練習相同的資料庫,其中包含代表人物、公司等節點,還有 WORKS_FOR 關係。這個圖形資料庫可透過 graph 取得。

本練習屬於課程

使用 LangChain 與 Neo4j 的 Graph RAG

檢視課程

練習說明

  • 更新 partial_variables 字典,將圖形結構(schema)插入到 prompt 中。
  • 使用 promptllm,並將輸出解析為字串,建立 text-to-Cypher chain。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

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)
編輯並執行程式碼