开始使用免费开始使用

构建 text-to-Cypher 链

您已经手写过 Cypher 查询了,现在试试用 LLM 来生成吧!从自然语言的用户查询生成 Cypher 查询,是 text-to-Cypher 的 Graph RAG 工作流中的关键一步。

您仍在使用与上一个练习相同的数据库,其中包含表示人员、公司节点,以及 WORKS_FOR 关系,并已作为 graph 提供。

本练习是课程的一部分

使用 LangChain 和 Neo4j 的 Graph RAG

查看课程

练习说明

  • 更新 partial_variables 字典,把图的模式信息插入到提示中。
  • 使用 promptllm 并解析为字符串,创建 text-to-Cypher 链。

交互式实操练习

通过完成这段示例代码来试试这个练习。

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