Large model training method for query conversion, database query method, computer equipment and computer readable storage medium

By training and fine-tuning the large model, a low-rank adaptation layer suitable for database natural language query is formed, which solves the problems of high computing resource requirements, low conversion rate and insufficient accuracy of large models in database natural language query application scenarios, and realizes efficient and accurate natural language to SQL query conversion.

CN120123367AActive Publication Date: 2025-06-10BERGMEIS (SHENZHEN) TECH CO LTD

Patent Information

Application Number
CN202510607500.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-13
Publication Date
2025-06-10
Estimated Expiration
2045-05-13

AI Technical Summary

Technical Problem

The computing resource requirements of large models in database natural language query application scenarios are high, the conversion rate is low, and the accuracy is insufficient, resulting in poor deployment and use efficiency.

Method used

By obtaining the original large model and training record set, processing the training records one by one, generating the first and second prompt statements, and fine-tuning the large model using Low-Rank Adaptation (LoRA) technology to form the first and second low-rank adaptation layers for collecting forms and fields and converting natural language queries into SQL queries.

Benefits of technology

It realizes the deployment of large models on civil-grade single GPUs, improves the conversion rate and accuracy of natural language to SQL queries, and ensures that the conversion results are executable in the database.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120123367A_ABST
    Figure CN120123367A_ABST
Patent Text Reader

Abstract

The embodiment of the invention discloses a large model training method for query conversion, a database query method, computer equipment and a computer readable storage medium. The training method comprises the steps of obtaining an original large model and a training record set; and processing all the training records to obtain the first prompt statement, the second prompt statement, the first output and the target SQL query so as to train the original large model, and deploying the trained original large model as a natural language conversion module in the database management system. According to the database query method, a historical query cache module and a natural language conversion module are newly added in a database management system. The historical query cache module is used for executing text vectorization, vector indexing and nearest neighbor retrieval according to an input query; the natural language conversion module is used for collecting related forms and fields according to input queries and converting the input queries into SQL (structured query language) queries. According to the method and the device, the natural language query can be more accurately converted into the SQL query by using fewer computing resources.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application belongs to the technical field of database management, and particularly relates to a large model training method for query transformation, a database query method, a computer device, and a computer-readable storage medium. Background Technique

[0002] In today's digital age, data has become an important basis for enterprise decision-making. Although traditional database management systems can efficiently store and query data, their query languages (such as Standard Query Language, abbreviated as SQL) are often obscure and difficult to understand for non-professionals, which to a certain extent limits the wide use and value mining of data. With the rapid development of artificial intelligence technology, especially the breakthrough in the field of natural language processing, machines are able to understand human natural language, providing new ideas for solving the above problems. The introduction of natural language query interfaces emerged precisely in this context. By adding natural language query interfaces, users can use everyday language to query information in the database without having to learn complex query languages. This greatly reduces the threshold for data use, enabling business personnel without a technical background to easily obtain the data they need for more in-depth analysis and decision-making. In addition, natural language query interfaces can also improve the flexibility and diversity of queries, allowing users to ask more complex and specific questions, rather than just asking based on predefined query templates. This not only helps to discover potential patterns and trends in the data but also contributes to driving innovation and business growth.

[0003] In recent years, people's desire to interact with databases using natural language has been increasing. Many researchers have used machine translation technology to train general and generalization-capable translation models to achieve this process. Its basic principle is to construct an encoder network model to learn the representations of natural language queries and underlying data patterns, and then generate corresponding query statements through a decoder network model to achieve interaction. With the recent leap in large model (also known as large language model, LLM) technology, a groundbreaking path has emerged in the form of interaction between natural language and databases. Different from traditional translation models trained through supervised learning, large models exhibit remarkable emergent capabilities, which exist in ultra-large-scale models with more than 100 billion parameters, enabling the model to seamlessly translate natural language into SQL without a dedicated model training process. This paradigm shift has subverted traditional interaction methods and is revolutionizing the way of natural language query in databases.

[0004] However, there are still some challenging problems to be solved when applying large models to the natural language query interface of databases. First of all, the complexity and computational resource requirements of large models are an important challenge. Large models usually require a large amount of computational resources and storage space, which limits their feasibility in actual production environments. Secondly, as a natural language interface, large models regard the translation process from natural language to database queries as a text generation task, without considering whether the generated content is traceable. Therefore, in some cases, they will generate some data entities that do not actually exist. Moreover, although the world knowledge learned by large models helps to understand the semantics in natural language, in specific data structures, such as when there are complex semantic associations between different data tables, large models are prone to errors in the process of mapping natural language to data entities. Therefore, how to improve the easy deployability of large models themselves and the reliability of large models as query tools in the application scenario of database natural language queries is a technical problem that those skilled in the art need to solve urgently.

[0005] The foregoing description is provided to give general background information and does not necessarily constitute prior art. Summary of the Invention

[0006] Based on this, in view of the above problems, it is necessary to propose a large model training method for query conversion, a database query method, a computer device, and a computer-readable storage medium, which can use fewer computational resources to achieve faster and more accurate natural language SQL queries.

[0007] The technical problems of this application are solved by adopting the following technical solutions: This application provides a large model training method for query conversion, which is applied to a database management system and includes the following steps: obtaining an original large model and a training record set, where the training record set includes multiple training records; processing all training records one by one to obtain a first prompt statement, a second prompt statement, a first output, and a target SQL query; training the original large model according to the first prompt statement, the second prompt statement, the first output, and the target SQL query; deploying the trained original large model as a natural language conversion module within the database management system, and the natural language conversion module is used to collect forms and fields related to the input natural language query, and to convert the input natural language query into an SQL query.

[0008] In an alternative embodiment of the present application, each training record is a binary tuple composed of a natural language query and a target SQL query. All training records are processed one by one, including: converting the target SQL query into an abstract syntax tree, determining the forms and fields involved in the target SQL query based on the abstract syntax tree, which are referred to as the involved forms and involved fields; synthesizing a first output based on all the involved forms and involved fields. The first output is a JSON dictionary, including multiple dictionary entries, each dictionary entry is represented in the form of a key-value pair, the key is the form name of a certain involved form, and the value is the list of fields corresponding to the involved form; determining all candidate field values in the database according to the natural language query. Each candidate field value is a certain value of a character-type field stored in the database, and the edit distance between each candidate field value and the natural language query is less than a preset threshold. The edit distance refers to the sum of the costs of character addition operations, character deletion operations, and character modification operations required to rewrite the candidate field value into the natural language query. The unit costs of character addition operations, character deletion operations, and character modification operations are preset by the user; synthesizing a first prompt statement according to the natural language query, all candidate field values, and all forms and fields stored in the database; determining the foreign key definition according to all the involved forms and involved fields; synthesizing a second prompt statement according to the natural language query, all the involved forms and involved fields, as well as the candidate field values and foreign key definition in all the involved fields.

[0009] In an alternative embodiment of the present application, the original large model is trained according to the first prompt statement, the second prompt statement, the first output, and the target SQL query, including: using the first prompt statement as the input and the first output corresponding to the first prompt statement as the output to form a set of input-output pairs to fine-tune the original large model to obtain a first low-rank adaptation layer. The first low-rank adaptation layer is used to collect the involved forms and fields according to the input natural language query; using the second prompt statement as the input and the target SQL query corresponding to the second prompt statement as the output to form a set of input-output pairs to fine-tune the original large model to obtain a second low-rank adaptation layer. The second low-rank adaptation layer is used to convert the input natural language query into an SQL query.

[0010] The present application also provides a database query method, which is applied to a database management system. The database management system is used to manage a database. The database stores multiple forms, where the rows of the form are records and the columns are fields. The database management system adds a historical query cache module and a natural language conversion module; the historical query cache module consists of a vector data engine and is used to perform text vectorization, vector indexing, and nearest neighbor retrieval based on the input query execution text; text vectorization refers to converting the input query into a numerical vector; vector indexing refers to saving the numerical vector obtained by converting the input query into a vector index library managed by the vector data engine; nearest neighbor retrieval refers to finding the vector with the highest similarity to the numerical vector obtained by converting the input query from the vector index library and its corresponding similarity, and the similarity is calculated using a similarity metric specified by the user; the natural language conversion module consists of a large model including a first low-rank adaptation layer and a second low-rank adaptation layer. The large model is trained according to a preset method. The first low-rank adaptation layer is used to collect the forms and fields involved according to the input natural language query, and the second low-rank adaptation layer is used to convert the input natural language query into an SQL query; the database query method includes the following steps: using the vector data engine to convert the obtained input query into a numerical vector, and the numerical vector is called the input query vector; calling the nearest neighbor retrieval function of the vector data engine to retrieve the vector with the highest similarity to the input query vector and the corresponding similarity from the vector index library. The vector with the highest similarity to the input query vector is called the nearest neighbor of the input query vector; determining whether the similarity is greater than a preset similarity threshold; if the similarity is greater than the preset similarity threshold, then output the historical SQL query associated with the nearest neighbor as the return result of the input query; if the similarity is less than or equal to the preset similarity threshold, then it is determined that there is no approximate historical query; after determining that there is no approximate historical query, determine all candidate field values in the database according to the input query. Each candidate field value is a certain value of a character-type field stored in the database, and the edit distance between the candidate field value and the input query is less than a preset threshold. The edit distance refers to the sum of the costs of character addition operations, character deletion operations, and character modification operations required to rewrite the candidate field value into the input query. The unit costs of character addition operations, character deletion operations, and character modification operations are preset by the user; synthesize a first prompt statement according to the input query, all candidate field values, and all forms and fields in the database; use the first prompt statement as the input of the first low-rank adaptation layer and call the first low-rank adaptation layer for processing to obtain a first output; determine the forms and fields involved in the first output, mark them as the involved forms and involved fields, and determine the foreign key definition according to all the involved forms and involved fields; synthesize a second prompt statement according to the input query, all the involved forms and fields, and the candidate field values and foreign key definitions in all the involved fields; use the second prompt statement as the input of the second low-rank adaptation layer and call the second low-rank adaptation layer for processing to obtain a second output, and the second output is an SQL query statement;Return a second output and the answer set obtained by answering the second output in the database.

[0011] In an alternative embodiment of the present application, invoking a first low-rank adaptation layer for processing to obtain a first output includes: the first low-rank adaptation layer processes a first prompt statement to obtain a first candidate output with a preset first candidate number; determining whether each first candidate output meets a first output specification, where the first output specification is determined by the natural language conversion module during the training phase; if all first candidate outputs do not meet the first output specification, discard all first candidate outputs and regenerate the first candidate output according to the first prompt statement; if there is a first candidate output that meets the first output specification, select the first candidate output with the most repeated times and determine whether the first candidate output with the most repeated times is multiple; if there is only one, mark the unique first candidate output with the most repeated times as the first output; if there are multiple, select any one of the first candidate outputs with the most repeated times and the most total number of fields and mark it as the first output.

[0012] In an alternative embodiment of the present application, invoking a second low-rank adaptation layer for processing to obtain a second output includes: the second low-rank adaptation layer processes a second prompt statement to obtain a second candidate output with a preset second candidate number; selecting SQL statements that meet the execution conditions from the second candidate output and marking them as candidate SQLs; if there are no candidate SQLs, regenerate the second candidate output according to the second prompt statement; if there are candidate SQLs, divide all candidate SQLs into multiple equivalent classes according to the answer set of the candidate SQLs in the database, so that the answer sets of the candidate SQLs included in the same equivalent class are the same, and the answer sets of the candidate SQLs included in different equivalent classes are different; determining a target equivalent class from the equivalent classes, where the target equivalent class is the equivalent class with the largest number of candidate SQLs among all equivalent classes, or, when there are multiple equivalent classes that all include the most candidate SQLs, arbitrarily select one of these equivalent classes with the largest number of answers in the corresponding answer set as the target equivalent class; arbitrarily select one candidate SQL from the target equivalent class and mark it as the second output.

[0013] In an alternative embodiment of the present application, before returning the second output, the method further includes: using a vector data engine to add the input query vector to the vector index library and saving the second output as the historical SQL query associated with the input query vector.

[0014] The present application also provides a computer device, including a processor and a memory: the processor is configured to execute a computer program stored in the memory to implement the method as described above.

[0015] The present application also provides a computer-readable storage medium, storing a computer program, which when executed by a processor, implements the method as described above.

[0016] Adopting the embodiments of the present application has the following beneficial effects: The present application can achieve the following expected technical effects: (1) The computing resource requirements are not high, and it can be deployed and implemented on a PC with a single GPU at the civilian level (24GB video memory), that is, the large model service trained by the present application can be deployed with a single GPU at the civilian level; (2) The conversion time is in seconds, that is, the natural language query can be converted into an SQL query within an average of several seconds; (3) Reliable conversion, that is, it can at least generate an SQL query that can be actually executed in the database, and effectively limits that all data entities involved in the query exist in the database.

[0017] The above description is only an overview of the technical solution of the present application. In order to understand the technical means of the present application more clearly, it can be implemented according to the content of the specification. And in order to make the above and other purposes, features and advantages of the present application more obvious and understandable, the following specific preferred embodiments are given and described in detail in conjunction with the accompanying drawings. It should be understood that the above general description and the following detailed description are only exemplary and explanatory, and cannot limit the present application. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the drawings in the following description are only some embodiments of the present application. For those of ordinary skill in the art, without creative efforts, other drawings can be obtained based on these drawings.

[0019] Figure 1 It is a schematic flowchart of a large model training method for query conversion provided for an embodiment.

[0020] Figure 2 It is a schematic flowchart of a database query method provided for an embodiment.

[0021] Figure 3 It is a schematic block diagram of the structure of a computer device provided for an embodiment. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0022] The following will clearly and completely describe the technical solutions in the embodiments of the present application in conjunction with the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative efforts belong to the scope of protection of the present application.

[0023] Large model technology has now become the mainstream technology for natural language queries in databases, but there are still outstanding challenge issues with this technology itself. For example, there are the following problems: 1. It has high computational resource requirements, and it is usually difficult to deploy and implement on a civilian-grade single GPU (with video memory not exceeding 24GB); 2. The conversion rate is relatively low, and it often takes a long time to wait for converting natural language queries into SQL queries; 3. The accuracy is insufficient, and there may be either no conversion from natural language queries to SQL queries or the converted SQL cannot be actually executed. To solve the above problems, this application provides a natural language query interface for databases, which is applied to a database management system to achieve automatic conversion from natural language queries to SQL queries. It can not only be deployed on a civilian-grade single GPU, but also has a significant improvement in the conversion rate and conversion reliability. Among them, the technical solution of this application is divided into a training stage and an application stage. In the training stage, it is a large model training method for query conversion, and in the application stage, it is a database query method. It should be noted that the solution provided by this application must first have a module based on the large model generated in the training stage, and then this module is used to support the processing in the query stage. It cannot be separately treated as two independent claims, and the two are integrated.

[0024] First, describe the large model training method for query conversion in the training stage. To clearly describe the large model training method for query conversion provided in this embodiment, please refer to Figure 1 , including steps S110 to S140.

[0025] Step S110: Obtain an original large model and a training record set, where the training record set includes multiple training records.

[0026] In one implementation, the original large model and the training record set are obtained in the training stage. Among them, the original large model is an open-source large model that can be deployed on a civilian-grade single GPU. A typical civilian-grade single GPU is a GPU with video memory not exceeding 24GB. Most of the open-source large models that can be deployed on a civilian-grade single GPU are large models with no more than 10 billion parameters, including but not limited to glm-4-9b-chat, internlm2_5-chat-7b, chatglm3-6b, Qwen2.5-7B-Instruct, etc. This application does not limit the specific open-source large model used. Whether it is the ones listed or not, as long as it is an open-source large model that can be deployed on a civilian-grade single GPU, it belongs to the original large model referred to in this application.

[0027] The training record set includes multiple training records. Each training record is a binary tuple composed of a natural language query and a target SQL query.

[0028] Step S120: Process all training records one by one to obtain a first prompt statement, a second prompt statement, a first output, and a target SQL query.

[0029] In one embodiment, when processing all training records one by one, the following order can be followed for processing.

[0030] First step, for the target SQL query in each training record, convert the target SQL query into an Abstract Syntax Tree (AST). Tools including but not limited to sqlparser, pglast, etc. can be used to convert the target SQL query into an AST representation, and this application does not limit the specific conversion tool used.

[0031] Second step, determine the forms and fields involved in the target SQL query according to the abstract syntax tree, which are respectively called the involved forms and involved fields. The specific method is to traverse the nodes in the AST and identify the nodes with the form name type or field name type as the forms and fields output in this step, which are respectively denoted as the involved forms and involved fields.

[0032] Third step, determine all candidate field values in the database according to the natural language query. Each candidate field value is a certain value of a character type field stored in the database, and the edit distance between each candidate field value and the natural language query is less than a preset threshold. The edit distance refers to the sum of the costs of character addition operations, character deletion operations, and character modification operations required to rewrite the candidate field value into the natural language query. The unit costs of character addition operations, character deletion operations, and character modification operations are preset by the user. Among them, for example: the unit cost of adding characters is fixed at 0, and the unit costs of deleting characters and modifying characters are both preset positive integers. Intuitively, this step outputs all candidate field values that match at least one substring in the natural language query with a high enough degree.

[0033] Fourth step, synthesize a first output according to all the involved forms and involved fields. The first output is a JSON dictionary, including multiple dictionary items. Each dictionary item is represented in the form of a key-value pair, where the key is the form name of a certain involved form, and the value is the list of fields corresponding to the involved form. A specific template example of the corresponding specification of the first output is shown in the following table.

[0034] Table 1

[0035] It can be understood that the corresponding specification of the first output shown in the above table is only an illustrative example for easy understanding. It is not a limitation on the content therein, and the actual situation can be adjusted according to requirements.

[0036] Step 5: Synthesize the first prompt statement based on the natural language query, all candidate field values, and all forms and fields stored in the database. The first prompt statement has clear guidance, prompting the large model to output the forms and fields involved in descending order of relevance to the natural language query. The forms and fields involved are represented as a JSON dictionary with dictionary items as key-value pairs. In the dictionary item, the key is the name of the form involved, and the value is the list of fields involved in the corresponding form. This application does not limit the specific template of the first prompt statement. However, for the convenience of explanation, an example of a template that has passed validity verification is given, where {X} represents the text that needs to be replaced with a specific value. The specific guiding content can be referred to in Table 2 shown below.

[0037] Table 2

[0038] Continued Table 2

[0039] The guiding statements shown above are only examples of the technical solutions. This application does not limit the specific template of the prompt statement for form and field filtering, that is, the first prompt statement. As long as the template adopted has clear guidance and can prompt the large model to output the forms and fields involved in descending order of relevance to the natural language query, it can be used in the technical solution of this application. It should be noted that the content listed in the above code segment belongs to the guiding statement, which is used to guide the large model to filter the forms and fields in the database. Without these guiding sentences, the large model will not select the forms and fields involved in the natural language query. That is to say, all the content of the code segments exemplified in this application is necessary under the corresponding implementation manners and has corresponding technical meanings, and is used to achieve specific purposes to complete the functions required by this application.

[0040] Step 6: Determine the foreign key definition based on all the forms and fields involved. The form of each foreign key definition is "Form 1.Field 1 = Form 2.Field 2", where Form 1 and Form 2 are the forms involved, and Field 1 and Field 2 are the fields involved.

[0041] Step 7: Synthesize the second prompt statement based on the natural language query, all the forms and fields involved, as well as the candidate field values and foreign key definitions in all the fields involved. The second prompt statement has clear guidance, prompting the large model to reply with an SQL query. This application does not limit the specific template of the second prompt statement. Only an example of a template that has passed validity verification is given, where {X} represents the text that needs to be replaced with a specific value, as shown in the following table.

[0042] Table 3

[0043] The present application does not limit the specific template of the prompt statement for query conversion, i.e., the second prompt statement. As long as the adopted template has clear guidance and can prompt the large model to reply with an SQL query, it can be used in the technical solution of the present application. It should be noted that the symbol group "" in the above code segment is not the comment symbol in the normal code segment, but the title symbol in Markdown. The large model can understand this title symbol and perform corresponding processing. Also, there is no content after the final "SELECT", which is used to guide the large model to then give the SQL statement after SELECT.

[0044] Step S130: Train the original large model according to the first prompt statement, the second prompt statement, the first output, and the target SQL query.

[0045] In one embodiment, after processing all the training records in step S120 to obtain the first prompt statement, the second prompt statement, and the first output, collect the sets of all (first prompt statement, first output) pairs and all (second prompt statement, target SQL query) pairs, which can be used to train the original large model.

[0046] Among them, according to the set of (first prompt statement, first output) pairs, use the first prompt statement as the input and the first output corresponding to the first prompt statement as the output to form a set of input-output pairs, and adopt the LoRA (Low-Rank Adaptation) technology to fine-tune the original large model to obtain the first low-rank adaptation layer. The first low-rank adaptation layer is used to collect the forms and fields involved according to the input natural language query.

[0047] According to the set of (second prompt statement, target SQL query) pairs, use the second prompt statement as the input and the target SQL query corresponding to the second prompt statement as the output to form a set of input-output pairs, and adopt the LoRA technology to fine-tune the original large model to obtain the second low-rank adaptation layer. The second low-rank adaptation layer is used to convert the input natural language query into an SQL query.

[0048] The fine-tuning process of obtaining the first low-rank adaptation layer and the second low-rank adaptation layer can be implemented using tools such as LLaMA-Factory. The present application does not limit the specific fine-tuning tool adopted. As long as the adopted fine-tuning tool supports the low-rank adaptation (LoRA) fine-tuning mechanism, it can be used in the technical solution of the present application. After the above processing, the training can be completed to obtain the first low-rank adaptation layer and the second low-rank adaptation layer.

[0049] Step S140: Deploy the trained original large model as a natural language conversion module within the database management system.

[0050] In one embodiment, the natural language conversion module is used to collect forms and fields related to the input natural language query, and to convert the input natural language query into an SQL query. In fact, the natural language conversion module includes step S130 of training to obtain a first low-rank adaptation layer and a second low-rank adaptation layer. The first low-rank adaptation layer is used to collect the forms and fields involved according to the input natural language query, and the second low-rank adaptation layer is used to convert the input natural language query into an SQL query.

[0051] After the training phase is completed, the application phase can be entered according to the trained natural language conversion module. The application phase provides a database query method, which is applied to a database management system. The database management system is used to manage a database, and the database stores multiple forms, where the rows of the form are records and the columns are fields.

[0052] The database management system is newly added with a historical query cache module and a natural language conversion module.

[0053] The historical query cache module is composed of a vector data engine, and is used to perform text vectorization, vector indexing, and nearest neighbor retrieval according to the input query. The vector data engine can be implemented using the PGVector plugin of the PostgreSQL database or a separately deployed vector data management tool such as Milvus. The present application does not limit the specific implementation manner of the vector data engine.

[0054] Text vectorization refers to converting the input natural language query into a numerical vector. The present application can adopt a multilingual embedding model such as paraphrase-multilingual-MiniLM-L12-v2 as the implementation model of text vectorization to support natural language queries expressed in different languages. The present application does not limit the specific implementation model of text vectorization, as long as the model can ensure that natural language queries with similar semantics expressed in different languages are converted into similar vectors.

[0055] Vector indexing refers to saving the numerical vector obtained by converting the input natural language query into a vector index library managed by the vector data engine.

[0056] Nearest neighbor retrieval refers to finding the vector with the maximum similarity to the numerical vector obtained by converting the input natural language query and its corresponding similarity from the vector index library. The similarity is calculated using a similarity metric specified by the user. In order to ensure that the vector retrieval has the characteristics of short latency and high throughput, the vector data engine needs to support efficient approximate nearest neighbor retrieval algorithms, such as HNSW, IVF-Flat, and DiskANN, etc. The present application does not limit the nearest neighbor retrieval algorithm used during vector retrieval and its corresponding vector index data structure.

[0057] The natural language conversion module consists of an open-source large model including a first low-rank adaptation layer and a second low-rank adaptation layer. The open-source large model is trained according to the large model training method for query conversion in the training stage. The first low-rank adaptation layer is used to collect the forms and fields involved according to the input natural language query, and the second low-rank adaptation layer is used to convert the input natural language query into an SQL query. This application does not limit the deployment method of the natural language conversion module. The natural language conversion module can use a service framework such as vLLM that supports single-model multi-adaptation layer deployment (this deployment method only occupies a very small amount of additional video memory compared to the deployment of a pure single model) to provide large model services. That is to say, this application does not limit the specific deployment method of the natural language conversion module, as long as it is deployed using a service framework that supports single-model multi-adaptation layers.

[0058] To clearly describe the database query method proposed in this application, reference can be made to Figure 2 , including steps S210 to S290.

[0059] Step S210: Use the vector data engine to convert the obtained input query into a numerical vector, which is called the input query vector; call the nearest neighbor retrieval function of the vector data engine to retrieve from the vector index library the vector with the highest similarity to the input query vector and the corresponding similarity. The vector with the highest similarity to the input query vector is called the nearest neighbor of the input query vector.

[0060] In one embodiment, when a natural language query input by the user is received, the natural language query is marked as the input query. Call the vector data engine to convert the input query into a numerical vector of a preset dimension, which is marked as the input query vector.

[0061] Call the vector data engine again to retrieve the nearest neighbor of the input query vector, that is, the most similar query vector, from the historical query vector index, and obtain the similarity of the nearest neighbor of the input query vector.

[0062] Step S220: Determine whether the similarity is greater than a preset similarity threshold.

[0063] If the similarity is greater than the preset similarity threshold, then execute step S230: Output the historical SQL query associated with the nearest neighbor as the return result of the input query.

[0064] In one embodiment, if the similarity is greater than the preset similarity threshold, it means that a historical query similar to the input query has been retrieved and output in the historical database. Therefore, the historical SQL query associated with the nearest neighbor can be directly output as the return result of the input query. Otherwise, execute step S240 and its subsequent steps.

[0065] If the similarity is less than or equal to the preset similarity threshold, then step S240 is executed: It is determined that there is no approximate historical query, and all candidate field values in the database are determined according to the input query. Each candidate field value is a certain value of a character type field stored in the database, and the edit distance between the candidate field value and the input query is less than the preset threshold.

[0066] In one embodiment, if the similarity is less than or equal to the preset similarity threshold, it is directly determined that there is no approximate historical query. The candidate field values are obtained. Each candidate field value is a certain value of a character type field stored in the database, and the edit distance between the candidate field value and the input query is less than the preset threshold. The edit distance refers to the sum of the costs of character addition operations, character deletion operations, and character modification operations required to rewrite the candidate field value into the input query. The unit costs of character addition operations, character deletion operations, and character modification operations are preset by the user. For example, the unit cost of the character addition operation is fixed at 0, and the unit costs of the character deletion operation and the character modification operation are both preset positive integers.

[0067] After step S240, step S250 is executed: A first prompt statement is synthesized according to the input query, all candidate field values, and all forms and fields in the database; the first prompt statement is used as the input of the first low-rank adaptation layer, and the first low-rank adaptation layer is called to process to obtain a first output.

[0068] In one embodiment, the synthesis of the first prompt statement has been described in detail above. Specifically, it can refer to the processing process in the previous training process and will not be elaborated here. It should be noted that the output first prompt statement is defined according to the first prompt statement specification adopted in the training stage.

[0069] Taking the first prompt statement as the input of the first low-rank adaptation layer, the first low-rank adaptation layer processes the first prompt statement to obtain a first candidate output with a preset first candidate number. It is judged whether each first candidate output meets the first output specification. The first output specification is determined by the natural language conversion module in the training stage, that is, the first output specification is the JSON dictionary with the form name as the key-value pair described above, where the key is the form name and the value is the field list.

[0070] If all the first candidate outputs do not meet the first output specification, then all the first candidate outputs are discarded, and the first candidate output is regenerated according to the first prompt statement. Repeat the execution until the first output is obtained.

[0071] If there is a first candidate output that meets the first output specification, select the first candidate output with the most occurrences, and determine whether there are multiple first candidate outputs with the most occurrences. If there is only one, mark the unique first candidate output with the most occurrences as the first output; if there are multiple, select any one of the first candidate outputs with the most occurrences and the largest total number of fields and mark it as the first output.

[0072] Step S260: Determine the forms and fields involved in the first output, mark them as the involved forms and involved fields, and determine the foreign key definition based on all the involved forms and involved fields.

[0073] In one embodiment, obtain all foreign key definitions in the form of "Form 1.Field 1 = Form 2.Field 2", where Form 1 and Form 2 are the involved forms, and Field 1 and Field 2 are the involved fields.

[0074] Step S270: Synthesize a second prompt statement based on the input query, all the involved forms and involved fields, and the candidate field values and foreign key definitions in all the involved fields.

[0075] In one embodiment, for the synthesis process of the second prompt statement, the processing process of the second prompt statement in the previous text can be referred to and will not be elaborated here. The second prompt statement output here is defined according to the second prompt statement specification adopted in the training stage.

[0076] Step S280: Use the second prompt statement as the input to the second low-rank adaptation layer, and call the second low-rank adaptation layer for processing to obtain a second output, where the second output is an SQL query statement.

[0077] In one embodiment, calling the second low-rank adaptation layer for processing to obtain a second output includes: the second low-rank adaptation layer processes the second prompt statement to obtain a second candidate output with a preset second candidate number. Select the SQL statements that meet the execution conditions from the second candidate output and mark them as candidate SQLs. The judgment process can be to determine whether the second candidate output can be normally executed through the database management system in the system architecture of the present application; if the second candidate output can be parsed and executed by the database management system, mark the second candidate output as a candidate SQL and save its corresponding answer set; otherwise, ignore the second candidate output. If at least one candidate SQL cannot be selected from the second candidate output, regenerate the second candidate output according to the second prompt statement and execute the above selection steps again until a candidate SQL is obtained.

[0078] Divide all candidate SQLs into multiple equivalence classes according to their answer sets in the database, so that the answer sets of candidate SQLs contained in the same equivalence class are consistent, and the answer sets of candidate SQLs contained in different equivalence classes are different. Determine the target equivalence class from the equivalence classes, which is the equivalence class containing the largest number of candidate SQLs among all equivalence classes, or, when there are multiple equivalence classes that contain the largest number of candidate SQLs, select one of these equivalence classes with the largest number of answers in the corresponding answer set as the target equivalence class. Select one candidate SQL from the target equivalence class and mark it as the second output.

[0079] Step S290: Return the second output and the answer set obtained by answering the second output in the database.

[0080] In one embodiment, before step S290: returning the second output, the method further includes: using the vector data engine to add the input query vector to the vector index library, and saving the second output as a historical SQL query associated with the input query vector.

[0081] The above technical solution can achieve three technical effects that are difficult to achieve with existing technologies.

[0082] First, this technical solution does not require much computing resources. It only uses a moderate amount of computing resources to support natural language query conversion and can be deployed on a single GPU machine with 24GB of video memory at the civilian level. Experimental verification shows that most open source large models with no more than 10 billion parameters, such as glm-4-9b-chat, internlm2_5-chat-7b, chatglm3-6b, and Qwen2.5-7B-Instruct, can be deployed on a single GPU with 24GB of video memory as a large model service with a single model and multiple adaptation layers.

[0083] Secondly, a vector data engine is introduced to improve the efficiency of natural language query conversion. For a single natural language query, generally only two large model service calls are required to convert the natural language query into an SQL query in this technical solution. In addition, both the vectorization of natural language queries and vector-based nearest neighbor retrieval can be completed within milliseconds. Therefore, during the application stage, a natural language query can be converted into an SQL query within an average of several seconds. That is to say, this technical solution supports converting a natural language query into an SQL query within an average of several seconds. At the same time, the introduced vector data engine also provides a caching mechanism for the conversion of natural language queries. This mechanism converts historical natural language queries into vectors and saves them in the cache. When a new natural language query is first converted into a vector and compared with the historical natural language queries in the cache, if the most similar historical natural language query is similar enough, the conversion result of that historical natural language query, namely the corresponding SQL query, is directly returned. Since both the vectorization of natural language queries and vector-based nearest neighbor retrieval can be completed within milliseconds, a natural language query that hits the cache can be converted into an SQL query within milliseconds. In addition, the vectorization of natural language queries also facilitates multilingual applications. Even if the same query is expressed in natural languages of different languages, as long as the multilingual embedding model adopted in the vector data engine can ensure that texts with relatively close semantics have a high similarity, the input natural language query can find a matching query among the historical natural language queries in different languages, thus hitting the cache and immediately returning the converted SQL query.

[0084] Finally, this technical solution ensures the reasonable reliability of the natural language query conversion results through various measures, which are reflected in the following aspects. First, after filtering relevant forms and fields, the prompt statement for query conversion is synthesized, making the prompt statement for query conversion shorter and avoiding being misled by irrelevant forms and fields. Second, by adopting the mechanism of multi-candidate output of the large model, it is ensured that the filtering results of forms and fields are the majority voting results that conform to the specified JSON dictionary specification, and the finally output SQL query can be executed in the database and return the answer set supported by the majority voting mechanism. This measure essentially utilizes the general rule that the more frequently the output results of the large model appear, the more reasonable and reliable they are to improve the reasonable reliability of the query conversion results. Third, the data entities stored in the database related to the query are explicitly given in the prompt statement for query conversion, that is, the second prompt statement, so as to limit the range of data entities involved in the finally output SQL query and avoid the appearance of data entities not in the database. Fourth, the data entities stored in the database related to the query are also explicitly given in the prompt statement for form and field filtering, that is, the first prompt statement, so as to avoid missing the fields whose field names do not overlap with the natural language query but whose field values match a certain entity mentioned in the query. The comprehensive application of the above measures can effectively improve the execution accuracy of the natural language query conversion results. Through experimental verification, the open-source large model glm-4-9b-chat without fine-tuning only achieved an execution accuracy of about 70% on the validation sets of the two benchmark datasets Spider and CSpider, and after fine-tuning, the execution accuracy can be increased to more than 80% on the same validation sets.

[0085] Figure 3 The internal structure diagram of a computer device in an embodiment is shown. The computer device can specifically be a terminal or a server. As Figure 3 shown, the computer device includes a processor, a memory, and a network interface connected through a system bus. Among them, the memory includes a non-volatile storage medium and an internal memory. The non-volatile storage medium of the computer device stores an operating system and can also store a computer program. When the computer program is executed by the processor, the processor can implement the large model training method or database query method for query conversion. The internal memory can also store a computer program. When the computer program is executed by the processor, the processor can execute the large model training method or database query method for query conversion. Those skilled in the art can understand that Figure 3 the structure shown in

[0086] In one embodiment, the present application further provides a computer-readable storage medium storing a computer program, which when executed by a processor, causes the processor to perform the steps of the method described in any of the foregoing embodiments.

[0087] Those of ordinary skill in the art can understand that all or part of the processes of implementing the methods in the above embodiments can be completed by instructing relevant hardware through a computer program. The program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. Among them, any reference to a memory, storage, database, or other medium used in the various embodiments provided by the present application can include non-volatile and / or volatile memories. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in various forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDR SDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), Rambus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and Rambus dynamic RAM (RDRAM), etc.

[0088] The technical features of the above embodiments can be combined arbitrarily. For the sake of brevity of description, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, it should be considered as the scope described in this specification.

[0089] The above-described embodiments merely represent several implementation manners of the present application, and the description thereof is relatively specific and detailed, but it should not be construed as a limitation on the patent scope of the present application. It should be noted that for those of ordinary skill in the art, without departing from the concept of the present application, several modifications and improvements can still be made, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the appended claims.

Claims

1. A large model training method for query conversion, applied to database management system, characterized in that: The steps include: Obtaining an original large model and a training record set, wherein the training record set includes a plurality of training records; Processing all the training records one by one to obtain a first prompt statement, a second prompt statement, a first output, and a target SQL query; Training the original large model according to the first prompt statement, the second prompt statement, the first output, and the target SQL query; The trained original large model is deployed as a natural language conversion module in the database management system, and the natural language conversion module is used to collect forms and fields related to the input natural language query, and to convert the input natural language query into an SQL query.

2. The large model training method for query conversion according to claim 1, characterized in that: Each training record is a tuple consisting of a natural language query and a target SQL query. The processing of all training records one by one includes: Convert the target SQL query into an abstract syntax tree, and determine the form and fields involved in the target SQL query according to the abstract syntax tree, which are referred to as involved forms and involved fields; The first output is synthesized according to all the involved forms and the involved fields, wherein the first output is a JSON dictionary including a plurality of dictionary items, each of the dictionary items is represented in the form of a key-value pair, the key is a form name of a certain involved form, and the value is a field list corresponding to the involved form; Determine all candidate field values ​​in the database according to the natural language query, each of the candidate field values ​​is a certain value of a character field stored in the database, and the edit distance between each of the candidate field values ​​and the natural language query is less than a preset threshold, the edit distance refers to the sum of the costs of character addition operations, character deletion operations, and character modification operations required for rewriting the candidate field value into the natural language query, and the unit costs of the character addition operations, character deletion operations, and character modification operations are preset by the user; synthesize the first prompt statement according to the natural language query, all the candidate field values, and all the forms and fields stored in the database; Determine a foreign key definition based on all the involved forms and the involved fields; and synthesize a second prompt statement based on the natural language query, all the involved forms and the involved fields, and the candidate field values ​​in all the involved fields and the foreign key definition.

3. The large model training method for query conversion according to claim 1, characterized in that: The training of the original large model according to the target SQL query, the first prompt statement, the second prompt statement, the first output and the target SQL query includes: Taking the first prompt sentence as input and the first output corresponding to the first prompt sentence as output to form a set of input-output pairs to fine-tune the original large model to obtain a first low-rank adaptation layer, wherein the first low-rank adaptation layer is used to collect the forms and fields involved according to the input query; The second prompt statement is taken as input, and the target SQL query corresponding to the second prompt statement is taken as output to form a set of input-output pairs to fine-tune the original large model to obtain a second low-rank adaptation layer, wherein the second low-rank adaptation layer is used to convert the input natural language query into an SQL query.

4. A database query method, applied to a database management system, wherein the database management system is used to manage a database, wherein the database stores a plurality of tables, wherein the rows of the tables are records and the columns are fields, wherein: The database management system adds a historical query cache module and a natural language conversion module; The historical query cache module is composed of a vector data engine, which is used to perform text vectorization, vector indexing and nearest neighbor retrieval according to the input query; the text vectorization refers to converting the input query into a numerical vector; the vector indexing refers to saving the numerical vector converted from the input query into a vector index library managed by the vector data engine; the nearest neighbor retrieval refers to finding the vector with the greatest similarity to the numerical vector converted from the input query and its corresponding similarity from the vector index library, and the similarity is calculated using a similarity metric specified by the user; The natural language conversion module is composed of a large model including a first low-rank adaptation layer and a second low-rank adaptation layer, wherein the large model is trained according to the method according to any one of claims 1 to 3, wherein the first low-rank adaptation layer is used to collect the forms and fields involved according to the input natural language query, and the second low-rank adaptation layer is used to convert the input natural language query into an SQL query; The database query method comprises the following steps: The obtained input query is converted into a numerical vector by using the vector data engine, and the numerical vector is called the input query vector; the nearest neighbor search function of the vector data engine is called to retrieve the vector with the greatest similarity to the input query vector and the corresponding similarity from the vector index library, and the vector with the greatest similarity to the input query vector is called the nearest neighbor of the input query vector; Determining whether the similarity is greater than a preset similarity threshold; If the similarity is greater than a preset similarity threshold, the historical SQL query associated with the nearest neighbor is output as the return result of the input query; If the similarity is less than or equal to the preset similarity threshold, it is determined that there is no similar historical query; After determining that there is no similar historical query, all candidate field values ​​in the database are determined according to the input query, each of the candidate field values ​​is a certain value of a character field stored in the database, and the edit distance between the candidate field value and the input query is less than a preset threshold, the edit distance refers to the sum of the costs of character addition operations, character deletion operations, and character modification operations required to rewrite the candidate field value into the input query, and the unit costs of the character addition operations, character deletion operations, and character modification operations are preset by the user; synthesizing a first prompt statement according to the input query, all the candidate field values, and all the forms and fields in the database; using the first prompt statement as the first low-rank adaptation layer input, and calling the first low-rank adaptation layer for processing to obtain a first output; Determine the forms and fields involved in the first output, mark them as involved forms and involved fields, and determine foreign key definitions based on all the involved forms and involved fields; synthesizing a second prompt statement according to the input query, all the involved forms and the involved fields, and the candidate field values ​​in all the involved fields and the foreign key definition; Using the second prompt statement as the second low-rank adaptation layer input, calling the second low-rank adaptation layer for processing to obtain a second output, where the second output is a SQL query statement; The second output and an answer set obtained by answering the second output in the database are returned.

5. The database query method according to claim 4, characterized in that: The calling the first low-rank adaptation layer to obtain a first output includes: The first low-rank adaptation layer processes the first prompt sentence to obtain a first candidate output with a preset first candidate number; Determining whether each of the first candidate outputs meets a first output specification, the first output specification being determined by the natural language conversion module during a training phase; If all the first candidate outputs do not meet the first output specification, discard all the first candidate outputs, and regenerate the first candidate output according to the first prompt statement; If there is a first candidate output that meets the first output specification, select the first candidate output with the largest number of repetitions, and determine whether there are multiple first candidate outputs with the largest number of repetitions; if there is only one, mark the unique first candidate output with the largest number of repetitions as the first output; if there are multiple first candidate outputs, select any one of the first candidate outputs with the largest total number of fields and the largest number of repetitions as the first output.

6. The database query method according to claim 4, characterized in that: The calling the second low-rank adaptation layer to obtain a second output includes: The second low-rank adaptation layer processes the second prompt sentence to obtain a second candidate output with a preset second candidate number; Select an SQL statement that meets the execution condition from the second candidate output and mark it as a candidate SQL statement; If the candidate SQL does not exist, regenerate the second candidate output according to the second prompt statement; If the candidate SQL exists, then all the candidate SQLs are divided into multiple equivalence classes according to the answer set of the candidate SQL in the database, so that the answer set of the candidate SQL contained in the same equivalence class is consistent, and the answer set of the candidate SQL contained in different equivalence classes is different; determine the target equivalence class from the equivalence classes, the target equivalence class is the equivalence class that contains the largest number of candidate SQLs among all the equivalence classes, or, when there are multiple equivalence classes that all contain the largest number of candidate SQLs, select one of the equivalence classes with the largest number of answers in the corresponding answer set as the target equivalence class; select one candidate SQL from the target equivalence class and mark it as the second output.

7. The database query method according to claim 4, characterized in that: Before returning the second output, the method further includes: The input query vector is added to the vector index library by using the vector data engine, and the second output is saved as a historical SQL query associated with the input query vector.

8. A computer device, characterized in that: including a processor and a memory; The processor is configured to execute the computer program stored in the memory to implement the method according to any one of claims 1 to 3, or the method according to any one of claims 4 to 7.

9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 3 or the method according to any one of claims 4 to 7 is implemented.

Citation Information

Patent Citations

  • Method, system and equipment for converting natural language into SQL (Structured Query Language) and storage medium

    CN119538868A

  • Natural language database query method and system based on self-training normal form model

    CN119557390A

  • Private field Text-to-SQL (Structured Query Language) method and system based on large language model

    CN119646028A

  • Background knowledge augmentation-based SQL generation method and apparatus, device and medium

    WO2025066259A1

Cited By

  • Deep learning model training method

    CN120409595A

  • Retrieval question and answer method and device for table, medium, equipment and program product

    CN120448407A