A test case self-correction Text-to-SQL method, system and medium

By using a large-model multi-agent test case self-correction method to generate and optimize SQL query statements, the problem of semantic errors being difficult to correct in existing technologies is solved, and the accuracy and execution success rate of SQL query statements are improved.

CN120256320BActive Publication Date: 2025-09-30HARBIN INSTITUTE OF TECHNOLOGY (SHENZHEN) (INSTITUTE OF SCIENCE AND TECHNOLOGY INNOVATION HARBIN INSTITUTE OF TECHNOLOGY SHENZHEN)
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510741829.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-06-05
Publication Date
2025-09-30
Estimated Expiration
2045-06-05

AI Technical Summary

Technical Problem

Existing text-to-SQL methods are difficult to correct when faced with semantic errors, resulting in negative impacts on the execution success rate and query accuracy of SQL query statements.

Method used

A test case self-correction method based on a large model and multiple agents is adopted. By generating diverse SQL test cases, analyzing feedback information and iteratively correcting generated SQL query statements, a large language model is used to extract context and relationship patterns, generate test data and code segments, and combine with the Python interpreter to verify and optimize SQL query statements.

Benefits of technology

The accuracy of generated SQL query statements is improved, ensuring that the generated SQL query statements meet the expected output and avoiding the impact of semantic understanding deviation on subsequent data analysis.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120256320B_ABST
    Figure CN120256320B_ABST
Patent Text Reader

Abstract

The present invention provides a Text-to-SQL method, system, and medium for self-correcting test cases. The method utilizes a large language model to generate diverse SQL test cases; analyzes feedback from the SQL test cases, and generates SQL query statements through iterative correction. The present invention provides test cases that are as accurate as possible, specifically testing and checking for potential errors in SQL query statements, thereby improving the accuracy of generated SQL query statements. The system can correct subtle semantic errors in SQL query statements through a "SQL generation-test case feedback-SQL correction" design framework, thereby minimizing deviations from the semantic understanding of the original question and ensuring that the query results of the generated SQL query statements do not affect subsequent data analysis due to inconsistencies with the requirements of the original question.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of natural language technology, and in particular to a large-model multi-agent based test case self-correction Text-to-SQL method, system and medium. Background Art

[0002] The field of text-to-SQL research focuses on converting natural language questions into corresponding SQL queries. This allows business personnel lacking programming skills to directly retrieve data relevant to data analysis problems from databases. Currently, common text-to-SQL methods primarily utilize technologies such as pre-trained language models and large language models. These methods focus on designing specific processes to improve SQL generation accuracy, while neglecting to design solutions to detect errors in generated SQL. When faced with SQL queries containing semantic errors, these methods struggle to detect incorrect semantic understanding of the original data analysis problem, resulting in persistent and uncorrectable semantic errors in the generated SQL queries. This negatively impacts the SQL query execution success rate and query accuracy. Summary of the Invention

[0003] The main purpose of the present invention is to provide a large-model multi-agent based test case self-correction Text-to-SQL method, system and medium, aiming to solve the problem that SQL query statements contain difficult-to-correct semantic errors due to semantic understanding deviations of the original problem. By providing test cases that are as accurate as possible, possible errors in SQL query statements can be tested and checked in a targeted manner, thereby improving the accuracy of generated SQL query statements.

[0004] To achieve the above object, the present invention proposes a test case self-correction Text-to-SQL method, which includes the following steps:

[0005] Step S10, using the large language model to generate diverse SQL test cases;

[0006] Step S20: analyzing the feedback information of the SQL test case and generating an SQL query statement through iterative correction.

[0007] A further technical solution of the present invention is that step S10 includes:

[0008] Step S101, context retrieval: using a large language model to extract context information related to the SQL query statement to be generated from the database;

[0009] Step S102, relational schema selection: using the large language model to select the most relevant tables and fields from the database schema for the current query;

[0010] Step S103, generating test data and code segments: based on the context information and the relational model selection, generating test data and test code to construct an SQL test case;

[0011] The step S101 includes: extracting key query words from the input natural language question using a large language model, searching for data content values ​​similar to the key query words in the database content, and searching for relevant information in the database description information in the vector database;

[0012] Step S102 includes: using the large language model to utilize natural language questions and knowledge to identify tables that may be involved in the current query, sorting the relevance of these tables, and ultimately selecting tables related to the question and excluding irrelevant information; after determining the tables, further identifying and selecting fields related to the query;

[0013] The step S103 includes:

[0014] Generate test data for verifying SQL statement execution results based on original data analysis problems and database schema;

[0015] Generate test code based on test data to obtain correct results.

[0016] A further technical solution of the present invention is that the step of generating a test code for obtaining a correct result based on the test data includes:

[0017] The generated code is executed through the Python interpreter to verify its correctness. If an execution error occurs or the query result is empty, the system will adjust and regenerate the code based on the feedback information until the generated code can be executed correctly. If the execution is correct and the query result is obtained, the query result will be used as the expected output data of the test case.

[0018] A further technical solution of the present invention is that step S20 includes:

[0019] Step S201, generating a preliminary SQL query statement based on the SQL test case;

[0020] Step S202: Verify the initially generated SQL query statement in combination with predefined test cases;

[0021] Step S203: Optimize and correct the SQL query statement that has completed the execution verification according to the feedback information of the test case.

[0022] A further technical solution of the present invention is that step S201 includes: converting the natural language question 𝑄 into an SQL query 𝑌, which can retrieve relevant data from the database. The database can be represented as 𝐷= ⟨𝐶,𝑇>, where 𝐶 and 𝑇 refer to column and table information respectively. When processing complex database values, external knowledge 𝐾 must be combined to improve the model's understanding of the database values. The SQL generation stage can be expressed as the following formula, where the function A large language model with parameters 𝜃 can be represented as:

[0023] .

[0024] A further technical solution of the present invention is that step S202 includes: comparing the preliminarily generated SQL query statement with a predefined test case in a database management system. If the results are consistent, it means that the generated SQL query statement has passed the verification; if they are inconsistent, the feedback information is re-input into the model for further correction. The feedback information of the SQL execution verification is divided into two categories: one is the situation where the SQLite execution fails due to an SQL syntax error. In this case, the SQLite error message is fed back to the SQL correction stage, and the LLM performs targeted modifications. The other is the situation where the SQL is successfully executed but the query result is inconsistent with the expected output of the test case. In this case, the erroneous query result and the expected correct result are fed back to the SQL correction stage, and the LLM performs corresponding adjustments and corrections.

[0025] A further technical solution of the present invention is that step S203 includes: using the feedback information of the test case to correct possible errors in the SQL query statement to generate an SQL query statement that meets expectations, wherein the SQL query statement correction adopts the following function:

[0026] ;

[0027] Among them, the function It can represent a large language model with parameter 𝜃. The feedback information includes error information, expected correct results, and existing SQL execution results, which can be expressed as .

[0028] To achieve the above objectives, the present invention also proposes a test case self-correcting Text-to-SQL system, which includes a memory, a processor, and a test case self-correcting Text-to-SQL program stored on the processor. When the test case self-correcting Text-to-SQL program is executed by the processor, the steps of the above method are executed.

[0029] To achieve the above objectives, the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a test case self-correcting Text-to-SQL program, and the test case self-correcting Text-to-SQL program executes the steps of the above method when executed by a processor.

[0030] The beneficial effects of the test case self-correction Text-to-SQL method, system, and medium of the present invention are:

[0031] 1. The present invention can provide as accurate test cases as possible to specifically test and check possible errors in SQL query statements, thereby improving the accuracy of generated SQL query statements;

[0032] 2. The present invention can correct subtle semantic errors in SQL query statements through the design framework of "SQL generation-test case feedback-SQL correction", thereby avoiding semantic understanding deviations of the original problem as much as possible, and ensuring that the query results of the generated SQL query statements will not affect subsequent data analysis links due to inconsistency with the requirements of the original problem. BRIEF DESCRIPTION OF THE DRAWINGS

[0033] Figure 1 This is a flow chart of a preferred embodiment of the test case self-correction Text-to-SQL method of the present invention;

[0034] Figure 2 This is the overall framework diagram of the test case self-correction Text-to-SQL method based on large model multi-agent;

[0035] Figure 3 This is a diagram of the overall architecture for generating SQL test cases based on a large language model;

[0036] Figure 4 This is a diagram of the overall architecture for SQL generation for test cases;

[0037] Figure 5 This is a hardware architecture diagram of the test case self-correcting Text-to-SQL system of the present invention.

[0038] In order to make the objectives, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail below with reference to the accompanying drawings and embodiments. DETAILED DESCRIPTION

[0039] It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.

[0040] This paper proposes a test case self-correction Text-to-SQL method based on large model multi-agent, such as Figure 1As shown, a preferred embodiment of the test case self-correction Text-to-SQL method of the present invention includes the following steps:

[0041] Step S10: Generate diverse SQL test cases using the large language model.

[0042] A large language model (LLM) is a language model composed of an artificial neural network with many parameters (usually billions of weights or more). By using self-supervised learning or semi-supervised learning to train a large amount of unlabeled text, the large model is able to "remember" a large number of facts during training and can capture most of the syntax and semantics of human language, performing well in a wide range of tasks.

[0043] Step S20: analyzing the feedback information of the SQL test case and generating an SQL query statement through iterative correction.

[0044] Structured Query Language (SQL) is a special-purpose programming language used to manage relational database management systems or perform stream processing in relational stream data management systems.

[0045] Text-to-SQL (also known as NL2SQL) refers to converting natural language questions into corresponding SQL query statements.

[0046] Specifically, in this embodiment, step S10 includes:

[0047] Step S101, context retrieval: using a large language model to extract context information related to the SQL query statement to be generated from the database.

[0048] A database is a computer software system that stores and manages data according to data structures.

[0049] Step S102, relational schema selection: using the large language model to select the tables and fields most relevant to the current query from the database schema.

[0050] A database schema is a structure described in a formal language in a database system. It is a collection of objects and contains different types of objects in different types of databases. In the present invention, a database schema includes four parts: a table name field, a column name field, a data content value, and a database description. The table name field is the name of the data table stored in the database; the column name field records the column name contained in each data table stored in the database; the data content value refers to the unique and definite content value actually stored in the database; and the database description is a general statement of the information recorded in a specific column name field of the database.

[0051] Step S103, generating test data and code segments: based on the context information and the relational model selection, generating test data and test codes to construct SQL test cases.

[0052] A test case is a set of test inputs, execution conditions, and expected results compiled for a specific purpose, used to verify whether a specific software requirement is met. The test case designed for the Text-to-SQL task in this invention mainly consists of two parts: test data and test code. The test data requires the LLM to generate code with a specific format based on natural language questions and database schemas to verify the execution results of SQL statements. The test code requires the LLM to generate Python code segments for obtaining correct results based on the generated test data.

[0053] The step S101 includes: extracting key query words from the input natural language question using a large language model, searching for data content values ​​similar to the key query words in the database content, and searching for relevant information in the database description information in the vector database;

[0054] Step S102 includes: using the large language model to utilize natural language questions and knowledge to identify tables that may be involved in the current query, sorting the relevance of these tables, and ultimately selecting tables related to the question and excluding irrelevant information; after determining the tables, further identifying and selecting fields related to the query;

[0055] The step S103 includes:

[0056] Generate test data for verifying SQL statement execution results based on original data analysis problems and database schema;

[0057] Generate test code based on test data to obtain correct results.

[0058] In this embodiment, the step of generating test code for obtaining correct results based on test data includes:

[0059] The generated code is executed through the Python interpreter to verify its correctness. If an execution error occurs or the query result is empty, the system will adjust and regenerate the code based on the feedback information until the generated code can be executed correctly. If the execution is correct and the query result is obtained, the query result will be used as the expected output data of the test case.

[0060] Furthermore, in this embodiment, step S20 includes:

[0061] Step S201, generating a preliminary SQL query statement based on the SQL test case;

[0062] Step S202: Verify the initially generated SQL query statement in combination with predefined test cases;

[0063] Step S203: Optimize and correct the SQL query statement that has completed the execution verification according to the feedback information of the test case.

[0064] The step S201 specifically includes: converting the natural language question 𝑄 into an SQL query 𝑌, which can retrieve relevant data from the database. The database can be represented as 𝐷= ⟨𝐶,𝑇>, where 𝐶 and 𝑇 refer to column and table information respectively. When processing complex database values, it is necessary to combine external knowledge 𝐾 to improve the model's understanding of database values. The SQL generation stage can be expressed as the following formula, where the function A large language model with parameters 𝜃 can be represented as:

[0065] .

[0066] The step S202 specifically includes: comparing the preliminarily generated SQL query statement with the predefined test case in the database management system. If the results are consistent, it means that the generated SQL query statement has passed the verification; if they are inconsistent, the feedback information is re-entered into the model for further correction. The feedback information of SQL execution verification is divided into two categories: one is the situation where SQLite execution fails due to SQL syntax errors. In this case, the SQLite error information is fed back to the SQL correction stage, and the LLM performs targeted modifications; the other is the situation where SQL is successfully executed but the query result is inconsistent with the expected output of the test case. In this case, the erroneous query result and the expected correct result are fed back to the SQL correction stage, and the LLM performs corresponding adjustments and corrections.

[0067] The step S203 specifically includes: using the feedback information of the test case to correct possible errors in the SQL query statement, and generating an SQL query statement that meets expectations, wherein the SQL query statement correction adopts the following function:

[0068] ;

[0069] Among them, the function Represents a large language model with parameter 𝜃. Feedback information includes error information, expected correct results, and existing SQL execution results, which can be expressed as .

[0070] The following further describes the test case self-correction Text-to-SQL method of the present invention.

[0071] To address the problem that SQL query statements contain difficult-to-correct semantic errors due to deviations in the semantic understanding of the original problem, this paper innovatively proposes a Text-to-SQL method for self-correcting test cases based on large-model multi-agents. It designs three large-model agent interaction frameworks to generate reliable test cases including test data and test code, and constructs an iterative framework of "SQL generation-test case feedback-SQL correction" to check and correct semantic errors in SQL query statements. Figure 2 The overall implementation process of the present invention is presented, which mainly includes the following two modules: SQL test case generation based on a large language model and SQL generation based on test cases. The SQL test case generation part based on a large language model uses the powerful natural language processing capabilities of the large language model to automatically generate a variety of SQL test cases. The core of this part is how to interpret natural language descriptions through a large language model and convert them into valid SQL test cases, thereby covering various possible SQL query scenarios. The SQL generation part based on test cases uses the test cases generated in the previous stage to drive the generation of SQL queries. The core of this part is how to deeply analyze the feedback information of the test cases and generate more accurate and efficient SQL queries through iterative corrections.

[0072] 1. SQL test case generation based on large language model The overall design framework of the SQL test case generation method based on large language model is as follows Figure 3 As shown, it mainly includes three stages: context retrieval, relation pattern selection, and test data and code segment generation.

[0073] The primary goal of the context retrieval phase is to extract contextual information relevant to the SQL statement to be generated. The database data content values ​​are indexed using the Locality Sensitive Hashing (LSH) algorithm, while database description information (i.e., column descriptions, column aliases, value descriptions, etc.) is constructed into a vector database. The Large Language Model (LLM) first extracts key query terms from the input natural language question. These keywords serve as the basis for subsequent query generation. Using the keywords extracted in the question, the LLM searches the database content for values ​​similar to the keywords and retrieves relevant information from the database description information in the vector database.

[0074] The goal of the relational schema selection phase is to select the most relevant tables and fields from the database schema for the current query. The large language model leverages natural language questions and knowledge to identify tables that may be involved in the current query and rank these tables by relevance, ultimately selecting the relevant tables and eliminating irrelevant information. After determining the tables, it further identifies and selects fields relevant to the query. This step simplifies complex database schemas, reduces irrelevant information, and improves the accuracy of code generation.

[0075] During the test data and code snippet generation phase, a three-agent interaction framework was designed to generate test cases. The data generation agent generates test data (test data) based on the original data analysis question and database schema to verify the execution results of SQL statements. The code generation agent and code checking agent generate test code (test code) based on the test data to obtain correct results. Based on the previously selected context and relationship schema, the test data and preliminary test code generated through multi-agent interaction in this phase are used to construct SQL test cases. The generated code is then executed using the Python interpreter to verify its correctness. If an execution error occurs or the query result is empty, the system adjusts and regenerates the code based on the feedback until the generated code executes correctly. If the execution is correct and the query result is obtained, the query result is used as the expected output data of the test case.

[0076] Through the above process, the system will be able to generate SQL test cases with high accuracy, which can effectively improve the accuracy of subsequent SQL generation tasks.

[0077] 2. SQL generation based on test cases The overall design framework of the SQL generation method based on test cases is as follows Figure 4 As shown, it mainly includes three stages: SQL generation, execution verification and SQL correction.

[0078] First, preliminary SQL generation is performed through a large language model. The model receives the question, database relational schema, and related knowledge as input, and generates a preliminary SQL query statement. The goal of this stage is to quickly generate a possibly correct SQL statement for verification and correction in subsequent stages. Specifically, the SQL generation stage converts the natural language question 𝑄(Question) into an SQL query 𝑌, which can retrieve relevant data from the database. The database (Database) can be expressed as 𝐷= ⟨𝐶,𝑇>. Where 𝐶 and 𝑇 refer to the information of the column (Column) and table (Table), respectively. When processing complex database values, external knowledge 𝐾(Knowledge) must be combined to improve the model's understanding of the database values. The SQL generation stage can be expressed as the following formula, where the function A large language model with parameters 𝜃 can be represented as:

[0079] .

[0080] The generated SQL statement then enters the execution verification phase. During this phase, the SQL statement is executed in a database management system (such as SQLite) and verified for correctness by comparing it to pre-defined test cases. The query results after executing the SQL statement are compared with the expected output of the test case. If the results are consistent, the generated SQL statement has passed verification. If they are inconsistent, the feedback information is re-entered into the model for further correction. SQL execution verification feedback falls into two categories: First, when SQLite execution fails due to SQL syntax errors. In this case, the execution module feeds the SQLite error information back to the SQL correction phase, where the LLM makes targeted modifications. Second, when the SQL statement executes successfully but the query results do not match the expected output of the test case. In this case, the execution module feeds both the incorrect query results and the expected correct results back to the SQL correction phase, where the LLM makes appropriate adjustments and corrections.

[0081] Next, SQL statements that have completed execution verification enter the correction phase. The goal of this phase is to optimize and correct the generated SQL based on the feedback from the test case. To improve the accuracy of the SQL, the model will use the feedback to correct possible errors in the SQL, thereby generating SQL queries that meet expectations. SQL correction can be expressed as the following function:

[0082] .

[0083] Among them, the function It can represent a large language model with parameter 𝜃. Feedback information includes error information, expected correct results and existing SQL execution results, which can be expressed as By processing this information, the model can generate more accurate SQL query statements to pass the subsequent verification stage.

[0084] The entire process is iteratively generated, corrected, and verified, ultimately producing a SQL statement that meets the requirements and can pass all test cases.

[0085] This invention is applicable to real-world business scenarios involving data analysis. In these scenarios, data analysts need to write SQL queries for natural language data analysis questions and retrieve relevant data from a database for subsequent data analysis tasks. The key point of this invention is to detect and self-correct subtle semantic errors in SQL queries caused by misunderstandings of the original question, using test cases.

[0086] The SQL test case generation method based on a large language model proposed in this invention is based on the contextual semantic learning ability and multi-agent interactive iteration ability of the large model agent, and designs three large model agent interaction frameworks to generate test cases including test data and test code. Among them, the data generation agent generates test data (test data) for verifying the execution results of SQL statements based on the original data analysis problem and database model. The code generation agent and the code checking agent generate test code (test code) for obtaining correct results based on the test data through multiple interactive verification and correction. This method does not require the construction of a complete generation process framework from the problem to the SQL query statement. Instead, it constructs test cases that are as accurate as possible to specifically check for possible errors in the SQL query statement, thereby improving the accuracy of the generated SQL query statement.

[0087] The test case-based SQL generation method proposed in this paper uses the test cases (test data and corresponding test code) generated in the previous step to enable the large model to generate a SQL query statement for the original problem. The SQL statement and the test code are then executed on the test data, the feedback is compared, and the SQL statement is iteratively corrected based on the test code logic. This method, through an iterative framework of "SQL generation - test case feedback - SQL correction," detects and corrects subtle semantic errors in SQL queries, thereby correcting deviations in the semantic understanding of the original problem and improving the execution accuracy of the generated SQL queries.

[0088] The beneficial effects of the test case self-correction Text-to-SQL method of the present invention are:

[0089] Text-to-SQL technologies analyze natural language questions and database-related information to convert them into SQL queries that can be successfully executed in the database and produce the desired results. This technology improves SQL writing efficiency and enables non-professionals without basic programming skills to directly search databases for data relevant to natural language questions. This data contains the potential information needed to answer natural language questions. Analyzing this data can help relevant personnel adjust strategies and design solutions based on the characteristics, status, or situation reflected in the data, thereby leveraging the value of data.

[0090] Existing methods for text-to-SQL conversion primarily include those using pre-trained language models and those using large language models. The former, while training a deep learning model to parse natural language questions and generate SQL, further incorporates techniques such as neural networks, such as those shown in the figure, to obtain relevant information from the natural language question and the database. While these methods achieve good results in converting natural language questions into SQL, they overlook the fact that the generated SQL queries are prone to errors that require correction. This is because end-to-end pre-trained models can only output the SQL statement corresponding to the question, as required during the model training phase. Large language-based text-to-SQL methods, leveraging the large language model's powerful contextual learning and knowledge emergence capabilities, have achieved results far superior to other methods in text-to-SQL tasks. However, these methods focus more on designing processes to improve SQL generation accuracy and neglect designing solutions to detect errors in the generated SQL. This differs from the present invention, which eliminates the need for training and directly utilizes the large model to construct test cases to self-correct the SQL query statements generated by the large model.

[0091] The present invention proposes a SQL test case generation method based on a large language model, which can provide test cases that are as accurate as possible for targeted testing and checking of possible errors in SQL query statements, thereby improving the accuracy of generated SQL query statements.

[0092] The present invention proposes a test case-based SQL generation method, which can correct subtle semantic errors in SQL query statements through the design framework of "SQL generation-test case feedback-SQL correction", thereby avoiding semantic understanding deviations of the original problem as much as possible and ensuring that the query results of the generated SQL query statements will not affect the subsequent data analysis links due to inconsistency with the requirements of the original problem.

[0093] To achieve the above object, the present invention also proposes a test case self-correction Text-to-SQL system, such as Figure 5As shown, the system includes a processor 1001, a CPU, a network interface 1004, a user interface 1003, a memory 1005, a communication bus 1002, and a test case self-correcting text-to-SQL program stored on the processor. Communication bus 1002 is used to enable communication between these components. User interface 1003 may include a display and an input unit such as a keyboard. Optionally, user interface 1003 may also include a standard wired interface or a wireless interface. Network interface 1004 may optionally include a standard wired interface or a wireless interface (such as a Wi-Fi interface). Memory 1005 may be high-speed RAM or non-volatile memory, such as disk storage. Memory 1005 may also be a storage device independent of processor 1001.

[0094] Those skilled in the art will understand that Figure 5 The system structure shown in the figure does not constitute a limitation of the system, and may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently.

[0095] like Figure 5 As shown, the memory 1005 as a computer storage medium may include an operating device, a network communication module, a user interface module, and a test case self-correction Text-to-SQL program.

[0096] exist Figure 5 In the system shown, the network interface 1004 is mainly used to connect to the network server and communicate data with the network server; the user interface 1003 is mainly used to interact with the user terminal and receive instructions input by the user; and the processor 1001 can be used to call the test case self-correction Text-to-SQL program stored in the memory 1005.

[0097] To achieve the above objectives, the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a test case self-correcting Text-to-SQL program. When the test case self-correcting Text-to-SQL program is executed by a processor, the steps of the method described above are executed, which will not be repeated here.

[0098] The above description is only a preferred embodiment of the present invention and does not limit the scope of the invention. Any equivalent structure or process transformation made by using the contents of the present invention description and drawings, or directly or indirectly applied in other related technical fields, are also included in the scope of protection of the present invention.

Claims

1. A test case self-correction Text-to-SQL method, characterized in that: The method comprises the following steps: Step S10, using the large language model to generate diverse SQL test cases; Step S20, analyzing the feedback information of the SQL test case and generating an SQL query statement through iterative correction; The step S20 includes: Step S201, generating a preliminary SQL query statement based on the SQL test case; Step S202: Verify the initially generated SQL query statement in combination with predefined test cases; Step S203: Optimize and modify the SQL query statement that has completed the execution verification according to the feedback information of the test case; The step S201 includes: converting the natural language question Q into an SQL query Y, which can retrieve relevant data from a database. The database can be represented by D=<C,T> , where C and T refer to the information of columns and tables respectively. When processing complex database values, external knowledge K must be combined to improve the model's understanding of database values. The SQL generation stage can be expressed as the following formula, where the function f generate (·|θ) represents a large language model with parameters θ: Y=f generate (Q,D,K|θ)? The step S202 includes: comparing the preliminarily generated SQL query statement with a predefined test case in the database management system. If the results are consistent, it means that the generated SQL query statement has passed the verification; if they are inconsistent, the feedback information is re-entered into the model for further correction. The feedback information of the SQL execution verification is divided into two categories: one is the case where the SQLite execution fails due to an SQL syntax error. In this case, the SQLite error message is fed back to the SQL correction stage, and the LLM performs targeted modifications; the other is the case where the SQL is successfully executed but the query result is inconsistent with the expected output of the test case. In this case, the incorrect query result and the expected correct result are fed back to the SQL correction stage, and the LLM performs corresponding adjustments and corrections; The step S203 includes: using the feedback information of the test case to correct possible errors in the SQL query statement, and generating an SQL query statement that meets expectations, wherein the SQL query statement correction adopts the following function: Y′=f refine (F,Y,Q,D,K|θ); Among them, the function f refine (·|θ) represents a large language model with parameter θ, and the feedback information includes error message Err and expected correct result res expected and the existing SQL execution result res pred , which can be expressed as F= <Err,res expected ,res pred >.

2. The test case self-correction Text-to-SQL method according to claim 1, characterized in that: The step S10 includes: Step S101, context retrieval: using a large language model to extract context information related to the SQL query statement to be generated from the database; Step S102, relational schema selection: using the large language model to select the most relevant tables and fields from the database schema for the current query; Step S103, generating test data and code segments: based on the context information and the relational model selection, generating test data and test code to construct an SQL test case; The step S101 includes: extracting key query words from the input natural language question using a large language model, searching for data content values ​​similar to the key query words in the database content, and searching for relevant information in the database description information in the vector database; Step S102 includes: using the large language model to utilize natural language questions and knowledge to identify tables that may be involved in the current query, sorting the relevance of these tables, and ultimately selecting tables related to the question and excluding irrelevant information; after determining the tables, further identifying and selecting fields related to the query; The step S103 includes: Generate test data for verifying SQL statement execution results based on original data analysis problems and database schema; Generate test code based on test data to obtain correct results.

3. The test case self-correction Text-to-SQL method according to claim 2, characterized in that: The step of generating a test code for obtaining a correct result according to the test data includes: The generated code is executed through the Python interpreter to verify its correctness. If an execution error occurs or the query result is empty, the system will adjust and regenerate the code based on the feedback information until the generated code can be executed correctly. If the execution is correct and the query result is obtained, the query result will be used as the expected output data of the test case.

4. A test case self-correcting Text-to-SQL system, characterized in that: The system includes a memory, a processor, and a test case self-correcting Text-to-SQL program stored on the processor. When the test case self-correcting Text-to-SQL program is executed by the processor, the steps of the method according to any one of claims 1 to 3 are performed.

5. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a test case self-correcting Text-to-SQL program, and when the test case self-correcting Text-to-SQL program is executed by a processor, the steps of the method according to any one of claims 1 to 3 are performed.

Citation Information

Patent Citations

  • Large language model SQL (Structured Query Language) generation method based on high-quality context sample and self-correction

    CN118568127A

  • Intelligent SQL query generation method and system based on large language model

    CN118861081A