Dynamic Query Generation: The AI as Translator

Dynamic Query Generation: The AI as Translator

Turn natural language into Cypher queries. Learn how to build robust prompts that allow an LLM to dynamically generate graph traversals based on a user's intent and your graph schema.

Dynamic Query Generation: The AI as Translator

In a real-world Graph RAG application, you cannot hard-code every Cypher query. The user might ask anything from "Who is Sudeep?" to "Find the shortest path between the CEO and the coffee machine." To handle this variety, we use Text-to-Cypher: turning the user's natural language into a database query.

In this final lesson of Module 8, we will learn the architecture of a Dynamic Query Generator. We will explore the Few-Shot Prompting strategy, the importance of provide a Schema Map, and the "Self-Correction" loop that allows an LLM to fix its own Cypher syntax if the database returns an error.


1. The Schema-First Prompt

An LLM cannot guess your graph structure. If you don't tell it that employees are labeled :Person, it might try to use :User or :Human.

The Golden Prompt Requirement: You must include a "Schema Definition" in the system prompt.

  • Nodes: (:Person {name, age}), (:Project {title, budget})
  • Edges: (:Person)-[:LEADS]->(:Project)

Example Prompt: "You are a Neo4j expert. Given the schema above, translate the user's question into a Cypher query. Only return the query."


2. Few-Shot Examples: Teaching Patterns

LLMs are "Pattern Matchers." One of the best ways to ensure they write good Cypher is to give them a few "Examples" in the prompt.

  • Question: "Who works with Sudeep?"
  • Cypher: MATCH (p:Person {name: 'Sudeep'})-[:WORKS_WITH]-(c) RETURN c.name

By giving 3-5 of these pairs, you teach the model the specific "Style" of query you want (e.g., "Always use LIMIT 10", "Only use directed edges").


3. The Self-Correction Loop (The "Try-Catch" Agent)

Even the best LLMs occasionally hallucinate a relationship or mess up a comma in Cypher.

The Loop Architecture:

  1. LLM generates Cypher.
  2. Code attempts to run Cypher in the database.
  3. If it fails: The error message (e.g., "Invalid syntax at line 2...") is sent BACK to the LLM.
  4. LLM reads the error and generates a corrected query.

This process (often called "Reflexion") dramatically increases the success rate of complex graph queries.

graph TD
    User[Natural Language] --> P[Prompt with Schema]
    P --> LLM[LLM Generator]
    LLM --> Cypher[Cypher Query]
    Cypher --> DB[(Graph Database)]
    DB -->|Syntax Error| Corr[LLM Self-Correction]
    Corr --> Cypher
    DB -->|Success| Results[Context]
    
    style DB fill:#4285F4,color:#fff
    style LLM fill:#34A853,color:#fff

4. Implementation: A Text-to-Cypher Chain in Python

Let's look at a simplified version of the LangChain GraphCypherQAChain logic.

def generate_cypher(schema, question):
    prompt = f"""
    Schema: {schema}
    Helpful Examples:
    Q: "Who leads Project X?" -> MATCH (p:Person)-[:LEADS]->(project {{title: 'Project X'}}) RETURN p.name
    
    Q: "{question}"
    Cypher:"""
    
    # Call to LLM
    cypher_query = llm.generate(prompt)
    return cypher_query.strip()

# EXAMPLE
q = "Find the manager of Sudeep."
schema = "(:Person {name})-[:REPORTS_TO]->(:Person)"
print(generate_cypher(schema, q))
# OUTPUT: MATCH (p:Person {name: 'Sudeep'})-[:REPORTS_TO]->(m:Person) RETURN m.name

5. Summary and Exercises

Dynamic generation turns your graph into a "Consultant."

  • Schema Maps provide the LLM with the "Goggles" to see your database.
  • Few-Shot Examples teach the LLM the "Dialect" of your Cypher.
  • Self-Correction handles the edge cases of hallucination.
  • Sandboxing is mandatory: Ensure the LLM-generated query is Read-Only (No DELETE or DROP).

Exercises

  1. Prompt Engineering: Write a system prompt that explicitly forbids the LLM from deleting data. How would you test if it works?
  2. Schema Design: If your schema has 50 labels and 200 relationship types, should you send the whole schema to the LLM? What is the risk? (Hint: Token limits and confusion).
  3. Self-Correction Task: Imagine the LLM wrote MATCH (p:Person)-[:WORKS]->(o). The DB returns: "Relationship type 'WORKS' not found. Did you mean 'WORKS_AT'?". Write the text you would send back to the LLM to help it fix the query.

Congratulations! You have completed Module 8: Graph Querying for Retrieval. You are now a master of the "Bridge" between natural language and structured connections.

In Module 9: Graph RAG Retrieval Strategies, we will move to the "Strategy" level—choosing the best retrieval pattern for different types of intelligence.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn