开始使用免费开始使用

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)
编辑并运行代码