A method and system for converting natural language into SQL statements
By combining entity recognition, Faiss retrieval, and a generative large language model, the NL2SQL technology solves the problem of language understanding and conversion of complex SQL logical relationships, achieves high-quality SQL statement generation, and improves the accuracy and adaptability of conversion.
Patent Information
- Application Number
- CN202411405510.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-10
- Publication Date
- 2025-09-16
- Estimated Expiration
- 2044-10-10
AI Technical Summary
Existing NL2SQL technology has deficiencies in the accuracy of language understanding and understanding of complex SQL logical relationships, making it difficult to achieve high-quality conversion of natural language to SQL statements.
By obtaining user questions for entity recognition, the Faiss vectorized retrieval tool is used to determine the query library table, and a generative large language model is used to convert user questions into SQL intermediate semantic representation based on the preset JSON structure of SQL intermediate semantic representation. Finally, executable SQL statements are generated, and error checking and correction are performed in conjunction with an automatic program generator.
The accuracy and business adaptability of NL2SQL are improved, and high-quality conversion from natural language to SQL statements is achieved.
Smart Images

Figure CN118916381B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of natural language processing technology, and in particular to a method and system for converting natural language into SQL statements. Background Art
[0002] NL2SQL (Natural Language to SQL) technology converts natural language into SQL queries. This technology has numerous applications, including database queries, data analysis, and business intelligence, particularly in the financial sector. While NL2SQL has great potential for improving the convenience of database queries, it still faces challenges and difficulties in actual development and application, such as inaccurate language understanding, misunderstanding of complex SQL logical relationships, and insufficient support for diverse SQL statements.
[0003] Currently, no effective solution has been proposed for the problem of how to perform high-quality NL2SQL conversion in related technologies. Summary of the Invention
[0004] The embodiments of the present application provide a method and system for converting natural language to SQL statements, so as to at least solve the problem of how to perform high-quality NL2SQL conversion in the related art.
[0005] In a first aspect, an embodiment of the present application provides a method for converting natural language into SQL statements, the method comprising:
[0006] Acquire a user question and perform entity recognition to obtain a named entity of the user question, wherein the user question is a natural language question involving a preset field;
[0007] Determine a query database table for the user question from the business database table of the preset field;
[0008] Based on the preset JSON structure of the SQL intermediate semantic representation, the user question is converted into the SQL intermediate semantic representation by a generative large language model based on the named entities and query library tables of the user question;
[0009] Based on the SQL intermediate semantic representation and the user question, the SQL intermediate semantic representation is converted into an executable SQL statement through the generative large language model.
[0010] In some embodiments, determining the query database table of the user question from the business database table of the preset domain includes:
[0011] The query library table of the user question is determined from the business library table of the preset field by using the Faiss vectorized search tool.
[0012] In some embodiments, determining the query database table of the user question from the business database table of the preset domain using the Faiss vectorized search tool includes:
[0013] Obtain the business database table of the preset domain, and convert the information contained in the business database table based on the JSON string to obtain a database table JSON description document;
[0014] Vectorize the library table JSON description document and the user question to obtain a Faiss vector;
[0015] The similarity between corresponding Faiss vectors is calculated by a Faiss vectorization search tool, and a query library table of the user question is determined from the business library table of the preset field.
[0016] In some embodiments, before converting the user question into an SQL intermediate semantic representation using the generative large language model, the method includes:
[0017] A preset JSON structure of an SQL intermediate semantic representation is defined, wherein the SQL intermediate semantic representation includes a basic SQL intermediate semantic representation and a complex SQL intermediate semantic representation.
[0018] In some embodiments, the preset JSON structure defining the SQL intermediate semantic representation includes:
[0019] Define a preset JSON structure for the basic SQL intermediate semantic representation, where the preset JSON structure contains tables, fields, conditions, and limits. Tables represent the names of the tables to be queried related to the user's query, fields represent the fields to be queried related to the user's query, conditions represent the filtering conditions involved in the user's query, and limits represent the query result limits of the user's query.
[0020] Define a preset JSON structure for complex SQL intermediate semantic representations, wherein the preset JSON structure includes sqls and rels, the sqls represents all basic SQL intermediate semantic representations involved in the user question, and the rels represents the relationship between the SQL intermediate semantic representations in sqls.
[0021] In some embodiments, based on a preset JSON structure of the SQL intermediate semantic representation and based on the named entities and query library tables of the user question, converting the user question into the SQL intermediate semantic representation using a generative large language model includes:
[0022] Constructing a first prompt word template based on a preset JSON structure represented by SQL intermediate semantics, the named entities of the user question, and the query library table;
[0023] The first prompt word template is input into a trained generative large language model, and the user question is converted into an SQL intermediate semantic representation through the generative large language model.
[0024] In some embodiments, based on the SQL intermediate semantic representation and the user question, converting the SQL intermediate semantic representation into an executable SQL statement using the generative large language model includes:
[0025] Constructing a second prompt word template based on the SQL intermediate semantic representation and the user question;
[0026] The second prompt word template is input into a trained generative large language model, and the SQL intermediate semantic representation is converted into an executable SQL statement through the generative large language model.
[0027] In some embodiments, after converting the SQL intermediate semantic representation into an executable SQL statement using the generative large language model based on the SQL intermediate semantic representation and the user question, the method includes:
[0028] A first proxy role and a second proxy role are created by an automatic program generator AutoGen, wherein the first proxy role is used to perform error checking and error information output on the executable SQL statement, and the second proxy role is used to correct errors on the executable SQL statement.
[0029] In some embodiments, the preset field is the financial futures field.
[0030] In a second aspect, an embodiment of the present application provides a system for converting natural language into SQL statements, the system being used to execute the method described in any one of the first aspects above, the system comprising an acquisition module, a retrieval module, and a conversion module;
[0031] The acquisition module is configured to acquire a user question and perform entity recognition to obtain a named entity of the user question, wherein the user question is a natural language question involving a preset field;
[0032] The retrieval module is used to determine the query database table of the user question from the business database table of the preset field;
[0033] The conversion module is configured to convert the user question into an SQL intermediate semantic representation based on the preset JSON structure of the SQL intermediate semantic representation and the named entities and query library tables of the user question using a generative large language model;
[0034] The conversion module is used to convert the SQL intermediate semantic representation into an executable SQL statement through the generative large language model according to the SQL intermediate semantic representation and the user question.
[0035] Compared with related technologies, the embodiments of the present application provide a method and system for converting natural language into SQL statements, wherein the method obtains named entities of user questions by acquiring user questions and performing entity recognition, wherein the user questions are natural language questions involving a preset domain; a query library table of the user questions is determined from a business library table of the preset domain; based on the preset JSON structure of the SQL intermediate semantic representation, the user questions are converted into SQL intermediate semantic representations based on the named entities and query library tables of the user questions through a generative large language model; based on the SQL intermediate semantic representation and the user questions, the SQL intermediate semantic representation is converted into executable SQL statements through a generative large language model, thereby realizing two-stage SQL statement generation, first converting the user questions into SQL intermediate semantic representations based on the preset JSON structure, and then converting them into executable SQL statements, which can effectively structure the information in the user questions, thereby ultimately improving the accuracy and business adaptability of NL2SQL, and solving the problem of how to perform high-quality NL2SQL conversion. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0037] Figure 1 This is a flowchart of the steps of the method for converting natural language to SQL statements according to an embodiment of the present application;
[0038] Figure 2 This is a structural block diagram of a natural language to SQL statement system according to an embodiment of the present application;
[0039] Figure 3 Schematic diagram of the internal structure of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION
[0040] In order to make the purpose, technical solutions and advantages of this application more clearly understood, the present application is described and illustrated below in conjunction with the accompanying drawings and examples. It should be understood that the specific embodiments described herein are merely used to explain this application and are not intended to limit this application. Based on the embodiments provided in this application, all other embodiments obtained by those of ordinary skill in the art without making any creative efforts are within the scope of protection of this application.
[0041] Obviously, the drawings described below are merely examples or embodiments of the present application. Those skilled in the art can, without inventive effort, apply the present application to other similar scenarios based on these drawings. Furthermore, it is also understood that, although the effort involved in such a development process may be complex and lengthy, for those skilled in the art related to the content disclosed in this application, changes in design, manufacturing, or production based on the technical content disclosed in this application are merely conventional technical means and should not be construed as an insufficiency of the content disclosed in this application.
[0042] References to "embodiments" in this application mean that a particular feature, structure, or characteristic described in connection with the embodiment may be included in at least one embodiment of the application. The appearance of this phrase in various places in the specification does not necessarily refer to the same embodiment, nor does it refer to independent or alternative embodiments that are mutually exclusive of other embodiments. It is understood, both explicitly and implicitly, by those skilled in the art that the embodiments described in this application may be combined with other embodiments unless there is a conflict.
[0043] Unless otherwise defined, technical or scientific terms used herein shall have the ordinary meaning as understood by persons of ordinary skill in the art to which this application belongs. The terms "a," "an," "an," "the," and similar expressions used herein do not denote quantitative limitations and may refer to either the singular or the plural. The terms "comprise," "include," "have," and any variations thereof, used herein, are intended to cover non-exclusive inclusions. For example, a process, method, system, product, or apparatus comprising a series of steps or modules (units) is not limited to the listed steps or units but may also include steps or units not listed, or may include other steps or units inherent to the process, method, product, or apparatus. The terms "connected," "connected," "coupled," and similar expressions used herein are not limited to physical or mechanical connections but may include electrical connections, whether direct or indirect. As used herein, "plurality" means two or more. "And / or" describes an association between associated objects, indicating that three possible relationships exist. For example, "A and / or B" may mean: A exists alone; A and B exist simultaneously; or B exists alone. The character " / " generally indicates that the objects before and after are in an "or" relationship. The terms "first", "second", "third", etc. involved in this application are only used to distinguish similar objects and do not represent a specific order for the objects.
[0044] This application embodiment provides a method for converting natural language into SQL statements. Figure 1 This is a flowchart of the steps of the method for converting natural language to SQL statements according to an embodiment of the present application. Figure 1 As shown, the method includes the following steps:
[0045] Step S102: obtaining a user question and performing entity recognition to obtain a named entity of the user question, wherein the user question is a natural language question involving a preset field;
[0046] It should be noted that the predefined domain is preferably the financial futures sector. The Universal Information Extraction Model (UIE) used for question entity recognition in step S102 is fine-tuned and trained using financial futures data, enabling it to automatically extract information such as entities, events, and relationships within the financial futures sector from text. Named entity types in the financial futures sector generally include futures principal entities (such as futures sectors, varieties, contracts, and exchanges), futures indicator terminology entities (such as settlement price, opening price, trading volume, transaction amount, number of warehouse receipts, planted area, and operating rate), and basic entities such as dates, countries / regions, numerical values, and financial institutions.
[0047] Furthermore, event extraction is similar to the named entity extraction described above, differing in that event text is longer and more sentence-level. However, the underlying recognition principles are identical, both classifying and identifying sequential text strings. Therefore, based on UIE's powerful unified modeling capabilities and few-shot learning transfer capabilities, a small number of sample examples are annotated for each type of event in the financial futures sector. To provide comprehensive coverage of financial futures events, examples of various types of events can be sorted out from the perspectives of product, industry / sector, entire market, and macroeconomic factors. In terms of product, the main focus is on events such as price increases for futures products, reduced demand for products, and increases or decreases in product operating rates. Industry / sector events primarily include collective energy price increases and tight meat supply. Macroeconomic events include war conflicts, extreme weather such as floods / droughts, trade frictions, and industrial support.
[0048] Through the above approach, we comprehensively sorted out various entities and event types in the financial futures field, annotated text examples, and fine-tuned training through UIE's few-shot learning mechanism. The system can effectively identify various events and provide a solid foundation for NL2SQL's subsequent understanding and processing.
[0049] Step S104, determining a query table for the user's question from a business table in a preset field;
[0050] Step S104 specifically includes the following steps:
[0051] Step S1041: Obtain a business library table of a preset domain, and convert the information contained in the business library table based on a JSON string to obtain a library table JSON description document;
[0052] It should be noted that the database table name, database table description, database table field information, etc. included in the business database are carefully sorted and converted based on the JSON string to obtain the database table JSON description document, as shown in Table 1.
[0053] Table 1
[0054] "Library table name": "dw_fut_agmt" "Table Description": "Futures contract code information table, mainly stores information such as futures contract name, contract code, trading market, contract quantity multiplier, delivery year, delivery month, etc." "Database table field information": [ ["agmt_cd", "AGMT", 'Contract code', 'such as i2405, a2407, etc.'][" agmt_nam", "AGMT", 'Contract name', 'such as copper 2405, iron ore 2408'][" product_cd", "PRODUCT", 'Product code', 'cu, a, etc.']["product_ nam", "PRODUCT", 'Product code', 'Copper, soybean, etc.'][" exch_cd", "ORG_EXCH", 'Exchange code', 'such as F1, F2, etc.'][" exch_ nam", "ORG_EXCH", 'Exchange name', 'such as Guangzhou Exchange, etc.'][" agmt_mul","NUM", 'Contract quantity multiplier', 'such as 1000, etc.'][" dely_year", "DATE YEAY", 'Delivery year', 'xxxx, such as 2024'][" dely_ mon ", "DATE MON", 'Delivery month', '1-12'][" dely_typ ", "varchar", 'Delivery method', '']]
[0055] Step S1042: vectorize the database table JSON description document and the user question to obtain a Faiss vector;
[0056] It should be noted that the converted JSON description documents for the database tables are converted into fixed-size vectors. This is typically achieved using models such as Word2Vec, Bert, and TF-IDF. However, to express semantic meaning, this paper utilizes the RoBerta-wwm-ext model, which excels in Chinese semantic representation. This model combines the advantages of Whole Word Masking (WWM) and the RoBerta model. Using financial futures data, the RoBerta-wwm-ext model is fine-tuned and trained for futures finance, performing transfer learning. During the pre-training phase, the WWM strategy is used to mask the training data, while the Next Sentence Prediction (NSP) task is eliminated, and the number of training steps is appropriately extended. By fine-tuning the trained model, the database table JSON description documents are effectively converted into 768-dimensional vectors. User questions and the entity category information identified from the questions are reassembled into new questions and converted into 768-dimensional semantic vectors using the RoBerta-wwm-ext model.
[0057] Furthermore, Faiss provides multiple index types, including Flat (brute force search), IVF (inverted file), and PQ (product quantization). Given the correspondence between entities in user questions and database table fields, the correspondence between some limited-scope terms in user queries (such as daily and monthly lines) and database table names, and the relatively limited number of database tables (typically ranging from dozens to hundreds), the IndexFlatL2 index structure is used to construct the Faiss index of database table information. The IndexFlatL2 index structure uses the Euclidean distance (L2 norm) as a metric to measure the similarity between vectors, effectively capturing the semantic associations between questions and database tables.
[0058] Step S1043 : The similarity between corresponding Faiss vectors is calculated by using the Faiss vectorization search tool, and a query library table of the user's question is determined from the business library table of the preset field.
[0059] It should be noted that the semantic similarity calculation is performed using the Faiss index to find the K items of data that are most similar to the query vector. Since the number of database tables involved in a general query generally does not exceed 10, K is set to 10 here. This identifies at most 10 database tables that are potentially related to the user's question.
[0060] After identifying the query tables, we need to further perform deep semantic computation based on their JSON descriptions and the user's question to determine whether they are clearly relevant to the current question. This process primarily involves fine-tuning and training a Transformer-based BERT classification model, which effectively captures long-range dependencies and enables deep logical association between questions and table information. A SoftMax layer is applied to the BERT model's hidden layer output to determine the relevance between the user query and the table. Fine-tuning this model using a small amount of sample data with refined annotations for the form <question, table description, yes / no> can achieve excellent results in accurately screening and clarifying tables.
[0061] Step S106: Based on the preset JSON structure of the SQL intermediate semantic representation, the user question is converted into an SQL intermediate semantic representation through a generative large language model based on the named entities and query library tables of the user question; wherein the SQL intermediate semantic representation includes a basic SQL intermediate semantic representation and a complex SQL intermediate semantic representation.
[0062] Step S106 specifically includes the following steps:
[0063] Step S1061, define a preset JSON structure for the basic SQL intermediate semantic representation, where the preset JSON structure includes tables, fields, conditions, and limits. Tables represents the name of the database table to be queried related to the user's question, fields represents the fields to be queried related to the user's question, conditions represents the filtering conditions involved in the user's question, and limits represents the query result limitations of the user's question.
[0064] Note that tables is in the form of an array[], with elements within it being strings, for example: ["Table 5: futures_options_realtime", "Table 3: dwd_main_agmt_latest"] . SQL intermediate semantics table information, in addition to the table name, may also contain table description field information. Furthermore, to support the complex SQL intermediate semantics later on, the elements within tabels[] also need to support subqueries represented by "$SQL".
[0065] Fields is in the form of an array[], with internal elements being strings. Each field must reflect the table name to support duplicate fields in multiple tables, for example: ["Table 5.futu_code Contract Code", "Table 5.lastprice Latest Price"]. Because some complex elements exist, such as average, maximum, minimum, and quantity, AVG(field name) is used to represent the average, MAX(field name) the maximum value, MIN(field name) the minimum value, and COUNT(field name) is used to represent the number of records queried.
[0066] Conditions takes the form of an array[], where each condition is represented by a triple consisting of (field name, operator, value). The field name in the condition triple must come from fields. The operators in the condition triple include greater than >, equal to =, not equal to !=, less than <, greater than or equal to >=, less than or equal to <=, not greater than !>, not less than !<, contain, in, start with, end with, and not null. The values of the condition triple can be either specific strings or references to the field field. For example: [("Table 5.futu_code Contract Code", "equal to=", "Bronze 2305")], [("Table 5.futu_code Contract Code", "equal to=", "$Table 6.futu_code Contract Code")]. Similar to the tables section, to support complex SQL intermediate semantics later in the code, the "values" of the elements within conditions[] must also support subqueries represented by "$SQL".
[0067] Limits are stored in the form of an array [] . Each field consists of three parts: (constraint field name, constraint type, constraint value). The constraint field name can be empty. Constraint types include: LIMIT (limiting the number of entries), ascending sorting (ASC), descending sorting (DESC), grouping (GROUP), and group filtering (GROUP_HAVING). Constraint values can also be empty. Constraint values also support deterministic fixed values, SQL statements, and field references. For example, "Top 10 prices from highest to lowest" can be represented as ("Table 5.price", DESC, 10).
[0068] Step S1062: define a preset JSON structure for complex SQL intermediate semantic representations, wherein the preset JSON structure includes sqls and rels. sqls represents all basic SQL intermediate semantic representations involved in the user question, and rels represents the relationship between the SQL intermediate semantic representations in sqls.
[0069] It should be noted that the default JSON structure for defining complex SQL intermediate semantic representation is:
[0070] {"sqls":[SQL intermediate semantic list], "rels":[($ SQL-head, $ SQL- end, relation)]}
[0071] sqls represents all the basic SQL intermediate semantic representations involved in the user's question, stored in a list []. Each object in the list is an SQL intermediate semantic query. rels represents the relationship between the basic SQL intermediate semantic representations in sqls, also expressed as a triple: (SQL-head, SQL-end, relationship type). SQL-head and SQL-end refer to the specific SQL subscript positions in sqls. The main relationship types are as follows:
[0072] WHERE_EQ scalar subquery. This type of query is used when the SQL-end returns a single value, and the SQL-head is used in the WHERE clause to use the SQL-end result.
[0073] FROM_SQL row subquery. This type is used when the SQL-end returns multiple columns of a single row, while the SQL-head is usually used in the SELECT list.
[0074] WHERE_IN table subquery. This type is used when the SQL-end returns multiple rows, while the SQL-head is usually used in the FROM clause or with the IN operator.
[0075] HAVING_IN group filtering is only used in conjunction with GROUP HAVING filtering, that is, after the aggregation query, use nested query to further filter the group results.
[0076] Step S1063: construct a first prompt word template based on the preset JSON structure of the SQL intermediate semantic representation, the named entities of the user question, and the query library table;
[0077] It should be noted that the entire first prompt word template contains four parts: translation function role definition (that is, clearly telling the generative large language model what to do), structured basic knowledge of the financial futures industry (mainly involving the names and codes of financial futures sectors, varieties, contracts, and other types of entities), query library table field information (mainly inputting query library table field information and other information at the user query understanding level mentioned above into the large model), the preset JSON structure of the SQL intermediate semantic representation, and translation examples from user questions to the SQL intermediate semantic representation.
[0078] Step S1064: Input the first prompt word template into the trained generative large language model, and convert the user question into an SQL intermediate semantic representation through the generative large language model.
[0079] It should be noted that the production-oriented large language model used in step S1064 is the ChatGLM model. Specifically, a ChatGLM-6B model with 6.2 billion parameters is fine-tuned and trained to accurately convert user questions into SQL intermediate semantics. First, a training dataset of {questions, SQL intermediate semantic representation} is prepared. The questions generally cover field queries on major database tables, and the question forms need to include various user query types, such as single-table field queries, join queries, and subqueries. Furthermore, to train the large model's ability to provide negative answers, a batch of negative answers is also prepared. Specifically, for a given database table, questions are asked that are irrelevant to the query and negative answers such as "unable to answer" are prepared. This enables the model to deeply understand the logical relationship between questions, database table fields, and SQL intermediate semantics. Next, parameters for fine-tuning training are set, such as the learning rate, batch size, and number of training rounds. Fine-tuning techniques such as LoRA are used for fine-tuning training. Finally, a checkpoint model is saved every 1,000 iterations, ensuring the model has high-quality translation and conversion capabilities. Furthermore, since ChatGLM is a generative model, the generated text may contain some other non-SQL semantic strings. Therefore, the output text needs to be extracted to obtain a higher-quality JSON text string with SQL intermediate semantics.
[0080] Step S108 : Based on the SQL intermediate semantic representation and the user question, the SQL intermediate semantic representation is converted into an executable SQL statement through a generative large language model.
[0081] Step S108 specifically includes the following steps:
[0082] Step S1081: construct a second prompt word template based on the SQL intermediate semantic representation and the user question;
[0083] It should be noted that the conversion of executable SQL statements in step S108 and the conversion of SQL intermediate semantic representations in the above step S106 both use the ChatGLM model and are related to library table queries in the financial futures industry. The only difference is the prompt word commands. Therefore, a second prompt word template suitable for the conversion of executable SQL statements is constructed in step S1081.
[0084] Step S1082: Input the second prompt word template into the trained generative large language model, and convert the SQL intermediate semantic representation into an executable SQL statement through the generative large language model.
[0085] It should be noted that the conversion of executable SQL statements in step S1082 also requires fine-tuning the ChatGLM model. Training data is prepared in the form of <question, SQL intermediate semantics, executable SQL> triples. To improve the model's conversion capabilities, we try to cover as many query tables as possible for financial futures as possible, and include representative training examples for complex nested subqueries such as WHERE_IN, WHERE_EQ, FROM_SQL, and HAVING_IN. Furthermore, we supplement the model with some non-convertible training examples to teach it to reject conversions. Fine-tuning techniques such as LoRA are also used for fine-tuning training, and the model is finally saved as a checkpoint at a frequency of every 1,000 iterations.
[0086] Through steps S102 to S108 in the embodiment of the present application, a two-stage SQL statement generation is implemented. First, the user question is converted into an SQL intermediate semantic representation based on a preset JSON structure, and then converted into an executable SQL statement. This can effectively structure the information in the user question, thereby ultimately improving the accuracy and business adaptability of NL2SQL, and solving the problem of how to perform high-quality NL2SQL conversion.
[0087] It should be noted that the steps shown in the above process or the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0088] In some embodiments, after converting the SQL intermediate semantic representation into an executable SQL statement using a generative large language model based on the SQL intermediate semantic representation and the user question in step S108, the method further includes:
[0089] The first proxy role and the second proxy role are created by the automatic program generator AutoGen, wherein the first proxy role is used to perform error checking and error information output on the executable SQL statement, and the second proxy role is used to correct errors on the executable SQL statement.
[0090] It should be noted that since the executable SQL statement generated in step S108 may contain errors in the conversion of library table or field names, or illegal SQL syntax, etc., the generated SQL statement needs to be checked and corrected according to the library table and field names. A multi-role SQL check and error correction workflow based on AutoGen (The Automated Program Generator) is adopted here. Autogen provides customizable and conversational agents, making full use of the capabilities of large models (such as GPT-4, GLM, etc.), and can realize automated chat between multiple Agent agents (Agent can be regarded as a robot that can use prompt words to enable the LLM large language model to complete specific tasks. It is based on the specific capability output of the LLM large language model, so it becomes an agent).
[0091] Overall, SQL checking and error correction can be viewed as two conversational robots based on a large language model automatically evaluating the initial executable SQL statement. Based on the question, database tables, and intermediate semantics, they continuously adjust and refine the final executable SQL statement to meet SQL syntax rules. Two large language model agent roles can be defined: an error checking agent responsible for checking the SQL for invalid content, and a rewriting agent responsible for continuously adjusting the final executable SQL statement based on the error checking results. The error checking agent continuously determines whether the latest SQL statement contains errors based on information such as the database table name, database field, and SQL version. If errors are present, the SQL rewriting robot is invoked to rewrite the SQL statement and recheck it. If there are no SQL syntax errors, the SQL rewriting agent is not invoked and the final SQL statement is directly returned.
[0092] This application embodiment provides a system for converting natural language into SQL statements. Figure 2 This is a structural block diagram of the natural language to SQL statement system according to an embodiment of the present application. Figure 2 As shown, the system includes an acquisition module, a retrieval module and a conversion module;
[0093] An acquisition module is used to acquire user questions and perform entity recognition to obtain named entities of the user questions, wherein the user questions are natural language questions involving a preset field;
[0094] A retrieval module is used to determine the query database table of the user's question from the business database table of the preset field;
[0095] A conversion module is used to convert user questions into SQL intermediate semantic representations based on the preset JSON structure of the SQL intermediate semantic representation and the named entities and query database tables of the user questions through a generative large language model;
[0096] The conversion module is used to convert the SQL intermediate semantic representation into executable SQL statements through a generative large language model based on the SQL intermediate semantic representation and user questions.
[0097] Through the acquisition module, retrieval module, and conversion module in the embodiments of the present application, a two-stage SQL statement generation is implemented. First, the user question is converted into an SQL intermediate semantic representation based on a preset JSON structure, and then converted into an executable SQL statement. This can effectively structure the information in the user question, thereby ultimately improving the accuracy and business adaptability of NL2SQL and solving the problem of how to perform high-quality NL2SQL conversion.
[0098] It should be noted that the above modules can be functional modules or program modules, and can be implemented through software or hardware. For modules implemented through hardware, the above modules can be located in the same processor; or the above modules can be located in different processors in any combination.
[0099] This embodiment further provides an electronic device, including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to run the computer program to execute the steps in any one of the above method embodiments.
[0100] Optionally, the electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor, and the input / output device is connected to the processor.
[0101] It should be noted that the specific examples in this embodiment can refer to the examples described in the above embodiments and optional implementation modes, and this embodiment will not be repeated here.
[0102] In addition, in conjunction with the natural language to SQL statement method in the above embodiments, embodiments of the present application may provide a storage medium for implementation. The storage medium stores a computer program; when the computer program is executed by a processor, it implements any of the natural language to SQL statement methods in the above embodiments.
[0103] In one embodiment, a computer device is provided, which may be a terminal. The computer device includes a processor, memory, a network interface, a display screen, and an input device connected via a system bus. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and computer program in the non-volatile storage medium. The network interface of the computer device is used to communicate with an external terminal via a network connection. When the computer program is executed by the processor, a method for converting natural language into SQL statements is implemented. The display screen of the computer device may be a liquid crystal display or an electronic ink display screen, and the input device of the computer device may be a touch layer covering the display screen, or a key, trackball, or touchpad provided on the computer device housing, or an external keyboard, touchpad, or mouse.
[0104] In one embodiment, Figure 3 is a schematic diagram of the internal structure of an electronic device according to an embodiment of the present application, such as Figure 3 As shown, an electronic device is provided, which may be a server, and its internal structure diagram may be as shown in FIG. Figure 3 As shown. This electronic device includes a processor, a network interface, an internal memory, and a non-volatile memory connected via an internal bus, wherein the non-volatile memory stores an operating system, a computer program, and a database. The processor is used to provide computing and control capabilities, the network interface is used to communicate with external terminals via a network connection, the internal memory is used to provide an environment for the operation of the operating system and the computer program, and when the computer program is executed by the processor, a method for converting natural language to SQL statements is implemented. The database is used to store data.
[0105] Those skilled in the art will understand that Figure 3 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the electronic device to which the solution of the present application is applied. The specific electronic device may include more or fewer components than shown in the figure, or combine certain components, or have a different component arrangement.
[0106] Those skilled in the art will understand that all or part of the processes in the above-mentioned embodiments can be implemented by instructing the relevant hardware through a computer program. The computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application may include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in many forms such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), Synchronous Link DRAM (SLDRAM), Rambus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.
[0107] Those skilled in the art should understand that the various technical features of the above-described embodiments can be combined arbitrarily. In order to make the description concise, not all possible combinations of the various technical features in the above-described embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0108] The above-described embodiments merely represent several implementation methods of the present application. While the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the present invention. It should be noted that a person skilled in the art could make various modifications and improvements without departing from the spirit of the present application, all of which fall within the scope of protection of the present application. Therefore, the scope of protection of the present patent application shall be determined by the appended claims.
Claims
1. A method for converting natural language into SQL statements, characterized in that: The method comprises: Acquire a user question and perform entity recognition to obtain a named entity of the user question, wherein the user question is a natural language question involving a preset field; Determine a query database table for the user question from the business database table of the preset field; SQL intermediate semantic representation includes basic SQL intermediate semantic representation and complex SQL intermediate semantic representation; Define a preset JSON structure for the basic SQL intermediate semantic representation, where the preset JSON structure contains tables, fields, conditions, and limits. Tables represent the names of the tables to be queried related to the user's query, fields represent the fields to be queried related to the user's query, conditions represent the filtering conditions involved in the user's query, and limits represent the query result limits of the user's query. Define a preset JSON structure for complex SQL intermediate semantic representations, where the preset JSON structure contains sqls and rels. The sqls represents all basic SQL intermediate semantic representations involved in the user question, and the rels represents the relationship between the SQL intermediate semantic representations in sqls. Constructing a first prompt word template based on a preset JSON structure represented by SQL intermediate semantics, the named entities of the user question, and the query library table; Inputting the first prompt word template into a trained generative large language model, and converting the user question into an SQL intermediate semantic representation through the generative large language model; Constructing a second prompt word template based on the SQL intermediate semantic representation and the user question; Inputting the second prompt word template into a trained generative large language model, and converting the SQL intermediate semantic representation into an executable SQL statement through the generative large language model; Creating a first proxy role and a second proxy role through an automatic program generator AutoGen, wherein the first proxy role is used to perform error checking and error information output on the executable SQL statement, and the second proxy role is used to correct errors on the executable SQL statement; The first proxy role is used to determine whether the executable SQL statement contains error information. If error information exists, the second proxy role is called to rewrite the executable SQL statement until the executable SQL statement contains no error information.
2. The method according to claim 1, characterized in that Determining the query database table of the user question from the business database table of the preset field includes: The query library table of the user question is determined from the business library table of the preset field by using the Faiss vectorized search tool.
3. The method according to claim 2, characterized in that The query database table of the user question is determined from the business database table of the preset domain by using the Faiss vectorized search tool, including: Obtain the business database table of the preset domain, and convert the information contained in the business database table based on the JSON string to obtain a database table JSON description document; Vectorize the library table JSON description document and the user question to obtain a Faiss vector; The similarity between corresponding Faiss vectors is calculated by a Faiss vectorization search tool, and a query library table of the user question is determined from the business library table of the preset field.
4. The method according to any one of claims 1 to 3, characterized in that The preset field is the financial futures field.
5. A system for converting natural language to SQL statements, characterized in that: The system is used to perform the method according to any one of claims 1 to 4 above, and the system includes an acquisition module, a retrieval module and a conversion module; The acquisition module is configured to acquire a user question and perform entity recognition to obtain a named entity of the user question, wherein the user question is a natural language question involving a preset field; The retrieval module is used to determine the query database table of the user question from the business database table of the preset field; The conversion module is configured to convert the user question into an SQL intermediate semantic representation based on the preset JSON structure of the SQL intermediate semantic representation and the named entities and query library tables of the user question using a generative large language model; The conversion module is used to convert the SQL intermediate semantic representation into an executable SQL statement through the generative large language model according to the SQL intermediate semantic representation and the user question.
Citation Information
Patent Citations
Data file analysis processing method and device based on similar SQL (Structured Query Language) and electronic equipment
CN113190573A
SQL (Structured Query Language) statement generation method and system, medium and equipment
CN117251469A
Relationship perception production data query method and device, equipment, medium and product
CN118349569A