Get startedGet started for free

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.

This exercise is part of the course

Graph RAG with LangChain and Neo4j

View Course

Exercise instructions

  • 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.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

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)
Edit and Run Code