Tool

Build Neo4j Queries from Natural Language

LlamaIndex tool that turns natural-language questions into Cypher queries against a Neo4j database.

Works with neo4jopenai

69
Spark score
out of 100
Updated 2 days ago
Version 0.14.23
Models

Add to Favorites

Why it matters

Effortlessly query your Neo4j graph database using natural language. This asset translates user questions into accurate Cypher queries, even recovering from syntax errors.

Outcomes

What it gets done

01

Generate Cypher queries from natural language questions.

02

Connect to and query Neo4j databases.

03

Automatically correct Cypher syntax errors.

04

Leverage LLMs for intelligent query generation.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/li-tool-tools-neo4j | bash

Overview

Neo4j Schema Query Builder

Neo4jQueryToolSpec lets an agent query a Neo4j graph database in natural language, using an LLM to generate schema-guided Cypher queries with a self-healing mechanism that corrects syntax errors. Use it when an agent needs to answer questions against an existing Neo4j database without hand-writing Cypher. It requires a running Neo4j instance and an LLM to drive query generation.

What it does

Neo4jQueryToolSpec lets an agent query a Neo4j graph database by translating natural-language questions into Cypher queries, guided by the database's own schema. It has three named features: schema-based querying (extracts the Neo4j schema to guide Cypher generation), self-healing (corrects itself on a Cypher syntax error to produce a valid query), and language model integration (uses an LLM for natural, accurate Cypher generation).

When to use - and when NOT to

Use it when an agent needs to answer questions against an existing Neo4j graph database without you hand-writing Cypher -- for example asking "Where is JFK airport is located?" and getting back a generated Cypher query plus a natural-language answer. It requires a running Neo4j instance with connection credentials and its own LLM to drive query generation, so it is not usable without both already set up.

Inputs and outputs

Install with:

pip install llama-index-tools-neo4j

Initialize with your Neo4j connection details and an LLM, then attach to an agent:

from llama_index.tools.neo4j import Neo4jQueryToolSpec
from llama_index.llms.openai import OpenAI
from llama_index.core.agent.workflow import FunctionAgent

gds_db = Neo4jQueryToolSpec(
    url="neo4j-url",
    user="neo4j-user",
    [REDACTED],
    llm=llm,
    database="neo4j",
)

tools = gds_db.to_tool_list()
agent = FunctionAgent(
    tools=tools,
    llm=OpenAI(model="gpt-4.1"),
)

Required parameters are url (connection string), user, password, llm (any LLM, used for Cypher generation), and database (the database name). A natural-language question goes in, and the agent returns both the generated Cypher and a synthesized answer -- for example asking about JFK airport's location returns MATCH (p:Port {port_code: 'JFK'}) RETURN p.location_name_wo_diacritics AS Location plus "The port code JFK is located in New York, United States."

Who it's for

Developers building agents that need natural-language access to a Neo4j graph database without writing or debugging Cypher by hand.

Source README

Neo4j Schema Query Builder

pip install llama-index-tools-neo4j

The Neo4jQueryToolSpec class provides a way to query a Neo4j graph database based on a provided schema definition. The class uses a language model to generate Cypher queries from user questions and has the capability to recover from Cypher syntax errors through a self-healing mechanism.

Table of Contents

Usage

Initialization

Initialize the Neo4jQueryToolSpec class with:

from llama_index.tools.neo4j import Neo4jQueryToolSpec
from llama_index.llms.openai import OpenAI
from llama_index.core.agent.workflow import FunctionAgent

gds_db = Neo4jQueryToolSpec(
    url="neo4j-url",
    user="neo4j-user",
    [REDACTED],
    llm=llm,
    database="neo4j",
)

tools = gds_db.to_tool_list()
agent = FunctionAgent(
    tools=tools,
    llm=OpenAI(model="gpt-4.1"),
)

Where:

  • url: Connection string for the Neo4j database.
  • user: Username for the Neo4j database.
  • password: Password for the Neo4j database.
  • llm: A language model for generating Cypher queries (any type of LLM).
  • database: The database name.

Running a Query

To use the agent:

### use agent
resp = await agent.run("Where is JFK airport is located?")
Generated Cypher:

MATCH (p:Port {port_code: 'JFK'})
RETURN p.location_name_wo_diacritics AS Location

Final answer:
'The port code JFK is located in New York, United States.'

Features

  • Schema-Based Querying: The class extracts the Neo4j database schema to guide the Cypher query generation.
  • Self-Healing: On a Cypher syntax error, the class corrects itself to produce a valid query.
  • Language Model Integration: Uses a language model for natural and accurate Cypher query generation.

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.