NAT-NL2GQL: A Novel Multi-Agent Framework for Translating Natural Language to Graph Query Language

Yuanyuan Liang, Tingyu Xie, Gan Peng, Zihao Huang, Yunshi Lan, Weining QianarXiv 2024 · DOI

Abstract

Graph databases are powerful tools for managing complex relational data, yet their query languages (GQL) present a steep learning curve for non-experts. This paper introduces NAT-NL2GQL, a multi-agent framework designed to bridge the gap between natural language (NL) and GQL. By employing a collaborative loop consisting of a Preprocessor, a Generator, and a Refiner, the system mitigates the "error accumulation" problem prevalent in traditional pipelines. Evaluated on the SpCQL (Cypher) and a new StockGQL (nGQL) dataset, NAT-NL2GQL achieves state-of-the-art performance, demonstrating significant improvements in both exact match and execution accuracy.

Introduction

Graph databases like Neo4j and NebulaGraph are increasingly vital for uncovering intricate relationships within data. However, translating natural language questions into formal Graph Query Languages (GQL) like Cypher or nGQL remains a significant challenge. Unlike SQL, GQL involves complex graph schemas and lacks a unified standard, making the NL2GQL task particularly difficult for Large Language Models (LLMs).

An overview of the NL2GQL system translating a natural language question into a graph query.

Traditional approaches often follow a linear pipeline where errors in early stages—such as incorrect entity identification—propagate through the system, leading to total query failure. NAT-NL2GQL addresses this "error accumulation" problem by treating query generation as an iterative, multi-agent collaborative process.

The NAT-NL2GQL Framework

The core of the proposed system is a three-agent architecture that works in a feedback loop to refine queries. This design ensures that errors identified during execution can be corrected by revisiting earlier stages of the pipeline.

The multi-agent architecture of NAT-NL2GQL, featuring the Preprocessor, Generator, and Refiner agents.
  1. The Preprocessor Agent: Acting as a "data librarian," this agent performs Named Entity Recognition (NER) and aligns colloquial user terms with actual database entities using Locality-Sensitive Hashing (LSH). Crucially, it extracts a relevant subschema, filtering out irrelevant parts of the graph to reduce "noise" for the LLM.
  2. The Generator Agent: This is the core engine—a fine-tuned LLM (using LoRA for parameter-efficient tuning) trained specifically on NL-GQL pairs. It takes the cleaned context from the Preprocessor to draft the initial GQL statement.
  3. The Refiner Agent: Acting as "quality control," this agent executes the generated query against the database. If it encounters a syntax or logic error, it analyzes the error message and decides whether to fix the query directly or send feedback back to the Preprocessor to re-evaluate the context.

The system can iterate through this loop up to three times to converge on a correct answer, effectively using database feedback as a learning signal.

Datasets and Evaluation

The study evaluates the framework on two primary benchmarks:

  • SpCQL: A widely used dataset based on Neo4j’s Cypher syntax.
  • StockGQL (New): A benchmark introduced by the authors to address the scarcity of nGQL data. It is based on a real-world financial market graph (12 node types, 13 edge types, and 62 property types) and includes complex multi-hop queries (up to 6 hops).

Key Insights and Results

The framework achieved state-of-the-art (SOTA) results across the board:

  • StockGQL: Achieved an Exact Match (EM) accuracy of 85.44% and an Execution (EX) accuracy of 86.25%, outperforming the best baseline by roughly 2.4%.
  • SpCQL: Achieved 59.99% EM and 58.69% EX, representing a substantial ~5% improvement over previous SOTA methods.

One of the most significant findings was that subschema extraction is critical. Providing the LLM with the entire database schema often leads to confusion and hallucination. By extracting only the relevant subschema, the authors significantly boosted accuracy. Additionally, the Refiner's ability to treat error messages as features allowed the system to "backtrack" and learn from mistakes in real-time.

Limitations and Future Work

Despite these gains, the authors identify that 46% of remaining errors are still related to faulty schema extraction. If the system fails to identify the correct relationship path in a massive graph, the Generator cannot recover.

Future work aims to integrate Graph Neural Networks (GNNs) to improve the accuracy of subschema extraction and explore context-aware techniques to handle even larger, more complex schemas that might exceed current LLM token limits.

Summary updated

BibTeX
@misc{liang2024nat,
  author = {Yuanyuan Liang and Tingyu Xie and Gan Peng and Zihao Huang and Yunshi Lan and Weining Qian},
  title = {{NAT-NL2GQL: A Novel Multi-Agent Framework for Translating Natural Language to Graph Query Language}},
  year = {2024},
  eprint = {2412.10434},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2412.10434},
}
Figure