Text-to-SQL (Structured Query Language) method and device of mixed strategy, electronic equipment and storage medium

The hybrid strategy for Text-to-SQL systems addresses data leakage and high costs by integrating prompt engineering with supervised fine-tuning, enhancing accuracy and efficiency through vector databases and progressive tuning.

CN119166656APending Publication Date: 2024-12-20LANZHOU XIANGWEI YONGCHUANG INTELLIGENT TECHNOLOGY CO LTD
View PDF 0 Cites 5 Cited by

Patent Information

Application Number
CN202411174221.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-26
Publication Date
2024-12-20

AI Technical Summary

Technical Problem

Existing Text-to-SQL technologies face challenges such as data leakage, dependence on training data, and high economic costs, especially with methods like prompt engineering and RAG-based approaches, which require extensive manual data collection and large token usage.

Method used

A hybrid strategy that integrates prompt engineering with supervised fine-tuning, using a two-step process involving complementary mask processing and fine-tuning of data table and Text-to-SQL models, reducing the need for manual data collection and minimizing token usage.

Benefits of technology

Improves accuracy and efficiency in generating SQL statements from natural language queries by leveraging vector databases and progressive tuning, overcoming limitations of proprietary models like GPT4 and reducing computational overhead.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119166656A_ABST
    Figure CN119166656A_ABST
Patent Text Reader

Abstract

The invention discloses a mixed strategy Text-to-SQL method and device, electronic equipment and a storage medium, and relates to the technical field of natural language processing and database query, the method comprises the following steps: generating question and answer pairs based on a large language model, and constructing a vector database based on the question and answer pairs and schema information of a target database; in the vector database, performing complementary mask retrieval on the sample questions to obtain schema information to be used and question and answer pair examples; and analyzing the sample SQL statement to obtain target sc hema information. Constructing a first prompt template based on the sample problem and to-be-used schema information so as to perform fine adjustment on the data table recall model; constructing a second prompt template based on the sample question, the question and answer pair example and the target schema information so as to perform fine adjustment on the Text-to-SQL model; and based on the fine-tuned data table recall model and the Text-to-SQL model, reasoning an SQL statement of the problem to be processed. The problems of data leakage and dependence on training data are solved, and the Text-to-SQL performance is improved under the condition that the number of tokens is limited.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of natural language processing and database query, and in particular to a hybrid strategy Text-to-SQL method, device, electronic device and storage medium. Background Art

[0002] Text-to-SQL refers to the technology of converting natural language descriptions into corresponding SQL query statements. When sufficient accuracy is achieved, it can greatly reduce the professional threshold and effectively assist people in querying massive databases. Early Text-to-SQL related work required a lot of manual and user interaction. In recent years, with the development of deep learning and the release of large-scale open source data, Text-to-SQL models built by neural generative models have developed rapidly, including intermediate representation, tree decoding, graph network modeling problems and relationships between databases, reordering, data enhancement and other technologies, but they are still not enough to solve complex operations.

[0003] With the emergence of large language model technology, Text-to-SQL technology has been further developed. According to the optimization method of large language model, the current research is mainly divided into three methods: prompt engineering, fine-tuning and RAG-based.

[0004] Prompt engineering is a method that guides the model to generate output that is more in line with the user's intention by designing specific prompt words or sentences and constructing prompt templates. This method is low in complexity, but its effect is more dependent on excellent large language models such as GPT4, has high economic costs, has data leakage issues, and requires the design of more reasonable prompt templates.

[0005] Fine-tuning refers to fine-tuning the open source pre-trained model (LLM) using a dataset containing a large number of SQL statements to improve the model's ability to understand and generate SQL statements. This method can use a free and open source large language model, but it requires the collection and organization of a large amount of training data in advance and cannot solve the cold start problem.

[0006] RAG-based refers to the SQL generation method that uses search-enhanced generation technology. It stores existing database information data in a vector database and retrieves and generates it when users ask questions. This method can be used out of the box, but it takes up a large number of tokens, resulting in additional performance overhead. Summary of the invention

[0007] The present invention provides a hybrid strategy Text-to-SQL method, device, electronic device and storage medium to solve the problems of data leakage, dependence on training data, etc., and improve the performance and effect of Text-to-SQL under the condition of limited number of tokens.

[0008] According to one aspect of the present invention, a hybrid strategy Text-to-SQL method is provided, comprising:

[0009] Generate a question-answer pair corresponding to a target database based on a large language model, and construct a vector database based on the question-answer pair and schema information of the target database, wherein the question-answer pair includes a sample question and a sample SQL statement corresponding to the sample question;

[0010] Performing complementary mask processing on the sample question to obtain a mask processing result, and searching the vector database based on the mask processing result to obtain the schema information to be used and the question-answer pair example corresponding to the sample question;

[0011] Parsing the sample SQL statement to obtain target schema information, constructing a first prompt template based on the sample question and the schema information to be used, and fine-tuning the data table recall model based on the first prompt template and the target schema information to obtain a target data table recall model;

[0012] Building a second prompt template based on the sample question, the question-answer pair example, and the target schema information, and fine-tuning the Text-to-SQL model based on the second prompt template and the sample SQL statement to obtain a target Text-to-SQL model;

[0013] Based on the target data table recall model and the target Text-to-SQL model, a target SQL statement corresponding to the problem to be processed is inferred, wherein the target SQL statement is used to execute data query in the target database.

[0014] According to another aspect of the present invention, a hybrid strategy Text-to-SQL device is provided, comprising:

[0015] A vector database construction module, used to generate question-answer pairs corresponding to a target database based on a large language model, and to construct a vector database based on the question-answer pairs and schema information of the target database, wherein the question-answer pairs include sample questions and sample SQL statements corresponding to the sample questions;

[0016] A complementary mask retrieval module, used to perform complementary mask processing on the sample question to obtain a mask processing result, and search the vector database based on the mask processing result to obtain the schema information to be used and the question-answer pair example corresponding to the sample question;

[0017] A data table recall model fine-tuning module is used to parse the sample SQL statement to obtain target schema information, build a first prompt template based on the sample question and the schema information to be used, and fine-tune the data table recall model based on the first prompt template and the target schema information to obtain a target data table recall model;

[0018] A Text-to-SQL model fine-tuning module, configured to construct a second prompt template based on the sample question, the question-answer pair example, and the target schema information, and fine-tune the Text-to-SQL model based on the second prompt template and the sample SQL statement to obtain a target Text-to-SQL model;

[0019] A target SQL statement reasoning module is used to infer a target SQL statement corresponding to the problem to be processed based on the target data table recall model and the target Text-to-SQL model, wherein the target SQL statement is used to execute data query in the target database.

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

[0021] at least one processor;

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

[0023] The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the Text-to-SQL method of the hybrid strategy described in any embodiment of the present invention.

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

[0025] The technical solution of the embodiment of the present invention, first, generates a question-answer pair corresponding to the target database through a large language model, and constructs a vector database based on the target question-answer pair and the schema information of the target database, wherein the target question-answer pair includes a sample question and a sample SQL statement corresponding to the sample question. The training set can be automatically generated, and only manual verification is required, which solves the problem of manual collection and collation of a large amount of data in the database. Then, the target sample question is subjected to complementary masking to obtain a masking result, and a search is performed in the target vector database based on the target masking result to obtain the schema information to be used and the question-answer pair example corresponding to the target sample question; the target sample SQL statement is parsed to obtain the target schema information, and a first prompt template is constructed based on the target sample question and the target schema information to be used, and the data table recall model is fine-tuned based on the target first prompt template and the target schema information to obtain the target data table recall model; a second prompt template is constructed based on the target sample question, the target question-answer pair example and the target schema information, and the Text-to-SQL model is fine-tuned based on the target second prompt template and the target sample SQL statement to obtain the target Text-to-SQL model. Finally, based on the target data table recall model and the target Text-to-SQL model, the target SQL statement corresponding to the problem to be processed is inferred, where the target SQL statement is used to execute data queries in the target database. In the embodiment of the present invention, the prompt engineering is integrated into the supervised fine-tuning to avoid the limitation and data leakage of proprietary large models such as GPT4; the two-step progressive tuning method of first retrieval and then fine-tuning is adopted, which only requires a small number of tokens and removes noise, effectively improving the accuracy of generation.

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

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

[0028] Figure 1 A flowchart of a Text-to-SQL method of a hybrid strategy provided in Example 1 of the present invention;

[0029] Figure 2 A schematic diagram of complementary mask processing of sample problems provided in the first embodiment of the present invention;

[0030] Figure 3 A flow chart of model fine-tuning provided in Embodiment 1 of the present invention;

[0031] Figure 4 A flowchart of a hybrid strategy Text-to-SQL method provided in Embodiment 2 of the present invention;

[0032] Figure 5 A model reasoning flow chart provided for the second embodiment of the present invention;

[0033] Figure 6 A structural diagram of a Text-to-SQL device with a hybrid strategy provided in Embodiment 3 of the present invention;

[0034] Figure 7 This is a schematic diagram of the structure of an electronic device provided in Embodiment 4 of the present invention. DETAILED DESCRIPTION

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

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

[0037] Embodiment 1

[0038] Figure 1This is a flowchart of a hybrid strategy Text-to-SQL method provided in Embodiment 1 of the present invention. This embodiment can be applied to converting natural language into SQL query statements to implement data query operations in a database. The method can be executed by a hybrid strategy Text-to-SQL device, which can be implemented in the form of hardware and / or software. The device can be configured in an electronic device, for example, the electronic device can run a server of a large language model. Figure 1 As shown, the method includes:

[0039] S110. Generate question-answer pairs corresponding to the target database based on the large language model, and build a vector database based on the question-answer pairs and schema information of the target database.

[0040] Among them, the target database can be understood as a database that supports SQL statements. For example, the target database can be a MySQL database, and the target database can also be a database that stores private domain data; the question and answer pairs include sample questions and sample SQL statements corresponding to the sample questions. The sample questions can be natural language questions, and the sample SQL statements can be SQL statements corresponding to natural language questions; the schema information of the target database can be information that describes the structure and organization of the target database in detail, such as table name, field name, field type, field value, etc.

[0041] Specifically, a large language model can be used to generate question-answer pairs corresponding to the target database. The number of question-answer pairs can be multiple. Based on the SQL statements in the question-answer pairs, query results corresponding to the sample questions can be queried in the target database.

[0042] In an embodiment of the present invention, a question-answer pair corresponding to a database is generated based on a large language model, and a vector database is constructed based on the question-answer pair and the schema information of the database, including: constructing a question-answer pair guidance template based on the schema information of the target database and a difficulty level calculation formula, and guiding the large language model to generate at least two question-answer pairs with different difficulty levels through the question-answer pair guidance template; converting the question-answer pair and the schema information of the target database into a question-answer pair vector and a schema information vector, respectively, and constructing the vector database based on the question-answer pair vector and the schema information vector.

[0043] The difficulty level calculation formula is determined based on at least one of the number of SQL components, the number of selections, and the number of conditional statements, and the question-answer pair guidance template is used to guide the large language model to generate question-answer pairs.

[0044] Specifically, a guidance template for a large language model can be constructed based on the schema information of the target database and the difficulty level calculation formula. The guidance template will guide the large language model to generate question-answer pairs of different difficulty levels. The difficulty level is comprehensively evaluated based on the number of components, selections, and conditions of the SQL statement, that is, difficulty = number of components + number of selections + number of conditions.

[0045] Exemplarily, the constructed question-answer pair guiding template may be as follows:

[0046] custom_prompt="""Given the schemas of SQL tables and examples, yourjob is to generate different question-sql pairs in difficulty levels. Following the formula for difficulty level calculation:difficulty=number of SQL components+number of selections+number of conditional stateme nts.

[0047] Table Schemas

[0048] {database_schema}

[0049] ###Examples

[0050] {different question-sql pairs in difficulty levels}

[0051] ###

[0052] Just follow the mentioned four difficulty levels,generate 50question-sql pairs for each level.

[0053] """

[0054] custom_prompt="""Given the schema of a SQL table and data examples, your task is to generate question-SQL pairs of different difficulty levels. The difficulty level is calculated as follows: Difficulty = Number of SQL components + Number of choices + Number of conditional statements.

[0055] ###Table Schema

[0056] {Database schema} (This is a placeholder and should actually contain a specific SQL table structure description)

[0057] ###Example

[0058] {Questions and SQL pairs of different difficulty levels} (This is a placeholder and should contain specific examples of question and SQL pairs)

[0059] ###

[0060] Just follow the four difficulty levels mentioned and generate 50 question-SQL pairs for each level.

[0061] """

[0062] Furthermore, the generated question-answer pairs are checked. For some correct question-answer pairs, vectorization techniques such as BERT and Word2Vec can be used to convert the generated question-answer pairs into question-answer pair vectors in vector form, and the schema information of the target database is converted into schema information vectors in vector form. Finally, the question-answer pair vectors and schema information vectors are stored in a vector database, which supports efficient vector retrieval and query. The advantage of this is that when generating question-answer pairs through a large language model, there is no need to manually collect and organize a large amount of data, and only manual verification is required.

[0063] S120, performing complementary mask processing on the sample question to obtain a mask processing result, and searching in the vector database based on the mask processing result to obtain the schema information to be used and the question-answer pair example corresponding to the sample question.

[0064] Among them, performing complementary masking on the sample question refers to performing complementary masking on the text corresponding to the sample question, and the result obtained is the complementary masking result; the schema information to be used can be part of the schema information in the vector database that matches the sample question; the question-answer pair examples can be other question-answer pairs in the vector database that are related to or similar to the sample question.

[0065] In an embodiment of the present invention, the mask processing result includes a first mask processing result and a second mask processing result, and the complementary mask processing is performed on the sample problem to obtain the mask processing result, including: performing skeleton information mask processing on the sample problem to obtain the first mask processing result; performing key information mask processing on the sample problem to obtain the second mask processing result.

[0066] Among them, the skeleton information can be the part of the sample problem that belongs to the problem structure skeleton, and the first mask processing result is the result obtained after processing this part of the content in the sample problem; the key information can be the content related to data tables, fields, etc. in the sample problem, and the second mask processing result is the result obtained after masking the key information in the sample problem.

[0067] It can be understood that the first mask processing result is the result obtained after processing the content corresponding to the skeleton information in the sample question, so the first mask processing result contains the data table, field and other information corresponding to the sample question; the second mask processing result is the result obtained after masking the key information in the sample question, so the second mask processing result contains the skeleton information corresponding to the sample question. Figure 2 FIG. 1 is a schematic diagram of complementary mask processing for sample problems provided in Embodiment 1 of the present invention.

[0068] In an embodiment of the present invention, the step of searching the vector database based on the mask processing result to obtain the schema information to be used corresponding to the sample question includes: converting the first mask processing result into a first vector, searching the vector database for a schema information vector matching the first vector based on a similarity algorithm, and determining the schema information to be used based on the schema information vector.

[0069] Specifically, the first mask processing result is converted into a numerical vector, i.e., a first vector, so as to be efficiently searched in a vector database. Then, a similarity algorithm is used to search the vector database for a schema information vector that matches the first vector. The similarity algorithm may be a k-nearest neighbor (KNN) algorithm. During the search process, several schema information vectors with the highest similarity to the first vector are returned, and information corresponding to the schema information vector is used as the schema information to be used.

[0070] In an embodiment of the present invention, the search is performed in the vector database based on the mask processing result to obtain a question-answer pair example corresponding to the sample question, including: converting the second mask processing result into a second vector, searching the vector database for a question-answer pair vector matching the second vector based on a similarity algorithm, and determining the question-answer pair example based on the question-answer pair vector.

[0071] Specifically, in order to obtain a question-answer pair example corresponding to the sample question, the second mask processing result can be first converted into a second vector to facilitate retrieval in the vector database. Then, a similarity algorithm is used to retrieve a question-answer pair vector that matches the second vector in the vector database. The similarity algorithm can be KNN, which is used to evaluate the similarity between the second vector and each question-answer pair vector in the vector database. During the retrieval process, several question-answer pair vectors with the highest similarity to the second vector will be returned, and the question-answer pairs corresponding to the question-answer pair vectors will be used as question-answer pair examples.

[0072] Exemplarily, a question-answer pair may be:

[0073] {"quest ion":'How many singers do we have? ',

[0074] "query":'SELECT count(*)FROM singer'}

[0075] Question: "How many singers do we have?"

[0076] SQL query statement: "SELECT count(*)FROM singer" (which means "select and count the number of all rows from the singer table", that is, get the total number of singers).

[0077] It should also be noted that by retrieving question-answer pairs similar to the current sample question, that is, similar questions and SQL statements as question-answer pair examples, the advantage of this is that it can facilitate the subsequent provision of more context and examples for the Text-to-SQL model to help it learn how to convert similar questions into correct SQL statements.

[0078] S130, parsing the sample SQL statement to obtain target schema information, constructing a first prompt template based on the sample question and the schema information to be used, and fine-tuning the data table recall model based on the first prompt template and the target schema information to obtain a target data table recall model.

[0079] Among them, the first prompt template is used to guide the data table recall model to infer the schema information corresponding to the sample question; the data table recall model can be an open source base model, such as SQLCoder, which is a large language model (LLM) specially designed to convert natural language questions into SQL query statements.

[0080] Specifically, parsing the sample SQL statement can be to identify the various components in the SQL statement, such as clauses, and specific elements in the clauses, such as table names, field names, conditional expressions, etc. After extracting the table names and field names, the target schema information can be constructed based on this information. Then, a first prompt template is constructed based on the sample question and the schema information to be used, and the data table recall model is fine-tuned based on the first prompt template and the target schema information to obtain the target data table recall model.

[0081] Exemplarily, the first prompt template may be as follows:

[0082] user_message="""Given the schemas of retrieved SQL tables, your job is to determine the columns and tables that the quest ion is referring to.

[0083] Tables

[0084] {retrieved database schema}

[0085] ###

[0086] {question}

[0087]

[0088] In an embodiment of the present invention, the data table recall model is fine-tuned based on the first prompt template and the target schema information to obtain the target data table recall model, including: based on the first prompt template, guiding the data table recall model to infer the inference schema information corresponding to the sample question; and fine-tuning the data table recall model based on the inference schema information and the target schema information to obtain the target data table recall model.

[0089] The inferred schema information may be the schema information inferred and predicted by the data table recall model; the target schema information may be the schema information that the data table recall model is expected to learn and accurately recall, that is, the target schema information obtained by parsing the sample SQL statement mentioned above.

[0090] Specifically, in order to obtain the target data table recall model, the first prompt template is first used to guide the data table recall model to infer the reasoning schema information corresponding to the sample question. By inputting the sample question and the first prompt template into the data table recall model, the model can infer the reasoning schema information related to the sample question. The target schema information is the schema information that the model is expected to learn and accurately recall. Therefore, during the fine-tuning process, by comparing the reasoning schema information and the target schema information, the parameters of the data table recall model are adjusted to make it closer to generating the target schema information. After fine-tuning, the target data table recall model is obtained, which can better infer the correct schema information based on the input question.

[0091] The solution of the embodiment of the present invention can remove noise interference and reduce the number of tokens used, thereby improving reasoning efficiency by recalling related data tables and fields and then performing reasoning.

[0092] S140: construct a second prompt template based on the sample question, the question-answer pair example and the target schema information, and fine-tune the Text-to-SQL model based on the second prompt template and the sample SQL statement to obtain a target Text-to-SQL model.

[0093] The second prompt template is used to guide the Text-to-SQL model to infer the SQL statement corresponding to the sample question; the Text-to-SQL model can be an open source base model, and the Text-to-SQL model and the data table recall model can be the same type of open source base model or different types of open source base models.

[0094] Exemplarily, the second prompt template may be as follows:

[0095]

[0096]

[0097] In the embodiment of the present invention, a schema most relevant to the question is provided, and examples of question-answer pairs related to the question are added. The constructed guiding template can more effectively guide the model to infer the SQL statement corresponding to the question.

[0098] In an embodiment of the present invention, the fine-tuning of the Text-to-SQL model based on the second prompt template and the sample SQL statement to obtain the target Text-to-SQL model includes: guiding the Text-to-SQL model to infer the inference SQL statement corresponding to the sample question based on the second prompt template; and fine-tuning the Text-to-SQL model based on the inference SQL statement and the sample SQL statement to obtain the target Text-to-SQL model.

[0099] The inference SQL statement may be a text-to-SQL model that attempts to generate an inference SQL statement that is close to the expected SQL statement based on a prompt template and an input question.

[0100] Specifically, in order to obtain the target Text-to-SQL model, a second prompt template can be constructed based on the sample question, the question-answer pair example, and the target schema information. The purpose of this template is to guide the Text-to-SQL model to infer the SQL statement corresponding to the sample question. Using the constructed second prompt template, the Text-to-SQL model is guided to infer the inference SQL statement corresponding to the sample question.

[0101] Furthermore, the Text-to-SQL model is fine-tuned based on the inference SQL statements and sample SQL statements. The goal of fine-tuning is to make the inference SQL statements generated by the model closer to the real sample SQL statements, thereby improving the accuracy of the model. After fine-tuning, the target Text-to-SQL model is obtained, which can more accurately generate corresponding SQL statements based on the input questions, question-answer pair examples, and target schema information, thereby improving the conversion efficiency from natural language questions to SQL queries.

[0102] In a preferred embodiment, Figure 3 This is a flow chart of model fine-tuning provided in Embodiment 1 of the present invention. Model fine-tuning includes training set construction, question retrieval, prompt template construction and model fine-tuning.

[0103] Training set construction: For a specified database, a customized prompt template is created, and a large model is used to generate a certain amount of question-answer pairs of varying difficulty levels. These pairs are manually checked and stored in a vector database.

[0104] Question retrieval: For each question in the training set, the table schema information and question-query pair examples related to it are retrieved to construct two prompt templates. The table schema information refers to information such as table name, field name, field type, and field value.

[0105] Prompt template construction: Based on the training questions and the retrieved relevant schema information, construct prompt template 1 for fine-tuning the data table recall model; based on the training questions, the retrieved relevant question-answer pair examples, and the parsed correct tables and fields, construct prompt template 2 for fine-tuning the Text-to-SQL model.

[0106] S150: Based on the target data table recall model and the target Text-to-SQL model, infer a target SQL statement corresponding to the problem to be processed.

[0107] The problem to be processed may be a natural language problem that needs to be converted into an SQL statement; the target SQL statement is used to execute data query in the target database.

[0108] Specifically, when a database query operation needs to be performed, the user can input the question to be processed into the target data table recall model and the target Text-to-SQL model, and the target SQL statement is inferred through the two models. The technical solution of the embodiment of the present invention first generates a question-answer pair corresponding to the target database through a large language model, and constructs a vector database based on the target question-answer pair and the schema information of the target database, wherein the target question-answer pair includes a sample question and a sample SQL statement corresponding to the sample question. The training set can be automatically generated, and only manual verification is required, which solves the problem of manual collection and collation of large amounts of data in the database. Then, the target sample question is subjected to complementary masking to obtain the masking result, and the target vector database is searched based on the target masking result to obtain the schema information to be used and the question-answer pair example corresponding to the target sample question; the target sample SQL statement is parsed to obtain the target schema information, and the first prompt template is constructed based on the target sample question and the target schema information to be used, and the data table recall model is fine-tuned based on the target first prompt template and the target schema information to obtain the target data table recall model; the second prompt template is constructed based on the target sample question, the target question-answer pair example and the target schema information, and the Text-to-SQL model is fine-tuned based on the target second prompt template and the target sample SQL statement to obtain the target Text-to-SQL model. Finally, based on the target data table recall model and the target Text-to-SQL model, the target SQL statement corresponding to the problem to be processed is inferred, wherein the target SQL statement is used to execute data query in the target database. In the embodiment of the present invention, prompt engineering is integrated into supervised fine-tuning to avoid the limitations and data leakage problems of proprietary large models such as GPT4; a two-step progressive tuning method of first searching and then fine-tuning is adopted, which only requires a small number of tokens and removes noise, effectively improving the accuracy of generation.

[0109] The number of tokens can be the number of words, SQL keywords, table names, column names, etc. in the user query. When processing Text-to-SQL tasks, the length and complexity of the input text need to be taken into account, because longer queries may increase the number of tokens, thereby increasing the difficulty and computational cost of model processing.

[0110] Embodiment 2

[0111] Figure 4 This is a flowchart of a hybrid strategy Text-to-SQL method provided in Example 2 of the present invention. Based on the above embodiment, this embodiment describes in detail the process of generating the target SQL statement to be processed. The specific implementation method can refer to the technical solution of this embodiment. Among them, the technical terms that are the same or corresponding to the above embodiment are not repeated here. Figure 4 As shown, the method includes:

[0112] S210: Generate question-answer pairs corresponding to the target database based on the large language model, and build a vector database based on the question-answer pairs and schema information of the target database.

[0113] The question-answer pair includes a sample question and a sample SQL statement corresponding to the sample question.

[0114] S220: Perform complementary mask processing on the sample question to obtain a mask processing result, and search the vector database based on the mask processing result to obtain the schema information to be used and the question-answer pair example corresponding to the sample question.

[0115] S230, parsing the sample SQL statement to obtain target schema information, constructing a first prompt template based on the sample question and the schema information to be used, and fine-tuning the data table recall model based on the first prompt template and the target schema information to obtain a target data table recall model.

[0116] S240: construct a second prompt template based on the sample question, the question-answer pair example and the target schema information, and fine-tune the Text-to-SQL model based on the second prompt template and the sample SQL statement to obtain a target Text-to-SQL model.

[0117] S250. Determine the mask result to be processed corresponding to the question to be processed, and search the vector database based on the mask result to be processed to obtain the first schema information to be processed and the question-answer pair example to be processed corresponding to the question to be processed.

[0118] The mask result to be processed may be a result obtained after complementary mask processing is performed on the problem to be processed, and the complementary mask processing process of the problem to be processed and the sample problem may be the same. The first schema information to be processed may be schema information associated with the problem to be processed retrieved from the vector database, for example, the table and field information included in the first schema information to be processed may be used to answer the problem to be processed. The example of the question-answer pair to be processed may be multiple question-answer pairs corresponding to the problem to be processed in the vector database.

[0119] Specifically, when processing the problem to be processed, it is first necessary to perform complementary mask processing on the problem to be processed to obtain the corresponding mask result to be processed. Then, the mask result to be processed can be used to search in the vector database to find the vector most similar to the mask result to be processed, and determine the corresponding first schema information to be processed and the question-answer pair example to be processed based on these vectors. Schema information usually describes the structure of the database, including table names, field names, etc., while the question-answer pair example provides questions and SQL statement examples related to the problem to be processed.

[0120] S260: construct a third guiding template based on the problem to be processed and the first schema information to be processed, guide the target data table recall model through the third guiding template, and infer the second schema information to be processed corresponding to the problem to be processed.

[0121] Among them, the third guiding template is used to guide the target data table recall model to infer the second schema information to be processed corresponding to the problem to be processed. The construction method of the third template is the same as the construction method of the first guiding template; the second schema information to be processed can be more accurate schema information inferred by the target data table recall model.

[0122] Specifically, based on the problem to be processed and the first schema information to be processed, a third guiding template is constructed, which can guide the target data table recall model on how to infer the schema information related to the problem. Through the reasoning of the target data table recall model, the second schema information to be processed can be inferred, and the second schema information to be processed can more specifically and accurately describe the database structure information related to the problem to be processed. For example, the second schema information to be processed may include table names, field names, and the relationship between them, which is crucial for the subsequent generation of SQL query statements.

[0123] The target data table recall model can infer the second schema information to be processed based on the third guide template including the problem to be processed and the first schema information to be processed to narrow the scope of table lookup. For example, the first schema information to be processed is obtained through retrieval, and it may include 20 full data tables, but the second schema information to be processed further inferred by the target data table recall model may correspond to 2 full data tables. In this way, the schema information corresponding to the problem to be processed can be determined more accurately, so as to facilitate the subsequent construction of the fourth guide template and provide important structural information for the subsequent SQL query statement generation.

[0124] S270. Based on the problem to be processed, the second schema information to be processed and the question-answer pair example to be processed, a fourth guiding template is constructed, and the target Text-to-SQL model is guided by the fourth guiding template to infer a target SQL statement corresponding to the problem to be processed.

[0125] The fourth guide template may be constructed in the same manner as the second guide template.

[0126] Specifically, a guide template, namely the fourth guide template, is constructed based on the problem to be processed, the second schema information to be processed, and the example of the question-answer pair to be processed. The fourth guide template is used to guide the target Text-to-SQL model, so that the model can infer the target SQL statement corresponding to the problem to be processed according to the guidance of the template, and the target SQL statement can accurately query the data required by the problem to be processed in the target database.

[0127] In a preferred embodiment, Figure 5 The model reasoning flow chart provided in the second embodiment of the present invention is as follows: Figure 5 As shown in the figure, when a user asks a question, the skeleton information in the question is first masked, and the data table schema information related to it is retrieved from the training set data. This information is combined with the question to construct prompt template 1. After embedding, the data table recall model is used to infer the associated tables and fields corresponding to the question. Then, the special information in the question is masked, and the question-answer pair examples related to it are retrieved from the training set data. This information is combined with the question and the inferred associated tables and fields to construct prompt template 2. After embedding, the Text-to-SQL model is used to infer and generate SQL statements.

[0128] On the basis of the above embodiments, the method of the embodiments of the present invention also includes: based on the target SQL statement corresponding to the problem to be processed, performing a data query operation in the target database to obtain a target execution result; proofreading the target execution result to obtain a proofreading result, if the proofreading result is correct, then taking the problem to be processed and the target SQL statement as new sample question and answer pairs, and storing them in the form of vectors to the vector database; if the number of new sample question and answer pairs in the vector database is greater than a preset number threshold, then based on the new sample questions and answers, fine-tuning the target data table recall model and the target Text-to-SQL model.

[0129] Specifically, based on the target SQL statement corresponding to the problem to be processed, a data query operation is performed in the target database to obtain a target execution result. In order to verify the correctness of the target SQL statement, the target execution result obtained by the data query can be proofread to obtain a proofreading result. If the proofreading result is correct, it means that the SQL statement can accurately query the required data, and the problem to be processed and the corresponding target SQL statement can be used as a new sample question-answer pair. Furthermore, the new sample question-answer pair is stored in the vector database in the form of a vector so that it can be used for subsequent model training and fine-tuning.

[0130] In the vector database, when the number of newly added sample question-answer pairs exceeds the preset threshold, the target data table recall model and the target Text-to-SQL model are fine-tuned based on these newly added sample question-answer pairs. The purpose of fine-tuning is to enable the model to better adapt to new query requirements and data changes, and continuously improve the accuracy and generalization ability of the model.

[0131] In a preferred embodiment, in order to determine the generation effect of the target SQL statement, the evaluation index can refer to the spider data set, using the exact set matching accuracy EM and the execution accuracy EX. The exact set matching accuracy measures the consistency of the predicted SQL query and its corresponding real SQL on the component; on the other hand, the execution accuracy compares the execution output of the predicted SQL query with the execution output of the real SQL query on some database instances. Among them, Table 1 is an effect comparison table.

[0132] Table 1

[0133] algorithm EM EX Prompt engineering only 0.014 0.016 RAG only 0.178 0.2 The present invention 0.738 0.833

[0134] Since direct fine-tuning requires a large amount of data, only two single strategies, namely, prompt engineering and RAG, are compared. It can be seen that the present invention is far superior to other single strategies in terms of precise matching and execution accuracy.

[0135] The technical solution of the embodiment of the present invention, first, generates a question-answer pair corresponding to the target database through a large language model, and constructs a vector database based on the target question-answer pair and the schema information of the target database, wherein the target question-answer pair includes a sample question and a sample SQL statement corresponding to the sample question. The training set can be automatically generated, and only manual verification is required, which solves the problem of manual collection and collation of a large amount of data in the database. Then, the target sample question is subjected to complementary masking to obtain a masking result, and a search is performed in the target vector database based on the target masking result to obtain the schema information to be used and the question-answer pair example corresponding to the target sample question; the target sample SQL statement is parsed to obtain the target schema information, and a first prompt template is constructed based on the target sample question and the target schema information to be used, and the data table recall model is fine-tuned based on the target first prompt template and the target schema information to obtain the target data table recall model; a second prompt template is constructed based on the target sample question, the target question-answer pair example and the target schema information, and the Text-to-SQL model is fine-tuned based on the target second prompt template and the target sample SQL statement to obtain the target Text-to-SQL model. Finally, based on the target data table recall model and the target Text-to-SQL model, the target SQL statement corresponding to the problem to be processed is inferred, where the target SQL statement is used to execute data queries in the target database. In the embodiment of the present invention, the prompt engineering is integrated into the supervised fine-tuning to avoid the limitation and data leakage of proprietary large models such as GPT4; the two-step progressive tuning method of first retrieval and then fine-tuning is adopted, which only requires a small number of tokens and removes noise, effectively improving the accuracy of generation.

[0136] The present invention integrates prompt engineering and RAG retrieval into fine-tuning, proposes a progressive Text-to-SQL process with a hybrid strategy, and solves the problems of cumbersome data preparation, limitations of proprietary large models, and large token usage through the construction of multiple prompt templates.

[0137] Embodiment 3

[0138] Figure 6 A structural diagram of a hybrid strategy Text-to-SQL device provided in Embodiment 3 of the present invention.

[0139] like Figure 6 As shown, the device comprises:

[0140] A vector database construction module 310 is used to generate a question-answer pair corresponding to a target database based on a large language model, and to construct a vector database based on the question-answer pair and schema information of the target database, wherein the question-answer pair includes a sample question and a sample SQL statement corresponding to the sample question;

[0141] A complementary mask retrieval module 320 is used to perform complementary mask processing on the sample question to obtain a mask processing result, and to search the vector database based on the mask processing result to obtain the schema information to be used and the question-answer pair example corresponding to the sample question;

[0142] A data table recall model fine-tuning module 330 is used to parse the sample SQL statement to obtain target schema information, build a first prompt template based on the sample question and the schema information to be used, and fine-tune the data table recall model based on the first prompt template and the target schema information to obtain a target data table recall model;

[0143] A Text-to-SQL model fine-tuning module 340 is used to construct a second prompt template based on the sample question, the question-answer pair example and the target schema information, and fine-tune the Text-to-SQL model based on the second prompt template and the sample SQL statement to obtain a target Text-to-SQL model;

[0144] The target SQL statement reasoning module 350 is used to infer the target SQL statement corresponding to the problem to be processed based on the target data table recall model and the target Text-to-SQL model, wherein the target SQL statement is used to execute data query in the target database.

[0145] The technical solution of the embodiment of the present invention, first, generates a question-answer pair corresponding to the target database through a large language model, and constructs a vector database based on the target question-answer pair and the schema information of the target database, wherein the target question-answer pair includes a sample question and a sample SQL statement corresponding to the sample question. The training set can be automatically generated, and only manual verification is required, which solves the problem of manual collection and collation of a large amount of data in the database. Then, the target sample question is subjected to complementary masking to obtain a masking result, and a search is performed in the target vector database based on the target masking result to obtain the schema information to be used and the question-answer pair example corresponding to the target sample question; the target sample SQL statement is parsed to obtain the target schema information, and a first prompt template is constructed based on the target sample question and the target schema information to be used, and the data table recall model is fine-tuned based on the target first prompt template and the target schema information to obtain the target data table recall model; a second prompt template is constructed based on the target sample question, the target question-answer pair example and the target schema information, and the Text-to-SQL model is fine-tuned based on the target second prompt template and the target sample SQL statement to obtain the target Text-to-SQL model. Finally, based on the target data table recall model and the target Text-to-SQL model, the target SQL statement corresponding to the problem to be processed is inferred, where the target SQL statement is used to execute data queries in the target database. In the embodiment of the present invention, the prompt engineering is integrated into the supervised fine-tuning to avoid the limitation and data leakage of proprietary large models such as GPT4; the two-step progressive tuning method of first retrieval and then fine-tuning is adopted, which only requires a small number of tokens and removes noise, effectively improving the accuracy of generation.

[0146] Based on the above embodiment, the vector database construction module 310 includes:

[0147] A question-answer pair construction submodule is used to construct a question-answer pair guidance template based on the schema information of the target database and a difficulty level calculation formula, and guide the large language model to generate at least two question-answer pairs with different difficulty levels through the question-answer pair guidance template;

[0148] A vector database construction submodule, used to convert the question-answer pair and the schema information of the target database into a question-answer pair vector and a schema information vector respectively, and construct the vector database based on the question-answer pair vector and the schema information vector;

[0149] The difficulty level calculation formula is determined based on at least one of the number of SQL components, the number of selections, and the number of conditional statements.

[0150] Based on the above embodiment, the mask processing result includes a first mask processing result and a second mask processing result, and the complementary mask retrieval module 320 includes:

[0151] A first mask processing submodule, used for performing skeleton information mask processing on the sample problem to obtain the first mask processing result;

[0152] The second mask processing submodule is used to perform key information mask processing on the sample question to obtain the second mask processing result.

[0153] Based on the above embodiment, the complementary mask retrieval module 320 includes:

[0154] The schema information to be used determination submodule is used to convert the first mask processing result into a first vector, retrieve a schema information vector matching the first vector in the vector database based on a similarity algorithm, and determine the schema information to be used based on the schema information vector.

[0155] Based on the above embodiment, the complementary mask retrieval module 320 includes:

[0156] A question-answer pair example determination submodule is used to convert the second mask processing result into a second vector, retrieve a question-answer pair vector matching the second vector in the vector database based on a similarity algorithm, and determine the question-answer pair example based on the question-answer pair vector.

[0157] Based on the above embodiment, the data table recall model fine-tuning module 330 is specifically used for:

[0158] Based on the first prompt template, guiding the data table recall model to infer the reasoning schema information corresponding to the sample question;

[0159] The data table recall model is fine-tuned based on the inference schema information and the target schema information to obtain the target data table recall model.

[0160] Based on the above embodiment, the target Text-to-SQL model fine-tuning module 340 includes:

[0161] Based on the second prompt template, guiding the Text-to-SQL model to infer the inference SQL statement corresponding to the sample question;

[0162] The Text-to-SQL model is fine-tuned based on the inference SQL statement and the sample SQL statement to obtain the target Text-to-SQL model.

[0163] Based on the above embodiment, the target SQL statement reasoning module 350 is specifically used for:

[0164] Determine a to-be-processed mask result corresponding to the to-be-processed question, and search the vector database based on the to-be-processed mask result to obtain first to-be-processed schema information and an to-be-processed question-answer pair example corresponding to the to-be-processed question;

[0165] Building a third guiding template based on the problem to be processed and the first schema information to be processed, guiding the target data table recall model through the third guiding template, and inferring the second schema information to be processed corresponding to the problem to be processed;

[0166] Based on the problem to be processed, the second schema information to be processed and the question-answer pair example to be processed, a fourth guiding template is constructed, and the target Text-to-SQL model is guided by the fourth guiding template to infer a target SQL statement corresponding to the problem to be processed.

[0167] Based on the above embodiment, the device is also used for:

[0168] Based on the target SQL statement corresponding to the problem to be processed, a data query operation is performed in the target database to obtain a target execution result;

[0169] Proofreading the target execution result to obtain a proofreading result, if the proofreading result is correct, taking the to-be-processed question and the target SQL statement as a newly added sample question-answer pair, and storing them in the vector database in the form of a vector;

[0170] If the number of the newly added sample question-answer pairs in the vector database is greater than a preset number threshold, the target data table recall model and the target Text-to-SQL model are fine-tuned based on the newly added sample questions and answers.

[0171] The hybrid strategy Text-to-SQL device provided in the embodiment of the present invention can execute the hybrid strategy Text-to-SQL method provided in any embodiment of the present invention, and has the corresponding functional modules and beneficial effects of the execution method.

[0172] Embodiment 4

[0173] Figure 7A schematic diagram of the structure of an electronic device provided for Embodiment 4 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 may 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.

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

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

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

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

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

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

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

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

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

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

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

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

Claims

1. A hybrid strategy Text-to-SQL method, characterized in that: include: Generate a question-answer pair corresponding to a target database based on a large language model, and construct a vector database based on the question-answer pair and schema information of the target database, wherein the question-answer pair includes a sample question and a sample SQL statement corresponding to the sample question; Performing complementary mask processing on the sample question to obtain a mask processing result, and searching the vector database based on the mask processing result to obtain the schema information to be used and the question-answer pair example corresponding to the sample question; Parsing the sample SQL statement to obtain target schema information, constructing a first prompt template based on the sample question and the schema information to be used, and fine-tuning the data table recall model based on the first prompt template and the target schema information to obtain a target data table recall model; Building a second prompt template based on the sample question, the question-answer pair example, and the target schema information, and fine-tuning the Text-to-SQL model based on the second prompt template and the sample SQL statement to obtain a target Text-to-SQL model; Based on the target data table recall model and the target Text-to-SQL model, a target SQL statement corresponding to the problem to be processed is inferred, wherein the target SQL statement is used to execute data query in the target database.

2. The method according to claim 1, characterized in that The step of generating a question-answer pair corresponding to a database based on a large language model, and constructing a vector database based on the question-answer pair and schema information of the database, includes: Building a question-answer pair guidance template based on the schema information of the target database and a difficulty level calculation formula, and guiding the large language model to generate at least two question-answer pairs with different difficulty levels through the question-answer pair guidance template; Converting the question-answer pair and the schema information of the target database into a question-answer pair vector and a schema information vector respectively, and constructing the vector database based on the question-answer pair vector and the schema information vector; The difficulty level calculation formula is determined based on at least one of the number of SQL components, the number of selections, and the number of conditional statements.

3. The method according to claim 2, characterized in that The mask processing result includes a first mask processing result and a second mask processing result, and the complementary mask processing is performed on the sample problem to obtain the mask processing result, including: Performing skeleton information masking processing on the sample problem to obtain the first masking processing result; Perform key information masking processing on the sample question to obtain the second masking processing result.

4. The method according to claim 3, characterized in that The searching in the vector database based on the mask processing result to obtain the schema information to be used corresponding to the sample question includes: The first mask processing result is converted into a first vector, a schema information vector matching the first vector is retrieved from the vector database based on a similarity algorithm, and the schema information to be used is determined based on the schema information vector.

5. The method according to claim 3, characterized in that: The step of searching the vector database based on the mask processing result to obtain a question-answer pair example corresponding to the sample question includes: The second mask processing result is converted into a second vector, a question-answer pair vector matching the second vector is retrieved in the vector database based on a similarity algorithm, and the question-answer pair example is determined based on the question-answer pair vector.

6. The method according to claim 1, characterized in that The step of fine-tuning the data table recall model based on the first prompt template and the target schema information to obtain the target data table recall model includes: Based on the first prompt template, guiding the data table recall model to infer the reasoning schema information corresponding to the sample question; The data table recall model is fine-tuned based on the inference schema information and the target schema information to obtain the target data table recall model.

7. The method according to claim 1, characterized in that The fine-tuning of the Text-to-SQL model based on the second prompt template and the sample SQL statement to obtain a target Text-to-SQL model includes: Based on the second prompt template, guiding the Text-to-SQL model to infer the inference SQL statement corresponding to the sample question; The Text-to-SQL model is fine-tuned based on the inference SQL statement and the sample SQL statement to obtain the target Text-to-SQL model.

8. The method according to claim 1, characterized in that The inferring a target SQL statement corresponding to the problem to be processed based on the target data table recall model and the target Text-to-SQL model includes: Determine a to-be-processed mask result corresponding to the to-be-processed question, and search the vector database based on the to-be-processed mask result to obtain first to-be-processed schema information and an to-be-processed question-answer pair example corresponding to the to-be-processed question; Building a third guiding template based on the problem to be processed and the first schema information to be processed, guiding the target data table recall model through the third guiding template, and inferring the second schema information to be processed corresponding to the problem to be processed; Based on the problem to be processed, the second schema information to be processed and the question-answer pair example to be processed, a fourth guiding template is constructed, and the target Text-to-SQL model is guided by the fourth guiding template to infer a target SQL statement corresponding to the problem to be processed.

9. The method according to claim 1, characterized in that: Also includes: Based on the target SQL statement corresponding to the problem to be processed, a data query operation is performed in the target database to obtain a target execution result; Proofreading the target execution result to obtain a proofreading result, if the proofreading result is correct, taking the to-be-processed question and the target SQL statement as a newly added sample question-answer pair, and storing them in the vector database in the form of a vector; If the number of the newly added sample question-answer pairs in the vector database is greater than a preset number threshold, the target data table recall model and the target Text-to-SQL model are fine-tuned based on the newly added sample question-answer pairs.

10. A hybrid strategy Text-to-SQL device, characterized in that: include: A vector database construction module, used to generate question-answer pairs corresponding to a target database based on a large language model, and to construct a vector database based on the question-answer pairs and schema information of the target database, wherein the question-answer pairs include sample questions and sample SQL statements corresponding to the sample questions; A complementary mask retrieval module, used to perform complementary mask processing on the sample question to obtain a mask processing result, and search the vector database based on the mask processing result to obtain the schema information to be used and the question-answer pair example corresponding to the sample question; A data table recall model fine-tuning module is used to parse the sample SQL statement to obtain target schema information, build a first prompt template based on the sample question and the schema information to be used, and fine-tune the data table recall model based on the first prompt template and the target schema information to obtain a target data table recall model; A Text-to-SQL model fine-tuning module, configured to construct a second prompt template based on the sample question, the question-answer pair example, and the target schema information, and fine-tune the Text-to-SQL model based on the second prompt template and the sample SQL statement to obtain a target Text-to-SQL model; A target SQL statement reasoning module is used to infer a target SQL statement corresponding to the problem to be processed based on the target data table recall model and the target Text-to-SQL model, wherein the target SQL statement is used to execute data query in the target database.

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, and the computer program is executed by the at least one processor to enable the at least one processor to execute the Text-to-SQL method of the hybrid strategy 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 Text-to-SQL method of the hybrid strategy according to any one of claims 1 to 9 when the instructions are executed.

Citation Information

Cited By

  • Data query method, device and equipment based on recall strategy optimization RAG

    CN119807227A

  • Natural language query method and system for relational database

    CN119917519A

  • Natural language query method and system for relational database

    CN119917519B

  • Database query method and device based on large language model and storage medium

    CN121636528A

  • AI agent-based semantic data updater

    US12675446B1