liveBook · Manning

Chapter 4: Generating Cypher queries from natural language questions

What you will learn in chapter 4 of Essential GraphRAG

This chapter introduces generating Cypher queries from natural-language questions to make knowledge-graph retrieval more flexible and dynamic.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces generating Cypher queries from natural-language questions to make knowledge-graph retrieval more flexible and dynamic.

Key terms: Cypher queries, natural language questions, text2cypher, hardcoded Cypher queries, retrieval system, query language generation, text2cypher retriever, base model, specialized (finetuned) LLMs, Movies dataset

4.1 The basics of query language generation

This section defines text-to-Cypher generation, explains why graph schema is essential for accurate queries, and outlines the generation workflow.

Key terms: query language generation, Cypher queries, natural language questions, knowledge graph, schema, graph model, node labels, relationship types, properties, semantics

4.2 Where query language generation fits in the RAG pipeline

This section explains when text2cypher retrieval belongs in a RAG pipeline: for graph questions that require structured querying or aggregation rather than similarity search alone.

Key terms: vector similarity search, Cypher, text2cypher, aggregation, generated Cypher, knowledge graph, catchall retriever, retrievers, agentic system

4.3 Useful practices for query language generation

This section introduces practices for improving the correctness and relevance of Cypher queries generated by LLMs from natural-language questions.

Key terms: Cypher queries, natural language questions, database schema, schema elements, query language generation, correct, relevant

4.3.1 Using few-shot examples for in-context learning

This section explains how manually supplied, graph-specific question-and-Cypher examples help an LLM generate better Cypher queries through in-context learning.

Key terms: few-shot examples, in-context learning, zero-shot examples, text2cypher, knowledge graph, schema, property, traversal, Movie, Country

4.3.2 Using database schema in the prompt to show the LLM the structure of the knowledge graph

This section shows how to ground text-to-Cypher generation in an explicit graph schema, infer that schema from Neo4j, and format it for an LLM prompt.

Key terms: schema, node labels, relationship types, properties, Cypher, complete knowledge graph, schema inference, sample the database, Neo4j, APOC library

4.3.3 Adding terminology mapping to semantically map the user question to the schema

This section explains how graph-specific terminology mappings in the prompt help an LLM translate user wording into the correct knowledge-graph schema terms.

Key terms: terminology mappings, schema, labels, relationship types, properties, Person, Movie, node label, terminology mapping, knowledge graph specific

4.3.4 Format instructions

This section shows how explicit prompt format instructions make text2cypher LLM outputs consistently usable as raw Cypher queries.

Key terms: format instructions, Cypher statement, code blocks

4.4 Implementing a text2cypher generator using a base model

This section puts text2cypher prompting into practice by assembling a base-model prompt from graph guidance and using it to generate a Cypher statement.

Key terms: text2cypher generator, base model, schema, terminology mapping, few-shot examples, format instructions, Cypher statement, graph database schema, relationship types, properties

4.5 Specialized (finetuned) LLMs for text2cypher

This section introduces Neo4j's specialized finetuned text2cypher models, their efficiency tradeoff against larger models, and prompt practices that can improve them.

Key terms: text2cypher, finetuning, open source training data, Gemma2, Llama 3.1, finetuned models, GPT, Gemini, efficiency, production systems

4.6 What we’ve learned and what text2cypher enables

This section summarizes how to build and iteratively improve a text2cypher retriever that generates reliable Cypher queries for a knowledge graph.

Key terms: text2cypher retriever, Cypher queries, knowledge graph, few-shot examples, schema, terminology mappings, format instructions, prompt, generated queries, retriever

4.7 Summary

This summary positions text2cypher query generation as a complementary RAG retriever and recaps prompt-based and finetuned approaches for producing Cypher queries.

Where this is used later

Concepts explained in this chapter

← Chapter 3: Advanced vector retrieval strategies Chapter 5: Agentic RAG →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.