Text-to-structured query generation method and system based on large language model

CN122332419BActive Publication Date: 2026-09-08SHANDONG EVAYINFO TECH CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202610778531.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2026-06-02
Publication Date
2026-09-08
Estimated Expiration
2046-06-02

AI Technical Summary

Technical Problem

然而,该策略存在明显缺陷:即使对于简单问题,模型仍会进行不必要的分解,使问题理解过程反而复杂化;现有方法将全部生成的子问题及其中间结果直接作为输入,缺乏对子问题正确性的检验机制,无法排除错误的子问题或错误的子SQL语句,一旦某个子问题出错,错误便会沿链路传递,导致最终SQL生成错误

Benefits of technology

本发明通过三个阶段增强对自然语言的理解能力,通过问题-SQL架构对的遮蔽处理、向量检索,以及针对SELECT关键字需求的模型微调,让LLM深度理解自然语言意图与SQL语法的对应关系,减少语义偏差导致的查询逻辑错误,提升复杂场景适配与容错能力。通过子问题树形分解机制可拆解多表连接问题或者存在嵌套查询等复杂问题为简单子问题,逐步解决子问题,生成最终的SQL语句,覆盖简单查询无法应对的场景;SQL生成阶段的多采样生成,能有效规避单一SQL的生成,降低任务失败率;以达到降低专家人员的压力,非专家人员可以通过自然语言查询可靠的查询结果。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122332419B_ABST
    Figure CN122332419B_ABST
Patent Text Reader

Abstract

The application belongs to the field of data processing, and provides a text-to-structured query statement generation method and system based on a large language model, which filters an original question to obtain a related database, takes the related database and external knowledge of the question as relevant information of the original question, generates an initial SQL statement based on the original question and the relevant information, determines an error SQL statement according to query verification of the initial SQL statement, searches in a vector database according to the error SQL statement and the initial SQL statement that succeeds in query to determine whether a similar question-SQL architecture pair exists, generates three candidate SQL statements based on the original question and the relevant information if the similar question-SQL architecture pair exists, and otherwise, determines a statement generation path according to the complexity of the original question to obtain the three candidate SQL statements, and obtains a text-to-structured query statement based on verification of the three candidate SQL statements. The application can reduce query logic errors caused by semantic deviation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of data processing technology, specifically relating to a method and system for generating text-to-structured query statements based on a large language model. Background Technology

[0002] The statements in this section are merely background information related to the present invention and do not necessarily constitute prior art.

[0003] In recent years, Large Language Models (LLMs) have developed rapidly and become widely used, demonstrating great potential in text-to-SQL conversion tasks. However, current SQL generation methods based on LLMs still face significant challenges in terms of accuracy and practical usability.

[0004] Specifically, the SQL statements generated by the model often produce false negatives, meaning that although the statement can be executed, the returned results do not match the data the user actually wants to query, resulting in the user not obtaining useful information. The root cause of this problem lies in the model's misunderstanding of the user's query intent and the imbalance in the conversion logic: on the one hand, the model is prone to over-interpretation or under-interpretation, leading to the SQL statement omitting key information or returning redundant results; on the other hand, there is a contradiction of "complicating simple problems and simplifying complex problems."

[0005] For basic queries, the model may perform unnecessary complex reasoning, redundantly joining multiple data tables, prolonging computation time and ultimately generating erroneous statements. Many simple problems do not require such complex reasoning; complicating the process actually increases the probability of errors. For complex queries, the model may omit necessary table joins or field information, directly outputting invalid SQL. To solve complex problems, existing technologies often employ a problem decomposition strategy, generating several sub-problems and solving them sequentially to gradually approach the final answer. However, this strategy has significant drawbacks: even for simple problems, the model still performs unnecessary decomposition, complicating the problem understanding process; existing methods directly use all generated sub-problems and their intermediate results as input, lacking a mechanism to verify the correctness of sub-problems, failing to eliminate erroneous sub-problems or erroneous sub-SQL statements. Once a sub-problem is incorrect, the error propagates along the chain, leading to an incorrect final SQL statement. Furthermore, existing technologies only correct or debug after the final SQL statement is generated, failing to correct errors occurring during the reasoning process in real time and making it difficult to trace the root cause of errors. Therefore, the accuracy and robustness of the model in Text-to-SQL tasks are difficult to fundamentally improve. Summary of the Invention

[0006] To address the aforementioned issues, this invention proposes a method and system for generating text-to-structured query statements based on a large language model. This invention breaks down the Text-to-SQL task into three core stages: data processing, SQL generation, and SQL validation and correction. Through the coordinated operation of these stages, it ensures the final output of high-quality, usable SQL statements. This lowers the technical barrier to data querying: users without a database background can directly express their query needs using natural language, obtaining target data without needing to master SQL syntax. This not only effectively reduces the workload of database experts but also significantly improves the data acquisition efficiency for non-expert users, providing strong support for the inclusive mining of data value.

[0007] According to some embodiments, the first solution of the present invention provides a method for generating text-to-structured query statements based on a large language model, employing the following technical solution: A method for generating text-to-structured query statements based on a large language model includes: Filtering is performed based on the original question, the question database, and external knowledge of the question to obtain a relevant database. The relevant database and external knowledge of the question are used as relevant information for the original question. An initial SQL statement is generated based on the original question and its related information. A query is executed based on the initial SQL statement to identify erroneous SQL statements. The erroneous SQL statements and the successfully queried initial SQL statements are then searched in the vector database to determine whether similar question-SQL schema pairs exist. If a similar question-SQL schema pair is found, three candidate SQL statements are generated based on the original question and its related information. If no similar question-SQL schema pair is found, the corresponding statement generation path is determined based on the complexity of the original question, resulting in three candidate SQL statements; The three candidate SQL statements are validated to obtain a text-to-structured query statement.

[0008] Furthermore, the process involves generating an initial SQL statement based on the original question and its related information, performing query validation based on the initial SQL statement to identify erroneous SQL statements, and searching the vector database based on the erroneous SQL statements and the successfully queried initial SQL statements to determine if similar question-SQL schema pairs exist, including: An initial SQL statement is generated based on the original question and its related information. The query is executed in the question database based on the initial SQL statement. If the number of failed queries exceeds a set threshold, the SQL statement is marked as erroneous; otherwise, if the query is successful, the initial SQL statement that was successful is retained. Using erroneous SQL statements and initial SQL statements that succeeded in querying as the SQL statements to be retrieved, the user problem-SQL schema pair is determined based on the SQL statements to be retrieved. If the similarity between the user's question-SQL schema pair and the question-SQL schema pair in the vector database exceeds a threshold, then similar question-SQL schema pairs are retrieved; otherwise, no similar question-SQL schema pairs are retrieved.

[0009] Furthermore, if no similar question-SQL schema pair is found, the corresponding statement generation path is determined based on the complexity of the original question, resulting in three candidate SQL statements, including: If no similar question-SQL schema pair is found, the complexity of the original question is determined by whether the original question needs to be decomposed into sub-questions to generate SQL statements. If it is a simple question, the question-SELECT keyword is generated using a pre-built keyword query large language model, and three candidate SQL statements are generated based on the question-SELECT keyword, the original question and related information. For complex problems, the pre-built keyword query language model is used to generate the question-SELECT keyword, and the original problem is decomposed into multiple sub-problems. The corresponding sub-SQL statements are generated based on the sub-problems and validated. If the validation is successful, they are retained. Based on the retained sub-problems and their corresponding sub-SQL statements, the question-SELECT keyword, the original problem and its related information, three candidate SQL statements are generated.

[0010] Furthermore, the vector database stores the vectorized data of the problem-SQL schema.

[0011] Furthermore, the original problem is decomposed into multiple sub-problems. Corresponding sub-SQL statements are generated and validated based on the sub-problems; if validation is successful, they are retained. Based on the retained sub-problems and their corresponding sub-SQL statements, the problem-SELECT keyword, the original problem, and related information, three candidate SQL statements are generated, including: A tree structure is used to decompose complex problems and generate multiple decomposition routes. Perform query validation on the sub-SQL statements generated for each sub-problem along the decomposition path; If all sub-questions are successfully queried, the decomposition route is retained as the statement generation path; Randomly select one of the multiple statement generation paths as the execution route, and generate three candidate SQL statements from all the sub-problems and corresponding sub-SQL statements, the original problem, external knowledge of the problem, relevant databases, and the problem-SELECT keyword.

[0012] Furthermore, the validation based on three candidate SQL statements yields a text-to-structured query statement, including: Perform cross-consistency voting on the three candidate SQL statements; If all the voting results are consistent, then one of the three candidate SQL statements will be selected as the text-to-structured query statement. If the voting results are inconsistent, the SQL structure of each candidate SQL statement is decomposed and corrected, and one of the three corrected candidate SQL statements is selected as the text-to-structured query statement.

[0013] According to some embodiments, a second aspect of the present invention provides a text-to-structured query statement generation system based on a large language model, employing the following technical solution: A text-to-structured query generation system based on a large language model includes: The data processing module is configured to filter based on the original question, the question database, and external knowledge of the question to obtain a relevant database, and use the relevant database and external knowledge of the question as relevant information for the original question. The retrieval and matching module is configured to generate an initial SQL statement based on the original question and its related information, perform query validation based on the initial SQL statement to identify erroneous SQL statements, and search the vector database based on the erroneous SQL statements and the successfully queried initial SQL statements to determine whether similar question-SQL schema pairs exist. The successful search and matching module is configured to generate three candidate SQL statements based on the original question and its related information if a similar question-SQL schema pair is found. The retrieval matching failure generation module is configured to determine the corresponding statement generation path based on the complexity of the original question if no similar question-SQL schema pair is found, resulting in three candidate SQL statements. The validation module is configured to validate based on three candidate SQL statements to obtain a text-to-structured query statement.

[0014] According to some embodiments, a third aspect of the present invention provides a computer-readable storage medium.

[0015] A computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps in the text-to-structured query statement generation method based on a large language model as described in the first embodiment above.

[0016] According to some embodiments, a fourth aspect of the present invention provides a computer device.

[0017] A computer device includes a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the steps in the text-to-structured query statement generation method based on a large language model as described in the first embodiment above.

[0018] According to some embodiments, a fifth aspect of the present invention provides a computer program product or computer program.

[0019] A computer program product or computer program includes computer instructions stored in a computer-readable storage medium, wherein a processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform the steps in the text-to-structured query statement generation method based on a large language model as described in the first embodiment above.

[0020] Compared with the prior art, the beneficial effects of the present invention are as follows: This invention enhances natural language understanding capabilities through three stages: masking of problem-SQL architecture pairs, vector retrieval, and model fine-tuning for SELECT keyword requirements. This allows LLM to deeply understand the correspondence between natural language intent and SQL syntax, reducing query logic errors caused by semantic bias and improving adaptability and fault tolerance in complex scenarios. A sub-problem tree decomposition mechanism breaks down complex problems such as multi-table joins or nested queries into simpler sub-problems, progressively resolving them to generate the final SQL statement, covering scenarios where simple queries cannot handle. Multi-sampling generation in the SQL generation stage effectively avoids generating single SQL statements, reducing task failure rates. This reduces the workload for expert users, allowing non-experts to obtain reliable query results through natural language queries. Attached Figure Description

[0021] The accompanying drawings, which form part of this invention, are used to provide a further understanding of the invention. The illustrative embodiments of the invention and their descriptions are used to explain the invention and do not constitute an improper limitation of the invention.

[0022] Figure 1 This is a flowchart of the problem-SQL schema retrieval process in the SQL generation stage of this invention embodiment; Figure 2 This is a flowchart of the problem decomposition process in the SQL generation stage of this invention. Detailed Implementation

[0023] The present invention will be further described below with reference to the accompanying drawings and embodiments.

[0024] It should be noted that the following detailed description is illustrative and intended to provide further explanation of the invention. Unless otherwise specified, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains.

[0025] It should be noted that the terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit the scope of exemplary embodiments according to the invention. As used herein, the singular form is intended to include the plural form as well, unless the context clearly indicates otherwise. Furthermore, it should be understood that when the terms "comprising" and / or "including" are used in this specification, they indicate the presence of features, steps, operations, devices, components, and / or combinations thereof.

[0026] Where there is no conflict, the embodiments and features in the embodiments of the present invention can be combined with each other.

[0027] Example 1 This embodiment provides a method for generating text-to-structured query statements based on a large language model. This embodiment uses the application of this method to a server as an example for illustration. It is understood that this method can also be applied to terminals, and can also be applied to systems including terminals, servers, and other components, and can be implemented through interaction between the terminal and the server. The server can be an independent physical server, a server cluster composed of multiple physical servers, or a distributed system. It can also be a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network servers, cloud communication, middleware services, domain name services, CDN security services, and big data and artificial intelligence platforms. The terminal can be a smartphone, tablet, laptop, desktop computer, smart speaker, smartwatch, etc., but is not limited to these. The terminal and server can be directly or indirectly connected via wired or wireless communication, which is not limited herein. In this embodiment, the method includes the following steps: Step S1: Filter based on the original question, the question database, and external knowledge of the question to obtain the relevant database, and use the relevant database and external knowledge of the question as the relevant information of the original question; Specifically, the original question (the question input by the user), the question database corresponding to the question, and external knowledge of the question (numerical reasoning knowledge, domain knowledge, synonym knowledge, value knowledge) are used as inputs to the large language model; The prompt word engineering removes tables and columns from databases that are irrelevant to the SQL statement generated for the problem, and saves the tables and columns from relevant databases as output.

[0028]

[0029] in, The table indicating the selection, The column that indicates the selection. Represents the input to the large model. These represent the original problem, the problem database, and external knowledge related to the problem, respectively. This indicates the prompt word project.

[0030] In other words, based on the keywords in the original question, the corresponding tables and columns are matched in the question database corresponding to the original question to obtain the relevant database.

[0031] Before using the method described in this embodiment, a keyword query language model needs to be pre-trained using a training dataset to output the SELECT keyword in the SQL statement. The training process is as follows: First, obtain the question-SQL statement pairs from the training dataset, and mask the tables, columns, and stored values ​​of the corresponding database in the question-SQL statements, retaining only the question-SQL schema pairs; then use the bge-m3 model to vectorize the question-SQL schema pairs and store them in a vector database.

[0032] For example: The original question-SQL pair is as follows: Question: Query the total score for course number "0002"; SQL: SELECT SUM('Grade') FROM 'Grade' WHERE Course Number = '0002'; The masked problem-SQL schema pair is as follows: Question: Query the total score for the character whose mask is "[mask]"; SQL: SELECTSUM([mask])FROM[mask]WHERE[mask]='[mask]'.

[0033] Then, the question-SQL statement pair is used as input to the LLM, and the output is the result that needs to be displayed in the question corresponding to the SELECT keyword in the SQL statement, that is, the question-SELECT keyword in the SQL statement.

[0034] For example, if the question is: "What is the average ratio of male to female actors in movies?", and the desired result is "the average ratio of male to female actors", then the question-SELECT keyword would be "What is the average ratio of male to female actors in movies? - the average ratio of male to female actors"; Save the question - use the SELECT keyword to construct a fine-tuning dataset, use the fine-tuning dataset as input, and perform LoRA fine-tuning on the large language model. It should be noted that the large language model to be fine-tuned here can be, but is not limited to, the deepseek-coder-1.3b-base model.

[0035] The basic principle of LoRA fine-tuning is to freeze the pre-trained model parameters, then add a trainable side-path matrix (low-rank separable matrix) to each layer of the Transformer, and then add the side-path output to the initial path output and input it into the network, training only these newly added side-path matrix parameters, calculated as follows:

[0036] in, This is the original weight matrix. It refers to the updating or adjustment of weights. and It is a low-rank matrix. The question concerns the tensor transformed by the SELECT keyword. In LoRA, this... It is decomposed into two low-rank matrices and The product of . Due to the low-rank matrix and Dimensions and It is much smaller than that, thus significantly reducing the number of trainable parameters.

[0037] Finally, save the fine-tuned large language model as the keyword query large language model.

[0038] Step S2: Generate an initial SQL statement based on the original question and its related information. Perform a query validation based on the initial SQL statement to identify erroneous SQL statements. Then, search the vector database using the erroneous SQL statements and the successfully queried initial SQL statements to determine if similar question-SQL schema pairs exist, such as... Figure 1 As shown; Step S2.1: Generate an initial SQL statement based on the original question and its related information. Execute a query in the question database based on the initial SQL statement. If the number of failed queries exceeds a set threshold, mark the SQL statement as erroneous; otherwise, if the query is successful, retain the initial SQL statement that was successful. Each time a query fails, a modified SQL statement is generated based on the original question, external knowledge about the question, the question database, and the reason for the error. The initial SQL statement is generated by inputting the original problem, external knowledge about the problem, and relevant database information (tables, fields, storage types, value examples, and whether null values ​​exist) into the LLM. The initial SQL statement is executed in the problem database to check for results. If no results are found, the original problem, external knowledge about the problem, the problem database, and the cause of the error are input into the LLM. The model is then modified, and the modified SQL statement is output, excluding errors related to table or column names. This process is limited to two iterations. If errors still exist, the SQL statement is marked as erroneous and saved.

[0039] Step S2.2: Using the erroneous SQL statement and the initial SQL statement that was successfully queried as the SQL statement to be retrieved, determine the user question-SQL schema pair based on the SQL statement to be retrieved, and determine whether a similar question-SQL schema pair is retrieved based on the similarity between the user question-SQL schema pair and the question-SQL schema pair in the vector database. Use both erroneous SQL statements and successful initial SQL statements as the SQL statements to be retrieved. Based on the keywords in the SQL statement to be retrieved, the corresponding tables and columns and their stored values ​​in the SQL statement to be retrieved and the original question are masked to generate a user question-SQL schema pair and quantize it. If the similarity between a vectorized user question-SQL schema pair and a vectorized question-SQL schema pair in a pre-built vector database exceeds a threshold, then a similar question-SQL schema pair is retrieved; otherwise, no similar question-SQL schema pair is retrieved.

[0040] A search is performed in a pre-built vector database to match similar question-SQL schema pairs. Since the question-SQL schema pairs retrieved by the vector database are obtained through fuzzy matching, the search results undergo a two-layer refinement process. The original question and the question-SQL schema pairs are then compared based on distance similarity and ranked using a re-rank model. The top 5 are selected as the search results.

[0041] Step S3: If a similar question-SQL schema pair is found, generate three candidate SQL statements based on the original question and its related information; If a similar question-SQL schema pair is found, the retrieved question-SQL schema pair is used as contextual hints and the original question, external knowledge of the question, and relevant databases (tables, fields, storage types, value examples, existence of null values) as input to the LLM. At the same time, erroneous SQL statements are also used as input to avoid generating erroneous SQL statements again.

[0042] LLM can, but is not limited to, select deepseek, chatgpt, and GLM to output results simultaneously. Ultimately, it outputs three candidate SQL statements through three different large models.

[0043] Step S4: If no similar question-SQL schema pair is found, the corresponding statement generation path is determined based on the complexity of the original question, resulting in three candidate SQL statements, such as... Figure 2 As shown; Step S4.1: If no similar question-SQL schema pair is found, determine the complexity of the original question by generating SQL statements based on whether the original question needs to be decomposed into sub-questions. For questions not found by vector retrieval, SQL statements are generated using a sub-question generation method.

[0044] We use a function call tool to determine whether it's necessary to break down the original problem into subproblems and then generate SQL statements by solving these subproblems. Original problems that don't require subproblem generation are called simple problems. Conversely, problems that do require subproblem generation are called complex problems.

[0045] Step S4.2: If it is a simple question, use the pre-built keyword query large language model to generate the question-SELECT keyword, and generate three candidate SQL statements based on the question-SELECT keyword, the original question and related information; The original question is input into the keyword query language model to obtain the question-SELECT keyword in the question-generated SQL statement.

[0046] Input the original question, external knowledge about the question, relevant databases (tables, fields, storage types, value examples, whether null values ​​exist), and the question-SELECT keyword into the LLM to generate three candidate SQL statements.

[0047] Select deepseek, chatgpt, and GLM and output their results simultaneously. Output three candidate SQL statements.

[0048] Step S4.3: If it is a complex problem, use the pre-built keyword query big language model to generate the question-SELECT keyword, and decompose the original problem into multiple sub-problems. Generate the corresponding sub-SQL statements based on the sub-problems and verify them. If the verification is successful, keep them. Generate three candidate SQL statements based on the kept sub-problems and their corresponding sub-SQL statements, the question-SELECT keyword, the original problem and its related information. Step S4.3.1: Input the original question into the keyword query language model to obtain the corresponding question - SELECT keyword; Step S4.3.2: Decompose the complex problem based on a tree structure to generate multiple decomposition routes, and perform query verification on the sub-SQL statements generated for each sub-problem on each decomposition route. If all sub-problems are successfully queried, the decomposition route is retained as the statement generation path; the process is as follows: First, starting with the complex problem (i.e. the original problem), the complex problem is decomposed into three sub-problems, and the decomposed sub-problems become the child nodes of the tree. If one of the sub-problems cannot cover all the information for solving the complex problem, the decomposition continues until all the information for solving the complex problem is covered, resulting in multiple decomposition paths in the problem tree. Secondly, a sub-SQL statement is generated based on each sub-problem on the decomposition route, and a query is executed in the database for verification. If the query is successful, the sub-problem is retained; if the query fails, the modified sub-problem is regenerated until the number of generation attempts reaches the set number of correction attempts.

[0049] If a sub-SQL statement executes incorrectly or returns no result, the error message or no-result status is fed back to the parent node of the sub-problem, which then regenerates the corresponding sub-problem. This feedback and modification process is configured to be repeated twice.

[0050] Finally, if the generated sub-SQL statement still fails to be validated after the set number of times the number of generation attempts has been reached, then the entire decomposition path containing the sub-problem is discarded, and all decomposition paths with successful queries are retained as the statement generation path.

[0051] If, after two modifications, the sub-SQL statement generated by the subproblem still fails to execute or produces no result, then the subproblem is discarded, and the entire generation path extending from the subproblem is abandoned.

[0052] Step S4.3.3: Randomly select one of the multiple statement generation paths as the execution route, and generate three candidate SQL statements from all the sub-problems and corresponding sub-SQL statements, the original problem, external knowledge of the problem, related databases, and the problem-SELECT keyword on the execution route; Once the entire problem tree is generated, iterate through all the generated routes in the problem tree and select the route where all sub-SQLs can be executed correctly and return data results. If multiple correct routes exist, randomly select one as the final execution route.

[0053] It is important to note that if all sub-SQL statements in the decomposition route fail to execute or produce no results, a fallback query statement SELCET(column) FROM(table) LIMIT 1 is used to guarantee the output of the results.

[0054] Finally, all sub-problems along the execution path and their corresponding sub-SQL statements will be processed, including the original problem, external knowledge of the problem, relevant databases (tables, fields, storage types, value examples, existence of null values), and the problem-SELECT keyword as input to the LLM. LLM can select, but is not limited to, deepseek, chatgpt, and GLM to output results simultaneously, and output three candidate SQL statements.

[0055] This embodiment realizes the entire process of SQL generation through steps S2-S4. By using the sub-problem tree decomposition mechanism, complex problems such as multi-table join problems or nested queries can be broken down into simple sub-problems. The sub-problems are solved step by step to generate the final SQL statement, covering scenarios that simple queries cannot handle.

[0056] Step S5: Validate the three candidate SQL statements to obtain the text-to-structured query statement.

[0057] Step S5.1: Perform cross-consistency voting on the three candidate SQL statements; The three candidate SQL statements are selected by cross-consistency voting based on the answer.

[0058] Step S5.2: If all voting results are consistent, then select one of the three candidate SQL statements as the text-to-structured query statement; Step S5.3: If the voting results are inconsistent, decompose and correct the SQL structure of each candidate SQL statement, and select one of the three corrected candidate SQL statements as the text-to-structured query statement.

[0059] Specifically, if the three candidate SQL statements are inconsistent or all of them are incorrect, then the SQL structure of each candidate SQL statement is decomposed and corrected. This involves decomposing each candidate SQL statement according to its keyword structure, comparing the information of the decomposed keywords, and defaulting to errors if they do not match.

[0060] The error information after comparison is compared with the corresponding candidate SQL statements, the original problem, and the relevant database description. The data is then input into the large language model to indicate the location of the error and make corrections.

[0061] In other words, each candidate SQL statement is decomposed and corrected, as follows: Using SQL parsing tools, each candidate SQL statement that malfunctions is broken down into information corresponding to its keywords. For example, the information corresponding to the SELECT statement in the candidate SQL statement is as follows: Candidate SQL1: SELECT learning; Candidate SQL2: SELECT learning; Candidate SQL3: SELECT School; Based on the information corresponding to the SELECT above, the large model will select the SELECT to learn as the information corresponding to the corrected SELECT.

[0062] Example 2 This embodiment provides a text-to-structured query statement generation system based on a large language model, including: The data processing module is configured to filter based on the original question, the question database, and external knowledge of the question to obtain a relevant database, and use the relevant database and external knowledge of the question as relevant information for the original question. The retrieval and matching module is configured to generate an initial SQL statement based on the original question and its related information, perform query validation based on the initial SQL statement to identify erroneous SQL statements, and search the vector database based on the erroneous SQL statements and the successfully queried initial SQL statements to determine whether similar question-SQL schema pairs exist. The successful search and matching module is configured to generate three candidate SQL statements based on the original question and its related information if a similar question-SQL schema pair is found. The retrieval matching failure generation module is configured to determine the corresponding statement generation path based on the complexity of the original question if no similar question-SQL schema pair is found, resulting in three candidate SQL statements. The validation module is configured to validate based on three candidate SQL statements to obtain a text-to-structured query statement.

[0063] The examples and application scenarios implemented by the above modules and corresponding steps are the same, but are not limited to the content disclosed in Embodiment 1 above. It should be noted that the above modules, as part of the system, can be executed in a computer system such as a set of computer-executable instructions.

[0064] The descriptions of each embodiment in the above embodiments have different focuses. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions in other embodiments.

[0065] The proposed system can be implemented in other ways. For example, the system embodiments described above are merely illustrative, and the division of modules described above is only a logical functional division. In actual implementation, there may be other division methods. For example, multiple modules may be combined or integrated into another system, or some features may be ignored or not executed.

[0066] Example 3 This embodiment provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the steps in the text-to-structured query statement generation method based on a large language model as described in Embodiment 1 above.

[0067] Example 4 This embodiment provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the program, it implements the steps in the text-to-structured query statement generation method based on a large language model as described in Embodiment 1 above.

[0068] Example 5 This embodiment provides a computer program product or computer program, including computer instructions stored in a computer-readable storage medium. The processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform the steps in the text-to-structured query statement generation method based on a large language model as described in Embodiment 1 above.

[0069] Those skilled in the art will understand that embodiments of the present invention can provide methods, systems, or computer program products. Therefore, the present invention can take the form of hardware embodiments, software embodiments, or embodiments combining software and hardware aspects. Furthermore, the present invention can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage and optical storage) containing computer-usable program code.

[0070] This invention is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and / or block diagrams, as well as combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.

[0071] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.

[0072] These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable apparatus for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.

[0073] Those skilled in the art will understand that all or part of the processes in the above embodiments can be implemented by a computer program instructing related hardware. The program can be stored in a computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. The storage medium can be a magnetic disk, optical disk, read-only memory (ROM), or random access memory (RAM), etc.

[0074] While the specific embodiments of the present invention have been described above in conjunction with the accompanying drawings, this is not intended to limit the scope of protection of the present invention. Those skilled in the art should understand that various modifications or variations that can be made by those skilled in the art without creative effort based on the technical solutions of the present invention are still within the scope of protection of the present invention.

Claims

1. A method for generating text-to-structured query statements based on a large language model, characterized in that, include: Filtering is performed based on the original question, the question database, and external knowledge of the question to obtain a relevant database. The relevant database and external knowledge of the question are used as relevant information for the original question. An initial SQL statement is generated based on the original question and its related information. A query is then executed based on the initial SQL statement to identify erroneous SQL statements. It then searches the vector database based on the erroneous SQL statement and the initial SQL statement that succeeded in the query to determine if similar problem-SQL schema pairs exist, including: An initial SQL statement is generated based on the original question and its related information. The query is executed in the question database based on the initial SQL statement. If the number of failed queries exceeds a set threshold, the SQL statement is marked as erroneous; otherwise, if the query is successful, the initial SQL statement that was successful is retained. Using the erroneous SQL statement and the initial SQL statement that queried successfully as the SQL statement to be retrieved, the user question-SQL schema pair is determined based on the SQL statement to be retrieved; according to the keywords in the SQL statement to be retrieved, the corresponding tables and columns and their stored values ​​in the SQL statement to be retrieved and the original question are masked, and the user question-SQL schema pair is generated and quantized. If the similarity between a user's question-SQL schema pair and a question-SQL schema pair in the vector database exceeds a threshold, then similar question-SQL schema pairs are retrieved; otherwise, no similar question-SQL schema pairs are retrieved. If a similar question-SQL schema pair is found, three candidate SQL statements are generated based on the original question and its related information. If no similar question-SQL schema pair is found, the corresponding statement generation path is determined based on the complexity of the original question, resulting in three candidate SQL statements, including: If no similar question-SQL schema pair is found, the complexity of the original question is determined by whether the original question needs to be decomposed into sub-questions to generate SQL statements. If it is a simple question, the question-SELECT keyword is generated using a pre-built keyword query large language model, and three candidate SQL statements are generated based on the question-SELECT keyword, the original question and related information. If the problem is complex, the pre-built keyword query language model is used to generate the question-SELECT keyword, and the original problem is decomposed into multiple sub-problems. The corresponding sub-SQL statements are generated and validated according to the sub-problems. If the validation is successful, they are retained. Based on the retained sub-problems and their corresponding sub-SQL statements, the question-SELECT keyword, the original problem and its related information, three candidate SQL statements are generated. The three candidate SQL statements are validated to obtain a text-to-structured query statement.

2. The method for generating text-to-structured query statements based on a large language model as described in claim 1, characterized in that, The vector database stores data that has been vectorized from the problem-SQL schema.

3. The method for generating text-to-structured query statements based on a large language model as described in claim 1, characterized in that, The original problem is broken down into multiple sub-problems. Corresponding sub-SQL statements are generated and validated based on the sub-problems. If the validation is successful, they are retained. Based on the retained sub-problems and their corresponding sub-SQL statements, the question-SELECT keyword, the original question, and related information, three candidate SQL statements are generated, including: A tree structure is used to decompose complex problems and generate multiple decomposition routes. Perform query validation on the sub-SQL statements generated for each sub-problem along the decomposition path; If all sub-questions are successfully queried, the decomposition route is retained as the statement generation path; Randomly select one of the multiple statement generation paths as the execution route, and generate three candidate SQL statements from all the sub-problems and corresponding sub-SQL statements, the original problem, external knowledge of the problem, relevant databases, and the problem-SELECT keyword.

4. The method for generating text-to-structured query statements based on a large language model as described in claim 1, characterized in that, The validation based on three candidate SQL statements yields a text-to-structured query statement, including: Perform cross-consistency voting on the three candidate SQL statements; If all the voting results are consistent, then one of the three candidate SQL statements will be selected as the text-to-structured query statement. If the voting results are inconsistent, the SQL structure of each candidate SQL statement is decomposed and corrected, and one of the three corrected candidate SQL statements is selected as the text-to-structured query statement.

5. A text-to-structured query statement generation system based on a large language model, employing the text-to-structured query statement generation method based on a large language model as described in any one of claims 1-4, characterized in that, include: The data processing module is configured to filter based on the original question, the question database, and external knowledge of the question to obtain a relevant database, and use the relevant database and external knowledge of the question as relevant information for the original question. The retrieval and matching module is configured to generate an initial SQL statement based on the original question and its related information, and to perform query validation based on the initial SQL statement to identify erroneous SQL statements. It also searches the vector database based on the erroneous SQL statement and the initial SQL statement that succeeded in the query to determine if similar problem-SQL schema pairs exist; The successful search and matching module is configured to generate three candidate SQL statements based on the original question and its related information if a similar question-SQL schema pair is found. The retrieval matching failure generation module is configured to determine the corresponding statement generation path based on the complexity of the original question if no similar question-SQL schema pair is found, resulting in three candidate SQL statements. The validation module is configured to validate based on three candidate SQL statements to obtain a text-to-structured query statement.

6. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the steps in the method for generating text-to-structured query statements based on a large language model as described in any one of claims 1-4.

7. A computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the program, it implements the steps in the method for generating text-to-structured query statements based on a large language model as described in any one of claims 1-4.

8. A computer program product, characterized in that, The computer program product includes a computer program that, when executed by a processor, implements the steps in the text-to-structured query statement generation method based on a large language model as described in any one of claims 1-4.

Citation Information

Patent Citations

  • Text-to-structured query language statement generation method, system and equipment

    CN118820285A

  • Text-to-SQL (Structured Query Language) method based on large language model

    CN121412260A

  • Structured query statement generation method and device

    CN121858608A