Method and system for converting complex problems into SQL statements based on large models

Through a large model-based method, the correlation between user questions and database tables is judged, and corresponding SQL query statements are generated. This solves the problem that the existing system is difficult to directly generate SQL statements when dealing with complex query problems, and realizes complex query scenarios. Effective processing and convenient database query experience.

CN118796880BActive Publication Date: 2025-01-21SHANDONG INSPUR SCI RES INST CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411288305.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-09-14
Publication Date
2025-01-21
Estimated Expiration
2044-09-14

AI Technical Summary

Technical Problem

When existing systems deal with complex query problems, it is difficult to directly generate corresponding SQL statements, especially when it involves complex data calculations or data conversion in tables.

Method used

Through a large model-based method, it determines the correlation between user questions and database tables, and generates and executes SQL query statements based on the structural information of the current database table and the semantics of user questions. If SQL cannot be generated directly, few-shot learning technology is used to generate SQL query statements containing new syntax parts, and the syntax part is executed through the large model for querying.

Benefits of technology

It realizes the effective conversion of complex problems into SQL statements, can handle complex query scenarios, and provides a convenient and intelligent database query experience.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118796880B_ABST
    Figure CN118796880B_ABST
Patent Text Reader

Abstract

The present invention discloses a method and system for converting complex problems into SQL statements based on a big model, which belongs to the field of database technology. The technical problem to be solved is how to convert complex problems into SQL statements. The method includes the following steps: inputting a problem and a problem statement into a big model, and judging the SQL generation intention through the big model; if the problem is not related to the database table, analyzing the user intention of the problem through the big model and giving an answer; if the problem is related to the database table and can directly generate an SQL query statement, generating an SQL query statement through the big model; if the problem is related to the database table but cannot directly generate an SQL query statement, generating an SQL query statement containing a new grammar part through the big model based on few-shot learning technology, executing the grammar part through the big model to query, merging the new column into the current database table, and executing the SQL query statement containing the new grammar part.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of database technology, and in particular to a method and system for converting complex problems into SQL statements based on a large model. Background Art

[0002] In recent years, with the rapid development of artificial intelligence technology, especially the rise of large models (such as the GPT series), unprecedented opportunities have been brought to automatic database query systems. With its powerful natural language processing and knowledge reasoning capabilities, large models provide a solid foundation for understanding user query intent and generating accurate SQL statements. By applying large models to automatic database query systems, in-depth understanding and analysis of user questions can be achieved, and SQL query statements that better meet user needs can be generated.

[0003] However, although large models have shown great potential in the field of automatic database query, current systems still face many challenges. For example, when large models deal with complex query problems, such as those involving complex calculations or data conversions on table data, they may not be able to directly generate corresponding SQL statements.

[0004] How to convert complex problems into SQL statements is a technical problem that needs to be solved. Summary of the invention

[0005] The technical task of the present invention is to address the above shortcomings and provide a method and system for converting complex problems into SQL statements based on a large model to solve the technical problem of how to convert complex problems into SQL statements.

[0006] In a first aspect, the present invention provides a method for converting complex problems into SQL statements based on a large model, comprising the following steps:

[0007] For questions raised by users, whether the question is related to the database table is used as the problem statement. The question and the problem statement are input into the big model, and SQL (Structured Query Language) is used to generate intent judgment through the big model. The SQL intent judgment is used to determine whether the question raised by the user is related to the database table.

[0008] If the result of SQL generation intent judgment is that the question is not related to the database table, the user intent analysis of the question is performed through the big model and an answer is given;

[0009] If the result of the SQL generation intention judgment is that the question is related to the database table and the SQL query statement can be directly generated based on the current database table, the SQL query statement is generated through the big model based on the semantics of the question and the structure information of the current database table, and the SQL query statement is executed based on the current database table to obtain the answer;

[0010] If the result of SQL generation intention judgment is that the problem is related to the database table, but the SQL query statement cannot be directly generated based on the current database table, based on the few-shot learning technology, the SQL query statement containing the new grammar part is generated through the big model, and based on the current database table, the grammar part is executed through the big model to query, and the query result is obtained as a new column. The new column is merged into the current database table to obtain a new database table. Based on the new database table, the SQL query statement containing the new grammar part is executed to obtain the answer;

[0011] Among them, the large model is GPT and GPT-type natural language processing models based on deep learning.

[0012] GPT: The full name of GPT is Generative Pre-Trained Transformer. It is a generative pre-training model based on transformer. It is pre-trained on a large unlabeled text dataset and can generate text similar to human natural language.

[0013] Preferably, when judging the SQL generation intention through the big model, the semantics of the question is analyzed through the big model to identify whether the semantics contains keywords or phrases related to the current database table structure or data, so as to determine whether the question can generate an SQL query statement for query.

[0014] Preferably, when generating SQL query statements through the big model based on the semantics of the question and the structural information of the current database table, the big model uses natural language processing technology to parse the question, extract the intent and key information, including the table name, column name and filter conditions to be queried, and then generates query statements that conform to SQL syntax based on the intent and key information.

[0015] Preferably, when generating an SQL query statement containing a new grammatical part through a large model based on few-shot learning technology, a set of questions and SQL query statements containing similarity calculation logic are obtained as sample data, and learning is performed based on the sample data and answers to user questions are output as problem statements. The sample data, questions raised by the user, and problem statements are input into the large model as context, and an SQL query statement containing a new grammatical part is generated through the large model.

[0016] In a second aspect, the present invention provides a system for converting complex problems into SQL statements based on a large model, which is used to convert complex problems into SQL statements through the method for converting complex problems into SQL statements based on a large model as described in any one of the first aspects, wherein the system includes an SQL generation intention judgment module, a direct answer generation module, a direct SQL generation module, an indirect SQL generation module, and an SQL execution module;

[0017] The SQL generation intention judgment module is used to perform the following: for the question raised by the user, whether the question is related to the database table is used as the question statement, the question and the question statement are input into the big model, and the SQL generation intention judgment is performed through the big model. The SQL generation intention judgment is used to judge whether the question raised by the user is related to the database table. If the result of the SQL generation intention judgment is that the question is not related to the database table, the direct answer generation module is called. If the result of the SQL generation intention judgment is that the question is related to the database table and the SQL query statement can be directly generated according to the current database table, the direct SQL generation module is called. If the result of the SQL generation intention judgment is that the question is related to the database table, but the SQL query statement cannot be directly generated according to the current database table, the indirect SQL generation module is called;

[0018] The direct answer generation module is used to perform the following: analyze the user intent of the question through the large model and give the answer;

[0019] The direct SQL generation module is used to perform the following: based on the semantics of the problem and the structural information of the current database table, generate SQL query statements through the large model, and call the SQL execution module;

[0020] The indirect SQL generation module is used to perform the following: based on the few-shot learning technology, generate an SQL query statement containing a new grammar part through the big model, and query the grammar part based on the current database table through the big model, obtain the query result as a new column, merge the new column into the current database table to obtain a new database table, use the new database table as the current database table, use the SQL query statement containing the new grammar part as the final SQL query statement, and call the SQL execution module;

[0021] The SQL execution module is used to perform the following: based on the current database table, execute SQL query statements to obtain answers;

[0022] Among them, the large model is GPT and GPT-type natural language processing models based on deep learning.

[0023] Preferably, when the SQL generation intention is judged through the big model, the SQL generation intention judgment module is used to analyze the semantics of the problem through the big model, and identify whether the semantics contains keywords or phrases related to the current database table structure or data, so as to determine whether the problem can generate an SQL query statement for query.

[0024] Preferably, when generating SQL query statements through the big model based on the semantics of the question and the structural information of the current database table, the big model uses natural language processing technology to parse the question, extract the intent and key information, including the table name, column name and filter conditions to be queried, and then generates query statements that conform to SQL syntax based on the intent and key information.

[0025] Preferably, when generating SQL query statements containing a new grammatical part through a large model based on few-shot learning technology, the indirect SQL statement generation module is used to obtain a set of questions and SQL query statements containing similarity calculation logic as sample data, learn based on the sample data, output answers to user questions as problem statements, input the sample data, questions raised by the user, and problem statements as context into the large model, and generate SQL query statements containing the new grammatical part through the large model.

[0026] The method and system for converting complex problems into SQL statements based on a large model of the present invention have the following advantages: by combining multiple steps such as SQL generation intention judgment, direct SQL generation, new syntax SQL generation, and SQL execution to obtain answers, a flexible and efficient design method for generating SQL based on problems is provided. This method can not only handle simple query problems, but also cope with challenges in complex query scenarios, providing users with a more convenient and intelligent database query experience. BRIEF DESCRIPTION OF THE DRAWINGS

[0027] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. 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 paying creative work.

[0028] The present invention is further described below in conjunction with the accompanying drawings.

[0029] Figure 1 This is a flowchart of a method for converting complex problems into SQL statements based on a large model in Example 1. DETAILED DESCRIPTION

[0030] The present invention is further described below in conjunction with the accompanying drawings and specific embodiments so that those skilled in the art can better understand the present invention and implement it. However, the embodiments are not intended to limit the present invention. In the absence of conflict, the embodiments of the present invention and the technical features in the embodiments may be combined with each other.

[0031] The embodiments of the present invention provide a method and system for converting complex problems into SQL statements based on a large model, which are used to solve the technical problem of how to convert complex problems into SQL statements.

[0032] Embodiment 1: A method for converting complex problems into SQL statements based on a large model of the present invention comprises the following steps:

[0033] Step S100: For the question raised by the user, whether the question is related to the database table is used as the question statement, the question and the question statement are input into the big model, and the SQL generation intention is judged by the big model, and whether the question raised by the user is related to the database table is judged by the SQL generation intention;

[0034] Step S200: If the result of SQL generation intention judgment is that the question is irrelevant to the database table, the user intention analysis is performed on the question through the big model and an answer is given;

[0035] Step S300: If the result of the SQL generation intention judgment is that the question is related to the database table and the SQL query statement can be directly generated according to the current database table, the SQL query statement is generated through the big model based on the semantics of the question and the structural information of the current database table, and the SQL query statement is executed based on the current database table to obtain the answer;

[0036] Step S400: If the result of the SQL generation intention judgment is that the problem is related to the database table, but the SQL query statement cannot be directly generated based on the current database table, based on the few-shot learning technology, a SQL query statement containing a new grammatical part is generated through the big model, and based on the current database table, the grammatical part is executed through the big model to query, and the query result is obtained as a new column. The new column is merged into the current database table to obtain a new database table, and based on the new database table, the SQL query statement containing the new grammatical part is executed to obtain the answer.

[0037] Among them, the large model is GPT and GPT-type natural language processing models based on deep learning.

[0038] When the SQL generation intention is judged by the big model in step S100 of this embodiment, the semantics of the question is analyzed by the big model to identify whether the semantics contains keywords or phrases related to the current database table structure or data, so as to determine whether the question can generate an SQL query statement for query.

[0039] In step S300, when the SQL query statement is generated by the big model based on the semantics of the question and the structural information of the current database table, the big model uses natural language processing technology to parse the question, extract the intent and key information, including the table name, column name and filtering conditions to be queried, and then generates a query statement that conforms to the SQL syntax based on the intent and key information.

[0040] In step S400, when generating an SQL query statement containing a new grammatical part through a large model based on few-shot learning technology, a set of questions and SQL query statements containing similarity calculation logic are obtained as sample data, and learning is performed based on the sample data and answers to user questions are output as problem statements. The sample data, questions raised by the user, and problem statements are input into the large model as context, and an SQL query statement containing a new grammatical part is generated through the large model.

[0041] Based on the method disclosed in this embodiment, the SQL generation judgment is performed on the problem and the prompt is designed with the following requirements:

[0042] ①If the question is not related to the table information, answer the question directly;

[0043] ② If it is related to table information and SQL can be directly generated based on the current table data, then SQL is directly generated for output, and then the current SQL is executed based on the table data to get the answer;

[0044] ③ If it is related to table information, and some columns need further calculation to generate SQL according to the table information, then first output SQL with new syntax. When outputting SQL with new syntax, you can use a few-shot form as a few examples for the large model to learn. The new syntax contains new columns that need to be further generated. After obtaining the new column results, execute the SQL statement to get the answer.

[0045] Here are some examples:

[0046] For example, the table is the students table, and the column information is the student's age, height, class, and home address.

[0047] Question 1: How many classes are there in the school?

[0048] The big model determines that the question is not related to the student table, so it will not generate SQL and will answer directly.

[0049] Question 2: How many students are taller than 160cm?

[0050] The large model determines that the problem is related to the table student, and can directly generate sql, such as generating sql: SELECTCOUNT(*)

[0051] FROM students WHERE height>160;

[0052] Question 3: How many students are from provincial capital cities?

[0053] The big model judges that the problem is related to the student table, but it cannot directly give the SQL statement. It is necessary to generate SQL with new syntax based on the learned examples, such as: SELECT COUNT(*) FROM students WHERE NEW("Q@Is it from theprovincial capital?"; `adress`) = `yes`; where NEW("Q@Is it from theprovincial capital?"; `adress`) = `yes` is a new syntax, and its main purpose is to extract the adress column for judgment. The next step is to transform the adress column into a new column named NEW("Q@Is it from theprovincial capital?"; `adress`). The internal values ​​need to be filled in using the big model, that is, the big model is used to judge each value "Is it from the provincial capital?", and the values ​​are filled in with yes and no. Finally, the generated new column and the original table are used as known table information to perform table SQL query and output the answer.

[0054] The method disclosed in this embodiment first determines the SQL generation intention of the user's question. If it is not related to the table information, a large language model is directly used to answer it; if it is related to the table information and SQL can be directly generated, the SQL statement is executed to obtain the answer; if further calculations are required on the columns in the table to construct SQL, a few-shot learning technology is used to generate SQL containing new syntax, the new syntax part is executed to create new columns, and finally the SQL statement is executed to obtain the answer. This method effectively expands the application scope of SQL automatic generation and provides an effective solution for complex query problems.

[0055] Embodiment 2: The present invention provides a system for converting complex questions into SQL statements based on a large model, including an SQL generation intention judgment module, a direct answer generation module, a direct SQL generation module, an indirect SQL generation module and an SQL execution module.

[0056] The SQL generation intention judgment module is used to perform the following: for questions raised by users, whether the question is related to the database table is used as the problem statement, the question and the problem statement are input into the big model, and the SQL generation intention judgment is performed through the big model. The SQL generation intention judgment is used to judge whether the question raised by the user is related to the database table. If the result of the SQL generation intention judgment is that the question is not related to the database table, the direct answer generation module is called. If the result of the SQL generation intention judgment is that the question is related to the database table and the SQL query statement can be directly generated according to the current database table, the direct SQL generation module is called. If the result of the SQL generation intention judgment is that the question is related to the database table, but the SQL query statement cannot be directly generated according to the current database table, the indirect SQL generation module is called.

[0057] In this embodiment, when the SQL generation intention is judged through the big model, the SQL generation intention judgment module is used to analyze the semantics of the problem through the big model, identify whether the semantics contains keywords or phrases related to the current database table structure or data, so as to determine whether the problem can generate an SQL query statement for query.

[0058] The direct answer generation module is used to perform the following: analyze the user intent of the question through a large model and give an answer.

[0059] The direct SQL generation module is used to perform the following: based on the semantics of the problem and the structural information of the current database table, generate SQL query statements through the large model and call the SQL execution module.

[0060] Among them, based on the semantics of the problem and the structural information of the current database table, when the SQL query statement is generated through the big model, the big model uses natural language processing technology to parse the problem, extract the intention and key information, including the table name, column name and filtering conditions to be queried, and then generates a query statement that conforms to the SQL syntax based on the intention and key information.

[0061] The indirect SQL generation module is used to perform the following: based on few-shot learning technology, generate an SQL query statement containing a new grammatical part through a large model, and query the grammatical part based on the current database table through the large model, obtain the query result as a new column, merge the new column into the current database table to obtain a new database table, use the new database table as the current database table, use the SQL query statement containing the new grammatical part as the final SQL query statement, and call the SQL execution module.

[0062] When generating SQL query statements containing new grammatical parts through a large model based on few-shot learning technology, the indirect SQL generation module is used to perform the following: obtain a set of questions and SQL query statements containing similarity calculation logic as sample data, learn based on the sample data, and output answers to user questions as problem statements, input the sample data, questions raised by users, and problem statements as context into the large model, and generate SQL query statements containing new grammatical parts through the large model.

[0063] The SQL execution module is used to perform the following: based on the current database table, execute SQL query statements to obtain answers.

[0064] Among them, the large model is GPT and GPT-type natural language processing models based on deep learning.

[0065] The system disclosed in this embodiment can execute the method disclosed in Example 1 to convert complex questions into SQL query statements, and execute the SQL query statements to obtain answers.

[0066] The present invention has been shown and described in detail above through the accompanying drawings and preferred embodiments. However, the present invention is not limited to these disclosed embodiments. Based on the above multiple embodiments, those skilled in the art can know that the means in the above different embodiments can be combined to obtain more embodiments of the present invention, and these embodiments are also within the protection scope of the present invention.

Claims

1. A method for converting complex problems into SQL statements based on a large model, characterized in that: The steps include: For questions raised by users, whether the question is related to the database table is used as the problem statement. The question and the problem statement are input into the big model, and the SQL generation intention is judged through the big model. The SQL generation intention is judged to determine whether the question raised by the user is related to the database table; If the result of SQL generation intent judgment is that the question is not related to the database table, the user intent analysis of the question is performed through the big model and an answer is given; If the result of the SQL generation intention judgment is that the question is related to the database table and the SQL query statement can be directly generated based on the current database table, the SQL query statement is generated through the big model based on the semantics of the question and the structure information of the current database table, and the SQL query statement is executed based on the current database table to obtain the answer; If the result of SQL generation intention judgment is that the problem is related to the database table, but the SQL query statement cannot be directly generated based on the current database table, based on the few-shot learning technology, the SQL query statement containing the new grammar part is generated through the big model, and based on the current database table, the grammar part is executed through the big model to query, and the query result is obtained as a new column. The new column is merged into the current database table to obtain a new database table. Based on the new database table, the SQL query statement containing the new grammar part is executed to obtain the answer; Among them, the large model is GPT and GPT-type natural language processing models based on deep learning.

2. The method for converting complex problems into SQL statements based on a large model according to claim 1, characterized in that: When using the big model to judge the SQL generation intention, the big model analyzes the semantics of the problem and identifies whether the semantics contains keywords or phrases related to the current database table structure or data, so as to determine whether the problem can generate an SQL query statement for query.

3. The method for converting complex problems into SQL statements based on a large model according to claim 1, characterized in that: When generating SQL query statements through the big model based on the semantics of the question and the structural information of the current database table, the big model uses natural language processing technology to parse the question, extract the intent and key information, including the table name, column name and filter conditions to be queried, and then generates query statements that conform to SQL syntax based on the intent and key information.

4. The method for converting complex problems into SQL statements based on a large model according to claim 1, characterized in that: When generating SQL query statements containing new grammatical parts through a large model based on few-shot learning technology, a set of questions and SQL query statements containing similarity calculation logic are obtained as sample data, and learning is performed based on the sample data and answers to user questions are output as problem statements. The sample data, questions raised by users, and problem statements are input into the large model as context, and an SQL query statement containing new grammatical parts is generated through the large model.

5. A system for converting complex problems into SQL statements based on a large model, characterized in that: For converting complex questions into SQL statements by a method for converting complex questions into SQL statements based on a large model as described in any one of claims 1 to 4, the system comprising an SQL generation intention judgment module, a direct answer generation module, a direct SQL generation module, an indirect SQL generation module, and an SQL execution module; The SQL generation intention judgment module is used to perform the following: for the question raised by the user, whether the question is related to the database table is used as the question statement, the question and the question statement are input into the big model, and the SQL generation intention judgment is performed through the big model. The SQL generation intention judgment is used to judge whether the question raised by the user is related to the database table. If the result of the SQL generation intention judgment is that the question is not related to the database table, the direct answer generation module is called. If the result of the SQL generation intention judgment is that the question is related to the database table and the SQL query statement can be directly generated according to the current database table, the direct SQL generation module is called. If the result of the SQL generation intention judgment is that the question is related to the database table, but the SQL query statement cannot be directly generated according to the current database table, the indirect SQL generation module is called; The direct answer generation module is used to perform the following: analyze the user intent of the question through the large model and give the answer; The direct SQL generation module is used to perform the following: based on the semantics of the problem and the structural information of the current database table, generate SQL query statements through the large model, and call the SQL execution module; The indirect SQL generation module is used to perform the following: based on the few-shot learning technology, generate an SQL query statement containing a new grammar part through the big model, and query the grammar part based on the current database table through the big model, obtain the query result as a new column, merge the new column into the current database table to obtain a new database table, use the new database table as the current database table, use the SQL query statement containing the new grammar part as the final SQL query statement, and call the SQL execution module; The SQL execution module is used to perform the following: based on the current database table, execute SQL query statements to obtain answers; Among them, the large model is GPT and GPT-type natural language processing models based on deep learning.

6. The system for converting complex problems into SQL statements based on a large model according to claim 5 is characterized in that: When the SQL generation intention is judged through the big model, the SQL generation intention judgment module is used to analyze the semantics of the problem through the big model, identify whether the semantics contains keywords or phrases related to the current database table structure or data, and determine whether the problem can generate an SQL query statement for query.

7. The system for converting complex problems into SQL statements based on a large model according to claim 5, characterized in that: When generating SQL query statements through the big model based on the semantics of the question and the structural information of the current database table, the big model uses natural language processing technology to parse the question, extract the intent and key information, including the table name, column name and filter conditions to be queried, and then generates query statements that conform to SQL syntax based on the intent and key information.

8. The system for converting complex problems into SQL statements based on a large model according to claim 5, characterized in that: When generating SQL query statements containing new grammatical parts through a large model based on few-shot learning technology, the indirect SQL statement generation module is used to obtain a set of questions and SQL query statements containing similarity calculation logic as sample data, learn based on the sample data, and output answers to user questions as problem statements. The sample data, questions raised by the user, and problem statements are input into the large model as context, and an SQL query statement containing the new grammatical part is generated through the large model.

Citation Information

Patent Citations

  • Method for automatically generating database query statement based on NLP language model

    CN116991869A

  • Method and device for converting natural language into database statement, equipment and medium

    CN117370378A