Text-to-sql generation method and system based on large language model and storage medium

By employing a Text-to-SQL generation method based on a large language model, and utilizing keyword extraction, fuzzy detection, and encoded knowledge tables, the problem of insufficient understanding of database content in existing technologies is solved, achieving accurate generation of SQL query statements and improving the data search efficiency for non-professionals.

CN120743942BActive Publication Date: 2026-02-17HARBIN 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
CN202511255497.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-09-04
Publication Date
2026-02-17
Estimated Expiration
2045-09-04

AI Technical Summary

Technical Problem

Existing text-to-SQL methods cannot accurately perceive the relevant data content stored in the database when dealing with natural language problems in real business scenarios that only contain data content keywords. This leads to errors in the condition column names and condition content values ​​used in the generated SQL query statements, affecting the success rate and accuracy of execution.

Method used

A Text-to-SQL generation method based on a large language model is adopted. Seed SQL is generated through keyword extraction and fuzzy detection, and relevant knowledge is obtained by using an encoded knowledge table. The final SQL is generated through multiple rounds of modification. The Dmeta-embedding algorithm is combined to align the encoded knowledge and ensure the accuracy of the generated SQL query statement.

Benefits of technology

It improves the accuracy and efficiency of non-specialist users searching for relevant data in the database, and can directly generate accurate SQL query statements without a large amount of training data, thus improving the performance of processing complex text.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120743942B_ABST
    Figure CN120743942B_ABST
Patent Text Reader

Abstract

The application discloses a Text-to-SQL generation method and system based on a large language model and a storage medium, and the method comprises keyword extraction and fuzzy detection: data content keywords in the text are extracted, associated data tables and columns are speculated, data content synonyms are speculated, and executable seed SQL is generated according to the speculation result to perform fuzzy detection; coding knowledge acquisition and alignment: coding knowledge is acquired according to the fuzzy detection search result; the most relevant knowledge of each data content keyword is detected from the coding knowledge table to form a coding knowledge set; SQL statement generation and modification: fuzzy SQL is generated according to the coding knowledge set and is executed, and the final SQL is generated through multiple rounds of modification according to the feedback result and the coding knowledge set. The application improves the efficiency of acquiring Text-to-SQL and can accurately search the data related to the natural language question from the database.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of database query language generation, and in particular to a Text-to-SQL generation method and system based on a large language model and a storage medium. BACKGROUND

[0002] As the first step of data analysis, data analysis personnel need to find the database, table and field used for analysis from the database according to the data analysis task requirements, and then continue to carry out further data analysis tasks by writing data analysis SQL code. The field of Text-to-SQL research focuses on converting natural language questions into corresponding SQL query statements, which enables personnel without basic programming skills to directly search for data related to data analysis problems from the database. The current common Text-to-SQL method mainly focuses on generating SQL query statements by inputting only the database schema, and inputting the database schema and data content examples. These methods cannot accurately perceive the relevant data content stored in the database when facing data analysis natural language problems in real business scenarios that only contain data content keywords (for example: "Please answer what is the GDP growth rate in the first quarter of 2023?"), resulting in errors in the condition column name fields and condition content values used in the generated SQL query statements, negatively affecting the execution success rate and query accuracy of the SQL query statements. SUMMARY

[0003] To solve the above problems, the present application provides a Text-to-SQL generation method and system based on a large language model and a storage medium, aiming to improve the efficiency of obtaining Text-to-SQL, so that non-professional personnel without basic programming skills can accurately search for data related to natural language problems directly from the database.

[0004] According to a first aspect of the embodiments of the present disclosure, a Text-to-SQL generation method based on a large language model is provided, which comprises the following steps:

[0005] Keyword extraction and fuzzy detection: extract data content keywords in the text, infer associated data tables and columns, and data content synonyms, and generate executable seed SQL according to the inference results for fuzzy detection;

[0006] Encoding knowledge acquisition and alignment: acquire encoding knowledge according to the fuzzy detection search results; detect the most relevant knowledge from the encoding knowledge table for each data content keyword, and form an encoding knowledge set;

[0007] SQL statement generation and modification: generate fuzzy SQL according to the coding knowledge set and execute, and modify according to the feedback result and the coding knowledge set for multiple rounds to generate the final SQL.

[0008] In some embodiments, in the keyword extraction and fuzz detection process, based on the context semantic learning module of the large language model, the data content keywords and data content synonyms in the input natural language text are extracted, the database table name and column name field are confirmed according to the database schema, and a series of seed SQLs of storage content associated with the data content keywords are generated according to the database table name and column name field.

[0009] In some embodiments, the to-be-generated seed SQL is disassembled into three types of seeds independent of each other, namely column name field, data content, and SQL statement skeleton, and a seed pool is initialized for each type of seed, wherein for the column name field, the database column name field related to the extracted data content keywords is taken as the seed pool; for the data content, the synonyms generated by the large language model according to the data content keywords and the related columns are taken as the seed pool; and for the SQL statement skeleton, two candidate query SQL templates are set as the seed pool.

[0010] In some embodiments, for each data content keyword, one seed is randomly selected from each of the three seed pools to form a seed triple, and a new seed SQL is generated based on the seed triple, and the new seed SQL is executed in the database to obtain the search result; the non-repeated permutation and combination of the seed triples taken from the three seed pools is traversed, and the new seed SQL is repeatedly generated and executed to obtain the search result.

[0011] In some embodiments, in the coding knowledge acquisition process, if the fuzz detection search result shows that the data content keyword in the text is stored in the current associated column with a unique accurate content value, the data content keyword related content is standardized and put into the coding knowledge table as the available candidate knowledge for generating the final SQL, wherein the data content keyword related content standardization processing includes storing the data content keyword, the associated database name, the associated data table name, the associated data column name, and the accurate storage content value in the order as a piece of knowledge in a fixed format, and merging into the coding knowledge table; if the fuzz detection search result shows that the data content keyword in the question is stored in the current associated column with a non-unique accurate content value, or cannot be searched in the current associated column, it is considered that the current search result is invalid and is discarded directly.

[0012] In some embodiments, in the encoding knowledge alignment process, the Dmeta-embedding algorithm is used to calculate the embedding representation of each piece of knowledge in the natural language text data content keyword and the encoding knowledge table, and the cosine similarity of the embedding representation of the data content keyword and the embedding representation of each encoding knowledge is calculated; the encoding knowledge with the largest cosine similarity result is selected, and the column name field of the database schema and the exact value of the database storage content contained therein are used as available knowledge for generating the final SQL.

[0013] In some embodiments, in the SQL statement generation and modification process, a fuzzy SQL is generated and executed according to the encoding knowledge set, and according to the feedback result, the available knowledge corresponding to all data content keywords in the natural language text is repeatedly modified and executed until the modification is correct or the number of modifications reaches a preset number, and the final SQL is obtained.

[0014] According to a second aspect of the embodiments of the present disclosure, a Text-to-SQL generation system based on a large language model is provided, and the system comprises:

[0015] A keyword extraction and fuzz detection module is configured to extract data content keywords in the text, infer associated data tables and columns, and data content synonyms, and generate an executable seed SQL according to the inference result for fuzz detection;

[0016] An encoding knowledge acquisition and alignment module is configured to acquire encoding knowledge according to the fuzz detection search result; detect the most relevant knowledge for each data content keyword from the encoding knowledge table, and form an encoding knowledge set;

[0017] An SQL statement generation and modification module is configured to generate and execute a fuzzy SQL according to the encoding knowledge set, and perform multiple rounds of modification according to the feedback result and the encoding knowledge set to generate a final SQL.

[0018] According to a third aspect of the embodiments of the present disclosure, an electronic device is provided, which comprises a memory, a processor, and a computer program stored on the memory and executable on the processor, and the processor executes the program to implement the steps of the Text-to-SQL generation method based on a large language model.

[0019] According to a fourth aspect of the embodiments of the present disclosure, a non-transitory computer readable storage medium is provided, and the storage medium stores computer instructions, and the instructions are executed by a processor to implement the steps of the Text-to-SQL generation method based on a large language model.

[0020] The Text-to-SQL generation method, system and storage medium based on a large language model provided by the embodiments of the present disclosure have the following beneficial effects compared with the prior art:

[0021] Existing methods for text-to-SQL mainly include: text-to-SQL using database schema, text-to-SQL using database schema and data content examples. The text-to-SQL method using database schema generates SQL statements by parsing natural language questions through a deep learning model. Although it has achieved certain results in implementing natural language to SQL conversion, it ignores the importance of data content in SQL composition and is prone to generate incorrect condition values when dealing with cases where the problem description and database content are not completely consistent, because it lacks in-depth understanding of the data itself. The text-to-SQL method using database schema and data content examples combines database schema and data content examples to perform text-to-SQL operations in a way that is closer to actual data. However, these methods, while selecting data content examples based on natural language questions, lack the ability to understand the problem and automatically extract relevant data from the database, which can result in poor performance when dealing with complex problems. The present application can fully understand the text and automatically extract relevant data from the database, improving the effectiveness of handling complex text. In addition, existing methods generally still require a large amount of data to train the model, while the present application method does not require training and directly utilizes a large model to construct text-to-SQL.

[0022] It should be understood that the above general description and the following detailed description are only exemplary and explanatory, and cannot limit the present disclosure. BRIEF DESCRIPTION OF DRAWINGS

[0023] The accompanying drawings, which are incorporated into and form part of the specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the application;

[0024] Figure 1 is a flow chart of a text-to-SQL generation method based on a large language model in an embodiment of the present application;

[0025] Figure 2 is an example diagram of a keyword extraction and fuzz detection method in an embodiment of the present application;

[0026] Figure 3 is an example diagram of an encoding knowledge acquisition and alignment method in an embodiment of the present application;

[0027] Figure 4 is an example diagram of a SQL statement generation and modification method in an embodiment of the present application;

[0028] Figure 5 is a structure diagram of a text-to-SQL generation system based on a large language model in an embodiment of the present application;

[0029] Figure 6This is a schematic diagram of an electronic device according to an embodiment of the present invention. Detailed Implementation

[0030] The present invention will now be described in further detail with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are for illustrative purposes only and are not intended to limit the scope of the invention. Furthermore, it should be noted that, for ease of description, only the parts relevant to the present invention are shown in the drawings, not the entire structure.

[0031] Before discussing the exemplary embodiments in more detail, it should be noted that some exemplary embodiments are described as processes or methods depicted as flowcharts. Although the flowcharts describe the steps as sequential processes, many of these steps can be performed in parallel, concurrently, or simultaneously. Furthermore, the order of the steps can be rearranged. The process can be terminated when its operation is complete, but may also have additional steps not included in the figures. The process can correspond to a method, function, procedure, subroutine, subroutine, etc.

[0032] Text-to-SQL (TTO) technologies analyze the schema and data values ​​of natural language processing (NLP) databases to convert NLP questions into SQL queries that can be executed correctly in the database to produce the desired results. These technologies improve the efficiency of writing SQL queries and allow non-specialist personnel without basic programming skills to directly search for relevant data from the database. This data contains the potential information needed to answer NLP questions. By acquiring and analyzing this data, and extracting the hidden key information, relevant personnel can adjust strategies and design solutions in a timely manner based on the characteristics, state, or situation reflected by the data, thus leveraging the advantages brought by the data's value.

[0033] This invention relates to a Text-to-SQL generation method, system, and storage medium based on a large language model, and provides the following embodiments:

[0034] The implementation example addresses the natural language problem of data content keywords in real-world business scenarios of data analysis. It innovatively proposes a Text-to-SQL method for self-extraction of table content based on a large language model. It designs a fuzzy matching search strategy to fully perceive the database content, constructs an encoded knowledge table to enhance the retrieved data content results, and uses a generation-execution-correction framework to generate SQL query statements using the data content.

[0035] like Figure 1 As shown, a Text-to-SQL generation method based on a large language model includes the following steps:

[0036] Keyword extraction and fuzzy detection

[0037] 1.1 Extract data content keywords in the text, infer related data tables and columns, and data content synonyms;

[0038] 1.2 Generate executable seed SQL according to the inference results for fuzzy detection;

[0039] Coding knowledge acquisition and alignment

[0040] 2.1 Acquire coding knowledge according to the fuzzy detection search results;

[0041] 2.2 Detect the most relevant knowledge from the coding knowledge table for each data content keyword, and form a coding knowledge set;

[0042] SQL statement generation and modification

[0043] 3.1 Generate fuzzy SQL according to the coding knowledge set and execute it;

[0044] 3.2 Modify according to the feedback results and the coding knowledge set for multiple rounds to generate the final SQL.

[0045] Specifically, the embodiment extracts data content keywords from natural language questions, infers possible related table names, field names, and column names in the database schema, and possible stored content values in the database; then generates executable seed SQL according to the inference results to query the database, and combines the coding knowledge table to help correct the execution results, to obtain a coding knowledge set for generating SQL query statements, including the column names corresponding to the data content keywords in the question and the stored exact values of the data content; finally, using the obtained coding knowledge set, through multiple rounds of generating, executing and modifying SQL query statements, the final SQL result is obtained.

[0046] During the keyword extraction and fuzzy detection process, based on the context semantic learning module of the large language model, the data content keywords and data content synonyms in the input natural language text, the database schema, and the top N data content examples with the highest frequency are extracted, and the database table name and column name field are confirmed according to the database schema, and a series of seed SQLs of the stored content associated with the data content keywords are generated according to the database table name and column name field.

[0047] In one specific embodiment, the keyword extraction and fuzzy detection method is as follows Figure 2As shown. First, a keyword extraction method based on multi-round prompt instruction (few-shot prompt-based) is designed. The content of the input prompt instruction includes natural language questions, database schema, and the top six data content samples with the highest frequency. Based on the context semantic learning ability of LLM, LLM extracts data content keywords in natural language questions, and confirms the required database table name and column name field according to the provided database schema. It should be noted that the database table name field found here is required to be unique, and the column name field is not unique. The purpose of this is to accurately correspond the inaccurate data content keywords in the question to the associated data table as much as possible, while maximizing the search of the database for content values related to the data content keywords, so as to confirm which column name fields and accurate content values stored in the database are used when generating the final SQL query statement.

[0048] Based on the data content keywords and corresponding database table name and column name field obtained above, the application innovatively combines the idea of dynamic fuzz testing into the Text-to-SQL task and designs a fuzzy detection method based on multi-round prompt instruction. In the previous keyword extraction method, it has been emphasized that the application hopes to maximize the search for database stored content values related to inaccurate data content keywords in the question, so the fuzzy detection step needs to generate a series of SQL statements that fuzzily find the stored content in the specified column associated with the data content keywords, so that the subsequent coding knowledge acquisition and alignment method can confirm the accurate content values stored in the database corresponding to the inaccurate data content in the question from the result sub-table found. Specifically, the embodiment of the application introduces a large model to help generate diverse SQL query statements (i.e., Seed SQL) for fuzzy detection.

[0049] The seed SQL to be generated is divided into three types of column name fields, data content, and SQL statement skeletons, which are independent of each other, and the seed pool is initialized for each type of seed. For column name fields, the database column name fields related to the extracted data content keywords are used as the seed pool; for data content, synonyms generated by a large language model according to the data content keywords and related columns are used as the seed pool; and two candidate query SQL templates are set as the seed pool for SQL statement skeletons. It should be noted that the seed pools corresponding to the three types are also independent of each other.

[0050] In some embodiments, the Seed SQL to be generated is disassembled into three types of independent seeds, and a seed pool is initialized for each type of seed to promote the generation of Seed SQL. As shown in Table 1 below, three types of variable seeds are set for Seed SQL: column name field, data content, and SQL statement skeleton. For the column name field, all possible relevant database column name fields obtained in the previous keyword extraction part are used as the seed pool. For the data content, this paper uses the synonyms of the data content keywords in the question guessed by the large model. The prompt instruction content input into the large model includes the data content keywords in the question and the data content examples of the relevant columns. The synonyms guessed by the large model and the data content keywords from the original question are used as the seed pool. For the SQL statement skeleton, the following two candidate query SQL templates are set as the seed pool:

[0051] SELECT DISTINCT {column name} FROM {table name} WHERE {column name} ={data content}

[0052] SELECT DISTINCT {column name} FROM {table name} WHERE {column name}LIKE '%{data content}%.

[0053] Table 1 Seed type and seed pool construction details of type-aware variation

[0054]

[0055] For each data content keyword, a seed triplet is randomly selected from each of the three seed pools, and a new seed SQL is generated based on the seed triplet. The new seed SQL is executed in the database and the search result is obtained. The non-repeated permutation and combination of the seed triplets taken from the three seed pools is traversed, and the new seed SQL is repeatedly generated and executed to obtain the search result.

[0056] In the implementation process, for each extracted data content keyword, first, a new Seed SQL is obtained by randomly selecting a seed from the seed pool each time and then mutating it. Subsequently, the new Seed SQL is executed in the database and the search result is obtained. This process is repeated until each seed is selected. Specifically, each extracted data content keyword has three seed pools: column name field, data content, and SQL statement skeleton. The mutation process is as follows: for each extracted data content keyword, all seed triplets {column name, data content, SQL statement skeleton} that can be obtained by non-repeated permutation and combination are taken from the three seed pools mentioned above. Subsequently, a corresponding Seed SQL is generated for each seed triplet, and the search result is obtained by executing it in the database.

[0057] In the encoding knowledge acquisition process, if the fuzzy detection search result shows that the precise content value of the data content keyword in the text stored in the current associated column is unique, the data content keyword related content is standardized and processed as encoding knowledge and put into the encoding knowledge table as a usable candidate knowledge for generating the final SQL. The data content keyword related content standardization process includes storing the data content keyword, associated database name, associated data table name, associated data column name, and precise stored content value in the order of a fixed format of knowledge, and merging into the encoding knowledge table. If the fuzzy detection search result shows that the precise content value of the data content keyword in the question stored in the current associated column is not unique or cannot be searched in the current associated column, it is considered that the current search result is invalid and is discarded directly.

[0058] In the encoding knowledge alignment process, the Dmeta-embedding algorithm is used to calculate the embedding representation of the data content keyword in the natural language text and each piece of knowledge in the encoding knowledge table, and the cosine similarity of the embedding representation of the data content keyword and the embedding representation of each piece of encoding knowledge is calculated. The encoding knowledge with the largest cosine similarity result is selected, and the column name field of the database schema and the precise value of the database stored content contained therein are used as usable knowledge for generating the final SQL.

[0059] Specifically, the encoding knowledge acquisition and alignment method is as shown in Figure 3 First, the search results of the fuzzy detection section of the previous method are processed by category to promote the encoding knowledge acquisition section:

[0060] If the search result shows that the exact content value of the data content keyword in the problem is stored in the current associated column. This paper standardizes it into encoding knowledge and puts it into the encoding knowledge table as a usable candidate knowledge for generating the final SQL statement. The standardization here refers to storing the data content keyword, the associated database name, the associated data table name, the associated data column name, and the exact stored content value in the order of a piece of json format knowledge, and merging it into the encoding knowledge table.

[0061] If the search result shows that the exact content value of the data content keyword in the problem is not unique or cannot be searched in the current associated column. Then the current search result is considered invalid and is directly discarded.

[0062] It should be emphasized that the present application innovatively designs an encoding knowledge table based on retrieval augmented generation (RAG) and introduces its use in the method. Specifically, the table consists of five columns of data content keywords, associated database names, associated data table names, associated data column names, and exact stored content values. It mainly contains two types of knowledge: the first type of knowledge comes from the relationship matching data table existing in the real database, recording the corresponding relationship between the domain synonym stored in the database and the specific encoding column. The second type of knowledge comes from the candidate knowledge supplemented in the encoding knowledge acquisition link. There are two great benefits to introducing the designed encoding knowledge table: first, the table contains the encoding knowledge used in the construction process of the real database, which helps the large model to better understand the database content and improve the accuracy of generating SQL query statements. Second, the table is dynamic. With the continuous updating of knowledge in the encoding knowledge acquisition link, it helps the large model to further understand the data content of the specific database and improve the accuracy of generating SQL query statements.

[0063] Based on the enhanced encoding knowledge table output by the above link, further understanding of the database content related to the inaccurate data content keyword in the natural language question is carried out, and the encoding knowledge alignment link is carried out. First, the Dmeta-embedding algorithm is used to calculate the embedding representation of the data content keyword in the question and each piece of knowledge in the encoding knowledge table. The calculation formula is as follows, where represents the th data content keyword extracted from the natural language question, represents the th piece of knowledge in the encoding knowledge table:

[0064] ,

[0065] ,

[0066] Next, the cosine similarity is calculated for the embedding representation of the data content and the embedding representation of the encoded knowledge:

[0067] ,

[0068] Then, for each data content keyword in the question, the encoded knowledge with the largest cosine similarity result is selected, and the column name field and the exact value of the database storage contained therein are used as available knowledge for generating the final SQL query statement. Finally, the knowledge corresponding to all data content keywords in the question is input into the following SQL query statement generation and modification method to execute, modify, and generate the final SQL query statement result.

[0069] During the SQL statement generation and modification process, the fuzzy SQL is generated and executed according to the encoded knowledge set, and according to the feedback result, the available knowledge corresponding to all data content keywords in the natural language text is repeatedly modified and executed until the modification is correct or the number of modifications reaches the pre-set number of modifications, and the final SQL is obtained.

[0070] Specifically, the SQL statement generation and modification method is as shown in Figure 4 First, an initial version of the SQL query statement (Fuzzy SQL) is generated. The reason for calling it "Fuzzy SQL" is that the initial SQL query statement is given by the large model after fully understanding the question according to the input prompt instruction. It has a complete SQL statement skeleton, but the column name field and data content used therein are directly derived from the natural language question and are uncertain and may need to be modified. Next, the large model uses the encoded knowledge provided earlier to modify the Fuzzy SQL. The encoded knowledge includes the column name field related to the data content keyword in the question and the exact value stored in the database. Next, the modified Fuzzy SQL is executed in the database, and the execution result is used to evaluate whether the current SQL query statement is correct, feasible, and capable of obtaining a non-empty result. If the execution result does not report an error or return an empty result, the modified Fuzzy SQL is considered to be usable and is directly used as the final output result (Precise SQL). If the execution result reports an error or returns an empty set as the execution result, it is considered that the modified Fuzzy SQL still has problems and needs to be modified again according to the encoded knowledge. The above generation-execution-modification process will be repeated until the modification is correct or the number of modifications reaches the pre-set upper limit.

[0071] Another embodiment is used to illustrate a large language model-based Text-to-SQL generation system, as shown in Figure 5As shown, the system 500 includes:

[0072] The keyword extraction and ambiguity detection module 510 is configured to extract data content keywords in the text, infer associated data tables and columns, and data content synonyms, and generate executable seed SQL according to the inference results for ambiguity detection.

[0073] The encoding knowledge acquisition and alignment module 520 is configured to acquire encoding knowledge according to the ambiguity detection search results; detect the most relevant knowledge for each data content keyword from the encoding knowledge table, and form an encoding knowledge set.

[0074] The SQL statement generation and modification module 530 is configured to generate and execute ambiguous SQL according to the encoding knowledge set, and perform multiple rounds of modification according to the feedback results and the encoding knowledge set to generate a final SQL.

[0075] In addition to the above modules, the system 500 can also include other components, however, since these components are irrelevant to the content of the embodiments of the present disclosure, their illustrations and descriptions are omitted here.

[0076] The other specific working processes of the Text-to-SQL generation system 500 based on the large language model are described above with reference to the description of the Text-to-SQL generation method based on the large language model, and will not be repeated here.

[0077] Another embodiment is used to illustrate that the system of the present application can also be implemented by means of Figure 6 the architecture of the computing device shown. Figure 6 The architecture of the computing device is shown. As Figure 6 shown, the computer system 610, the system bus 630, one or more CPUs 640, the input / output 620, the memory 650, etc. The memory 650 can store various data or files used by the computer processing and / or communication, and the program instructions executed by the CPU including the embodiment of the Text-to-SQL generation method based on the large language model. Figure 6 The architecture shown is only exemplary, and when implementing different devices, the architecture Figure 6one or more components in the system. The memory 650, as a type of computer readable storage medium, can be used to store software programs, computer-executable programs, and modules, such as program instructions / modules corresponding to the large language model-based Text-to-SQL generation method in the embodiments of the present application (for example, the keyword extraction and ambiguity detection module 510, the encoded knowledge acquisition and alignment module 520, and the SQL statement generation and modification module 530 in the large language model-based Text-to-SQL generation system 500). The one or more CPUs 640 execute various functional applications and data processing of the system of the present application by running the software programs, instructions, and modules stored in the memory 650, that is, implement the large language model-based Text-to-SQL generation method as described above, which includes the following steps:

[0078] Keyword extraction and ambiguity detection: extract data content keywords in the text, infer associated data tables and columns, and data content synonyms, and generate executable seed SQL according to the inference results for ambiguity detection;

[0079] Encoded knowledge acquisition and alignment: acquire encoded knowledge according to the ambiguity detection search results; detect the most relevant knowledge from the encoded knowledge table for each data content keyword, and form an encoded knowledge set;

[0080] SQL statement generation and modification: generate and execute ambiguous SQL according to the encoded knowledge set, and perform multiple rounds of modification according to the feedback results and the encoded knowledge set to generate the final SQL.

[0081] Of course, the processor of the server provided by the embodiments of the present application is not limited to performing the method operations as described above, but can also perform related operations in the large language model-based Text-to-SQL generation method provided by any embodiment of the present application.

[0082] The memory 650 can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system and application programs required by at least one function; the data storage area can store data created according to the use of the terminal, etc. In addition, the memory 650 can include a high-speed random access memory, and can also include a non-volatile memory, such as at least one magnetic disk storage device, a flash memory device, or other non-volatile solid-state memory device. In some examples, the memory 650 can further include a memory disposed remotely with respect to the one or more CPUs 640, which can be connected to the device through a network. Examples of the above network include but are not limited to the Internet, an intranet, a local area network, a mobile communication network, and a combination thereof.

[0083] Input / output 620 can be used to receive input of digital or character information, as well as generate key signal input relating to user settings and function controls of the device. Input / output 620 can also include a display device, such as a display screen.

[0084] The embodiment of the present application also provides a non-temporary computer readable storage medium, which stores a computer program, and the computer program is executed by a processor to realize the Text-to-SQL generation method based on a large language model described in the above embodiment. The computer readable storage medium of the embodiment of the present application can adopt any combination of one or more computer readable media. The computer readable medium can be a computer readable signal medium or a computer readable storage medium. The computer readable storage medium may, for example, but is not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or apparatus, or any combination of the above. More specific examples (non-exhaustive list) of the computer readable storage medium include: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this document, the computer readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, device or apparatus.

[0085] The computer readable signal medium can include a data signal propagated in a baseband or as a part of a carrier wave, in which a computer readable program code is borne. Such a propagated data signal can take on multiple forms, including but not limited to an electromagnetic signal, an optical signal or any suitable combination of the above. The computer readable signal medium can also be any computer readable medium that is not a computer readable storage medium and that can transmit, propagate or transport a program for use by or in connection with an instruction execution system, device or apparatus.

[0086] The program code contained on the storage medium can be transmitted in any suitable medium, including but not limited to wireless, wire, cable, optical fiber, RF, etc., or any suitable combination of the above.

[0087] In addition, the other specific working processes of the non-temporary computer readable storage medium are described above with reference to the description of the Text-to-SQL generation method based on a large language model, and will not be repeated here.

[0088] In this document, the terms "comprise", "comprising", or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a step, method, process, machine, article of manufacture, or composition of matter that comprises a list of elements does not include only those elements but can include other elements not expressly listed or inherent to such step, method, process, machine, article of manufacture, or composition of matter.

[0089] The above description is further to specific preferred embodiments of the present application and cannot be deemed to limit the specific implementation of the present application to these descriptions. For those of ordinary skill in the art to which the present application belongs, a number of simple deductions or replacements can be made without departing from the concept of the present application, and all of these should be deemed to fall within the protection scope of the present application.

Claims

1. A Text-to-SQL generation method based on a large language model, characterized in that, The method comprises the following steps: Key word extraction and fuzzy detection: extract data content keywords in the text, infer associated data tables and columns, and data content synonyms, and generate executable seed SQL according to the inference results for fuzzy detection; Coding knowledge acquisition and alignment: acquire coding knowledge according to the fuzzy detection search results; detect the most relevant knowledge from the coding knowledge table for each data content keyword, and form a coding knowledge set; SQL statement generation and modification: generate fuzzy SQL according to the coding knowledge set and execute it, and perform multiple rounds of modification according to the feedback results and the coding knowledge set to generate the final SQL; During the keyword extraction and fuzzy detection process, based on the context semantic learning module of the large language model, the data content keywords and data content synonyms in the input natural language text, the database schema, and the top N data content examples with the highest frequency are extracted, and the database table name and column name fields are confirmed according to the database schema. A series of seed SQL associated with the data content keywords are generated according to the database table name and column name fields. The seed SQL to be generated is divided into three types of seeds that are independent of each other: column name fields, data content, and SQL statement skeletons. The seed pool is initialized for each type of seed. For column name fields, the database column name fields related to the extracted data content keywords are used as the seed pool. For data content, the data content synonyms generated by the large language model based on the data content keywords and related columns are used as the seed pool. Two candidate query SQL templates are set as the seed pool for SQL statement skeletons.

2. The method of claim 1, wherein the method further comprises: For each data content keyword, a seed triple is formed by randomly selecting one seed from each of the three seed pools, and a new seed SQL is generated based on the seed triple. The new seed SQL is executed in the database to obtain the search results. The non-repeated permutation and combination of seed triples is traversed in the three seed pools, and new seed SQL is repeatedly generated and executed to obtain the search results.

3. The method of claim 1, wherein the method further comprises: During the coding knowledge acquisition process, if the fuzzy detection search results show that the exact content value of the data content keyword stored in the current associated column is unique, the data content keyword related content is standardized and put into the coding knowledge table as a candidate knowledge for generating the final SQL. The data content keyword related content standardization process includes storing the data content keyword, associated database name, associated data table name, associated data column name, and exact content value in the order of a fixed format knowledge, and merging into the coding knowledge table. If the fuzzy detection search results show that the exact content value of the data content keyword stored in the current associated column is not unique, or the exact content value cannot be searched in the current associated column, the current search result is considered invalid and discarded.

4. The method of claim 1, wherein the method further comprises: In the encoding knowledge alignment process, the Dmeta-embedding algorithm is used to calculate the embedding representation of the data content keywords in the natural language text and each piece of knowledge in the encoding knowledge table, and the cosine similarity of the embedding representation of the data content keywords and the embedding representation of each encoding knowledge is calculated; the encoding knowledge with the maximum cosine similarity result is selected, and the column name field of the database schema and the precise value of the database storage content contained therein are used as available knowledge for generating the final SQL.

5. The method of claim 4, wherein the method further comprises: In the SQL statement generation and modification process, the fuzzy SQL is generated and executed according to the encoding knowledge set, and according to the feedback result, the available knowledge corresponding to all data content keywords in the natural language text is repeatedly modified and executed until the modification is correct or the pre-set number of modifications is reached, and the final SQL is obtained.

6. A large language model-based Text-to-SQL generation system, characterized by, The system comprises: A keyword extraction and fuzz detection module is configured to extract data content keywords in the text, infer associated data tables and columns, and data content synonyms, and generate executable seed SQL according to the inference result for fuzz detection. An encoding knowledge acquisition and alignment module is configured to acquire encoding knowledge according to the fuzz detection search result, detect the most relevant knowledge from the encoding knowledge table for each data content keyword, and form an encoding knowledge set. An SQL statement generation and modification module is configured to generate and execute fuzzy SQL according to the encoding knowledge set, and perform multiple rounds of modification according to the feedback result and the encoding knowledge set to generate a final SQL. In the keyword extraction and fuzz detection process, based on the context semantic learning module of the large language model, the data content keywords and data content synonyms in the input natural language text, the database schema, and the top N data content examples with the highest frequency are extracted, the database table name and column name field are confirmed according to the database schema, and a series of seed SQL associated with the data content keywords and storage content are generated according to the database table name and column name field. The to-be-generated seed SQL is disassembled into three types of seeds independent of each other, i.e., column name field, data content, and SQL statement skeleton, and a seed pool is initialized for each type of seed, wherein for the column name field, the database column name field related to the extracted data content keywords is used as the seed pool; for the data content, the synonyms generated by the large language model according to the data content keywords and the related columns are used as the seed pool; and for the SQL statement skeleton, two candidate query SQL templates are set as the seed pool.

7. An electronic device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, The processor executes the program to implement the steps of the Text-to-SQL generation method based on the large language model according to any one of claims 1 to 5.

8. A non-transitory computer readable storage medium having stored thereon computer instructions, wherein, The instructions are executed by the processor to implement the steps of the Text-to-SQL generation method based on the large language model according to any one of claims 1 to 5.

Citation Information

Patent Citations

  • Natural language-to-SQL interactive generation method based on large language model

    CN117493379A

  • Text-to-structured query language conversion method based on large language model

    CN119415546A