Method and device for converting natural language into SQL (Structured Query Language) based on large language model

Through multiple iteration and consistency check mechanisms, multiple large language models are used to form collaborative complementarity, which solves the problem of randomness and uncertainty of large language models in natural language to SQL, and improves the accuracy and user experience of SQL generation .

CN119248813APending Publication Date: 2025-01-03THE PEOPLES BANK OF CHINA NAT CLEARING CENT
View PDF 0 Cites 3 Cited by

Patent Information

Application Number
CN202411271729.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-11
Publication Date
2025-01-03

AI Technical Summary

Technical Problem

There is randomness and uncertainty in the process of natural language conversion to SQL, resulting in low efficiency and low accuracy in generating SQL, and uncertainty in entity recognition and mapping, which affects user experience and system performance.

Method used

Using multiple iteration mechanisms, input data into multiple large language models to generate query SQL statements and test data, and conduct consistency checks through the third largest language model. If the number of inconsistencies is less than the preset threshold, the feedback will be errored and iterated repeatedly. , until the consistency check passes, form a coordinated complement to improve the accuracy of SQL statements.

Benefits of technology

It effectively reduces the randomness of the output of large language models, improves the accuracy and user experience of natural language to SQL, and greatly improves the accuracy of generating SQL through consistency checking and iteration mechanisms.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119248813A_ABST
    Figure CN119248813A_ABST
Patent Text Reader

Abstract

The invention discloses a method and a device for converting a natural language into an SQL (Structured Query Language) based on a large language model, and relates to the technical field of artificial intelligence. Inputting the input data into a first large language model to generate a query SQL (Structured Query Language) statement; inputting the input data into a second large language model to generate a plurality of pieces of test data and a correlation identifier corresponding to each piece of test data; inputting the query SQL statement, the test data and the correlation identifier corresponding to the test data into a test database, and performing consistency check through a third language model; and if the consistency check is not passed and the inconsistency frequency is smaller than a preset threshold value, determining the large language model with an error and feeding back an error result to the large language model until the consistency check is passed or the inconsistency frequency reaches the preset threshold value. According to the method, the randomness of large language model output is reduced, the accuracy of converting the natural language into the SQL is improved, and the user experience is greatly improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of artificial intelligence technology and can also be used in the financial field, and in particular to a method and device for converting natural language into SQL based on a large language model. Background Art

[0002] This section is intended to provide a background or context to the embodiments of the invention recited in the claims. No admission is made that the description herein is prior art by inclusion in this section.

[0003] As an important branch of artificial intelligence technology, Natural Language Processing (NLP) plays an increasingly important role in many scenarios. In database query scenarios, the use of Natural Language To Structured Query Language (NL2SQL) technology can greatly improve the efficiency of database queries.

[0004] Converting natural language to SQL means converting human language queries into database query language (SQL). For example, for a query "Please help me find out how many users there are?", the technical goal of converting natural language to SQL is to convert the query into an SQL query, such as "SELECT COUNT(*) FROM user". The user enters the relevant information of the question and selects the corresponding database table structure information, and enters the above question information and database table structure information into the large language model (LLM). The large language model generates an SQL statement according to the user's query requirements, then calls the corresponding database, executes the SQL statement, obtains the corresponding result, and finally returns the result to the user for presentation.

[0005] In recent years, the rise of large language models (LLMs) has brought new opportunities for NL2SQL research. Through deep learning and a large amount of corpus training, LLMs have powerful text generation and understanding capabilities. In NL2SQL applications, LLMs can receive questions described in natural language and automatically generate corresponding SQL query statements, thereby achieving accurate retrieval of data in the database.

[0006] However, in practical applications, when LLM processes database tables and generates SQL, the LLM-based NL2SQL converter faces a series of technical challenges. First, the efficiency of query generation affects the user experience and the overall performance of the system. Second, the uncertainty of entity recognition and mapping also makes it difficult for the model to accurately map entities in natural language to the correct database fields. In addition, the output of LLM is unstable and random. Since there is only one generation in the process of generating SQL, the output randomness will have a great impact on the accuracy of generating SQL. Summary of the invention

[0007] The embodiment of the present invention provides a method for converting natural language to SQL based on a large language model, so as to reduce the randomness of the output of the large language model, improve the accuracy of converting natural language to SQL, and improve the user experience.

[0008] In order to solve the above technical problems, this application provides the following technical solutions:

[0009] In a first aspect, the present application provides a method for converting natural language to SQL based on a large language model, comprising:

[0010] Perform the following iterative operations:

[0011] Obtain input data; wherein the input data includes: a natural language question, a database table, and an input database corresponding to the database table;

[0012] Inputting the input data into a first language model to generate a query SQL statement;

[0013] Inputting the input data into the second largest language model to generate a plurality of test data and a correlation identifier corresponding to each of the test data; wherein the correlation identifier includes correlation and irrelevance;

[0014] Inputting the query SQL statement, the test data and the correlation identifier corresponding to the test data into a test database and performing consistency check through a third language model;

[0015] If the consistency check fails and the number of inconsistencies is less than a preset threshold, the large language model in which the error occurs is determined and the error result is fed back to the large language model until the consistency check passes or the number of inconsistencies reaches the preset threshold.

[0016] Furthermore, the step of inputting the query SQL statement, the test data and the correlation identifier corresponding to the test data into the test database and performing a consistency check through the third language model includes:

[0017] Clear the data in the test database;

[0018] Inputting the test data and the correlation identifier corresponding to the test data into the test database;

[0019] Run the query SQL statement on the test database to obtain the query result;

[0020] The test data, the correlation identifier corresponding to the test data, and the query result are input into the third language model for consistency check.

[0021] Furthermore, the step of inputting the test data, the correlation identifier corresponding to the test data, and the query result into the third language model for consistency check includes:

[0022] Checking whether the query result is consistent with the test data identified as relevant by the third language model;

[0023] Before determining the large language model having an error and feeding back the error result to the large language model, the method further includes:

[0024] Check whether the number of inconsistencies is less than the preset threshold.

[0025] Further, the determining of the large language model in which an error occurs and feeding back the error result to the large language model includes:

[0026] Determining whether there are generation errors in the first large language model and the second large language model according to the input data, the query SQL statement, the test data and the correlation identifier corresponding to the test data, and the query result;

[0027] If an error occurs in the first largest language model or the second largest language model, the error result is sent to the large language model where the error occurs;

[0028] If it cannot be determined that the first largest language model or the second largest language model has an error, the error result is assigned to the first largest language model and the second largest language model.

[0029] Furthermore, after acquiring the input data, the method further includes: generating a prompt of the large language model according to the input data.

[0030] In a second aspect, an embodiment of the present invention further provides a device for converting natural language to SQL based on a large language model, so as to reduce the randomness of the output of the large language model, improve the accuracy of converting natural language to SQL, and improve the user experience. The device includes:

[0031] A data collection module, used for the input data; wherein the input data includes: a natural language question, a database table, and an input database corresponding to the database table;

[0032] A first data generation module, used for inputting the input data into a first language model to generate a query SQL statement;

[0033] A second data generating module, configured to input the input data into a second large language model to generate a plurality of test data and a correlation identifier corresponding to each of the test data;

[0034] A data verification module, used for inputting the query SQL statement, the test data and the correlation identifier corresponding to the test data into a test database and performing consistency check through a third language model;

[0035] The error identification module is used to determine the large language model in which the error occurs and feed back the error result to the large language model when the consistency check fails and the number of inconsistencies is less than a preset threshold.

[0036] Furthermore, the data verification module includes:

[0037] A data clearing unit, used to clear the data in the test database;

[0038] An input unit, used for inputting the test data and the correlation identifier corresponding to the test data into the test database;

[0039] A query unit, used to run the query SQL statement on the test database to obtain a query result;

[0040] The second data verification unit is used to input the test data, the correlation identifier corresponding to the test data, and the query result into the third language model for consistency check.

[0041] Furthermore, the second data verification unit is specifically used to check whether the query result is consistent with the test data identified as relevant by the third language model;

[0042] The device also includes: a detection module: used to detect whether the number of inconsistencies is less than the preset threshold.

[0043] Furthermore, the error identification module includes:

[0044] a judging unit, configured to judge whether there are generation errors in the first large language model and the second large language model according to the input data, the query SQL statement, the test data and the correlation identifier corresponding to the test data, and the query result;

[0045] A first feedback unit is used for, if an error occurs in the first large language model or the second large language model, sending an error result to the large language model where the error occurs;

[0046] The second feedback unit is used to provide an error result to the first large language model and the second large language model if it cannot be determined that the first large language model or the second large language model has an error.

[0047] Furthermore, the device also includes: an encapsulation module, used to generate a prompt of the large language model according to the input data.

[0048] An embodiment of the present invention further provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the above-mentioned method for converting natural language to SQL based on a large language model when executing the computer program.

[0049] An embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method for converting natural language to SQL based on a large language model is implemented.

[0050] An embodiment of the present invention further provides a computer program product, which includes a computer program. When the computer program is executed by a processor, the method for converting natural language into SQL based on a large language model is implemented.

[0051] The embodiment of the present invention provides a method and device for converting natural language into SQL based on a large language model.

[0052] In the embodiment of the present invention, input data is respectively input into the first largest language model and the second largest language model to obtain query SQL statements and test data with correlation identifiers, thereby realizing natural language to SQL conversion; due to the randomness of the large language model, the obtained query SQL statements and the test data with correlation identifiers are input into the test database for consistency check, and by introducing the second largest language model, a synergistic complement is formed with the original first largest language model, and the generated query SQL statements are checked for consistency, thereby improving the accuracy of the generated query SQL statements; if the consistency check fails and the number of inconsistencies is less than a preset threshold, the large language model with an error is determined and the error result is fed back to the large language model, and multiple rounds of iterations are performed until the consistency check is passed or the number of inconsistencies reaches the preset threshold, and the accuracy of the generated query SQL statements is effectively improved through the consistency check and iteration mechanism, greatly improving the user experience. BRIEF DESCRIPTION OF THE DRAWINGS

[0053] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the prior art descriptions. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work. In the drawings:

[0054] Figure 1 A flowchart of a method for converting natural language to SQL based on a large language model in an embodiment of the present invention;

[0055] Figure 2 A flowchart of another method for converting natural language to SQL based on a large language model in an embodiment of the present invention;

[0056] Figure 3 A flowchart of another method for converting natural language to SQL based on a large language model in an embodiment of the present invention;

[0057] Figure 4 A schematic diagram of the structure of a device for converting natural language to SQL based on a large language model in an embodiment of the present invention;

[0058] Figure 5 It is a schematic diagram of another device for converting natural language into SQL based on a large language model in an embodiment of the present invention;

[0059] Figure 6 It is a schematic diagram of another device for converting natural language into SQL based on a large language model in an embodiment of the present invention;

[0060] Figure 7 A schematic diagram of a physical structure of a computer device in an embodiment of the present invention;

[0061] Figure 8 The present invention is a flowchart of another method for converting natural language to SQL based on a large language model in an embodiment of the present invention. DETAILED DESCRIPTION

[0062] To make the purpose, technical solution and advantages of the embodiments of the present invention more clear, the embodiments of the present invention are further described in detail below in conjunction with the accompanying drawings. Here, the exemplary embodiments of the present invention and their descriptions are used to explain the present invention, but are not intended to limit the present invention.

[0063] The inventors found that the current large language model cannot accurately generate SQL. For example, when a user inputs a natural language question into the large language model, due to the randomness of the large language model, the results of the two SQL generation are different, making the accuracy of the large language model generating SQL not high. Based on this, the inventors proposed a method for converting natural language to SQL based on a large language model.

[0064] Figure 1 FIG. 1 is a flow chart of a method for converting natural language to SQL based on a large language model in an embodiment of the present invention; Figure 1 As shown, the method includes:

[0065] Step 100: Obtain input data; wherein the input data includes: a natural language question, a database table, and an input database corresponding to the database table;

[0066] Step 200: Input the input data into the first language model to generate a query SQL statement;

[0067] Step 300: Input the input data into the second largest language model to generate multiple test data and a correlation mark corresponding to each test data; wherein the correlation mark includes correlation and irrelevance;

[0068] Step 400: input the query SQL statement, test data and the correlation identifier corresponding to the test data into the test database and perform consistency check through the third language model;

[0069] Step 500: If the consistency check fails and the number of inconsistencies is less than a preset threshold, determine the large language model in which the error occurs and feed back the error result to the large language model until the consistency check passes or the number of inconsistencies reaches the preset threshold.

[0070] from Figure 1It can be seen from the process shown that in the embodiment of the present invention, the input data is respectively input into the first language model and the second language model to obtain the query SQL statement and the test data with the correlation mark, thereby realizing the conversion of natural language into SQL; due to the randomness of the large language model, the obtained query SQL statement and the test data with the correlation mark are input into the test database, and the consistency check is performed through the third language model. By introducing the second language model, a synergistic complement is formed with the original first language model, and the generated query SQL statement is checked for consistency through the third language model, thereby improving the accuracy of the generated query SQL statement; if the consistency check fails and the number of inconsistencies is less than the preset threshold, the large language model with an error is determined and the error result is fed back to the large language model, and multiple rounds of iterations are performed until the consistency check is passed or the number of inconsistencies reaches the preset threshold. The accuracy of the generated query SQL statement is effectively improved through the consistency check and iteration mechanism, and the user experience is greatly improved.

[0071] like Figure 8 As shown, each step is explained in detail below.

[0072] Step 100: Obtain input data; wherein the input data includes: a natural language question, a database table, and an input database corresponding to the database table.

[0073] First, obtain natural language questions, that is, obtain information input by users. Users can ask questions to the input database, and users input query statements in some way; for example, keyboard input, voice input, etc. Secondly, the present invention requires a database (for example, a relational database can be used) as an input database, such as a MySQL database or other database as an input database; or use DDL (Data Definition Language) to create a database, data table and column. Generally, the input database contains one or more database tables (table), and each table contains multiple columns (column).

[0074] For example, suppose there is a relational database as the input database, which contains multiple database tables. For example, the "student" database table stores student-related information, such as student ID, name, birthday, class ID class_id and other fields.

[0075] In one embodiment, after acquiring the input data, the method further includes: using prompts to generate a large language model based on the input data.

[0076] By reading the database table structure information, organizing the natural language questions and the SQL table creation statements related to the natural language questions, and encapsulating the above data into prompts, the prompts of the large language model are generated, so that the large language model can understand which database tables are used to generate SQL for the next question. Among them, the prompt is a short text fragment used to guide the large language model to generate a specific type of output. The prompt can be in the form of questions, instructions, examples, etc., to provide context and direction for the large language model.

[0077] This embodiment encapsulates Prompt, and can use a few-shot method or other methods to provide a small number of labeled samples (examples) to help the large language model understand the task. The example usually includes input and expected output so that the large language model can more accurately grasp the nature and requirements of the task. By encapsulating Prompt, Prompt can help the large language model better understand the target task, clarify the intention of the natural language question and the required answer type and standard, and respond accordingly.

[0078] The prompt encapsulation example is as follows:

[0079] You are an expert in SQL generation. Please generate corresponding SQL query statements based on user questions and database table definitions.

[0080] The generated table names and table fields are from the following table:

[0081] Table name: Student (student table)

[0082] Table fields: id (student ID) / name (student name) / birthday (birthday) / class_id (class ID)

[0083] User question: How many students have birthdays after January 1, 2010?

[0084] Among them, the table name and table fields are the database table definitions.

[0085] In an embodiment of the present invention, the prompts cleverly designed by the Prompt project can guide the large language model to generate more accurate, targeted answers and creative outputs, thereby improving the accuracy of SQL generated by the large language model. In addition, Prompt can also improve the interpretability and accessibility of the large language model. Prompt provides a "hint" or "guidance" to the AI ​​model (i.e., the large language model) to help it better understand the target task and complete the task. At the same time, the process of encapsulating Prompt ensures the rigor, formality and professionalism of the text, and meets the language standards.

[0086] Step 200: Input the input data into the first language model to generate a query SQL statement.

[0087] The input data is encapsulated in Prompt and then input into the first language model to generate a query SQL statement; the first language model is the NL2SQL large language model; the NL2SQL large language model can be an open source large model; a closed source model, such as the GPT model, can also be selected as a deep learning model; the GPT model has a high accuracy rate in the field of natural language processing and can accurately classify, annotate and parse text.

[0088] The following is an example of NL2SQL large language model input:

[0089] You are an expert in SQL generation. Please generate corresponding SQL query statements based on user questions and database table definitions.

[0090] The generated table names and table fields are from the following table:

[0091] Table name: Student (student table)

[0092] Table fields: id (student ID) / name (student name) / birthday (birthday) / class_id (class ID)

[0093] User question: How many students have birthdays after January 1, 2010?

[0094] The following is an example of the NL2SQL large language model output:

[0095] Output query SQL statement:

[0096] select count(*)from student where birthday>='2010-01-01'

[0097] Step 300: Input the input data into the second largest language model to generate multiple test data and a correlation identifier corresponding to each test data; wherein the correlation identifier includes related and unrelated.

[0098] The input data is Prompt-encapsulated and then input into the second largest language model to generate multiple test data and correlation identifiers corresponding to each test data; wherein the test data is an insert SQL statement with a correlation identifier; the second largest language model is the NL2Data large language model, which can be an open source large model or a closed source model, such as the GPT model as a deep learning model. The GPT model has a high accuracy in the field of natural language processing and can accurately classify, annotate and parse texts; the first largest language model and the second largest language model can use the same type of open source or closed source model, or different types of open source or closed source models, and this embodiment does not make specific restrictions.

[0099] The NL2Data large language model input example is as follows:

[0100] You are a senior data testing expert. Based on the user's question and database table definition, you generate SQL statements to insert test data. To fully test, you need to generate multiple test data. Please indicate whether each data is "related" or "irrelevant" to the problem.

[0101] Table name: Student (student table)

[0102] Table fields: id (student ID) / name (student name) / birthday (birthday) / class_id (class ID)

[0103] User question: How many students have birthdays after January 1, 2010?

[0104] The following is an example of the NL2SQL large language model output:

[0105] Output of insert SQL statement:

[0106] Data 1:

[0107] insert student(id,name,birthday)values(18182622,A,'2010-01-01')

[0108] Is it relevant: Not relevant

[0109] Data 2:

[0110] insert student(id,name,birthday)values(18182117,B,'2010-01-02')

[0111] Is it relevant: relevant

[0112] Step 400: Input the query SQL statement, test data and the correlation identifier corresponding to the test data into the test database and perform consistency check through the third language model.

[0113] The query SQL statement generated by the NL2SQL large language model, the test data generated by the NL2Data large language model, and the correlation identifier corresponding to the test data are input into the test database, and a consistency check is performed through the third language model. Among them, the third language model can be an open source large model, or a closed source model, such as a GPT model, can be selected as a deep learning model; the third language model can be the same model as the first language model and the second language model, or it can be a different model. In this embodiment, no specific limitation is made.

[0114] like Figure 2 As shown, step 400 also includes steps 401 to 404.

[0115] Step 401: Clear the data in the test database.

[0116] Step 402: Input the test data and the correlation identifier corresponding to the test data into the test database.

[0117] Generally, a test database can be created through DDL or other methods. When a test database is initialized, it is empty.

[0118] If the current test database is empty, the test data generated by the NL2Data large language model and the correlation identifier corresponding to the test data are inserted into the test database. If the current test database is not empty, it is determined whether the NL2Data large language model in step 300 regenerates the test data and the correlation identifier corresponding to the test data.

[0119] During the iteration process, if the NL2Data large language model regenerates the test data and the correlation identifier corresponding to the test data in step 300, the data in the current test database needs to be cleared first, and then the newly generated test data and the correlation identifier corresponding to the test data are inserted into the test database. Otherwise, there is no need to clear the data in the current test database.

[0120] Step 403: Run the query SQL statement in the test database to obtain the query result.

[0121] Specifically, after the test data and the correlation identifiers corresponding to the test data are input into the test database, the query SQL statements generated by the NL2SQL large language model are run in the test database to obtain the query results, wherein the query results are multiple insert SQL statements with correlation identifiers.

[0122] Step 404: input the test data, the correlation identifier corresponding to the test data, and the query result into the third language model for consistency check.

[0123] Step 404 also includes: checking whether the query result is consistent with the test data marked as relevant by the third language model.

[0124] Specifically, the third language model compares the test data marked as relevant with the query results one by one and counts the number of inconsistencies.

[0125] If the query result is consistent with the test data marked as relevant by the correlation, it means that the query SQL statement generated by the NL2SQL large language model is correct, then the query SQL statement is returned to the user, and the above process is controlled to end.

[0126] If the query result is inconsistent with the test data marked as relevant, it means that the generated query SQL statement and / or the generated test data and the correlation mark corresponding to the test data may be wrong. It is then further determined whether the query SQL statement generated by the NL2SQL large language model has an error; or the test data generated by the NL2Data large language model and the correlation mark corresponding to the test data have an error; or the output data of both large language models have errors; therefore, it is necessary to use the model to determine whether there are generation errors in the NL2SQL large language model and the NL2Data large language model.

[0127] Before determining that an erroneous large language model occurs and feeding back an erroneous result to the large language model, the method further includes: detecting whether the number of inconsistencies is less than a preset threshold.

[0128] Specifically, the counted number of inconsistencies is compared with a preset threshold;

[0129] If the consistency check fails and the number of inconsistencies is greater than or equal to the preset threshold, a prompt message indicating that the query SQL statement generation failed is returned to the user, and the above process is controlled to end.

[0130] Step 500: If the consistency check fails and the number of inconsistencies is less than a preset threshold, determine the large language model in which the error occurs and feed back the error result to the large language model until the consistency check passes or the number of inconsistencies reaches the preset threshold.

[0131] like Figure 3 As shown, determining a large language model having an error and feeding back an error result to the large language model includes steps 501 to 503;

[0132] Step 501: determine whether there are generation errors in the first language model and the second language model according to input data, query SQL statements, test data and correlation identifiers corresponding to the test data, and query results.

[0133] Specifically, the input data, the query SQL statement, the test data, the correlation identifier corresponding to the test data, and the query result are input into the fourth language model, and the fourth language model determines whether there are generation errors in the NL2SQL large language model and the NL2Data large language model based on the above data. Among them, the fourth language model can be an open source large model, or a closed source model, such as a GPT model, can be selected as a deep learning model; the fourth language model can be the same model as the first language model, the second language model, and the third language model, or it can be a different model. In this embodiment, no specific limitation is made.

[0134] Step 502: If an error occurs in the first language model or the second language model, the error result is sent to the large language model where the error occurs.

[0135] Specifically, if the fourth largest language model determines that there is a generation error in the NL2SQL large language model, an instruction to regenerate the query SQL statement is sent to the NL2SQL large language model, and the large language model is controlled to execute step 200 to regenerate the query SQL statement, and steps 100, 400 to 503 are repeated until the consistency check passes, and the query SQL statement is returned to the user, and the above process is controlled to end; or, until the number of inconsistencies reaches a preset threshold, a prompt message indicating that the query SQL statement generation failed is returned to the user, and the above process is controlled to end.

[0136] If the fourth largest language model determines that there is a generation error in the NL2Data large language model, an instruction to regenerate the test data and the correlation identification corresponding to the test data is sent to the NL2Data large language model, and the large language model is controlled to execute step 300 to regenerate the test data and the correlation identification corresponding to the test data, and steps 300, 400 to 503 are repeatedly executed until the consistency check passes, and the query SQL statement is returned to the user, and the above process is controlled to end; or, until the number of inconsistencies reaches a preset threshold, a prompt message indicating that the query SQL statement generation failed is returned to the user, and the above process is controlled to end.

[0137] The error result is an instruction to regenerate a query SQL statement or an instruction to regenerate test data and a correlation identification instruction corresponding to the test data.

[0138] Step 503: If it cannot be determined that an error occurs in the first language model or the second language model, the error result is sent to the first language model and the second language model.

[0139] If the fourth large language model cannot determine whether there are generation errors in the first or second large language model, it will send the instruction to regenerate the query SQL statement to the NL2SQL large language model, and send the instruction to regenerate the test data and the corresponding relevance identifier of the test data to the NL2Data large language model, and control the large language model to repeatedly execute steps 200 to 503 until the consistency check passes or the number of inconsistent times reaches the preset threshold.

[0140] The input example of the fourth large language model is as follows:

[0141] You are a senior review expert. Based on the user's question and the database table definition, you have generated an SQL query statement and a test data SQL statement. Now they are inconsistent. Please judge according to the actual situation whether it is an error in the SQL query, an error in the test data, or it is impossible to judge.

[0142] Table name: Student (Student Table)

[0143] Table fields: id (student ID) / name (student name) / birthday (birthday) / class_id (class ID)

[0144] User's question: How many students have birthdays after January 1, 2010?

[0145] SQL query statement: select count(*)from student where birthday>=‘2010-01-01’

[0146] Test data: Data 1: insert student(id,name,birthday)values(18182622, A,‘2010-01-01’). Is it relevant: Not relevant

[0147] Data 2: insert student(id,name,birthday)values(18182117, B,‘2010-01-02’). Is it relevant: Relevant

[0148] The output example of the fourth large language model is as follows:

[0149] Based on the input conditions, it is impossible to make a judgment

[0150] In the embodiment of the present invention, the input data is input into the first language model and the second language model to obtain the query SQL statement and the test data with correlation identification, realizing the conversion of natural language to SQL; through the cleverly designed prompts of the Prompt project, the large language model can be guided to generate more accurate and targeted answers and creative output data, thereby improving the accuracy of the large language model in generating SQL, and also improving the interpretability and accessibility of the large language model. At the same time, the process of encapsulating the Prompt ensures the rigor, formality and professionalism of the text, and meets the language logic standards. Due to the randomness of the large language model, the obtained query SQL statement and the test data with correlation identifiers are input into the test database for consistency check. By introducing the second largest language model, it forms a synergistic complement with the original first largest language model, and the generated query SQL statement is checked for consistency through the third largest language model, thereby improving the accuracy of the generated query SQL statement; when the consistency check fails and the number of inconsistencies is less than the preset threshold, the fourth largest language model is used to determine the large language model with errors and feedback the error results to the large language model, and multiple rounds of iterations are performed until the consistency check passes or the number of inconsistencies reaches the preset threshold; the above consistency check and iteration mechanism effectively improves the accuracy of the generated query SQL statement, greatly improving the user experience.

[0151] The embodiment of the present invention also provides a device for converting natural language to SQL based on a large language model, as described in the following embodiment. Since the principle of solving the problem by the device is similar to the method for converting natural language to SQL based on a large language model, the implementation of the device can refer to the implementation of the method for converting natural language to SQL based on a large language model, and the repeated parts will not be repeated.

[0152] Figure 4 FIG. 1 is a schematic diagram of a structure of a device for converting natural language to SQL based on a large language model in an embodiment of the present invention; Figure 4 As shown, the device comprises:

[0153] The data acquisition module 10 is used to obtain input data; wherein the input data includes: natural language questions, database tables, and input databases corresponding to the database tables;

[0154] A first data generation module 20, used to input the input data into a first language model to generate a query SQL statement;

[0155] A second data generating module 30, configured to input the input data into a second large language model to generate a plurality of test data and a correlation identifier corresponding to each test data;

[0156] A data verification module 40, for inputting the query SQL statement, the test data and the correlation identifier corresponding to the test data into the test database and performing consistency check through the third language model;

[0157] The error identification module 50 is used to determine the large language model in which an error occurs and feed back the error result to the large language model when the consistency check fails and the number of inconsistencies is less than a preset threshold.

[0158] like Figure 5 As shown, the data verification module 40 includes:

[0159] A data clearing unit 41, used to clear the data in the test database;

[0160] An input unit 42, used to input the test data and the correlation identifier corresponding to the test data into a test database;

[0161] A query unit 43 is used to run a query SQL statement on the test database to obtain a query result;

[0162] The second data verification unit 44 is used to input the test data, the correlation identifier corresponding to the test data, and the query result into the third language model for consistency check.

[0163] In one embodiment, the second data verification unit 44 is specifically used to check whether the query result is consistent with the test data marked as relevant by the relevance through the third language model;

[0164] The device also includes: a detection module 60: used to detect whether the number of inconsistencies is less than a preset threshold.

[0165] like Figure 6 As shown, the error identification module 50 includes:

[0166] A judging unit 51 is used to judge whether there are generation errors in the first language model and the second language model according to the input data, the query SQL statement, the test data and the correlation identifier corresponding to the test data, and the query result;

[0167] A first feedback unit 52 is used to feedback the error result to the large language model where the error occurs if the first large language model or the second large language model has an error;

[0168] The second feedback unit 53 is used for sending the error result to the first largest language model and the second largest language model if it cannot be determined that the first largest language model or the second largest language model has an error.

[0169] In one embodiment, the device further includes: an encapsulation module 70, configured to generate a prompt of a large language model according to input data.

[0170] Specifically, before the input data is input into the first language model and the second language model, the input data needs to be packaged by the packaging module 70. Moreover, before the input data, the query SQL statement, the test data and the correlation identifier corresponding to the test data, and the query result are input into the third language model, the above data also needs to be packaged by the packaging module 70.

[0171] In the embodiment of the present invention, the input data is obtained by the data acquisition module 10, and the input data is respectively input into the first data generation module 20 and the second data generation module 30 to generate query SQL statements and test data with correlation identification, thereby realizing natural language to SQL conversion; the prompt generated by the encapsulation module 70 can guide the large language model to generate more accurate, targeted answers and creative outputs, thereby improving the accuracy of the large language model to generate SQL, improving the interpretability and accessibility of the large language model, and the process of data encapsulation by the encapsulation module 70 ensures the rigor, formality and professionalism of the text, and conforms to the standard language logic. Due to the randomness of the data generated by the large language model, the generated query SQL statement is checked for consistency by the data verification module 40, thereby improving the accuracy of the query SQL statement; when the consistency check fails and the number of inconsistencies is less than the preset threshold, the error identification module 50 determines the large language model with errors and feeds back the error results to the large language model, and performs multiple rounds of iterations, thereby improving the accuracy of the query SQL statement; the consistency check and iteration mechanism effectively improve the accuracy of the generated query SQL statement, greatly improving the user experience.

[0172] Figure 7 A schematic diagram of a computer device entity structure provided by an embodiment of the present invention, such as Figure 7 As shown, the device includes: a processor (processor) a, a memory (memory) b and a bus c.

[0173] Among them, processor a and memory b communicate with each other through bus c.

[0174] Processor a is used to call a computer program stored in memory b and executable on processor a. When processor a executes the computer program, the method for converting natural language to SQL based on a large language model is implemented.

[0175] An embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method for converting natural language to SQL based on a large language model is implemented.

[0176] An embodiment of the present invention further provides a computer program product, which includes a computer program. When the computer program is executed by a processor, the method for converting natural language into SQL based on a large language model is implemented.

[0177] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0178] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0179] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0180] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0181] The specific embodiments described above further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above description is only a specific embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.

Claims

1. A method for converting natural language to SQL based on a large language model, characterized in that: This includes performing the following iterative operations: Obtain input data; wherein the input data includes: a natural language question, a database table, and an input database corresponding to the database table; Inputting the input data into a first language model to generate a query SQL statement; Inputting the input data into the second largest language model to generate a plurality of test data and a correlation identifier corresponding to each of the test data; wherein the correlation identifier includes correlation and irrelevance; Inputting the query SQL statement, the test data and the correlation identifier corresponding to the test data into a test database and performing consistency check through a third language model; If the consistency check fails and the number of inconsistencies is less than a preset threshold, the large language model in which the error occurs is determined and the error result is fed back to the large language model until the consistency check passes or the number of inconsistencies reaches the preset threshold.

2. The method according to claim 1, characterized in that: The step of inputting the query SQL statement, the test data, and the correlation identifier corresponding to the test data into the test database and performing consistency check through the third language model includes: Clear the data in the test database; Inputting the test data and the correlation identifier corresponding to the test data into the test database; Run the query SQL statement on the test database to obtain the query result; The test data, the correlation identifier corresponding to the test data, and the query result are input into the third language model for consistency check.

3. The method according to claim 2, characterized in that The step of inputting the test data, the correlation identifier corresponding to the test data, and the query result into the third language model for consistency check includes: Checking whether the query result is consistent with the test data identified as relevant by the third language model; Before determining the large language model in which an error occurs and feeding back the error result to the large language model, the method further includes: detecting whether the number of inconsistencies is less than the preset threshold.

4. The method according to claim 3, characterized in that The determining of the large language model having an error and feeding back the error result to the large language model comprises: Determining whether there are generation errors in the first large language model and the second large language model according to the input data, the query SQL statement, the test data and the correlation identifier corresponding to the test data, and the query result; If an error occurs in the first largest language model or the second largest language model, the error result is sent to the large language model where the error occurs; If it cannot be determined that the first largest language model or the second largest language model has an error, the error result is assigned to the first largest language model and the second largest language model.

5. The method according to claim 1, characterized in that After acquiring the input data, the method further includes: generating a prompt of the large language model according to the input data.

6. A device for converting natural language to SQL based on a large language model, characterized in that: include: A data acquisition module is used to obtain input data; wherein the input data includes: natural language questions, database tables, and input databases corresponding to the database tables; A first data generation module, used for inputting the input data into a first language model to generate a query SQL statement; A second data generating module, configured to input the input data into a second large language model to generate a plurality of test data and a correlation identifier corresponding to each of the test data; A data verification module, used for inputting the query SQL statement, the test data and the correlation identifier corresponding to the test data into a test database and performing consistency check through a third language model; The error identification module is used to determine the large language model in which the error occurs and feed back the error result to the large language model when the consistency check fails and the number of inconsistencies is less than a preset threshold.

7. The device according to claim 6, characterized in that The data verification module comprises: A data clearing unit, used to clear the data in the test database; An input unit, used for inputting the test data and the correlation identifier corresponding to the test data into the test database; A query unit, used to run the query SQL statement on the test database to obtain a query result; The second data verification unit is used to input the test data, the correlation identifier corresponding to the test data, and the query result into the third language model for consistency check.

8. The device according to claim 7, characterized in that The second data verification unit is specifically configured to check, through the third language model, whether the query result is consistent with the test data identified as relevant by the relevance indicator; The device also includes: a detection module: used to detect whether the number of inconsistencies is less than the preset threshold.

9. The device according to claim 8, characterized in that The error identification module comprises: a judging unit, configured to judge whether there are generation errors in the first large language model and the second large language model according to the input data, the query SQL statement, the test data and the correlation identifier corresponding to the test data, and the query result; A first feedback unit is used for, if an error occurs in the first large language model or the second large language model, sending an error result to the large language model where the error occurs; The second feedback unit is used to provide an error result to the first large language model and the second large language model if it cannot be determined that the first large language model or the second large language model has an error.

10. The device according to claim 6, characterized in that Also includes: The encapsulation module is used to generate a prompt of the large language model according to the input data.

11. A computer device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the method according to any one of claims 1 to 5 is implemented.

12. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 5 is implemented.

13. A computer program product, characterized in that The computer program product comprises a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 5 is implemented.

Citation Information

Cited By

  • System and method for improving reflection ability of NL2SQL model based on SQL grammar check

    CN119988419A

  • Structured query language statement validation based on machine learning

    US12769726B2

  • Structured query language statement validation based on machine learning

    US20260127163A1