Text2Cypher

Text2Cypher turns a question in natural language into a Cypher query, so users and agents can get exact answers from the graph without writing Cypher. It’s the GraphRAG pipeline for analytical questions, where the answer is a value in the graph, such as a count or a statistic.

A Text2Cypher pipeline usually has three steps:

  1. Get the graph’s schema with the SHOW SCHEMA INFO query.
  2. Send the schema and the question to an LLM, which writes a Cypher query that returns the data needed for the answer.
  3. Run the query. If it fails, send the error message back to the LLM so it can correct the query, or let the user review and edit the query before it runs.

The generated query can be wrong when the LLM misreads the question, when the schema doesn’t explain what labels and properties mean, or when the LLM gets the syntax wrong. To improve the results:

  • Give the LLM a prompt written for Memgraph. See Cypher generation.
  • Add server-side descriptions to labels, edge types and properties. SHOW SCHEMA INFO returns them as description fields, so the LLM gets the units, meaning and relationships to other systems that names alone don’t convey.

Text2Cypher is built into GraphChat in Memgraph Lab, with all three steps. An agent connected through the Memgraph MCP server can also generate a query and run it with the run_query tool.

Cypher generation

LLMs learn Cypher mostly from public openCypher and Neo4j examples. Memgraph extends openCypher and uses MAGE procedures instead of APOC, so a prompt written for Memgraph produces better queries. The integrations use this prompt:

MEMGRAPH_GENERATION_TEMPLATE = """Your task is to directly translate natural language inquiry into precise and executable Cypher query for Memgraph database.
You will utilize a provided database schema to understand the structure, nodes and relationships within the Memgraph database.
Instructions:
- Use provided node and relationship labels and property names from the
schema which describes the database's structure. Upon receiving a user
question, synthesize the schema to craft a precise Cypher query that
directly corresponds to the user's intent.
- Generate valid executable Cypher queries on top of Memgraph database.
Any explanation, context, or additional information that is not a part
of the Cypher query syntax should be omitted entirely.
- Use Memgraph MAGE procedures instead of Neo4j APOC procedures.
- Do not include any explanations or apologies in your responses.
- Do not include any text except the generated Cypher statement.
- For queries that ask for information or functionalities outside the direct
generation of Cypher queries, use the Cypher query format to communicate
limitations or capabilities. For example: RETURN "I am designed to generate
Cypher queries based on the provided schema only."
Schema:
{schema}

With all the above information and instructions, generate Cypher query for the
user question.

The question is:
{question}"""

GraphChat in Memgraph Lab uses this prompt:

Your task is to directly translate natural language inquiry into precise and executable Cypher query for Memgraph database. You will utilize a provided database schema to understand the structure, nodes and relationships within the Memgraph database.

Rules:
Use provided node and relationship labels and property names from the schema which describes the database's structure. Schema is defined after keyword Schema down below. Upon receiving a user question, synthesize the schema to craft a precise Cypher query that directly corresponds to the user's intent.
Generate valid executable Cypher queries on top of Memgraph database. Any explanation, context, or additional information that is not a part of the Cypher query syntax should be omitted entirely.
Use Memgraph MAGE procedures instead of Neo4j APOC procedures.
Do not include any explanations or apologies in your responses.
Do not include any text except the generated Cypher statement.
For queries that ask for information or functionalities outside the direct generation of Cypher queries, use the Cypher query format to communicate limitations or capabilities. For example: RETURN "I am designed to generate Cypher queries based on the provided schema only."

GraphChat also sends the schema, the conversation history when it’s needed, and rules for write permissions. For example, if writes aren’t allowed, it refuses a DELETE query with “Sorry, can’t do that! DELETE queries are not permitted.” It adds notes on Cypher that Memgraph handles differently:

Atom expressions
Atom expressions are not supported, so if you need to do something like this:
MATCH (n)
RETURN n, size((n)-->()) AS edgeCount

Use the following structure with OPTIONAL MATCH or a combination of queries using the keyword WITH:
MATCH (n)
OPTIONAL MATCH (n)-[r]->()
RETURN n, count(r) AS edgeCount

When a generated query fails, GraphChat sends the query and Memgraph’s error message back to the LLM with these instructions:

Generate a new correct version of the Cypher query:
Do not include any explanations or apologies in your responses.
Do not wrap the Cypher query in backticks.