Large model SQL (Structured Query Language) generation method integrating few-sample prompt and multi-choice mechanism

Through the large-model SQL generation method that integrates a small sample prompt and multiple selection mechanism, the query text input by the user is analyzed and similar samples are filtered. The pre-trained model is used to generate and filter SQL statements, which solves the problem of inaccurate SQL statement generation in traditional technology. , improves the accuracy of generating SQL statements for natural language problems.

CN119088818APending Publication Date: 2024-12-06CHINA SOUTHERN POWER GRID ARTIFICIAL INTELLIGENCE TECHNOLOGY CO LTD

Patent Information

Application Number
CN202411035054.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-31
Publication Date
2024-12-06

AI Technical Summary

Technical Problem

Traditional technology in the conversion between natural language problems and SQL statements is limited by the comprehensiveness of templates or rulesets, resulting in the generated SQL statements being unable to accurately execute user intentions, affecting the accuracy of SQL statements generated by natural language problems.

Method used

Using a large model SQL generation method that integrates a small sample prompt and multiple selection mechanism, determines the database schema information by analyzing the query text input by the user, filters similar samples, uses pre-trained statements to generate a model to generate candidate SQL statements, and selects them based on the execution results. Target SQL statement.

Benefits of technology

Improve the accuracy of generating SQL statements for natural language problems, ensure that the generated SQL statements can accurately execute user intentions, and enhance the efficiency of SQL generation based on natural language.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119088818A_ABST
    Figure CN119088818A_ABST
Patent Text Reader

Abstract

The invention relates to a large model SQL (Structured Query Language) generation method integrating few-sample prompt and a multi-choice mechanism. The method comprises the following steps: analyzing a query text input by a user, and determining database mode information corresponding to the query text; according to the query text and the database mode information corresponding to the query text, screening out similar samples from a preset sample library; inputting the query text, the database mode information corresponding to the query text and the similar samples into a pre-trained statement generation model to obtain candidate SQL statements; and selecting a target SQL statement from the candidate SQL statements according to the execution result of the candidate SQL statements. By the adoption of the method, the natural language input by the user can be analyzed, the corresponding database mode can be accurately obtained, the multiple candidate SQL statements are generated in combination with the corresponding similar samples input by the user, the target SQL statement matched with the input of the user is accurately screened out from the candidate SQL statements on the basis of the execution results of the candidate SQL statements, and the user experience is improved. And the accuracy of generating the SQL statement based on the natural language problem is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a large-model SQL generation method, apparatus, computer device, computer-readable storage medium, and computer program product that integrates a few-sample prompt and multiple-selection mechanism. Background Art

[0002] Natural language processing can parse and understand users' natural language queries, identify users' intentions, extract key information, and use relevant tools or systems to convert users' natural language questions into structured SQL commands. Using natural language processing to convert natural language questions into SQL statements can help users accurately and quickly generate corresponding SQL statements based on natural language, without the need for users to specifically learn complex SQL syntax, thereby improving the efficiency of SQL statement generation.

[0003] Traditional technologies mainly use pre-defined templates or rule sets to convert natural language questions into SQL statements. However, traditional technologies are limited by the comprehensiveness of templates or rule sets. When the templates or rule sets are not compatible with the complexity of natural language questions, the SQL statements generated based on traditional technologies cannot accurately execute user intentions, which is not conducive to improving the accuracy of generating SQL statements based on natural language questions. Summary of the invention

[0004] Based on this, it is necessary to provide a large-model SQL generation method, apparatus, computer device, computer-readable storage medium and computer program product that integrates a small number of sample prompts and multiple selection mechanisms and can improve the accuracy of generating SQL statements based on natural language questions in response to the above-mentioned technical problems.

[0005] In a first aspect, the present application provides a large model SQL generation method integrating a few sample prompts and a multiple selection mechanism, comprising:

[0006] Analyze the query text input by the user and determine the database model information corresponding to the query text;

[0007] According to the query text and the database mode information corresponding to the query text, similar samples are screened out in a preset sample library; the similar samples include sample query text, database mode information corresponding to the sample query text and sample SQL statements;

[0008] Inputting the query text, the database model information corresponding to the query text, and the similar samples into a pre-trained statement generation model to obtain a candidate SQL statement;

[0009] According to the execution results of the candidate SQL statements, a target SQL statement is selected from the candidate SQL statements.

[0010] In one embodiment, analyzing the query text input by the user to determine the database mode information corresponding to the query text includes:

[0011] Determine the data table information corresponding to the query text according to the query text;

[0012] Determine the column information corresponding to the query text according to the data table information;

[0013] According to the data table information corresponding to the query text and the column information corresponding to the query text, the database mode information corresponding to the query text is determined.

[0014] In one embodiment, determining the column information corresponding to the query text according to the data table information includes:

[0015] Using the column information corresponding to the data table information as candidate column information;

[0016] Sorting the candidate column information according to the relevance between the candidate column information and the query text to obtain a sorting result;

[0017] Column information corresponding to the query text is selected from the sorting results.

[0018] In one embodiment, the filtering out similar samples in a preset sample library according to the query text and the database mode information corresponding to the query text includes:

[0019] Generate a text pair according to the query text and database schema information corresponding to the query text;

[0020] According to the similarity between the text pair and each sample in the preset sample library, the similar samples are screened out from the preset sample library.

[0021] In one embodiment, the step of inputting the query text, the database schema information corresponding to the query text, and the similar samples into a pre-trained statement generation model to obtain a candidate SQL statement includes:

[0022] Acquire a first arrangement order of data table information in the database schema information and a second arrangement order of column information in the database schema information;

[0023] sorting the data table information in the database schema information according to the first sorting order, and sorting the column information in the database schema information according to the second sorting order, to obtain candidate database schema information;

[0024] Obtaining a third arrangement order of the similar samples, and sorting the similar samples according to the third arrangement order to obtain candidate samples;

[0025] Generate prompt words according to the query text, the candidate database pattern information and the candidate samples;

[0026] The prompt word is input into the pre-trained sentence generation model to obtain the candidate SQL statement.

[0027] In one embodiment, selecting a target SQL statement from the candidate SQL statements according to the execution result of the candidate SQL statements includes:

[0028] According to the execution result of the candidate SQL statement, the candidate SQL statement is divided into at least one statement set; the statement set includes at least one candidate SQL statement;

[0029] Filtering a target statement set from the statement set according to the number of candidate SQL statements in the statement set;

[0030] The candidate SQL statements in the target statement set are used as the target SQL statements.

[0031] In a second aspect, the present application also provides a large model SQL generation device integrating a few sample prompts and a multiple selection mechanism, including:

[0032] An analysis module, used to analyze the query text input by the user and determine the database model information corresponding to the query text;

[0033] A screening module, used to screen similar samples in a preset sample library according to the query text and the database model information corresponding to the query text; the similar samples include sample query text, database model information corresponding to the sample query text and sample SQL statements;

[0034] A generation module, used for inputting the query text, the database model information corresponding to the query text and the similar samples into a pre-trained statement generation model to obtain a candidate SQL statement;

[0035] The selection module is used to select a target SQL statement from the candidate SQL statements according to the execution results of the candidate SQL statements.

[0036] In a third aspect, the present application further provides a computer device, wherein the computer device comprises a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the steps of the above method are implemented.

[0037] In a fourth aspect, the present application further provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program implements the steps of the above method when executed by a processor.

[0038] In a fifth aspect, the present application further provides a computer program product, wherein the computer program product comprises a computer program, and when the computer program is executed by a processor, the steps of the above method are implemented.

[0039] The above-mentioned large-model SQL generation method, device, computer equipment, computer-readable storage medium and computer program product integrating a few-sample prompt and multiple-selection mechanism analyzes the query text input by the user to determine the database mode information corresponding to the query text, thereby accurately obtaining the corresponding database mode from the query text input by the user, which is convenient for the generation of subsequent SQL statements; based on the query text and the database mode information corresponding to the query text, similar samples are screened out in a preset sample library, thereby combining the query text and the database mode information corresponding to the query text to obtain accurate similar samples, which is convenient for using similar samples to prompt the generation of subsequent SQL statements; similar samples include sample query text, database mode information corresponding to the sample query text and sample SQL statements; the query text is selected according to the query text and the database mode information corresponding to the query text. The query text, the database mode information corresponding to the query text and similar samples are input into the pre-trained statement generation model to obtain candidate SQL statements, so as to use the statement generation model to accurately generate the corresponding candidate SQL statements; according to the execution results of the candidate SQL statements, the target SQL statements are selected from the candidate SQL statements, so as to screen out the accurate target SQL statements based on the execution results, and the natural language input by the user can be analyzed to accurately obtain the corresponding database mode, and generate several candidate SQL statements in combination with similar samples corresponding to the user input, so as to accurately screen out the target SQL statements matching the user input from the candidate SQL statements based on the execution results of each candidate SQL statement, thereby improving the accuracy of generating SQL statements based on natural language questions. BRIEF DESCRIPTION OF THE DRAWINGS

[0040] In order to more clearly illustrate the technical solutions in the embodiments of the present application or related technologies, the drawings required for use in the embodiments of the present application or related technical descriptions will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.

[0041] Figure 1 It is an application environment diagram of a large model SQL generation method integrating a few sample prompts and a multiple selection mechanism in one embodiment;

[0042] Figure 2A flowchart of a method for generating SQL for a large model that integrates a few-sample prompt and a multiple-selection mechanism in one embodiment;

[0043] Figure 3 A schematic diagram of a process of generating an SQL statement in one embodiment;

[0044] Figure 4 It is a structural block diagram of a large model SQL generation device integrating a few sample prompts and multiple selection mechanisms in one embodiment;

[0045] Figure 5 The figure is a diagram of the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION

[0046] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.

[0047] The large model SQL generation method provided by the embodiment of the present application, which integrates a small number of sample prompts and a multiple selection mechanism, can be applied to Figure 1 In the application environment shown. Among them, the terminal 102 communicates with the server 104 through the network. The data storage system can store the data that the server 104 needs to process. The data storage system can be integrated on the server 104, or it can be placed on the cloud or other network servers. The terminal 102 analyzes the query text input by the user and determines the database mode information corresponding to the query text; the terminal 102 screens out similar samples in the preset sample library according to the query text and the database mode information corresponding to the query text; the similar samples include the sample query text, the database mode information corresponding to the sample query text, and the sample SQL statement; the terminal 102 inputs the query text, the database mode information corresponding to the query text, and the similar samples into the pre-trained statement generation model to obtain the candidate SQL statement; the terminal 102 selects the target SQL statement from the candidate SQL statement according to the execution result of the candidate SQL statement. Among them, the terminal 102 can be, but is not limited to, various personal computers, laptops, smart phones, tablet computers, Internet of Things devices and portable wearable devices, and the Internet of Things devices can be smart TVs, etc. The portable wearable device can be a head-mounted device, etc. The head mounted device may be a virtual reality (VR) device, an augmented reality (AR) device, smart glasses, etc. The server 104 may be an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server providing cloud computing services.

[0048] In an exemplary embodiment, Figure 2 As shown, a large model SQL generation method integrating a few sample prompts and a multiple selection mechanism is provided, and the method is applied to a terminal as an example for explanation, including the following steps S202 to S208. Among them:

[0049] Step S202: Analyze the query text input by the user to determine the database model information corresponding to the query text.

[0050] Here, the user may refer to a subject that inputs a query text through a terminal.

[0051] The query text may refer to a text in a natural language format. In practical applications, the query text may include a natural language question. The natural language question may be used to describe information such as a database schema and conditional values.

[0052] The database schema information may refer to information describing the organization and structure of data in a database. In practical applications, the database schema information may include but is not limited to data table information, data entity information, column information, relationships and constraints between data entities, and other information.

[0053] As an example, when a user needs to obtain an SQL statement, the user can input a query text in natural language through the operation terminal. The terminal obtains the query text input by the user and analyzes the query text sentence by sentence, and determines the database model information corresponding to the query text based on the context corresponding to each sentence in the query text.

[0054] Step S204, based on the query text and the database model information corresponding to the query text, similar samples are screened out in the preset sample library; the similar samples include the sample query text, the database model information corresponding to the sample query text and the sample SQL statement.

[0055] The preset sample library may refer to a pre-set database containing a number of samples. In actual applications, each sample in the preset sample library may include a sample query text, database model information corresponding to the sample query text, and a sample SQL statement.

[0056] Among them, similar samples can refer to samples whose similarity between database pattern information in a preset sample library and database pattern information corresponding to the query text and / or similarity between sample query text in a preset sample library and the query text meets preset similarity requirements.

[0057] As an example, the terminal can filter out similar samples in a preset sample library based on the query text and the database pattern information corresponding to the query text. Specifically, the terminal can calculate the first similarity between the database pattern information of each sample in the preset sample library and the database pattern information corresponding to the query text and / or the second similarity between the sample query text of each sample in the preset sample library and the query text. When the first similarity is greater than a first similarity threshold and / or the second similarity is greater than a second similarity threshold, the terminal takes the samples corresponding to the first similarity and / or the second similarity as similar samples.

[0058] Step S206: input the query text, the database model information corresponding to the query text, and similar samples into a pre-trained statement generation model to obtain a candidate SQL statement.

[0059] The pre-trained statement generation model may refer to a model for generating SQL statements. In practical applications, the pre-trained statement generation model may include a large language model.

[0060] The candidate SQL statement may refer to an SQL statement generated by a pre-trained statement generation model for the query text.

[0061] As an example, the terminal can input the query text, the database schema information corresponding to the query text, and similar samples into a pre-trained statement generation model. The pre-trained statement generation model can generate several candidate SQL statements based on the query text and the database schema information corresponding to the query text under the prompt of similar samples. The terminal can also display the above candidate SQL statements to the user through the terminal's display interface.

[0062] Step S208: selecting a target SQL statement from the candidate SQL statements according to the execution results of the candidate SQL statements.

[0063] The execution result may refer to information representing output or state changes generated after the candidate SQL statement is executed.

[0064] The target SQL statement may refer to an SQL statement among the candidate SQL statements that can solve the natural language problem described in the query text.

[0065] As an example, after the terminal obtains the candidate SQL statements, the terminal may execute each candidate SQL statement respectively, and then the terminal may compare the differences between the execution results of each candidate SQL statement, and select the target SQL statement from the candidate SQL statements.

[0066] In the above-mentioned large-model SQL generation method integrating a few-sample prompt and a multiple-selection mechanism, by analyzing the query text input by the user, the database schema information corresponding to the query text is determined, so as to accurately obtain the corresponding database schema from the query text input by the user, which is convenient for the generation of subsequent SQL statements; according to the query text and the database schema information corresponding to the query text, similar samples are screened out in the preset sample library, so as to combine the query text and the database schema information corresponding to the query text to obtain accurate similar samples, which is convenient for using similar samples to prompt the generation of subsequent SQL statements; similar samples include sample query text, database schema information corresponding to the sample query text, and sample SQL statements; the query text and the database schema information corresponding to the query text are combined into a database library, and the similar samples are combined into a database library, so as to obtain accurate similar samples, which is convenient for using similar samples to prompt the generation of subsequent SQL statements; similar samples include sample query text, database schema information corresponding to the sample query text, and sample SQL statements; the query text and the database schema information corresponding to the query text are combined into a database library, and the similar samples are combined into a database library, so as to obtain accurate similar samples, which is convenient for using similar samples to prompt the generation of subsequent SQL statements; the similar samples include sample query text, database schema information corresponding to the sample query text, and sample SQL statements; the query text and the database schema information corresponding to the query text are combined into a database library, and the similar samples ... The database model information and similar samples are input into the pre-trained statement generation model to obtain candidate SQL statements, so as to use the statement generation model to accurately generate corresponding candidate SQL statements; according to the execution results of the candidate SQL statements, the target SQL statements are selected from the candidate SQL statements, so as to screen out accurate target SQL statements based on the execution results, and the natural language input by the user can be analyzed to accurately obtain the corresponding database model, and generate several candidate SQL statements in combination with similar samples corresponding to the user input, so as to accurately screen out target SQL statements that match the user input from the candidate SQL statements based on the execution results of each candidate SQL statement, thereby improving the accuracy of generating SQL statements based on natural language questions.

[0067] In an exemplary embodiment, a query text input by a user is analyzed to determine database schema information corresponding to the query text, including: determining data table information corresponding to the query text based on the query text; determining column information corresponding to the query text based on the data table information; and determining database schema information corresponding to the query text based on the data table information corresponding to the query text and the column information corresponding to the query text.

[0068] The data table information corresponding to the query text may refer to information representing the name and structure of the data table related to the query text. In practical applications, the data table related to the query text is used to store data related to the query text (such as data described by the query text).

[0069] The column information corresponding to the query text may refer to information representing the name and data type of a data column related to the query text.

[0070] As an example, the terminal can analyze the data described by the query text and use the data described by the query text to determine the data table information corresponding to the query text. The terminal can also determine the column information corresponding to the query text based on the column information corresponding to the data table information. Then, the terminal can determine the database model information corresponding to the query text based on the data table information corresponding to the query text and the column information corresponding to the query text.

[0071] In this embodiment, by determining the data table information corresponding to the query text according to the query text; determining the column information corresponding to the query text according to the data table information; and determining the database schema information corresponding to the query text according to the data table information corresponding to the query text and the column information corresponding to the query text, the query text can be analyzed to obtain accurate data table information and column information, thereby improving the accuracy of the database schema information corresponding to the query text, providing a data basis for subsequent generation of SQL statements based on the query text, the database schema information corresponding to the query text, and similar samples, thereby improving the accuracy of generating SQL statements based on natural language questions.

[0072] In some embodiments, determining the column information corresponding to the query text based on the data table information includes: using the column information corresponding to the data table information as candidate column information; sorting the candidate column information according to the degree of correlation between the candidate column information and the query text to obtain a sorting result; and selecting the column information corresponding to the query text from the sorting result.

[0073] The column information corresponding to the data table information may refer to information representing the name and data type of the data column in each data table.

[0074] The sorting result may refer to information obtained by sorting the candidate column information according to the degree of relevance between the candidate column information and the query text. In practical applications, the sorting result may represent the arrangement order between the candidate column information.

[0075] As an example, the terminal can use the column information corresponding to the data table information as candidate column information, and then the terminal can analyze the correlation between the candidate column information and the query text, sort the candidate column information, and obtain the sorting results. The terminal can select a preset number of column information from the sorting results as the column information corresponding to the query text.

[0076] In this embodiment, the column information corresponding to the data table information is used as candidate column information; the candidate column information is sorted according to the correlation between the candidate column information and the query text to obtain the sorting result; the column information corresponding to the query text is selected from the sorting result. Based on the correlation between the candidate column information and the query text, the column information corresponding to the query text can be accurately selected from the candidate column information, thereby improving the accuracy of the column information corresponding to the query text and the database schema information, providing a data basis for the subsequent generation of SQL statements based on the query text, the database schema information corresponding to the query text and similar samples, thereby improving the accuracy of generating SQL statements based on natural language questions.

[0077] In some embodiments, similar samples are screened out from a preset sample library based on the query text and the database pattern information corresponding to the query text, including: generating text pairs based on the query text and the database pattern information corresponding to the query text; and screening out similar samples from the preset sample library based on the degree of similarity between the text pairs and each sample in the preset sample library.

[0078] The text pair may refer to information obtained by associating a query text and database schema information corresponding to the query text into a set of information.

[0079] As an example, the terminal can associate the query text and the database pattern information corresponding to the query text into a set of information as a text pair based on the query text and the database pattern information corresponding to the query text, and then the terminal can filter out similar samples from the preset sample library based on the degree of similarity between the text pair and each sample in the preset sample library. Specifically, the terminal can obtain the vector features of the text pair and the vector features corresponding to each sample in the preset sample library, and then the terminal can calculate the vector similarity between the vector features of the text pair and the vector features corresponding to each sample, and sort the vector similarities to obtain the sample sorting results. The terminal can select a preset number of samples from each sample in the preset sample library as similar samples based on the sample sorting results.

[0080] In this embodiment, a text pair is generated based on a query text and database schema information corresponding to the query text; similar samples are screened out from the preset sample library based on the degree of similarity between the text pair and each sample in the preset sample library. Accurate similar samples can be obtained based on the degree of similarity between the text pair formed by the query text and the database schema information corresponding to the query text and each sample in the preset sample library, so that the generation of SQL statements can be prompted / guided based on the accurate similar samples, thereby improving the accuracy of generating SQL statements based on natural language questions.

[0081] In some embodiments, a query text, database schema information corresponding to the query text, and similar samples are input into a pre-trained statement generation model to obtain candidate SQL statements, including: obtaining a first arrangement order of data table information in the database schema information and a second arrangement order of column information in the database schema information; sorting the data table information in the database schema information according to the first arrangement order, and sorting the column information in the database schema information according to the second arrangement order to obtain candidate database schema information; obtaining a third arrangement order of similar samples, and sorting the similar samples according to the third arrangement order to obtain candidate samples; generating prompt words according to the query text, the candidate database schema information, and the candidate samples; and inputting the prompt words into the pre-trained statement generation model to obtain candidate SQL statements.

[0082] The first arrangement order may refer to information representing the arrangement position of the data table information in the database schema information in the prompt word.

[0083] The second arrangement order may refer to information representing the arrangement position of the column information in the database schema information in the prompt word.

[0084] The candidate database schema information may refer to database schema information obtained by sorting the data table information in the database schema information according to the first sorting order and sorting the column information in the database schema information according to the second sorting order.

[0085] The third arrangement order may refer to information representing the arrangement positions of similar samples in the prompt word.

[0086] The candidate samples may refer to information obtained by sorting similar samples according to the third arrangement order.

[0087] Among them, the prompt word can refer to the information used to guide the statement generation model to generate the corresponding SQL statement or complete the SQL statement generation task. In actual applications, the prompt word can include but is not limited to the query text, the database model information corresponding to the query text, and similar samples.

[0088] As an example, in order to prevent the arrangement order of data table information, column information and similar samples from affecting the output result of the statement generation model, the terminal can perform diversified sorting on the data table information, column information and similar samples input to the statement generation model. For example, the terminal can obtain the first arrangement order of the data table information in the database schema information and the second arrangement order of the column information in the database schema information, and sort the data table information in the database schema information according to the first arrangement order, and sort the column information in the database schema information according to the second arrangement order to obtain candidate database schema information. The terminal can also obtain the third arrangement order of similar samples, and sort the similar samples according to the third arrangement order to obtain candidate samples. Then, the terminal can generate prompt words according to the query text, the candidate database schema information and the candidate samples, and input the prompt words into the pre-trained statement generation model to obtain candidate SQL statements. In a specific implementation, the terminal can randomly shuffle the arrangement order of the data table information, column information and similar samples, and generate prompt words in combination with the query text. Then, the terminal inputs the prompt words into the pre-trained statement generation model to obtain candidate SQL statements.

[0089] In this embodiment, by obtaining a first arrangement order of data table information in the database schema information and a second arrangement order of column information in the database schema information; sorting the data table information in the database schema information according to the first arrangement order, and sorting the column information in the database schema information according to the second arrangement order, candidate database schema information is obtained; a third arrangement order of similar samples is obtained, and similar samples are sorted according to the third arrangement order to obtain candidate samples; prompt words are generated according to the query text, the candidate database schema information and the candidate samples; the prompt words are input into a pre-trained statement generation model to obtain candidate SQL statements, which can adjust the arrangement order of data table information, column information and similar samples to avoid the solidification of the arrangement order of data table information, column information and similar samples affecting the output result of the statement generation model, thereby improving the accuracy of the candidate SQL statements output by the statement generation model, providing a data basis for subsequent screening of target SQL statements from the candidate SQL statements, and further improving the accuracy of generating SQL statements based on natural language questions.

[0090] In some embodiments, a target SQL statement is selected from the candidate SQL statements based on the execution results of the candidate SQL statements, including: dividing the candidate SQL statements into at least one statement set based on the execution results of the candidate SQL statements; the statement set includes at least one candidate SQL statement; based on the number of candidate SQL statements in the statement set, filtering out a target statement set from the statement set; and using the candidate SQL statements in the target statement set as target SQL statements.

[0091] The statement set may refer to a set consisting of several candidate SQL statements. In practical applications, each statement set may include at least one candidate SQL statement.

[0092] The target statement set may refer to a statement set including the largest number of candidate SQL statements in each statement set.

[0093] As an example, the terminal can divide the candidate SQL statements into at least one statement set according to the execution results of the candidate SQL statements, and each statement set can include at least one candidate SQL statement. Then, the terminal can filter out a target statement set from the statement set according to the number of candidate SQL statements in the statement set, and use the candidate SQL statements in the target statement set as the target SQL statements. For example, after the terminal obtains the candidate SQL statements, the terminal can execute each candidate SQL statement to obtain the execution result of each candidate SQL statement. The terminal can filter the candidate SQL statements whose execution results are execution failures, and then group the filtered candidate SQL statements. Specifically, the terminal can divide the candidate SQL statements with the same execution results into a group as a statement set, and then the terminal can use the statement set with the largest number of candidate SQL statements contained in each statement set as the target statement set. The terminal can display the SQL statements in the target statement set to the user through the display interface of the terminal.

[0094] In this embodiment, candidate SQL statements are divided into at least one statement set according to the execution results of the candidate SQL statements; the statement set includes at least one candidate SQL statement; a target statement set is screened out from the statement set according to the number of candidate SQL statements in the statement set; the candidate SQL statements in the target statement set are used as target SQL statements, and each candidate SQL statement can be grouped based on the execution results, and the target SQL statement can be accurately obtained from the group, thereby improving the accuracy of the target SQL statement and improving the accuracy of generating SQL statements based on natural language questions.

[0095] In some embodiments, Figure 3As shown, a flow chart of generating SQL statements is provided. The terminal can take the natural language query text input by the user as input, and then the terminal can perform pattern linking on the natural language query text based on natural language understanding technology (NLU), wherein the pattern linking can be used to identify references to database patterns and condition values ​​in the natural language query text. It can be understood that if the statement generation model (such as the Large Language Model Large Language Including the entire database schema in the context of the prompt word of the SQL statement generation model (LLM) will lead to two problems: (1) Introducing too many irrelevant items in the prompt word will increase the possibility that the statement generation model will generate irrelevant database schemas in the process of outputting SQL statements; (2) Using the complete database schema will cause the text length of the prompt word to be too long, thereby causing unnecessary sentence generation model inference costs, and even exceeding the maximum context length of the sentence generation model. Therefore, the terminal can determine the database schema information through table recall and column recall. Among them, table recall can use zero-sample prompts to recall data tables related to the natural language query text asked by the user in the table names and table descriptions of all data tables in the preset database. Column recall can further retrieve columns in the data table based on the table recall results. The terminal can also use zero-sample prompts to rank all columns in each data table according to the degree of relevance to the natural language query text, thereby obtaining column information related to the natural language query text. The terminal can generate database schema information related to the natural language query text based on the data table information related to the natural language query text and the column information related to the natural language query text.

[0096] The terminal can use similar examples (such as similar samples) to prompt the generation of SQL statements. Specifically, compared with zero samples, the terminal can add a small number of samples for prompting, thereby greatly increasing the accuracy of SQL statement generation of the statement generation model. Specifically, the terminal can inject similar examples into the prompt words and use the context learning ability of the statement generation model to improve the accuracy of the output SQL statement. Among them, the samples (or samples) in the sample library (such as the preset sample library) are jointly constructed by business personnel and technical personnel. Each sample can include a query text and an SQL statement pair, and the sample can be obtained using the company's existing test set. The terminal can calculate the most similar samples (including sample query text and sample SQL statements) as similar samples by a vector similarity sorting method. The similar samples can be assembled / injected into the prompt word Prompt. Specifically, the terminal can obtain the vector features of the text pair and the vector features corresponding to each sample in the preset sample library. Then, the terminal can calculate the vector similarity between the vector features of the text pair and the vector features corresponding to each sample, and sort the vector similarities to obtain the sample sorting results. Then, based on the sample sorting results, the most similar samples (such as the samples with the highest vector similarity) are taken out from the sample library as similar samples.

[0097] Then the terminal can splice the natural language query text, database schema information and similar samples into prompt words, and input the prompt words into a pre-trained sentence generation model (such as a large language model). The sentence generation model can output several candidate SQL statements. Considering that the output of the sentence generation model is significantly affected by the order of the input sentences, in order to reduce the influence of the order of data table information, column information and similar samples in the prompt words on the pattern linking results and / or the SQL statements generated by the sentence generation model, the terminal can randomly shuffle the arrangement order of the data table, the arrangement order of the column information and the arrangement order of the similar samples, thereby generating multiple prompt words. The sentence generation model can generate multiple candidate SQL statements based on the above multiple prompt words. Then the terminal can "vote" on the SQL execution results of the above multiple candidate SQL statements based on the voting selection mechanism, and select the target SQL statement from the multiple candidate SQL statements. For example, the statement generation model generates 4 SQL statements A, B, C and D, wherein the execution results of A, B and D are consistent, and the execution result of C is inconsistent with the execution results of A, B and D. The statement set composed of A, B and D can be used as the target statement set, and A, B and D can be used as the target SQL statement.

[0098] In this embodiment, through effective large language model support, the current mainstream large language model can be seamlessly integrated and used to ensure accurate conversion of text to SQL. Combined with pattern linking, the input text is analyzed through the large language model to identify and link the most likely data entities and column information, rather than the entire database structure. At the same time, multiple prompt pattern links are used to generate multiple prompts in different orders, high sampling is used to obtain multiple responses from the statement generation model, table links and column links are optimized by union results, and similar examples are injected into the prompt words to enhance the contextual learning ability of the statement generation model, thereby improving the accuracy of generating SQL statements. A multiple selection mechanism is used to generate multiple candidate SQL statements, and "voting" is performed based on the execution results of the candidate SQL statements to select the SQL statement with the highest accuracy and executability as the final output, which can effectively solve the problems of low accuracy and high uncertainty in generating SQL statements, thereby improving the accuracy of generating SQL statements based on natural language questions.

[0099] It should be understood that, although the various steps in the flowcharts involved in the above-mentioned embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps does not have a strict order restriction, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above-mentioned embodiments can include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the steps or stages in other steps.

[0100] Based on the same inventive concept, the embodiment of the present application also provides a large model SQL generation device with integrated few sample prompts and multiple selection mechanisms for implementing the large model SQL generation method with integrated few sample prompts and multiple selection mechanisms involved above. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme recorded in the above method, so the specific limitations in the embodiments of one or more large model SQL generation devices with integrated few sample prompts and multiple selection mechanisms provided below can be found in the above limitations on the large model SQL generation method with integrated few sample prompts and multiple selection mechanisms, and will not be repeated here.

[0101] In an exemplary embodiment, Figure 4 As shown, a large model SQL generation device integrating a small sample prompt and a multiple selection mechanism is provided, including: an analysis module 402, a screening module 404, a generation module 406 and a selection module 408, wherein:

[0102] The analysis module 402 is used to analyze the query text input by the user and determine the database model information corresponding to the query text.

[0103] The screening module 404 is used to screen similar samples in a preset sample library according to the query text and the database model information corresponding to the query text; the similar samples include sample query text, database model information corresponding to the sample query text and sample SQL statements.

[0104] The generation module 406 is used to input the query text, the database model information corresponding to the query text and the similar samples into the pre-trained statement generation model to obtain a candidate SQL statement.

[0105] The selection module 408 is used to select a target SQL statement from the candidate SQL statements according to the execution results of the candidate SQL statements.

[0106] In one of the exemplary embodiments, the above-mentioned analysis module 402 is specifically used to determine the data table information corresponding to the query text based on the query text; determine the column information corresponding to the query text based on the data table information; determine the database model information corresponding to the query text based on the data table information corresponding to the query text and the column information corresponding to the query text.

[0107] In one of the exemplary embodiments, the above-mentioned analysis module 402 is specifically used to use the column information corresponding to the data table information as candidate column information; sort the candidate column information according to the degree of correlation between the candidate column information and the query text to obtain a sorting result; and select the column information corresponding to the query text from the sorting result.

[0108] In one of the exemplary embodiments, the above-mentioned screening module 404 is specifically used to generate a text pair based on the query text and the database model information corresponding to the query text; and screen out the similar samples from the preset sample library based on the similarity between the text pair and each sample in the preset sample library.

[0109] In one of the exemplary embodiments, the generation module 406 is further specifically used to obtain a first arrangement order of the data table information in the database schema information and a second arrangement order of the column information in the database schema information; sort the data table information in the database schema information according to the first arrangement order, and sort the column information in the database schema information according to the second arrangement order to obtain candidate database schema information; obtain a third arrangement order of the similar samples, and sort the similar samples according to the third arrangement order to obtain candidate samples; generate prompt words according to the query text, the candidate database schema information and the candidate samples; and input the prompt words into the pre-trained statement generation model to obtain the candidate SQL statement.

[0110] In one of the exemplary embodiments, the selection module 408 is further specifically used to divide the candidate SQL statements into at least one statement set according to the execution results of the candidate SQL statements; the statement set includes at least one candidate SQL statement; according to the number of candidate SQL statements in the statement set, a target statement set is filtered out from the statement set; and the candidate SQL statements in the target statement set are used as the target SQL statements.

[0111] Each module in the above-mentioned large model SQL generation device integrating a few sample prompts and multiple selection mechanisms can be implemented in whole or in part by software, hardware, and a combination thereof. Each of the above-mentioned modules can be embedded in or independent of a processor in a computer device in the form of hardware, or can be stored in a memory in a computer device in the form of software, so that the processor can call and execute the operations corresponding to each of the above modules.

[0112] In an exemplary embodiment, a computer device is provided. The computer device may be a terminal, and its internal structure diagram may be as shown in FIG. Figure 5As shown. The computer device includes a processor, a memory, an input / output interface, a communication interface, a display unit and an input device. The processor, the memory and the input / output interface are connected through a system bus, and the communication interface, the display unit and the input device are connected to the system bus through the input / output interface. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The input / output interface of the computer device is used to exchange information between the processor and the external device. The communication interface of the computer device is used to communicate with an external terminal in a wired or wireless manner, and the wireless manner can be implemented through WIFI, a mobile cellular network, near field communication (NFC) or other technologies. When the computer program is executed by the processor, a large model SQL generation method integrating a few sample prompts and a multiple selection mechanism is implemented. The display unit of the computer device is used to form a visually visible picture, which can be a display screen, a projection device or a virtual reality imaging device. The display screen can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer device can be a touch layer covering the display screen, or a button, trackball or touchpad set on the computer device shell, or an external keyboard, touchpad or mouse.

[0113] Those skilled in the art will understand that Figure 5 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0114] In one embodiment, a computer device is further provided, including a memory and a processor, wherein a computer program is stored in the memory, and the processor implements the steps in the above method embodiments when executing the computer program.

[0115] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps in the above-mentioned method embodiments are implemented.

[0116] In one embodiment, a computer program product is provided, including a computer program, which implements the steps in the above method embodiments when executed by a processor.

[0117] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with relevant regulations.

[0118] A person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment method can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to the memory, database or other medium used in the embodiments provided in the present application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. Non-relational databases may include distributed databases based on blockchains, etc., but are not limited to this. The processor involved in each embodiment provided in this application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, an artificial intelligence (AI) processor, etc., but are not limited to this.

[0119] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this application.

[0120] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.

Claims

1. A large model SQL generation method integrating a few sample prompts and multiple selection mechanisms, characterized in that: The method comprises: Analyze the query text input by the user and determine the database model information corresponding to the query text; According to the query text and the database mode information corresponding to the query text, similar samples are screened out in a preset sample library; the similar samples include sample query text, database mode information corresponding to the sample query text and sample SQL statements; Inputting the query text, the database model information corresponding to the query text, and the similar samples into a pre-trained statement generation model to obtain a candidate SQL statement; According to the execution results of the candidate SQL statements, a target SQL statement is selected from the candidate SQL statements.

2. The method according to claim 1, characterized in that The step of analyzing the query text input by the user and determining the database mode information corresponding to the query text includes: Determine the data table information corresponding to the query text according to the query text; Determine the column information corresponding to the query text according to the data table information; According to the data table information corresponding to the query text and the column information corresponding to the query text, the database mode information corresponding to the query text is determined.

3. The method according to claim 2, characterized in that The determining, according to the data table information, the column information corresponding to the query text includes: Using the column information corresponding to the data table information as candidate column information; Sorting the candidate column information according to the relevance between the candidate column information and the query text to obtain a sorting result; Column information corresponding to the query text is selected from the sorting results.

4. The method according to claim 1, characterized in that: The step of screening out similar samples from a preset sample library according to the query text and database mode information corresponding to the query text includes: Generate a text pair according to the query text and database schema information corresponding to the query text; According to the similarity between the text pair and each sample in the preset sample library, the similar samples are screened out from the preset sample library.

5. The method according to claim 1, characterized in that The step of inputting the query text, the database model information corresponding to the query text, and the similar samples into a pre-trained statement generation model to obtain a candidate SQL statement includes: Acquire a first arrangement order of data table information in the database schema information and a second arrangement order of column information in the database schema information; sorting the data table information in the database schema information according to the first sorting order, and sorting the column information in the database schema information according to the second sorting order, to obtain candidate database schema information; Obtaining a third arrangement order of the similar samples, and sorting the similar samples according to the third arrangement order to obtain candidate samples; Generate prompt words according to the query text, the candidate database pattern information and the candidate samples; The prompt word is input into the pre-trained sentence generation model to obtain the candidate SQL statement.

6. The method according to claim 1, characterized in that The step of selecting a target SQL statement from the candidate SQL statements according to the execution results of the candidate SQL statements includes: According to the execution result of the candidate SQL statement, the candidate SQL statement is divided into at least one statement set; the statement set includes at least one candidate SQL statement; Filtering a target statement set from the statement set according to the number of candidate SQL statements in the statement set; The candidate SQL statements in the target statement set are used as the target SQL statements.

7. A large model SQL generation device integrating a few sample prompts and multiple selection mechanisms, characterized in that: The device comprises: An analysis module, used to analyze the query text input by the user and determine the database model information corresponding to the query text; A screening module, used to screen similar samples in a preset sample library according to the query text and the database model information corresponding to the query text; the similar samples include sample query text, database model information corresponding to the sample query text and sample SQL statements; A generation module, used for inputting the query text, the database model information corresponding to the query text and the similar samples into a pre-trained statement generation model to obtain a candidate SQL statement; The selection module is used to select a target SQL statement from the candidate SQL statements according to the execution results of the candidate SQL statements.

8. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 6 are implemented.

9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.

10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.

Citation Information

Patent Citations

  • SQL statement generation method and device, equipment and storage medium

    CN114780577A

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

    CN116991869A

  • SQL statement generation method, device and equipment

    CN117056351A

  • Method for generating structural query language and data query equipment

    CN117112590A

  • Database query statement generation method and device, equipment and storage medium

    CN117520363A

Cited By

  • Intelligent number asking method for large model index query

    CN120336360A

  • A Smart Data Query Method for Large Model Indicators

    CN120336360B

  • SQL statement generation model training method and device

    CN120744484A

  • Generative AI-based algorithm for improved cache hit performance in text-to-SQL systems

    US12748755B1

  • Database query-based processing method and apparatus, device, and medium

    WO2026123824A1