SQL statement generation method, device, equipment and storage medium

By obtaining query intent types and knowledge, building a thinking chain prompt template and iteratively training the model, the accuracy of NL2SQL technology in semantic capture and complex query processing is solved, and the accuracy of SQL statement generation is improved.

CN117931843BActive Publication Date: 2024-11-05TRANSWARP TECHNOLOGY (SHANGHAI) CO LTD +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410138630.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-01-31
Publication Date
2024-11-05
Estimated Expiration
2044-01-31

AI Technical Summary

Technical Problem

When NL2SQL technology handles natural language queries, it is difficult to accurately capture semantic details and context information, resulting in the generated SQL statements that do not match user needs, and it is difficult to generate a complete syntax structure when processing complex queries, reducing query accuracy.

Method used

By obtaining the query intent type set and related knowledge of the target problem, constructing a thinking chain prompt template, input the thinking chain determination model and SQL statement generation model, and iteratively train the thinking chain determination model to improve the accuracy of SQL statement generation.

Benefits of technology

It improves the accuracy of SQL statement generation, can more accurately understand the purpose of user query and generate SQL statements that meet the needs, reducing the query error rate.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117931843B_ABST
    Figure CN117931843B_ABST
Patent Text Reader

Abstract

The present invention discloses a method, device, equipment and storage medium for generating SQL statements. The method comprises: obtaining a target query intention type set and related knowledge corresponding to a target question; constructing a thinking chain prompt example corresponding to the target question according to the target query intention type set corresponding to the target question and a thinking chain prompt template corresponding to a target SQL statement generation model; inputting the target question, the target query intention type set corresponding to the target question, the thinking chain prompt example and related knowledge into a target thinking chain determination model to obtain a target thinking chain corresponding to the target question, wherein the target thinking chain determination model is obtained by iteratively training a to-be-trained thinking chain determination model with a first sample set; inputting the target question, the target thinking chain corresponding to the target question and related knowledge into the target SQL statement generation model to obtain a target SQL statement. The technical solution of the present invention can improve the accuracy of SQL statement generation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present invention relate to the field of computer technology, and in particular to a method, device, equipment and storage medium for generating an SQL statement. Background Art

[0002] Users of database management systems need to have a good understanding of the structure of the database (such as table names, column names, entity relationships, etc.) and be familiar with the database query language in order to interact with the database, but this may be a difficult task for non-professionals.

[0003] In this context, the industry is exploring a new way of human-computer interaction, namely, through NL2SQL (Natural Language To SQL) technology, which converts the natural language of business personnel into SQL statements executable by the database, eliminating the process of manually constructing SQL statements, thereby greatly improving data query efficiency and lowering the threshold for database use.

[0004] In NL2SQL application scenarios, since some natural language query problems may involve multiple entities, relations, and conditions, the model may not be able to accurately capture all semantic details and contextual information, and thus cannot understand the user's query purpose, resulting in the generated SQL statements not meeting user needs, reducing query accuracy. Secondly, the diversity of query types and the complexity of SQL grammatical structure make it difficult for the model to generate a grammatically complete SQL statement at one time when dealing with problems involving lengthy reasoning chains or multi-step solutions, increasing the difficulty of the NL2SQL task. Summary of the invention

[0005] Embodiments of the present invention provide a method, apparatus, device and storage medium for generating a SQL statement, which can improve the accuracy of generating a SQL statement.

[0006] According to one aspect of the present invention, there is provided a method for generating a SQL statement, comprising:

[0007] Obtain the target query intent type set and related knowledge corresponding to the target question;

[0008] Construct a thinking chain prompt example corresponding to the target question according to the target query intention type set corresponding to the target question and the thinking chain prompt template corresponding to the target SQL statement generation model;

[0009] Input the target question, the target query intention type set corresponding to the target question, the thinking chain prompt example and the relevant knowledge into the target thinking chain determination model to obtain the target thinking chain corresponding to the target question, wherein the target thinking chain determination model is obtained by iteratively training the to-be-trained thinking chain determination model with the first sample set;

[0010] The target problem, the target thinking chain corresponding to the target problem and related knowledge are input into the target SQL statement generation model to obtain the target SQL statement.

[0011] Furthermore, the target query intent type set and related knowledge corresponding to the target question are obtained, including:

[0012] Inputting the target question into the target multi-level intent recognition model to obtain a target query intent type set, wherein the target multi-level intent recognition model is obtained by iteratively training the multi-level intent recognition model to be trained with a second sample set, wherein the second sample set includes: a question sample and a query intent type set corresponding to the question sample;

[0013] Get the knowledge base corresponding to the database table;

[0014] The knowledge base is queried according to the target question to obtain association table structure information and related knowledge corresponding to the target question.

[0015] Further, iteratively training the to-be-trained thinking chain to determine the model through the first sample set includes:

[0016] Acquire a first sample set, wherein the first sample set includes: question samples and SQL statements corresponding to the question samples;

[0017] Obtain a query intention type set and related knowledge corresponding to the question samples in the first sample set;

[0018] Construct a thinking chain prompt example corresponding to the question sample according to the query intention type set corresponding to the question sample and the thinking chain prompt template corresponding to the target SQL statement generation model;

[0019] Inputting the question sample, the query intention type set corresponding to the question sample, the thinking chain prompt example and the relevant knowledge into the thinking chain determination model to be trained to obtain the thinking chain corresponding to the question sample;

[0020] Inputting the question sample, the thought chain corresponding to the question sample and related knowledge into the target SQL statement generation model to obtain a predicted SQL statement;

[0021] The parameters of the to-be-trained thinking chain determination model are trained according to a first function generated by the predicted SQL statement and the SQL statement corresponding to the problem sample to obtain a target thinking chain determination model.

[0022] Further, the parameters of the to-be-trained thinking chain determination model are trained according to a first function generated by the predicted SQL statement and the SQL statement corresponding to the problem sample to obtain a target thinking chain determination model, including:

[0023] Determine a target score for the predicted SQL statement based on the predicted SQL statement and the SQL statement corresponding to the problem sample;

[0024] Determine the loss value of the SQL statement generated by the model according to the target score of the predicted SQL statement, the predicted SQL statement, and the thought chain to be trained before training;

[0025] The parameters of the thought chain determination model to be trained are trained according to the loss value to obtain the target thought chain determination model.

[0026] Further, determining a target score for the predicted SQL statement according to the predicted SQL statement and the SQL statement corresponding to the problem sample includes:

[0027] Execute the predicted SQL statement and the SQL statement corresponding to the problem sample;

[0028] Determine a first score according to the execution result corresponding to the predicted SQL statement and the execution result of the SQL statement corresponding to the problem sample;

[0029] Determining a second score according to the similarity between the predicted SQL statement and the SQL statement corresponding to the problem sample;

[0030] Determine a third score according to the query intention type set corresponding to the predicted SQL statement and the query intention type set corresponding to the SQL statement corresponding to the question sample;

[0031] A target score for the predicted SQL statement is determined according to the first score, the second score, and the third score.

[0032] Further, a thinking chain prompt example corresponding to the target question is constructed according to the target query intention type set corresponding to the target question and the thinking chain prompt template corresponding to the target SQL statement generation model, including:

[0033] Obtaining hierarchical information corresponding to each query intent type in the target query intent type set corresponding to the target question;

[0034] A thinking chain prompt example corresponding to the target question is constructed according to the target query intention type set corresponding to the target question, the hierarchical information corresponding to each query intention type in the target query intention type set corresponding to the target question, and the thinking chain prompt template corresponding to the target SQL statement generation model.

[0035] Further, obtaining the hierarchical information corresponding to each query intention type in the target query intention type set corresponding to the target question includes:

[0036] Get the list of structure levels;

[0037] Query the structure hierarchy list to obtain the hierarchy information corresponding to each query intent type in the target query intent type set corresponding to the target question.

[0038] Furthermore, it also includes:

[0039] Get the query intent type library;

[0040] Obtain the SQL statement generation accuracy corresponding to each query intent type in the query intent type library generated based on the target SQL statement generation model;

[0041] A thinking chain prompt template corresponding to a target SQL statement generation model is generated according to a target query intent type in a query intent type library whose SQL statement generation accuracy is less than an accuracy threshold.

[0042] Further, the multi-level intent recognition model to be trained is iteratively trained through the second sample set, including:

[0043] Obtain a query intention type set corresponding to each question sample in the first sample set;

[0044] Generate a second sample set according to the question samples in the first sample set and the query intention type set corresponding to each question sample;

[0045] Inputting the question samples in the second sample set into the multi-level intent recognition model to be trained to obtain a set of predicted query intent types;

[0046] Training the parameters of the multi-level intent recognition model to be trained according to the objective function generated by the predicted query intent type set and the query intent type set corresponding to the question sample;

[0047] Return to execute the operation of inputting the question samples in the second sample set into the multi-level intent recognition model to be trained to obtain a set of predicted query intent types, until the target multi-level intent recognition model is obtained.

[0048] According to another aspect of the present invention, there is provided a SQL statement generating device, the SQL statement generating device comprising:

[0049] The first acquisition module is used to obtain a target query intention type set and related knowledge corresponding to the target question;

[0050] A thinking chain prompt example construction module is used to construct a thinking chain prompt example corresponding to the target question according to a target query intention type set corresponding to the target question and a thinking chain prompt template corresponding to a target SQL statement generation model;

[0051] a target thinking chain determination module, used for inputting the target question, the target query intention type set corresponding to the target question, a thinking chain prompt example and related knowledge into a target thinking chain determination model, and obtaining a target thinking chain corresponding to the target question, wherein the target thinking chain determination model is obtained by iteratively training the to-be-trained thinking chain determination model with the first sample set;

[0052] The target SQL statement generation module is used to input the target problem, the target thinking chain corresponding to the target problem and related knowledge into the target SQL statement generation model to obtain the target SQL statement.

[0053] Furthermore, the first acquisition module is specifically used for:

[0054] Inputting the target question into the target multi-level intent recognition model to obtain a target query intent type set, wherein the target multi-level intent recognition model is obtained by iteratively training the multi-level intent recognition model to be trained with a second sample set, wherein the second sample set includes: a question sample and a query intent type set corresponding to the question sample;

[0055] Get the knowledge base corresponding to the database table;

[0056] The knowledge base is queried according to the target question to obtain association table structure information and related knowledge corresponding to the target question.

[0057] Furthermore, the target thinking chain determination module is specifically used for:

[0058] Acquire a first sample set, wherein the first sample set includes: question samples and SQL statements corresponding to the question samples;

[0059] Obtain a query intention type set and related knowledge corresponding to the question samples in the first sample set;

[0060] Construct a thinking chain prompt example corresponding to the question sample according to the query intention type set corresponding to the question sample and the thinking chain prompt template corresponding to the target SQL statement generation model;

[0061] Inputting the question sample, the query intention type set corresponding to the question sample, the thinking chain prompt example and the relevant knowledge into the thinking chain determination model to be trained to obtain the thinking chain corresponding to the question sample;

[0062] Inputting the question sample, the thought chain corresponding to the question sample and related knowledge into the target SQL statement generation model to obtain a predicted SQL statement;

[0063] The parameters of the to-be-trained thinking chain determination model are trained according to a first function generated by the predicted SQL statement and the SQL statement corresponding to the problem sample to obtain a target thinking chain determination model.

[0064] Furthermore, the target thinking chain determination module is specifically used for:

[0065] Determine a target score for the predicted SQL statement based on the predicted SQL statement and the SQL statement corresponding to the problem sample;

[0066] Determine the loss value of the SQL statement generated by the model according to the target score of the predicted SQL statement, the predicted SQL statement, and the thought chain to be trained before training;

[0067] The parameters of the thought chain determination model to be trained are trained according to the loss value to obtain the target thought chain determination model.

[0068] Furthermore, the target thinking chain determination module is specifically used for:

[0069] Execute the predicted SQL statement and the SQL statement corresponding to the problem sample;

[0070] Determine a first score according to the execution result corresponding to the predicted SQL statement and the execution result of the SQL statement corresponding to the problem sample;

[0071] Determining a second score according to the similarity between the predicted SQL statement and the SQL statement corresponding to the problem sample;

[0072] Determine a third score according to the query intention type set corresponding to the predicted SQL statement and the query intention type set corresponding to the SQL statement corresponding to the question sample;

[0073] A target score for the predicted SQL statement is determined according to the first score, the second score, and the third score.

[0074] Furthermore, the thought chain prompt example building module is specifically used for:

[0075] Obtaining hierarchical information corresponding to each query intent type in the target query intent type set corresponding to the target question;

[0076] A thinking chain prompt example corresponding to the target question is constructed according to the target query intention type set corresponding to the target question, the hierarchical information corresponding to each query intention type in the target query intention type set corresponding to the target question, and the thinking chain prompt template corresponding to the target SQL statement generation model.

[0077] Furthermore, the thought chain prompt example building module is specifically used for:

[0078] Get the list of structure levels;

[0079] Query the structure hierarchy list to obtain the hierarchy information corresponding to each query intent type in the target query intent type set corresponding to the target question.

[0080] Furthermore, it also includes:

[0081] A query intent type library acquisition module is used to acquire a query intent type library;

[0082] An SQL statement generation accuracy acquisition module is used to obtain the SQL statement generation accuracy corresponding to each query intent type in the query intent type library generated based on the target SQL statement generation model;

[0083] The thinking chain prompt template generation module corresponding to the target SQL statement generation model is used to generate the thinking chain prompt template corresponding to the target SQL statement generation model according to the target query intent type in the query intent type library whose SQL statement generation accuracy is less than the accuracy threshold.

[0084] Furthermore, the first acquisition module is specifically used for:

[0085] Obtain a query intention type set corresponding to each question sample in the first sample set;

[0086] Generate a second sample set according to the question samples in the first sample set and the query intention type set corresponding to each question sample;

[0087] Inputting the question samples in the second sample set into the multi-level intent recognition model to be trained to obtain a set of predicted query intent types;

[0088] Training the parameters of the multi-level intent recognition model to be trained according to the objective function generated by the predicted query intent type set and the query intent type set corresponding to the question sample;

[0089] Return to execute the operation of inputting the question samples in the second sample set into the multi-level intent recognition model to be trained to obtain a set of predicted query intent types, until the target multi-level intent recognition model is obtained.

[0090] According to another aspect of the present invention, an electronic device is provided, the electronic device comprising:

[0091] at least one processor; and

[0092] a memory communicatively connected to the at least one processor; wherein,

[0093] The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the SQL statement generating method described in any embodiment of the present invention.

[0094] According to another aspect of the present invention, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement the SQL statement generation method described in any embodiment of the present invention when executed.

[0095] The embodiment of the present invention obtains a target query intention type set and related knowledge corresponding to a target question; constructs a thinking chain prompt example corresponding to the target question according to the target query intention type set corresponding to the target question and a thinking chain prompt template corresponding to a target SQL statement generation model; inputs the target question, the target query intention type set corresponding to the target question, a thinking chain prompt example and related knowledge into a target thinking chain determination model to obtain a target thinking chain corresponding to the target question, wherein the target thinking chain determination model is obtained by iteratively training a thinking chain determination model to be trained with a first sample set; inputs the target question, the target thinking chain corresponding to the target question and related knowledge into the target SQL statement generation model to obtain a target SQL statement, thereby improving the accuracy of SQL statement generation.

[0096] It should be understood that the contents described in this section are not intended to identify the key or important features of the embodiments of the present invention, nor are they intended to limit the scope of the present invention. Other features of the present invention will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS

[0097] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments are briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present invention and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without creative work.

[0098] Figure 1 is a flow chart of a method for generating a SQL statement in an embodiment of the present invention;

[0099] Figure 2 is a schematic diagram of hierarchical information in an embodiment of the present invention;

[0100] Figure 3 is a flowchart of another SQL statement generation method in an embodiment of the present invention;

[0101] Figure 4 is a flow chart of AutoCoT model training in an embodiment of the present invention;

[0102] Figure 5 It is a structural schematic diagram of a SQL statement generating device in an embodiment of the present invention;

[0103] Figure 6 It is a structural schematic diagram of an electronic device in an embodiment of the present invention. DETAILED DESCRIPTION

[0104] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.

[0105] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0106] It is understandable that before using the technical solutions disclosed in the embodiments of the present disclosure, the types, scope of use, usage scenarios, etc. of the personal information involved in the present disclosure should be informed to the user and the user's authorization should be obtained in an appropriate manner in accordance with relevant laws and regulations.

[0107] Embodiment 1

[0108] Figure 1 A flowchart of a method for generating a SQL statement provided in an embodiment of the present invention. This embodiment is applicable to the case of generating SQL statements. The method can be executed by a device for generating a SQL statement in an embodiment of the present invention. The device can be implemented in software and / or hardware. Figure 1 As shown, the method specifically comprises the following steps:

[0109] S110, obtaining a target query intention type set and related knowledge corresponding to the target question.

[0110] Among them, the target query intention type set corresponding to the target question can be at least one in a pre-set query intention type list.

[0111] Specifically, the method for obtaining the target query intention type set corresponding to the target question may be: performing intent recognition on the target question to obtain the target query intention type set corresponding to the target question. The method for obtaining the target query intention type set corresponding to the target question may also be: inputting the target question into a target multi-level intent recognition model to obtain the target query intention type set.

[0112] The relevant knowledge includes: structural information of the association table and background knowledge, and the structural information of the association table may be Schema information of the association table. Specifically, the method of obtaining the relevant knowledge may be: querying the knowledge base corresponding to the database table to obtain the relevant knowledge of the target problem.

[0113] Optionally, obtain a target query intent type set and related knowledge corresponding to the target question, including:

[0114] Inputting the target question into the target multi-level intent recognition model to obtain a target query intent type set, wherein the target multi-level intent recognition model is obtained by iteratively training the multi-level intent recognition model to be trained with a second sample set, wherein the second sample set includes: a question sample and a query intent type set corresponding to the question sample;

[0115] Get the knowledge base corresponding to the database table;

[0116] The knowledge base is queried according to the target question to obtain relevant knowledge corresponding to the target question.

[0117] It should be noted that the query intent type set corresponding to the question samples in the second sample set includes at least one of the query intent types preset by the user. The query intent type set corresponding to all question samples in the second sample set needs to cover all query intent types preset by the user.

[0118] The target multi-level intent recognition model may be obtained by training the multi-level intent recognition model to be trained using the SFT (Supervised Fine-tuning) method.

[0119] The target query intent type set may include at least one query intent type. For example, the target query intent type set may include: nested query, multi-condition query, and aggregate calculation.

[0120] The second sample set may be related to the first sample set, for example, the second sample set may be generated based on the first sample set. The second sample set may also be unrelated to the first sample set, which is not limited in the embodiment of the present invention.

[0121] Specifically, the method of obtaining the knowledge base corresponding to the database table can be: identifying the table name of the database table to obtain the keywords in the table name of the database table, obtaining the professional knowledge, industry jargon, table structure, field value range, etc. corresponding to the keywords in the table name of the database table; building a knowledge base according to the professional knowledge, industry jargon, table structure, field value range, etc. corresponding to the keywords in the table name of the database table.

[0122] It should be noted that when a user inputs a target question, relevant knowledge of the target question can be recalled from the corresponding knowledge base based on methods such as semantic similarity comparison.

[0123] Optionally, iteratively training the multi-level intent recognition model to be trained through the second sample set includes:

[0124] Obtain a query intention type set corresponding to each question sample in the first sample set;

[0125] Generate a second sample set according to the question samples in the first sample set and the query intention type set corresponding to each question sample;

[0126] Inputting the question samples in the second sample set into the multi-level intent recognition model to be trained to obtain a set of predicted query intent types;

[0127] Training the parameters of the multi-level intent recognition model to be trained according to the objective function generated by the predicted query intent type set and the query intent type set corresponding to the question sample;

[0128] Return to execute the operation of inputting the question samples in the second sample set into the multi-level intent recognition model to be trained to obtain a set of predicted query intent types, until the target multi-level intent recognition model is obtained.

[0129] Specifically, a method for obtaining a query intention type set corresponding to each question sample in the first sample set may be: determining a query intention type set corresponding to each question sample according to a SQL statement corresponding to each question sample in the first sample set.

[0130] The embodiment of the present invention uses the target question as the input of the target multi-level intention recognition model to perform user query intention recognition and obtain multiple levels of user query intention.

[0131] Optionally, iteratively training the multi-level intent recognition model to be trained through the second sample set includes:

[0132] Acquire a second sample set, wherein the second sample set includes: question samples and a set of query intent types corresponding to the question samples;

[0133] Inputting the question samples in the second sample set into the multi-level intent recognition model to be trained to obtain a set of predicted query intent types;

[0134] Training the parameters of the multi-level intent recognition model to be trained according to the objective function generated by the predicted query intent type set and the query intent type set corresponding to the question sample;

[0135] Return to execute the operation of inputting the question samples in the second sample set into the multi-level intent recognition model to be trained to obtain a set of predicted query intent types, until the target multi-level intent recognition model is obtained.

[0136] It should be noted that the second sample set may be different from the first sample set.

[0137] Specifically, the second sample set may be obtained by presetting a list of query intent types, obtaining a question sample, and obtaining a query intent type set corresponding to the question sample based on a list of query intent types corresponding to the SQL statement corresponding to the question sample.

[0138] S120, constructing a thinking chain prompt example corresponding to the target question according to the target query intention type set corresponding to the target question and the thinking chain prompt template corresponding to the target SQL statement generation model.

[0139] The construction of the thought chain prompt template corresponding to the target SQL statement generation model can be carried out by testing the target SQL statement generation model, and the test result obtained determines the thought chain prompt template corresponding to the target SQL statement generation model. For example, the thought chain prompt template corresponding to the model can be generated according to the query intent type with poor SQL statement generation ability in the test result.

[0140] For example, the thinking chain prompt template may include: prompt information corresponding to nested queries, prompt information corresponding to conditional grouping, prompt information corresponding to deduplication processing, prompt information corresponding to conditional expressions, prompt information corresponding to single-condition queries, prompt information corresponding to multiple-condition queries, prompt information corresponding to simple grouping, prompt information corresponding to simple sorting, prompt information corresponding to maximum value sorting, prompt information corresponding to top N sorting, prompt information corresponding to aggregate calculations, prompt information corresponding to indicator calculations, prompt information corresponding to algebraic calculations and other types (there is no prompt information for other types).

[0141] It should be noted that the method of constructing a thinking chain prompt example corresponding to the target problem according to the target query intention type set corresponding to the target problem and the thinking chain prompt template corresponding to the target SQL statement generation model can be: pre-establish a thinking chain prompt template corresponding to the target SQL statement generation model, and construct a thinking chain prompt example corresponding to the target problem according to the target query intention type set corresponding to the target problem, the hierarchical information corresponding to each query intention type in the target query intention type set corresponding to the target problem, and the thinking chain prompt template corresponding to the target SQL statement generation model.

[0142] The embodiment of the present invention provides a reference for the subsequent targeted improvement of the SQL generation capability of the target SQL statement generation model by evaluating the SQL statement generation capability of the target SQL statement generation model. According to the evaluation results, a set of thought chain prompt templates adapted to the target SQL statement generation model are designed for different models. Based on the CoT prompt template, the embodiment of the present invention constructs a CoT example with SQL statement generation logic according to the hierarchical order from macro to micro of SQL grammar based on the multi-level intent recognition results of the target question.

[0143] Optionally, a thinking chain prompt example corresponding to the target question is constructed according to a target query intention type set corresponding to the target question and a thinking chain prompt template corresponding to a target SQL statement generation model, including:

[0144] Obtaining hierarchical information corresponding to each query intent type in the target query intent type set corresponding to the target question;

[0145] A thinking chain prompt example corresponding to the target question is constructed according to the target query intention type set corresponding to the target question, the hierarchical information corresponding to each query intention type in the target query intention type set corresponding to the target question, and the thinking chain prompt template corresponding to the target SQL statement generation model.

[0146] The embodiments of the present invention aim at the weaknesses of the SQL generation capability of the SQL statement generation model, generate prompt examples, and guide the model to generate SQL statements with correct structures.

[0147] In the case of multiple intent recognition tags, an embodiment of the present invention combines the thinking chain generation prompts corresponding to the multiple intent recognition tags in order from high to low levels of the intent recognition tag levels to form a thinking chain prompt example that is more in line with the SQL statement generation logic.

[0148] Optionally, also include:

[0149] Get the query intent type library;

[0150] Obtain the SQL statement generation accuracy corresponding to each query intent type in the query intent type library generated based on the target SQL statement generation model;

[0151] A thinking chain prompt template corresponding to a target SQL statement generation model is generated according to a target query intent type in a query intent type library whose SQL statement generation accuracy is less than an accuracy threshold.

[0152] The query intent type library includes all query intent types pre-created by the user.

[0153] Specifically, the method for obtaining the SQL statement generation accuracy corresponding to each query intent type in the query intent type library generated based on the target SQL statement generation model can be: establish a test sample set according to the query intent type library, wherein the test sample set includes: test questions, SQL statements corresponding to the test questions, and a query intent type set corresponding to the test questions; input all test questions in the test set into the target SQL statement generation model in sequence to obtain the predicted SQL statement corresponding to each test question in the test set, and determine the SQL statement generation accuracy corresponding to each query intent type in the query intent type library according to the predicted SQL statement corresponding to each test question in the test set, the SQL statement corresponding to each test question, and the query intent type set corresponding to each test question. For example, if the query intent type library includes: query intent type A, query intent type B, query intent type C, query intent type D, the test sample set includes 8 test questions, namely: test question X, test question Y, test question Z, test question W, test question S, test question F, test question G, and test question H.The query intent types corresponding to test question X and test question Y are both query intent type A, the query intent types corresponding to test question Z and test question W are both query intent type B, the query intent types corresponding to test question S and test question F are both query intent type C, and the query intent types corresponding to test question G and test question H are both query intent type D. If test question X is input into the target SQL statement generation model, the predicted SQL statement obtained is the same as the SQL statement corresponding to test question X, test question Y is input into the target SQL statement generation model, the predicted SQL statement obtained is the same as the SQL statement corresponding to test question X (the execution results of the two SQL statements are the same), test question Z is input into the target SQL statement generation model, the predicted SQL statement obtained is different from the SQL statement corresponding to test question X (the execution results of the two SQL statements are different), and test question W is input into the target SQL statement generation model. The predicted SQL statement obtained is different from the SQL statement corresponding to test question X. When test question S is input into the target SQL statement generation model, the predicted SQL statement obtained is the same as the SQL statement corresponding to test question X; when test question F is input into the target SQL statement generation model, the predicted SQL statement obtained is the same as the SQL statement corresponding to test question X; when test question G is input into the target SQL statement generation model, the predicted SQL statement obtained is different from the SQL statement corresponding to test question X; when test question H is input into the target SQL statement generation model, the predicted SQL statement obtained is different from the SQL statement corresponding to test question X. Then, it is determined that the SQL statement generation accuracy corresponding to query intention type A is 2 / 8, the SQL statement generation accuracy corresponding to query intention type B is 0, the SQL statement generation accuracy corresponding to query intention type C is 2 / 8, and the SQL statement generation accuracy corresponding to query intention type D is 0. If the accuracy threshold is 1 / 8, the thinking chain prompt template corresponding to the target SQL statement generation model is generated according to query intention type A and query intention type C.

[0154] It should be noted that you can also build a corpus pool for generating prompts for thought chains, and evaluate and screen all the corpora in the pool. Prune the corpora with poor quality and mixed logic, and only keep the corpora with high quality. High-quality templates for generating prompts for thought chains can help improve the accuracy of model SQL generation.

[0155] In a specific example, the thinking chain prompt template corresponding to the target SQL statement generation model includes:

[0156] Nested query: "This is a nested query problem. The solution to the problem can be divided into two parts: subquery and main query. First, generate a subquery statement. To avoid repeated queries, you can use the [As] statement to alias the query results. Second, based on the query results of the subquery, perform relevant calculations to complete the main query." Conditional grouping: "This is a conditional grouping problem. Based on the grouping object, you need to use the [Having] keyword to set search conditions for the grouping and delete the groups that do not meet the conditions from the results."

[0157] Deduplication: "This is still a statistical problem. When counting numbers, it is required to use [Distinct] to perform deduplication."

[0158] Conditional expressions: "The problem also involves conditional expressions, which need to be implemented using the [Case When] keyword."

[0159] Single condition query: "Confirm the filter condition."

[0160] Multiple-condition query: "This is a multiple-condition query question. If the question contains the word [or], the conditions are connected by [or], otherwise, they are connected by [and]."

[0161] Simple grouping: "Determine the grouping object and group the query results according to the value of the grouping object."

[0162] Simple sorting: "Determine the sorting object, and sort the query results in [ascending] or [descending] order according to the value of the sorting object."

[0163] Sort by maximum value: "Determine the sorting object, sort the query results in [ascending] or [descending] order according to the value of the sorting object, and return the first piece of data."

[0164] First N Sort: "Determine the sort object, sort the query results in [ascending] or [descending] order according to the value of the sort object, and return the first N data."

[0165] Aggregate calculation: "The problem also involves aggregate calculation. You need to perform corresponding aggregate calculation on the query results. The corresponding operators are: ['maximum value': 'max', 'minimum value': 'min', 'count': 'count', 'sum': 'sum', 'average': 'avg']."

[0166] Indicator calculation: depends on background knowledge.

[0167] Algebraic computation: depends on background knowledge.

[0168] Other types: None.

[0169] In another specific example, the SQL generation capability of the SQL statement generation model for all query intent types is first evaluated, and the weak points of the SQL generation capability of the SQL statement generation model are obtained, which provides a reference for the subsequent targeted improvement of the SQL generation capability of the SQL statement generation model. Secondly, according to the evaluation results of the SQL generation capability of the SQL statement generation model, a set of thought chain generation prompt templates are designed for different SQL statement generation models. For the weak points of the SQL generation capability of the SQL statement generation model, the designed thought chain generation prompt example template is required to guide the model to generate SQL statements with correct structure. Finally, a set of thought chain generation prompt template libraries adapted to the SQL statement generation model is obtained. When a query question is input, the CoT example generation module is based on the CoT prompt template library, according to the results of multi-level intent recognition, and in accordance with the hierarchical order from the macro-architecture to the micro-structure of the SQL grammar, to construct a CoT example with SQL generation logic.

[0170] The embodiment of the present invention can understand the weaknesses of the SQL generation capability of the target SQL statement generation model by evaluating the SQL generation capability of the target SQL statement generation model in advance, and provide a reference for subsequent targeted improvement of the SQL generation capability of the target SQL statement generation model.

[0171] Optionally, obtaining hierarchical information corresponding to each query intent type in the target query intent type set corresponding to the target question includes:

[0172] Get the list of structure levels;

[0173] Query the structure hierarchy list to obtain the hierarchy information corresponding to each query intent type in the target query intent type set corresponding to the target question.

[0174] The structure hierarchy list is a pre-created hierarchy list, for example, Figure 2 As shown, in the embodiment of the present invention, the levels from the macro-architecture to the micro-structure of the SQL grammar are divided into four levels: subquery layer, clause layer, expression layer, and qualifier layer, that is, the subquery layer is the first level, the clause layer is the second level, the expression layer is the third level, and the qualifier layer is the fourth level. It is more in line with the generation logic of SQL. The subquery layer includes: non-nested queries and nested queries, and the nested queries include: subqueries and main queries. The clause layer includes: conditional queries, grouping queries, sorting queries and other queries. The conditional queries include: single-condition queries and multi-conditional queries. The grouping queries include: simple grouping and conditional grouping. The sorting queries include: simple sorting, maximum value sorting, and top N sorting. The expression layer includes: conditional expressions and indicator calculations. The qualifier layer includes: deduplication processing, aggregation calculations, and algebraic calculations. Indicator calculations and algebraic calculations are intent types that require background knowledge prompts.

[0175] For example, if the query intent type is a nested query, the query structure hierarchy list is obtained and the query intent type hierarchy is level 1.

[0176] S130, input the target question, the target query intention type set corresponding to the target question, thinking chain prompt examples and related knowledge into the target thinking chain determination model to obtain the target thinking chain corresponding to the target question, wherein the target thinking chain determination model is obtained by iteratively training the thinking chain determination model to be trained with the first sample set.

[0177] The embodiment of the present invention uses the target question, the target query intent type set corresponding to the target question, the CoT example with SQL generation logic, and the relevant knowledge recalled from the knowledge base to construct the Prompt generated by COT, and uses the Prompt as the input of the target AutoCoT model to generate a thinking chain from the target question to the SQL statement. The embodiment of the present invention proposes a Prompt adaptive method for structure-enhanced SQL statement generation to train the AutoCoT model to be trained, thereby generating a Prompt that actively adapts to the target SQL statement generation model, effectively ensuring the accuracy and effectiveness of the thinking chain in the prompt.

[0178] In a specific example, Figure 3As shown, a second sample set, that is, an intent recognition training sample set, is constructed, and a multi-level intent recognition model to be trained is trained based on the second sample set to obtain a target multi-level intent recognition model. The table name of the database table is identified to obtain the keywords in the table name of the database table, and the professional knowledge, industry jargon, table structure, field value range, etc. corresponding to the keywords in the table name of the database table are obtained; a knowledge base is constructed according to the professional knowledge, industry jargon, table structure, field value range, etc. corresponding to the keywords in the table name of the database table. The SQL generation capability of the target SQL statement generation model is evaluated, and a CoT prompt template is designed according to the weaknesses of the SQL generation capability of the target SQL statement generation model. The first sample set is iteratively trained to train the AutoCoT model to be trained to obtain the target AutoCoT model. The target question (the question that the user needs to query) is obtained, and the target query intent type set and related knowledge corresponding to the target question are obtained. The CoT example corresponding to the target question is constructed according to the target query intent type set corresponding to the target question and the CoT prompt template corresponding to the target SQL statement generation model. The input Prompt of the target AutoCoT model is generated according to the target question, the target query intent type set corresponding to the target question, the CoT example, and the related knowledge. The Prompt is input into the target AutoCoT model to obtain the target CoT corresponding to the target problem; the input Prompt of the NL2SQL model is generated according to the target problem, the target CoT corresponding to the target problem and related knowledge. The Prompt is input into the NL2SQL model to obtain the target SQL statement.

[0179] Optionally, iteratively training the to-be-trained thinking chain to determine the model through the first sample set includes:

[0180] Acquire a first sample set, wherein the first sample set includes: question samples and SQL statements corresponding to the question samples;

[0181] Obtain a query intention type set and related knowledge corresponding to the question samples in the first sample set;

[0182] Construct a thinking chain prompt example corresponding to the question sample according to the query intention type set corresponding to the question sample and the thinking chain prompt template corresponding to the target SQL statement generation model;

[0183] Inputting the question sample, the query intention type set corresponding to the question sample, the thinking chain prompt example and the relevant knowledge into the thinking chain determination model to be trained to obtain the thinking chain corresponding to the question sample;

[0184] Inputting the question sample, the thought chain corresponding to the question sample and related knowledge into the target SQL statement generation model to obtain a predicted SQL statement;

[0185] The parameters of the to-be-trained thinking chain determination model are trained according to a first function generated by the predicted SQL statement and the SQL statement corresponding to the problem sample to obtain a target thinking chain determination model.

[0186] Specifically, the method for constructing a thinking chain prompt example corresponding to the question sample according to the query intention type set corresponding to the question sample and the thinking chain prompt template corresponding to the target SQL statement generation model can be: obtaining a structural hierarchy list, querying the structural hierarchy list, and obtaining the hierarchy information corresponding to each query intention type in the query intention type set corresponding to the question sample; constructing a thinking chain prompt example corresponding to the question sample according to the query intention type set corresponding to the question sample, the hierarchy information corresponding to each query intention type in the query intention type set corresponding to the question sample, and the thinking chain prompt template corresponding to the target SQL statement generation model.

[0187] Optionally, training the parameters of the to-be-trained thinking chain determination model according to a first function generated by the predicted SQL statement and the SQL statement corresponding to the problem sample to obtain a target thinking chain determination model includes:

[0188] Determine a target score for the predicted SQL statement based on the predicted SQL statement and the SQL statement corresponding to the problem sample;

[0189] Determine the loss value of the SQL statement generated by the model according to the target score of the predicted SQL statement, the predicted SQL statement, and the thought chain to be trained before training;

[0190] The parameters of the thought chain determination model to be trained are trained according to the loss value to obtain the target thought chain determination model.

[0191] The loss value of the SQL statement generated by the model to be trained before training is determined by the SQL statement generated by the model to be trained before training, that is, the SQL statement generated by the model to be trained before adjusting the parameters. The SQL statement generated by the model to be trained before training is added here to prevent the generated SQL statement from deviating too much from the initial first model.

[0192] The embodiment of the present invention uses the hierarchical intent recognition results and SQL multi-dimensional evaluation scores as environmental feedback, the structure-enhanced AutoCo model as the Actor, and the target SQL statement generation model as the Critic. Through continuous alignment learning, a prompt that actively adapts to the target SQL statement generation model is generated, which effectively ensures the accuracy and effectiveness of the thinking chain in the prompt and improves the accuracy of generated SQL statements.

[0193] Optionally, determining a target score for the predicted SQL statement according to the predicted SQL statement and the SQL statement corresponding to the problem sample includes:

[0194] Execute the predicted SQL statement and the SQL statement corresponding to the problem sample;

[0195] Determine a first score according to the execution result corresponding to the predicted SQL statement and the execution result of the SQL statement corresponding to the problem sample;

[0196] Determining a second score according to the similarity between the predicted SQL statement and the SQL statement corresponding to the problem sample;

[0197] Determine a third score according to the query intention type set corresponding to the predicted SQL statement and the query intention type set corresponding to the SQL statement corresponding to the question sample;

[0198] A target score for the predicted SQL statement is determined according to the first score, the second score, and the third score.

[0199] Specifically, the method for determining the second score based on the similarity between the predicted SQL statement and the SQL statement corresponding to the problem sample can be: split the predicted SQL statement to obtain a first sub-statement set, split the SQL statement corresponding to the problem sample to obtain a second sub-statement set, obtain the similarity between each first sub-statement in the first sub-statement set and a second sub-statement of the same type as the first sub-statement in the second sub-statement set, and determine the second score based on the similarity between each first sub-statement in the first sub-statement set and a second sub-statement of the same type as the first sub-statement in the second sub-statement set.

[0200] Specifically, the method for determining the third score based on the query intention type set corresponding to the predicted SQL statement and the query intention type set corresponding to the SQL statement corresponding to the problem sample can be: obtaining the query intention type set corresponding to the predicted SQL statement and the query intention type set corresponding to the SQL statement corresponding to the problem sample; determining the third score based on the query intention type set corresponding to the predicted SQL statement and the query intention type set corresponding to the SQL statement corresponding to the problem sample.

[0201] Specifically, the method of determining the target score of the predicted SQL statement according to the first score, the second score, and the third score may be: determining the target score of the predicted SQL statement according to the first score, the second score, the third score, the weight corresponding to the first score, the weight corresponding to the second score, and the weight corresponding to the third score. For example, the first score, the second score, and the third score may be weightedly summed to obtain the target score of the predicted SQL statement.

[0202] In a specific example, an embodiment of the present invention proposes a Prompt adaptive method for structure-enhanced SQL generation to train the AutoCoT model to be trained. The hierarchical intent recognition results and SQL multi-dimensional evaluation scores are used as environmental feedback, the structure-enhanced AutoCoT model is used as the Actor, and the general SQL generation model is used as the Critic. Through continuous alignment learning, a prompt that actively adapts to the original model is generated, which effectively ensures the accuracy and effectiveness of the thinking chain in the prompt and improves the accuracy of generated SQL statements.

[0203] The specific training process is as follows: Figure 4 As shown:

[0204] 1. Use the question sample as the input of the multi-level intent recognition model to identify the user query intent and obtain the user query intent at multiple SQL structure logical levels.

[0205] 2. Based on the CoT prompt template library, according to the multi-level intent recognition results of the question sample, and in the hierarchical order from macro to micro of SQL grammar, a CoT example with SQL generation logic is constructed;

[0206] 3. According to methods such as semantic similarity comparison, relevant knowledge of the problem sample can be recalled from the corresponding professional knowledge base, and the relevant knowledge includes: Schema information of the association table.

[0207] 4. Based on the question sample, the query intent type set corresponding to the question sample, the CoT example corresponding to the question sample, and the relevant knowledge recalled from the professional knowledge base, construct the Prompt generated by COT, and use the Prompt as the input of the AutoCoT model to be trained to generate CoT.

[0208] 5. Based on the problem sample, the thinking chain of SQL generation and related knowledge, construct the prompt for SQL generation, and use the prompt as the input of the NL2SQL model to generate the SQL statement corresponding to the problem sample;

[0209] 6. Perform multi-dimensional evaluation on the generated SQL statements, and use the SQL evaluation results as the reward score of the general NL2SQL model as the Critic model. The embodiment of the present invention uses three-dimensional evaluation criteria: comprehensive execution accuracy index, structural matching index, and SQL intent recognition label matching index to evaluate the SQL statements generated in step 5. They are:

[0210] Execution accuracy evaluation (whether the execution results of the SQL statement corresponding to the problem sample in the database are consistent) to obtain the SQL execution accuracy score;

[0211] Structural matching degree evaluation (matching the SQL statement structure corresponding to the problem sample is the basis, focusing on keywords) to obtain the SQL matching degree score;

[0212] SQL intent recognition label matching evaluation (whether the number and type of intent labels of the SQL statement corresponding to the problem sample are consistent) to obtain the intent Figure 1 Consistency score;

[0213] The evaluation scores of the above three dimensions are weighted and summed according to certain weights to generate the final score of the SQL statement. In the embodiment of the present invention, the weight of each evaluation index can be set by a custom method or a variety of weight optimization methods such as the hierarchical analysis method.

[0214] 7. Use the SQL statement evaluation score as environmental feedback and adopt the reinforcement learning training method to train the AutoCoT model to be trained. Specifically, calculate the loss value based on the SQL statement evaluation score and update the gradient of the AutoCoT model to be trained.

[0215] S140, inputting the target problem, the target thinking chain corresponding to the target problem and related knowledge into the target SQL statement generation model to obtain the target SQL statement.

[0216] The target SQL statement generation model is a NL2SQL model, for example, the target SQL statement generation model is a Critic model.

[0217] In a specific example, the embodiment of the present invention takes a database query application scenario of a securities company in the financial industry as an implementation case. This embodiment selects the Transwarp Infinity LLM model as the SQL statement generation model. Assume that the Transwarp Infinity LLM model has poor SQL generation capabilities for four intent types: nested queries, conditional grouping, deduplication processing, and conditional expressions, and this embodiment has completed the training of the multi-level intent recognition model and the AutoCoT generation prompt model in advance. Assume that the securities company database contains several tables, such as: "Stock incentive enterprise information table", "Stock trading information table", "Enterprise registration information", etc., and each table has complete table structure information. Now the user enters a query question: "Find the name of the company with the largest number of incentives or the largest actual discount rate among private enterprises that implement restricted stock incentives."

[0218] Step 1: Build a knowledge base. Specifically including: "Stock incentive" related professional knowledge, industry jargon, table structure, field value range, etc. Among them, professional knowledge such as: "Stock incentive is a system that motivates and retains employees by issuing company stocks or equity to employees, and is linked to the company's performance and value growth." Industry jargon such as: "In actual business, if the exercise ratio is written as 1:1, it needs to be automatically converted to 50:50. If the ratio is written as 334, it needs to be automatically converted to 30:30:40, etc." Table structure information can be created using table creation statements. Field value ranges such as: "There are 5 types of incentive tools: first-class restricted stock, second-class restricted stock, stock options, stock appreciation rights, and employee stock ownership plans."

[0219] Step 2: Design a CoT prompt template. Since the SQL statement generation model in this embodiment has poor SQL generation capabilities for the four intent types of nested queries, conditional grouping, deduplication processing, and conditional expressions, this embodiment focuses on designing the thinking chain generation prompt templates for these four intent types, and other intent types can use general thinking chain generation prompt templates. Among them, indicator calculation, algebraic calculation and other types of intent types have no thinking chain generation prompt templates. This is because indicator calculation and algebraic calculation generally use background knowledge to achieve prompts, while other types of intent types are generally aimed at simple SQL statements with only select and from structures, which do not require thinking chain prompts.

[0220] Step 3: Use the trained multi-level intent recognition model to perform multi-level intent recognition for user query questions. The query question "Find the names of private enterprises that have implemented restricted stock incentives and have the largest number of incentive employees or the largest actual discount rate" has the following intent types: nested query, multi-condition query, and aggregate calculation.

[0221] Step 4: Combine the CoT prompt template designed in step 2 and construct a CoT prompt example from query question to SQL generation in the hierarchical order of query intent type from high to low.

[0222] In this embodiment, the level of nested query is level 1, multi-condition query is level 2, and aggregate calculation is level 3. According to the hierarchical order of query intent type from high to low, the constructed CoT prompt example is:

[0223] "Step 1: This is a nested query problem. The solution to the problem can be divided into two parts: subquery and main query. First, generate a subquery statement. To avoid repeated queries, you can use the [As] statement to alias the query results. Secondly, based on the query results of the subquery, perform relevant calculations to complete the main query. Step 2: This is a multi-condition query problem. If there is a [or] semantic word in the problem, use [or] to connect the conditions, otherwise use [and] to connect. Step 3: The problem also involves aggregation calculations. The query results need to be further aggregated. The corresponding operators are: ['maximum value': 'max', 'minimum value': 'min', 'count': 'count', 'sum': 'sum', 'average': 'avg']."

[0224] Step 5: Based on the knowledge base, recall the structure information and background knowledge of the associated tables for the query question. In this case, the database table associated with the query question "Which state-owned enterprises in Zhejiang Province have issued restricted stocks" is the "Stock Incentive Enterprise Information Table", which contains: "Stock Code", "Stock Name", "Draft Announcement Date", "Grant Date", "Grant Price", "Total Incentives (10,000 Shares / 10,000 Copies)", "Equity Incentive Return Rate (%)", "Industry", "Province", "City", "Nature of Company", "Program Progress", "Unlocking Ratio", "Discount Rate", "Cumulative Incentive Times", "Incentive Number", "Total Number of Employees", "Lock-up Period", "Listing Date" and other fields.

[0225] Step 6: Construct the prompt for AutoCoT model input based on user query questions, multi-level intent types, CoT prompt examples, table structure, and background knowledge.

[0226] For example, the AutoCoT model input prompt design in this case is as follows:

[0227] prompt=f"Now you are a COT thinking chain generator for NL2SQL application scenarios. You need to generate a COT thinking chain from query questions to SQL statements according to the data table information, query questions, background knowledge, and intent recognition type, following the COT prompt example. Given the data table information is: "{schemas}", the query question is: "{question}", the background knowledge is: "{knowledge}", and the intent recognition type is: "{intentions}", please follow the COT prompt example format: "{cot_temp}" and explain in detail how the query question generates SQL statements step by step. You are required to explain the SQL keywords and SQL functions used in each step clearly. At the same time, the entire explanation should be clear and simple, and the number of steps should not exceed 10."

[0228] Step 7: Generate CoT. Take the prompt in step 6 as input and use the trained AutoCoT model to generate a thought chain from query questions to SQL generation.

[0229] "This is a nested query problem. The solution to the problem can be divided into two parts: subquery and main query. First, generate a subquery statement. In order to avoid repeated queries, you can use the [As] statement to alias the query results. Secondly, based on the query results of the subquery, perform relevant calculations to complete the main query. Among them, the main query is a multi-condition query problem. If there are [or] semantic words in the problem, [or] is used to connect the conditions. The subqueries also involve maximum value calculations. The maximum value calculation needs to be performed on the subquery results. The corresponding operator is 'max'."

[0230] Step 8: Based on the user query question, CoT thinking chain, table structure and background knowledge, construct the prompt design of the NL2SQL model input. For example, the input prompt design of the NL2SQL model in this embodiment is as follows:

[0231] prompt=f"Now you are a SQL generator. The known data table structure is: {schemas}, the query question to be performed is: {question}, and the background knowledge is: "{knowledge}". Please generate an SQL statement that meets the SQLite syntax according to the SQL generation thinking chain: {cot}. The generated SQL statement is:"

[0232] Step 9: Generate the target SQL statement. Take the prompt in step 8 as input and use the NL2SQL model to generate the SQL statement corresponding to the query question. In this embodiment, the SQL statement generated by the query question is:

[0233] "SELECT s_info_name FROM t_inc_esop_info WHERE wind_sec_code='Private enterprise'AND script_type='First restricted stock'AND person_count=(SELECT MAX(person_count)FROM t_inc_esop_info)OR discount_real=(SELECT MAX(discount_real)FROMt_inc_esop_info);".

[0234] The technical solution of this embodiment is to obtain a target query intention type set and related knowledge corresponding to the target question; construct a thinking chain prompt example corresponding to the target question according to the target query intention type set corresponding to the target question and the thinking chain prompt template corresponding to the target SQL statement generation model; input the target question, the target query intention type set corresponding to the target question, the thinking chain prompt example and related knowledge into the target thinking chain determination model to obtain the target thinking chain corresponding to the target question, wherein the target thinking chain determination model is obtained by iteratively training the thinking chain determination model to be trained with the first sample set; input the target question, the target thinking chain corresponding to the target question and related knowledge into the target SQL statement generation model to obtain the target SQL statement, which can improve the accuracy of SQL statement generation.

[0235] Embodiment 2

[0236] Figure 5 This is a schematic diagram of the structure of a SQL statement generation device provided by an embodiment of the present invention. This embodiment is applicable to the situation of SQL statement generation. The device can be implemented in software and / or hardware. The device can be integrated in any device that provides SQL statement generation function, such as Figure 5 As shown, the SQL statement generating device specifically includes: a first acquiring module 510, a thinking chain prompting example building module 520, a target thinking chain determining module 530 and a target SQL statement generating module 540.

[0237] The first acquisition module is used to acquire a target query intention type set and related knowledge corresponding to the target question;

[0238] A thinking chain prompt example construction module is used to construct a thinking chain prompt example corresponding to the target question according to a target query intention type set corresponding to the target question and a thinking chain prompt template corresponding to a target SQL statement generation model;

[0239] a target thinking chain determination module, used for inputting the target question, the target query intention type set corresponding to the target question, a thinking chain prompt example and related knowledge into a target thinking chain determination model, and obtaining a target thinking chain corresponding to the target question, wherein the target thinking chain determination model is obtained by iteratively training the to-be-trained thinking chain determination model with the first sample set;

[0240] The target SQL statement generation module is used to input the target problem, the target thinking chain corresponding to the target problem and related knowledge into the target SQL statement generation model to obtain the target SQL statement.

[0241] The above-mentioned product can execute the method provided by any embodiment of the present invention, and has the corresponding functional modules and beneficial effects of the execution method.

[0242] Embodiment 3

[0243] Figure 6 A schematic diagram of the structure of an electronic device 10 that can be used to implement an embodiment of the present invention is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices (such as helmets, glasses, watches, etc.) and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present invention described and / or required herein.

[0244] like Figure 6 As shown, the electronic device 10 includes at least one processor 11, and a memory connected to the at least one processor 11, such as a read-only memory (ROM) 12, a random access memory (RAM) 13, etc., wherein the memory stores a computer program that can be executed by at least one processor, and the processor 11 can perform various appropriate actions and processes according to the computer program stored in the read-only memory (ROM) 12 or the computer program loaded from the storage unit 18 to the random access memory (RAM) 13. In the RAM 13, various programs and data required for the operation of the electronic device 10 can also be stored. The processor 11, the ROM 12, and the RAM 13 are connected to each other through a bus 14. An input / output (I / O) interface 15 is also connected to the bus 14.

[0245] A number of components in the electronic device 10 are connected to the I / O interface 15, including: an input unit 16, such as a keyboard, a mouse, etc.; an output unit 17, such as various types of displays, speakers, etc.; a storage unit 18, such as a disk, an optical disk, etc.; and a communication unit 19, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 19 allows the electronic device 10 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.

[0246] The processor 11 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the processor 11 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special artificial intelligence (AI) computing chips, various processors running machine learning model algorithms, a digital signal processor (DSP), and any appropriate processor, controller, microcontroller, etc. The processor 11 executes the various methods and processes described above, such as a SQL statement generation method.

[0247] In some embodiments, the SQL statement generation method may be implemented as a computer program, which is tangibly contained in a computer-readable storage medium, such as a storage unit 18. In some embodiments, part or all of the computer program may be loaded and / or installed on the electronic device 10 via the ROM 12 and / or the communication unit 19. When the computer program is loaded into the RAM 13 and executed by the processor 11, one or more steps of the SQL statement generation method described above may be performed. Alternatively, in other embodiments, the processor 11 may be configured to execute the SQL statement generation method in any other appropriate manner (e.g., by means of firmware).

[0248] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), load programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.

[0249] Computer programs for implementing the methods of the present invention may be written in any combination of one or more programming languages. These computer programs may be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, so that when the computer program is executed by the processor, the functions / operations specified in the flow chart and / or block diagram are implemented. The computer program may be executed entirely on the machine, partially on the machine, partially on the machine and partially on a remote machine as a stand-alone software package, or entirely on a remote machine or server.

[0250] In the context of the present invention, a computer-readable storage medium may be a tangible medium that may contain or store a computer program for use by or in combination with an instruction execution system, device or equipment. A computer-readable storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices or equipment, or any suitable combination of the foregoing. Alternatively, a computer-readable storage medium may be a machine-readable signal medium. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.

[0251] To provide interaction with a user, the systems and techniques described herein may be implemented on an electronic device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and a pointing device (e.g., a mouse or trackball) through which the user can provide input to the electronic device. Other types of devices may also be used to provide interaction with the user; for example, the feedback provided to the user may be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user may be received in any form (including acoustic input, voice input, or tactile input).

[0252] The systems and techniques described herein may be implemented in a computing system that includes backend components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes frontend components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such backend components, middleware components, or frontend components. The components of the system may be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), a blockchain network, and the Internet.

[0253] A computing system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The client and server relationship is generated by computer programs running on the corresponding computers and having a client-server relationship with each other. The server may be a cloud server, also known as a cloud computing server or cloud host, which is a host product in the cloud computing service system to solve the defects of difficult management and weak business scalability in traditional physical hosts and VPS services.

[0254] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps described in the present invention can be executed in parallel, sequentially or in different orders, as long as the desired results of the technical solution of the present invention can be achieved, and this document does not limit this.

[0255] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.

Claims

1. A method for generating a SQL statement, characterized in that: include: Obtain the target query intent type set and related knowledge corresponding to the target question; Construct a thinking chain prompt example corresponding to the target question according to the target query intention type set corresponding to the target question and the thinking chain prompt template corresponding to the target SQL statement generation model; Input the target question, the target query intention type set corresponding to the target question, the thinking chain prompt example and the relevant knowledge into the target thinking chain determination model to obtain the target thinking chain corresponding to the target question, wherein the target thinking chain determination model is obtained by iteratively training the to-be-trained thinking chain determination model with the first sample set; Inputting the target problem, the target thinking chain corresponding to the target problem, and related knowledge into the target SQL statement generation model to obtain a target SQL statement; The step of constructing a thinking chain prompt example corresponding to the target question according to the target query intention type set corresponding to the target question and the thinking chain prompt template corresponding to the target SQL statement generation model includes: Obtaining hierarchical information corresponding to each query intent type in the target query intent type set corresponding to the target question; Construct a thinking chain prompt example corresponding to the target question according to the target query intention type set corresponding to the target question, the hierarchical information corresponding to each query intention type in the target query intention type set corresponding to the target question, and the thinking chain prompt template corresponding to the target SQL statement generation model; Wherein, iteratively training the to-be-trained thinking chain to determine the model through the first sample set includes: Acquire a first sample set, wherein the first sample set includes: question samples and SQL statements corresponding to the question samples; Obtain a query intention type set and related knowledge corresponding to the question samples in the first sample set; Construct a thinking chain prompt example corresponding to the question sample according to the query intention type set corresponding to the question sample and the thinking chain prompt template corresponding to the target SQL statement generation model; Inputting the question sample, the query intention type set corresponding to the question sample, the thinking chain prompt example and the relevant knowledge into the thinking chain determination model to be trained to obtain the thinking chain corresponding to the question sample; Inputting the question sample, the thought chain corresponding to the question sample and related knowledge into the target SQL statement generation model to obtain a predicted SQL statement; The parameters of the to-be-trained thinking chain determination model are trained according to a first function generated by the predicted SQL statement and the SQL statement corresponding to the problem sample to obtain a target thinking chain determination model.

2. The method according to claim 1, characterized in that: Obtain the target query intent type set and related knowledge corresponding to the target question, including: Inputting a target question into a target multi-level intent recognition model to obtain a target query intent type set, wherein the target multi-level intent recognition model is obtained by iteratively training a multi-level intent recognition model to be trained with a second sample set, wherein the second sample set includes: a question sample and a query intent type set corresponding to the question sample; Get the knowledge base corresponding to the database table; The knowledge base is queried according to the target question to obtain association table structure information and related knowledge corresponding to the target question.

3. The method according to claim 1, characterized in that The first function generated by the predicted SQL statement and the SQL statement corresponding to the problem sample is used to train the parameters of the to-be-trained thinking chain determination model to obtain a target thinking chain determination model, including: Determine a target score for the predicted SQL statement based on the predicted SQL statement and the SQL statement corresponding to the problem sample; Determine the loss value of the SQL statement generated by the model according to the target score of the predicted SQL statement, the predicted SQL statement, and the thought chain to be trained before training; The parameters of the thought chain determination model to be trained are trained according to the loss value to obtain the target thought chain determination model.

4. The method according to claim 3, characterized in that Determining a target score for the predicted SQL statement according to the predicted SQL statement and the SQL statement corresponding to the problem sample includes: Execute the predicted SQL statement and the SQL statement corresponding to the problem sample; Determine a first score according to the execution result corresponding to the predicted SQL statement and the execution result of the SQL statement corresponding to the problem sample; Determining a second score according to the similarity between the predicted SQL statement and the SQL statement corresponding to the problem sample; Determine a third score according to the query intention type set corresponding to the predicted SQL statement and the query intention type set corresponding to the SQL statement corresponding to the question sample; A target score for the predicted SQL statement is determined according to the first score, the second score, and the third score.

5. The method according to claim 1, characterized in that Obtaining hierarchical information corresponding to each query intent type in the target query intent type set corresponding to the target question, including: Get the list of structure levels; Query the structure hierarchy list to obtain the hierarchy information corresponding to each query intent type in the target query intent type set corresponding to the target question.

6. The method according to claim 1, characterized in that Also includes: Get the query intent type library; Obtain the SQL statement generation accuracy corresponding to each query intent type in the query intent type library generated based on the target SQL statement generation model; Generate a thinking chain prompt template corresponding to the target SQL statement generation model according to the target query intent type in the query intent type library whose SQL statement generation accuracy is less than the accuracy threshold.

7. The method according to claim 2, characterized in that The multi-level intent recognition model to be trained is iteratively trained through the second sample set, including: Obtain a query intention type set corresponding to each question sample in the first sample set; Generate a second sample set according to the question samples in the first sample set and the query intention type set corresponding to each question sample; Inputting the question samples in the second sample set into the multi-level intent recognition model to be trained to obtain a set of predicted query intent types; Training the parameters of the multi-level intent recognition model to be trained according to the objective function generated by the predicted query intent type set and the query intent type set corresponding to the question sample; Return to execute the operation of inputting the question samples in the second sample set into the multi-level intent recognition model to be trained to obtain a set of predicted query intent types, until the target multi-level intent recognition model is obtained.

8. A SQL statement generating device, characterized in that: include: The first acquisition module is used to obtain a target query intention type set and related knowledge corresponding to the target question; A thinking chain prompt example construction module is used to construct a thinking chain prompt example corresponding to the target question according to a target query intention type set corresponding to the target question and a thinking chain prompt template corresponding to a target SQL statement generation model; a target thinking chain determination module, used for inputting the target question, the target query intention type set corresponding to the target question, a thinking chain prompt example and related knowledge into a target thinking chain determination model, and obtaining a target thinking chain corresponding to the target question, wherein the target thinking chain determination model is obtained by iteratively training the to-be-trained thinking chain determination model with the first sample set; A target SQL statement generation module, used for inputting the target problem, the target thinking chain corresponding to the target problem and related knowledge into the target SQL statement generation model to obtain a target SQL statement; The thought chain prompt example building module is configured to: Obtaining hierarchical information corresponding to each query intent type in the target query intent type set corresponding to the target question; Construct a thinking chain prompt example corresponding to the target question according to the target query intention type set corresponding to the target question, the hierarchical information corresponding to each query intention type in the target query intention type set corresponding to the target question, and the thinking chain prompt template corresponding to the target SQL statement generation model; Wherein, iteratively training the to-be-trained thinking chain to determine the model through the first sample set includes: Acquire a first sample set, wherein the first sample set includes: question samples and SQL statements corresponding to the question samples; Obtain a query intention type set and related knowledge corresponding to the question samples in the first sample set; Construct a thinking chain prompt example corresponding to the question sample according to the query intention type set corresponding to the question sample and the thinking chain prompt template corresponding to the target SQL statement generation model; Inputting the question sample, the query intention type set corresponding to the question sample, the thinking chain prompt example and the relevant knowledge into the thinking chain determination model to be trained to obtain the thinking chain corresponding to the question sample; Inputting the question sample, the thought chain corresponding to the question sample and related knowledge into the target SQL statement generation model to obtain a predicted SQL statement; The parameters of the to-be-trained thinking chain determination model are trained according to a first function generated by the predicted SQL statement and the SQL statement corresponding to the problem sample to obtain a target thinking chain determination model.

9. An electronic device, characterized in that: The electronic device comprises: at least one processor; and a memory communicatively connected to the at least one processor; wherein, The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the SQL statement generation method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement the SQL statement generation method according to any one of claims 1 to 7 when executed.