Building a text-to-Cypher chain
You've written you're own Cypher queries, so let's try using LLMs to generate them! Generating a Cypher query from a user query in natural language is a key part of a text-to-Cypher Graph RAG workflow.
You're working with the same database as the previous exercise, containing nodes that represent people, companies, and WORKS_FOR
relationships available as graph
.
Este exercício faz parte do curso
Graph RAG with LangChain and Neo4j
Instruções do exercício
- Update the
partial_variables
dictionary to insert the graph schema into the prompt. - Create the text-to-Cypher chain using the
prompt
,llm
, and parsing to a string.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
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)