SQL statement generation method and apparatus, device, and storage medium

By constructing a thinking chain prompt example and iterative training model, the problem of inaccurate query intention capture in NL2SQL technology is solved, and the accuracy of SQL statement generation is improved.

WO2025161708A1PCT designated stage Publication Date: 2025-08-07TRANSWARP TECHNOLOGY (SHANGHAI) CO LTD +1

Patent Information

Application Number
PCT/CN2024/138529
Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
Priority Date
2024-01-31
Filing Date
2024-12-11
Publication Date
2025-08-07

AI Technical Summary

Technical Problem

When handling complex natural language queries, existing NL2SQL technology is difficult to accurately capture the user's query intent and generate SQL statements that meet user needs, resulting in low query accuracy.

Method used

By obtaining the query intent type set and related knowledge of the target problem, construct a thinking chain prompt example, use iteratively trained thinking chain to determine the model and SQL statement generation model, and generate accurate SQL statements.

Benefits of technology

Improves the accuracy of SQL statement generation, can better understand the user's query intention and generate SQL statements that meet the needs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN2024138529_07082025_PF_FP_ABST
    Figure CN2024138529_07082025_PF_FP_ABST
Patent Text Reader

Abstract

Disclosed in the present invention are an SQL statement generation method and apparatus, a device, and a storage medium. The method comprises: obtaining a target query intention type set and related knowledge corresponding to a target question; on the basis of the target query intention type set corresponding to the target question and a chain-of-thought prompt template corresponding to a target SQL statement generation model, constructing a chain-of-thought prompt example corresponding to the target question; inputting the target question, and the target query intention type set, chain-of-thought prompt example and related knowledge corresponding to the target question into a target chain-of-thought determination model to obtain a target chain-of-thought corresponding to the target question, wherein the target chain-of-thought determination model is obtained by using a first sample set to iteratively train a chain-of-through determination model to be trained; and inputting the target question, and the target chain-of-thought and related knowledge corresponding to the target question 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

SQL statement generation method, device, equipment and storage medium Technical Field

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

[0002] Users of a database management system need to have a very 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 human-computer interaction method, namely, using NL2SQL (Natural Language To SQL) technology to convert business personnel's natural language 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 applications, some natural language queries may involve multiple entities, relations, and conditions, and the model may not accurately capture all semantic details and contextual information, making it unable to understand the user's query intent. This can cause the generated SQL statements to be inconsistent with user needs, reducing query accuracy. Furthermore, the diversity of query types and the complexity of SQL syntax make it difficult for the model to generate a fully structured SQL statement in one go 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 an SQL statement, which can improve the accuracy of generating an SQL statement.

[0006] According to one aspect of the present invention, a method for generating an SQL statement is provided, 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] Inputting the target question, the target query intent type set corresponding to the target question, a thought chain prompt example, and relevant knowledge into a target thought chain determination model to obtain a target thought chain corresponding to the target question, wherein the target thought chain determination model is obtained by iteratively training a to-be-trained thought chain determination model with a 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 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 to-be-trained multi-level intent recognition model 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] Furthermore, iteratively training the to-be-trained thinking chain through the first sample set to determine the model includes:

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

[0017] Obtaining 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 thought chain prompt example and the relevant knowledge into the thought chain determination model to be trained to obtain the thought 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 question sample to obtain a target thinking chain determination model.

[0022] Furthermore, 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 question 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 question sample;

[0024] Determine the loss value of the SQL statement generated by the model based on the target score of the predicted SQL statement, the predicted SQL statement, and the to-be-trained thinking chain 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] Furthermore, determining a target score for the predicted SQL statement based on the predicted SQL statement and the SQL statement corresponding to the question sample includes:

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

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

[0029] Determining a second score based on the similarity between the predicted SQL statement and the SQL statement corresponding to the question sample;

[0030] Determine a 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 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] Furthermore, 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 based on 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] Furthermore, obtaining hierarchical information corresponding to each query intent type in the target query intent type set corresponding to the target question includes:

[0036] Get the list of structure levels;

[0037] Query the structure hierarchy list to obtain the hierarchical 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] Furthermore, the multi-level intent recognition model to be trained is iteratively trained using 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 based on 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 device for generating an SQL statement, the device comprising:

[0049] The first acquisition module is used to obtain the target query intent 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 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;

[0051] a target thinking chain determination module, configured to input the target question, a target query intent type set corresponding to the target question, a thinking chain prompt example, and relevant 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;

[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 configured to:

[0054] 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 to-be-trained multi-level intent recognition model 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 to:

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

[0059] Obtaining 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 thought chain prompt example and the relevant knowledge into the thought chain determination model to be trained to obtain the thought 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 question sample to obtain a target thinking chain determination model.

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

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

[0066] Determine the loss value of the SQL statement generated by the model based on the target score of the predicted SQL statement, the predicted SQL statement, and the to-be-trained thinking chain 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 to:

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

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

[0071] Determining a second score based on the similarity between the predicted SQL statement and the SQL statement corresponding to the question sample;

[0072] Determine a 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 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 to:

[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 based on 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 to:

[0078] Get the list of structure levels;

[0079] Query the structure hierarchy list to obtain the hierarchical 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 obtain 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 intention type in the query intention type library whose SQL statement generation accuracy is less than the accuracy threshold.

[0084] Furthermore, the first acquisition module is specifically configured to:

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

[0086] Generate a second sample set based on 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, 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. 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 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 according to any embodiment of the present invention when executed.

[0095] An embodiment of the present invention obtains a target query intention type set and related knowledge corresponding to a target problem; constructs a thinking chain prompt example corresponding to the target problem according to the target query intention type set corresponding to the target problem and a thinking chain prompt template corresponding to a target SQL statement generation model; inputs the target problem, the target query intention type set corresponding to the target problem, 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 problem, wherein the target thinking chain determination model is obtained by iteratively training a thinking chain determination model to be trained through a first sample set; inputs 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, thereby improving the accuracy of SQL statement generation.

[0096] It should be understood that the content described in this section is not intended to identify the key or important features of the embodiments of the present invention, nor is it intended to limit the scope of the present invention. Other features of the present invention will become readily 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 following briefly introduces the drawings required for use in the embodiments. It should be understood that the following drawings only illustrate certain embodiments of the present invention and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other relevant drawings can be obtained based on these drawings without paying any creative work.

[0098] FIG1 is a flow chart of a method for generating an SQL statement according to an embodiment of the present invention;

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

[0100] FIG3 is a flowchart of another method for generating SQL statements in an embodiment of the present invention;

[0101] FIG4 is a flowchart of the AutoCoT model training in an embodiment of the present invention;

[0102] FIG5 is a schematic structural diagram of an SQL statement generating device according to an embodiment of the present invention;

[0103] FIG6 is a schematic structural diagram of an electronic device according to an embodiment of the present invention. DETAILED DESCRIPTION

[0104] In order to enable those skilled in the art to better understand the solutions of the present invention, the technical solutions 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 embodiments described 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 making creative efforts should fall within the scope of protection of the present invention.

[0105] It should be noted that the terms "first", "second", etc. in the description 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 numbers 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 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 various embodiments of this disclosure, the type, scope of use, usage scenarios, etc. of the personal information involved in this 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] Example 1

[0108] FIG1 is a flowchart of a method for generating SQL statements provided by an embodiment of the present invention. This embodiment is applicable to the generation of SQL statements. The method can be executed by an SQL statement generation device in an embodiment of the present invention. The device can be implemented in software and / or hardware. As shown in FIG1 , the method specifically includes 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, a method for obtaining a target query intent type set corresponding to a target question may be: performing intent recognition on the target question to obtain a target query intent type set corresponding to the target question. Another method for obtaining a target query intent type set corresponding to a target question may be: inputting the target question into a target multi-level intent recognition model to obtain a target query intent type set.

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

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

[0114] 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 to-be-trained multi-level intent recognition model 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 can 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 this embodiment of the present invention.

[0121] Specifically, the method for 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; and constructing a knowledge base based on 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 using 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 based on 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 the query intention type set corresponding to each question sample in the first sample set may be: determining the query intention type set corresponding to each question sample based on the 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 using 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 in accordance with a 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 target SQL statement generation model can be constructed by testing the target SQL statement generation model, and the test results can be used to determine the corresponding thought chain prompt template. For example, the target SQL statement generation model can be tested to determine the corresponding thought chain prompt template. For example, the target SQL statement generation model can be tested to determine the corresponding thought chain prompt template.

[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 multi-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 (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 based on 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-establishing a thinking chain prompt template corresponding to the target SQL statement generation model, and constructing a thinking chain prompt example corresponding to the target problem based on 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] This embodiment of the present invention evaluates the SQL generation capabilities of a target SQL generation model, providing a reference for subsequent targeted improvements to that model's capabilities. Based on the evaluation results, a set of thought chain prompt templates tailored to the target SQL generation model is designed for different models. Based on the CoT prompt template and the multi-level intent recognition results for the target question, this embodiment of the present invention constructs a CoT example with SQL generation logic, following a macro-to-micro hierarchy of SQL syntax.

[0143] Optionally, 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:

[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 based on 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 embodiment of the present invention addresses the weaknesses of the SQL generation capability of the SQL statement generation model and generates prompt examples, which can guide the model to generate SQL statements with correct structures.

[0147] For the case of multiple intent recognition tags, an embodiment of the present invention combines the thought chain generation prompts corresponding to the multiple intent recognition tags in descending order of the intent recognition tag hierarchy to form a thought 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 created in advance 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: establishing a test sample set based on the query intent type library, wherein the test sample set includes: test questions, SQL statements corresponding to the test questions, and a set of query intent types corresponding to the test questions; inputting all test questions in the test set into the target SQL statement generation model in sequence to obtain a predicted SQL statement corresponding to each test question in the test set, and determining the SQL statement generation accuracy corresponding to each query intent type in the query intent type library based on the predicted SQL statement corresponding to each test question in the test set, the SQL statement corresponding to each test question, and the set of query intent types 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, and 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 questions X and Y are both query intent type A, the query intent types corresponding to test questions Z and W are both query intent type B, the query intent types corresponding to test questions S and F are both query intent type C, and the query intent types corresponding to test questions G and 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, if 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), if 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 if 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. 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's important to note that you can also build a corpus pool for thought chain generation prompts and evaluate and screen all the corpora in the pool. Prune low-quality or logically mixed corpora to retain only high-quality ones. High-quality thought chain generation prompt templates can help improve the accuracy of model SQL generation.

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

[0156] Nested query: "This is a nested query problem. The solution can be divided into two parts: subquery and main query. First, generate the subquery statement. To avoid repeated queries, use the [As] statement to alias the query results. Second, perform relevant calculations based on the query results of the subquery to complete the main query." Conditional grouping: "This is a conditional grouping problem. Based on the grouped objects, use the [Having] keyword to set search conditions for the groups 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] Multi-condition query: "This is a multi-condition query question. If the question contains the semantic word [or], the conditions are connected using [or], otherwise they are connected using [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 data item."

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

[0165] Aggregate calculation: "The problem also involves aggregate calculations. You need to perform corresponding aggregate calculations on the query results. The corresponding operators are: ['Maximum': 'max', 'Minimum': 'min', 'Count': 'count', 'Sum': 'sum', 'Average': 'avg']."

[0166] Indicator calculation: depends on background knowledge.

[0167] Algebraic calculations: Depends on background knowledge.

[0168] Other types: None.

[0169] In another specific example, the SQL generation model's SQL generation capabilities for all query intent types are first evaluated, and the weak points of the SQL generation capabilities of the SQL generation model are obtained, providing a reference for subsequent targeted improvement of the SQL generation capabilities of the SQL generation model. Secondly, based on the evaluation results of the SQL generation capabilities 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 capabilities of the SQL statement generation model, the designed thought chain generation prompt example templates are 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, based on the CoT prompt template library and the results of multi-level intent recognition, constructs a CoT example with SQL generation logic in a hierarchical order from the macro-architecture to the micro-structure of the SQL grammar.

[0170] By evaluating the SQL generation capability of the target SQL statement generation model in advance, the embodiment of the present invention can understand the weaknesses of the SQL generation capability of the target SQL statement generation model, providing 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 hierarchical 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, as shown in FIG2 . In an embodiment of the present invention, the hierarchy from the macroscopic structure to the microscopic structure of the SQL grammar is divided into four levels: the subquery level, the clause level, the expression level, and the qualifier level. That is, the subquery level is the first level, the clause level is the second level, the expression level is the third level, and the qualifier level is the fourth level. This is more in line with the generation logic of SQL. The subquery level includes: non-nested queries and nested queries. Nested queries include: subqueries and main queries. The clause level includes: conditional queries, grouping queries, sorting queries, and other queries. Conditional queries include: single-condition queries and multi-condition queries. Grouping queries include: simple grouping and conditional grouping. Sorting queries include: simple sorting, maximum value sorting, and top N sorting. The expression level includes: conditional expressions and indicator calculations. The qualifier level 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 hierarchy of the query intent type is level 1.

[0176] S130, 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.

[0177] This embodiment of the present invention utilizes a target question, a set of target query intent types corresponding to the target question, CoT examples with SQL generation logic, and relevant knowledge retrieved from a knowledge base to construct a CoT-generated prompt. This prompt is then used as input to the target AutoCoT model to generate a thought chain from the target question to the SQL statement. This 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 thought chain in the prompt.

[0178] In a specific example, as shown in Figure 3, a second sample set, i.e., an intent recognition training sample set, is constructed. 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. Database table names are identified to obtain keywords in the database table names. Professional knowledge, industry jargon, table structure, field value ranges, etc. corresponding to the keywords in the database table names are obtained. A knowledge base is constructed based on the professional knowledge, industry jargon, table structure, field value ranges, etc. corresponding to the keywords in the database table names. The SQL generation capability of the target SQL statement generation model is evaluated, and a CoT prompt template is designed based on weaknesses in the target SQL statement generation model's SQL generation capability. The AutoCoT model to be trained is iteratively trained using the first sample set to obtain a target AutoCoT model. A target question (the question the user needs to query) is obtained, along with a target query intent type set and related knowledge corresponding to the target question. A CoT example corresponding to the target question is constructed based on the target query intent type set and the CoT prompt template corresponding to the target SQL statement generation model. An input prompt for the target AutoCoT model is generated based on the target question, the target query intent type set corresponding to the target question, the CoT example, and related knowledge. The Prompt is then 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 based on the target problem, the target CoT corresponding to the target problem, and related knowledge. The Prompt is then 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] Obtaining a first sample set, wherein the first sample set includes: question samples and SQL statements corresponding to the question samples;

[0181] Obtaining 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 thought chain prompt example and the relevant knowledge into the thought chain determination model to be trained to obtain the thought 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 question sample to obtain a target thinking chain determination model.

[0186] Specifically, the method of constructing a thinking chain prompt example corresponding to the problem sample based on the query intention type set corresponding to the problem 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 problem sample; constructing a thinking chain prompt example corresponding to the problem sample based on the query intention type set corresponding to the problem sample, the hierarchy information corresponding to each query intention type in the query intention type set corresponding to the problem 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 question 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 question sample;

[0189] Determine the loss value of the SQL statement generated by the model based on the target score of the predicted SQL statement, the predicted SQL statement, and the to-be-trained thinking chain 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 pre-trained model for determining the chain of thought to be trained is determined by the SQL statement generated by the untrained model for determining the chain of thought to be trained, that is, the SQL statement generated by the model for determining the chain of thought to be trained before the parameters are adjusted. The SQL statement generated by the pre-trained model for determining the chain of thought to be trained 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, effectively ensuring the accuracy and effectiveness of the thinking chain in the prompt and improving the accuracy of generated SQL statements.

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

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

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

[0196] Determining a second score based on the similarity between the predicted SQL statement and the SQL statement corresponding to the question sample;

[0197] Determine a 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 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: splitting the predicted SQL statement to obtain a first sub-statement set, splitting the SQL statement corresponding to the problem sample to obtain a second sub-statement set, obtaining the similarity between each first sub-statement in the first sub-statement set and the second sub-statement of the same type as the first sub-statement in the second sub-statement set, and determining the second score based on the similarity between each first sub-statement in the first sub-statement set and the 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 target score for the predicted SQL statement may be determined based on the first score, the second score, and the third score by: determining the target score for the predicted SQL statement based on the first score, the second score, the third score, a weight corresponding to the first score, a weight corresponding to the second score, and a weight corresponding to the third score. For example, the target score for the predicted SQL statement may be obtained by taking a weighted sum of the first score, the second score, and the third score.

[0202] In a specific example, an embodiment of the present invention proposes a structure-enhanced SQL generation prompt adaptive method to train the AutoCoT model to be trained. This method uses hierarchical intent recognition results and SQL multi-dimensional evaluation scores as environmental feedback, with the structure-enhanced AutoCoT model as the Actor and the general SQL generation model as the Critic. Through continuous alignment learning, a prompt that actively adapts to the original model is generated, effectively ensuring the accuracy and effectiveness of the thought chain in the prompt and improving the accuracy of generated SQL statements. The specific training process is shown in Figure 4:

[0203] 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.

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

[0205] 3. Based on 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.

[0206] 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.

[0207] 5. Based on the question sample, the SQL generation thought chain, and related knowledge, a prompt for SQL generation is constructed. This prompt is used as the input of the NL2SQL model to generate the SQL statement corresponding to the question sample.

[0208] 6. Perform a 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. This embodiment of the present invention uses three evaluation criteria: comprehensive execution accuracy index, structural matching index, and SQL intent identification label matching index to evaluate the SQL statements generated in step 5. They are:

[0209] Execution accuracy assessment (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;

[0210] 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;

[0211] SQL intent recognition label matching evaluation (whether the number and type of intent labels of the SQL statement corresponding to the question sample are consistent) to obtain the intent consistency score;

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

[0213] 7. Using the SQL statement evaluation scores as environmental feedback, a reinforcement learning method is used to train the AutoCoT model to be trained. Specifically, the loss value is calculated based on the SQL statement evaluation scores, and the gradient of the AutoCoT model to be trained is updated.

[0214] 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 a target SQL statement.

[0215] The target SQL statement generation model is an NL2SQL model. For example, the target SQL statement generation model may be a Critic model.

[0216] In a specific example, the embodiment of the present invention takes the 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. It is assumed that the Transwarp Infinity LLM model has poor SQL generation capabilities for the four intent types of 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. It is assumed 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 private enterprise that implements restricted stock incentives with the largest number of incentives or the largest actual discount rate."

[0217] Step 1: Build a knowledge base. This includes specialized knowledge related to "stock incentives," industry jargon, table structures, and field value ranges. Expertise includes information such as "Stock incentives are a system that motivates and retains employees by issuing company stock or equity to them, and is tied to the company's performance and value growth." Industry jargon includes information such as "In actual business operations, 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 obtained using table creation statements. Field value ranges include information such as "There are five types of incentive tools: Class I restricted stock, Class II restricted stock, stock options, stock appreciation rights, and employee stock ownership plans."

[0218] 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 do not have a thinking chain generation prompt template. 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.

[0219] Step 3: Use the trained multi-level intent recognition model to perform multi-level intent recognition for the user's query. The query "Find the names of private companies implementing restricted stock incentives with the largest number of employees or the highest actual discount rates" has the following intent types: nested query, multi-condition query, and aggregate calculation.

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

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

[0222] "Step 1: This is a nested query problem. Solving the problem can be divided into two parts: subqueries and main queries. First, generate a subquery statement. To avoid repeated queries, use the [As] statement to alias the query results. Second, perform relevant calculations based on the subquery results to complete the main query. Step 2: This is a multi-condition query problem. If the question contains the semantic word [or], use [or] to connect the conditions; otherwise, use [and] to connect. Step 3: This problem also involves aggregation calculations. The query results need to be further aggregated. The corresponding operators are: ['Maximum': 'max', 'Minimum': 'min', 'Count': 'count', 'Sum': 'sum', 'Average': 'avg']."

[0223] Step 5: Based on the knowledge base, recall the associated table structure information and background knowledge 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 fields such as "Stock Code", "Stock Name", "Draft Announcement Date", "Grant Date", "Grant Price", "Total Incentives (10,000 shares / 10,000 shares)", "Equity Incentive Return Rate (%)", "Industry", "Province", "City", "Company Nature", "Program Progress", "Unlocking Ratio", "Discount Rate", "Cumulative Incentive Times", "Number of Incentives", "Total Number of Employees", "Lock-up Period", and "Listing Date".

[0224] Step 6: Construct the prompt for the AutoCoT model input based on the user query question, multi-level intent type, CoT prompt example, table structure, and background knowledge.

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

[0226] prompt=f"You are now a COT thinking chain generator for the NL2SQL application scenario. Based on the data table information, query question, background knowledge, and intent recognition type, you need to follow the COT prompt example to generate a COT thinking chain from the query question to the SQL statement. Given the data table information: "{schemas}", the query question: "{question}", the background knowledge: "{knowledge}", and the intent recognition type: "{intentions}", please follow the COT prompt example format: "{cot_temp}" and explain in detail how to generate the SQL statement for this query step by step. Clearly explain the SQL keywords and SQL functions used in each step. The entire explanation must be clear and concise, and the number of steps should not exceed 10."

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

[0228] This is a nested query problem, and the solution can be divided into two parts: subqueries and main queries. 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. Among them, the main query is a multi-condition query problem. If there is an [or] semantic word in the problem, the conditions are connected using [or]. The subqueries also involve maximum value calculations, and the maximum value calculation needs to be performed on the subquery results. The corresponding operator is 'max'.

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

[0230] prompt=f"You are now a SQL generator. The data table structure is known as {schemas}, the query question is {question}, and the background knowledge is "{knowledge}". Please generate an SQL statement that meets the SQLite syntax based on the SQL generation chain of thought: {cot}. The generated SQL statement is:"

[0231] Step 9: Generate the target SQL statement. Use the prompt from 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:

[0232] "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) FROM t_inc_esop_info);".

[0233] The technical solution of this embodiment is to obtain a target query intention type set and related knowledge corresponding to the target problem; construct 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; input the target problem, the target query intention type set corresponding to the target problem, 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 problem, wherein the target thinking chain determination model is obtained by iteratively training the thinking chain determination model to be trained through the first sample set; 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, which can improve the accuracy of SQL statement generation.

[0234] Example 2

[0235] FIG5 is a schematic diagram of the structure of an SQL statement generation device provided by an embodiment of the present invention. This embodiment is applicable to the generation of SQL statements. The device can be implemented in software and / or hardware and can be integrated into any device that provides SQL statement generation functionality. As shown in FIG5 , the SQL statement generation device specifically includes: a first acquisition module 510, a thought chain prompt example construction module 520, a target thought chain determination module 530, and a target SQL statement generation module 540.

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

[0237] A thinking chain prompt example construction module is used to 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;

[0238] a target thinking chain determination module, configured to input the target question, a target query intent type set corresponding to the target question, a thinking chain prompt example, and relevant 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;

[0239] 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.

[0240] 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.

[0241] Example 3

[0242] FIG6 shows a block diagram of an electronic device 10 that can be used to implement an embodiment of the present invention. 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.

[0243] As shown in FIG6 , the electronic device 10 includes at least one processor 11 and a memory, such as a read-only memory (ROM) 12 and a random access memory (RAM) 13, that is communicatively connected to the at least one processor 11. The memory stores a computer program that can be executed by the 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 into the random access memory (RAM) 13. Various programs and data required for the operation of the electronic device 10 can also be stored in the RAM 13. The processor 11, ROM 12, and RAM 13 are connected to each other via a bus 14. An input / output (I / O) interface 15 is also connected to the bus 14.

[0244] Multiple 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 magnetic 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 via a computer network such as the Internet and / or various telecommunication networks.

[0245] The processor 11 can be any general-purpose and / or specialized processing component 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 specialized artificial intelligence (AI) computing chips, various processors that run machine learning model algorithms, a digital signal processor (DSP), and any other suitable processor, controller, microcontroller, etc. The processor 11 executes the various methods and processes described above, such as the SQL statement generation method.

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

[0247] Various embodiments of the systems and techniques described 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), system-on-chip systems (SOCs), programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments can include being implemented in one or more computer programs that are executable and / or interpreted on a programmable system that includes 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.

[0248] 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, such that when the computer program is executed by the processor, the functions / operations specified in the flowcharts and / or block diagrams are implemented. The computer program may be executed entirely on the machine, partially on the machine, as a stand-alone software package, partially on the machine and partially on a remote machine, or entirely on a remote machine or server.

[0249] In the context of the present invention, computer-readable storage media can be tangible media that can contain or store a computer program for use with an instruction execution system, device or equipment or used in combination with an instruction execution system, device or equipment. Computer-readable storage media can include but are not limited to electronic, magnetic, optical, electromagnetic, infrared or semiconductor systems, devices or equipment, or any suitable combination of the foregoing. Alternatively, computer-readable storage media can be machine-readable signal media. More specific examples of machine-readable storage media can include electrical connections based on one or more lines, portable computer disks, hard disks, random access memories (RAM), read-only memories (ROM), erasable programmable read-only memories (EPROM or flash memory), optical fibers, portable compact disk read-only memories (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.

[0250] To provide interaction with a user, the systems and techniques described herein can 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 pointing device (e.g., a mouse or trackball) through which the user can provide input to the electronic device. Other types of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).

[0251] The systems and techniques described herein can be implemented in a computing system that includes back-end 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 front-end components (e.g., a user computer with a graphical user interface or 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 back-end components, middleware components, or front-end components. The components of the system can 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.

[0252] A computing system may include clients and servers. The clients and servers are typically remote from each other and typically interact via a communication network. This client-server relationship arises through computer programs running on the respective computers, creating a client-server relationship. The server may be a cloud server, also known as a cloud computing server or cloud host. This server is a hosting product within the cloud computing service ecosystem that addresses the management difficulties and limited scalability of traditional physical hosting and VPS services.

[0253] It should be understood that the various forms of the processes shown above can be used to reorder, add, or delete steps. For example, the steps described in the present invention can be performed in parallel, sequentially, or in a different order, as long as the desired results of the technical solution of the present invention can be achieved. This is not limited herein.

[0254] The above specific embodiments do not limit the scope of protection of the present invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions may be made based on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention are intended to be included within the scope of protection of the present invention.

Claims

1. A method for generating an 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; Inputting the target question, the target query intent type set corresponding to the target question, a thought chain prompt example, and relevant knowledge into a target thought chain determination model to obtain a target thought chain corresponding to the target question, wherein the target thought chain determination model is obtained by iteratively training a to-be-trained thought chain determination model with a first sample set; 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.

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 to-be-trained multi-level intent recognition model 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 Iteratively training the thought chain to be trained by the first sample set to determine the model includes: Obtaining a first sample set, wherein the first sample set includes: question samples and SQL statements corresponding to the question samples; Obtaining 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 thought chain prompt example and the relevant knowledge into the thought chain determination model to be trained to obtain the thought 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 question sample to obtain a target thinking chain determination model.

4. The method according to claim 3, characterized in that 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 question sample to obtain a target thinking chain determination model includes: Determine a target score for the predicted SQL statement based on the predicted SQL statement and the SQL statement corresponding to the question sample; Determine the loss value of the SQL statement generated by the model based on the target score of the predicted SQL statement, the predicted SQL statement, and the to-be-trained thinking chain 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.

5. The method according to claim 4, characterized in that Determining a target score for the predicted SQL statement based on the predicted SQL statement and the SQL statement corresponding to the question sample includes: Execute the predicted SQL statement and the SQL statement corresponding to the problem sample; Determining a first score according to an execution result corresponding to the predicted SQL statement and an execution result of the SQL statement corresponding to the problem sample; Determining a second score based on the similarity between the predicted SQL statement and the SQL statement corresponding to the question sample; Determine a 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 question sample; A target score for the predicted SQL statement is determined according to the first score, the second score, and the third score.

6. The method according to claim 1, characterized in that 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: Obtaining hierarchical information corresponding to each query intent type in the target query intent type set corresponding to the target question; A thinking chain prompt example corresponding to the target question is constructed based on 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.

7. The method according to claim 6, 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 hierarchical information corresponding to each query intent type in the target query intent type set corresponding to the target question.

8. 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; 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.

9. The method according to claim 2, characterized in that The multi-level intent recognition model to be trained is iteratively trained using 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 based on 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.

10. A device for generating SQL statements, characterized in that: include: The first acquisition module is used to obtain the target query intent 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 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; a target thinking chain determination module, configured to input the target question, a target query intent type set corresponding to the target question, a thinking chain prompt example, and relevant 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; 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.

11. 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. The computer program is executed by the at least one processor to enable the at least one processor to execute the SQL statement generation method according to any one of claims 1 to 9.

12. 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 9 when executed.

Citation Information

Patent Citations

  • SQL (Structured Query Language) statement generation method, device and equipment based on large language model

    CN117271561A

  • Table question and answer processing method, device and equipment

    CN117332054A

  • Data processing method and device, electronic equipment and storage medium

    CN117370373A

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

    CN117453717A

  • SQL (Structured Query Language) statement generation method and device, equipment and storage medium

    CN117931843A

Cited By

  • SQL intelligent generation method and system for business query

    CN120873005A

  • Method and system for generating Text2SQL (Structured Query Language) driven by large language model

    CN121144343A

  • Sentence-level relation extraction method and device, computer equipment, medium and program product

    CN121412386A

  • Dynamic hybrid analysis and certainty verification system and method for bond fuzzy instruction

    CN122240646A

  • Workflow construction method and device fusing thinking chain and execution sandbox self-verification

    CN122388009A