Abstract
Integrating Large Language Models (LLMs) with existing Knowledge Graph (KG) databases presents a promising avenue for enhancing LLMs' efficacy and mitigating their "hallucinations". Given that most KGs reside in graph databases accessible solely through specialized query languages (e.g., Cypher), it is critical to connect LLMs with KG databases by automating the translation of natural language into Cypher queries (termed as "Text2Cypher" task). Prior efforts tried to bolster LLMs' proficiency in Cypher generation through Supervised Fine-Tuning (SFT). However, these explorations are hindered by the lack of annotated datasets of Query-Cypher pairs, resulting from the labor-intensive and domain-specific nature of such annotation. In this study, we propose SyntheT2C, a methodology for constructing a synthetic Query-Cypher pair dataset, comprising two distinct pipelines: (1) LLM-based prompting and (2) template-filling. SyntheT2C is applied to two medical KG databases, culminating in the creation of a synthetic dataset, MedT2C. Comprehensive experiments demonstrate that the MedT2C dataset effectively enhances the performance of backbone LLMs on Text2Cypher task via SFT. Both the SyntheT2C codebase and the MedT2C dataset are released at https://github.com/ZGChung/SyntheT2C.
Introduction
While Large Language Models (LLMs) have demonstrated remarkable capabilities across various domains, they are frequently susceptible to "hallucinations"—generating factual inaccuracies. Knowledge Graphs (KGs) offer a structured and verifiable alternative for information retrieval. However, most KGs are stored in graph databases like Neo4j, which require specialized query languages such as Cypher.
The "Text2Cypher" task—translating natural language questions into functional Cypher queries—is essential for enabling LLMs to interact with these databases. Training models for this task is challenging due to the scarcity of high-quality, annotated datasets. Creating such datasets manually requires expertise in both the specific domain (e.g., medicine) and Cypher syntax. SyntheT2C addresses this bottleneck by automating the generation of high-quality synthetic data.
Methodology: The SyntheT2C Framework
The SyntheT2C framework employs a dual-pipeline approach to generate diverse and syntactically rigorous Query-Cypher pairs.
Generation Pipelines
- LLM-based Prompting ($P_1$): This pipeline leverages a "teacher" model (e.g., GPT-4o) to generate natural language questions and corresponding Cypher queries based on extracted KG metadata. This ensures semantic variety, capturing the diverse ways users might phrase their inquiries.
- Template-filling ($P_2$): To ensure syntactic complexity, the framework utilizes manually crafted templates populated with actual values sampled from the database. This allows for the generation of advanced queries involving multi-hop paths and complex aggregations that zero-shot models often fail to produce.
Quality Validation
To ensure the reliability of the synthetic data, SyntheT2C includes a five-stage automatic validation suite:
- Grammatical Validator: Verifies the executability of the Cypher code.
- Semantic Validator: Uses an LLM to back-translate the Cypher into English for comparison with the original question.
- Entity Validator: Confirms that all entities in the question are present in the query.
- Schema Validator: Ensures the relationships used in the query exist within the database schema.
- Coherence Validator: Executes the query and checks if the results are logically consistent with the question.
Experimental Setup and Datasets
The researchers applied SyntheT2C to two prominent medical knowledge graphs: LHY (a Chinese-language medical database) and Hetionet (an English-language biomedical database). The resulting synthetic dataset, MedT2C, contains 3,000 high-quality pairs. Evaluation was conducted using a separate, manually verified "gold standard" set of 300 questions.
The study focused on fine-tuning 7B-parameter models, including Llama3, Qwen2, and InternLM2, using Low-Rank Adaptation (LoRA).
Key Results and Insights
The impact of Supervised Fine-Tuning (SFT) with the MedT2C dataset was substantial:
- Improved Accuracy: For Llama3, execution result accuracy increased from 27.83% to 39.65% post-SFT.
- Enhanced Query Quality: Fine-tuned models showed a significantly higher "win rate" in head-to-head comparisons judged by GPT-4o. For example, InternLM2's win rate against the baseline improved by over 10 percentage points.
- The Hybrid Advantage: Results indicate that combining both pipelines is superior to using either alone. LLM prompting provides natural language diversity, while templates provide the structural rigor necessary for complex operations.
- Validation Efficacy: The multi-stage validation process is critical; removing it leads to a noticeable drop in the quality of the fine-tuned models. Scaling tests also suggest that performance gains begin to plateau around the 3,000-sample mark.
Conclusion and Future Work
SyntheT2C demonstrates that synthetic data can effectively bridge the expertise gap in the Text2Cypher task, allowing smaller models to achieve high performance. While the template-filling pipeline still requires some manual design, the overall framework significantly reduces the human effort required for data annotation. Future research will focus on automating template generation and implementing iterative debugging processes where LLMs can correct queries that fail initial validation.
Summary updated