Before Generation, Align it! A Novel and Effective Strategy for Mitigating Hallucinations in Text-to- SQL Generation (2024)

Abstract: Large Language Models (LLMs) driven by In-Context Learning (ICL) have significantly improved the performance of text-to-SQL. Previous methods generally employ a two-stage reasoning framework, namely 1) schema linking and 2) logical synthesis, making the framework not only effective but also interpretable. Despite these advancements, the inherent bad nature of the generalization of LLMs often results in hallucinations, which limits the full potential of LLMs. In this work, we first identify and categorize the common types of hallucinations at each stage in text-to-SQL. We then introduce a novel strategy, Task Alignment (TA), designed to mitigate hallucinations at each stage. TA encourages LLMs to take advantage of experiences from similar tasks rather than starting the tasks from scratch. This can help LLMs reduce the burden of generalization, thereby mitigating hallucinations effectively. We further propose TA-SQL, a text-to-SQL framework based on this strategy. The experimental results and comprehensive analysis demonstrate the effectiveness and robustness of our framework. Specifically, it enhances the performance of the GPT-4 baseline by 21.23% relatively on BIRD dev and it yields significant improvements across six models and four mainstream, complex text-to-SQL benchmarks.

## introduction in the age of big data, relational databases, as carriers for storing massive amounts of data, play a crucial role in information processing and data analysis. text-to-sql, which aims to convert natural language (nl) queries to executable sql queries, facilitates access to ubiquitous relational data for a broader range of non-technical users, thereby attracting remarkable attention @xcite @xcite . recently, large language models (llms) have shown impressive success on a wide range of nlp tasks through in-context learning (icl) @xcite , such as question answering @xcite , logic reasoning @xcite , and code generation @xcite . the application of llms has also improved the performance of text-to-sql to another level of intelligence @xcite @xcite . delving into their crafted designs, these works generally approach text-to-sql through a two-stage paradigm. the first stage, schema linking, involves the precise mapping of natural language queries to the relevant entities within a database schema @xcite @xcite . this meticulous alignment is crucial for the following execution of the query and provides transparency by illustrating how natural language queries are interpreted in relation to the database schema. the second step is logical synthesis, which refers to the process of generating accurate sql queries based on the understanding of the logic of the natural language query and the structure of the database @xcite . nevertheless, hallucination, a notorious problem in llms that refers to the generation of content that is irrelevant, erroneous, or inconsistent with user intents @xcite , remains a considerable barrier to current frameworks as a reliable automatic text-to-sql parser. in this work, we first study and conclude primary hallucinations presented in the aforementioned two stages of current text-to-sql frameworks and attribute them to two main categories: schema-based hallucinations and logic-based hallucinations, as shown in on the other hand, logic-based schema-based definition example schema contradiction (30%) refers to the instance where incorrect sql contradicts schema structure. question: what language is the set of 180 cards that belongs to the ravnica block translated into? gold: select t2.language from sets as t1 inner join set_translations as t2 on where t1.@xmath0.@xmath1: select language from sets where @xmath2, leading to a contradiction with the intended result format. question: which player is the tallest? gold: select player_name from player order by height desc limit 1 wrong sql: select player_name, height from player order by height desc limit 1 value misrepresentation (24%) refers to the instance where the model imagines a reasonable but non-existent value format in the schema. question: give the race of the blue-haired men superhero. gold: select ... where colour.@xmath3.@xmath4: select ... where colour.@xmath5.@xmath6. question: determine the bond type formed in the chemical compound containing element tellurium. gold: select t2.bond_type from atom as t1 inner join bond as t2 on where t1.@xmath7: select bond_type from bond inner join connected on ... inner join atom on ... where atom.@xmath8, disrupting the correct order or limitation of results. question: among the posts that were voted by user 14, what is the id of the most valuable post? gold: select post.id ... where votes.@xmath9.favoritecount desc limit 1 wrong sql: select post.id from votes inner join posts on ... where votes.@xmath10.id order by post.favoritecount desc limit 1 mathematical delusion (17%) refers to the instance where the model fails to convert mathematical knowledge or logic into correct sql functions, resorting to expressions such as imagined functions. question: what is the percentage of the amount 50 received by the student club among members? gold: select cast(sum(case when income.@xmath11.0 else 0 end) as real) * 100 / count(income.income_id) from ... where member.@xmath12: select divide(sum(case when income.@xmath13, count(member.member_id)) from ... where member.@xmath14: definitions and examples of schema-based and logic-based hallucinations. the aforementioned challenges reinforce the demand for a robust text-to-sql framework to minimize hallucinations and improve overall performance while maintaining interpretability. we posit that hallucinations often arise when models misinterpret the decomposed stages of a task as entirely new challenges, for which they lack prior training. this situation is comparable to human experiences, where unfamiliarity with a task can lead to disorientation and a higher propensity for errors @xcite . thus, just as experienced individuals can draw on familiar situations to reduce cognitive load and enhance task performance @xcite , we introduce task alignment (ta), a novel strategy to mitigate hallucinations of llms in this way. ta fundamentally adjusts the approach of models to unfamiliar tasks by aligning them with tasks they have previously trained on. this method reduces the dependence of models on their generalization capability for generating responses from scratch, thereby significantly reducing the incidence of hallucinations. we further propose a text-to-sql framework named ta-sql, which consists of a task-aligned schema linking (tasl) module and a task-aligned logical synthesis (talog) module. ta is employed to mitigate hallucinations in these ## preliminaries problem definition given a natural language question @xmath15, .. in-context learning in-context learning (icl) is a paradigm that allows language models to learn tasks with only a few examples in the form of demonstrations @xcite , or even without examples. it requires no additional training and is directly applicable to pre-trained llms. in this work, we only discuss hallucinations in icl-based text-to-sql frameworks. few-shot prompting is a scenario in icl where it uses task descriptions i and a set few-shot input-output (i/o) prompting demonstrations @xmath16, y 1 ), ..., (x k , y k )} to assist llm m to solve the input problem x which belongs to a task m by: ## conclusion in this research, we first systematically identify and classify common hallucination types in text-to-sql. subsequently, we propose task alignment (ta), a novel strategy to mitigate hallucinations in large language models (llms) during the textto-sql process. based on this strategy, we further propose ta-sql, a framework to mitigate hallucinations at each stage of this process. experimental results and comprehensive analysis show the importance of hallucination research in text-to-sql and data science and suggest promising directions for future work.