Large model training method for query conversion, database query method, computer device, and computer-readable storage medium
By fine-tuning the large model and combining the vector data engine, the problems of large models with large computing resources demand, low conversion rate and insufficient accuracy in database natural language queries are solved, and fast and reliable natural language to SQL query conversion on civil-grade single GPUs are achieved.
Patent Information
- Application Number
- CN202510607500.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-13
- Publication Date
- 2025-07-08
- Estimated Expiration
- 2045-05-13
AI Technical Summary
Large models have problems such as large computing resource requirements, low conversion rates and insufficient accuracy in database natural language query applications, especially on civil-grade single GPUs, which are difficult to deploy and implement reliable natural language to SQL query conversion.
Through the training stage, the big model is fine-tuned, the first low-rank adaptation layer and the second low-rank adaptation layer are built, and the vector data engine and historical query cache are combined to realize the conversion from natural language query to SQL query.
Fast and reliable natural language to SQL query conversion is realized on civil-grade single GPUs, with low computing resource requirements and improved conversion rate. The conversion results can be executed in the database and improved accuracy.
Smart Images

Figure CN120123367B_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the field of database management technology, and in particular, relates to a large model training method for query conversion, a database query method, a computer device, and a computer-readable storage medium. Background Art
[0002] In today's digital age, data has become an important basis for corporate decision-making. Although traditional database management systems can store and query data efficiently, their query languages (such as Standard Query Language, or SQL) are often obscure and difficult to understand for non-professionals, which to a certain extent limits the widespread use and value mining of data. With the rapid development of artificial intelligence technology, especially breakthroughs in the field of natural language processing, machines can understand human natural language, providing new ideas for solving the above problems. The introduction of natural language query interfaces came into being 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 lowers the threshold for data use, allowing business personnel with non-technical backgrounds to easily obtain the data they need, so as to conduct 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 questions based on predefined query templates. This not only helps to discover potential patterns and trends in data, but also helps to drive innovation and business growth.
[0003] In recent years, people's desire to interact with databases in natural language has been growing. Many researchers use machine translation technology to train general and generalizable translation models to achieve this process. The basic principle is to build an encoder network model to learn the representation 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, LargeLanguage Model, LLM) technology, a pioneering path has emerged in the form of interaction between natural language and databases. Unlike traditional translation models trained through supervised learning, large models have demonstrated 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 the need for a dedicated model training process. This paradigm shift has subverted traditional interaction methods and is revolutionizing the way databases are queried in natural language.
[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 and do not consider 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 urgently need to solve.
[0005] The foregoing description is intended to provide general background information and does not necessarily constitute prior art. Summary of the Invention
[0006] Based on this, 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 for the above problems, which can use fewer computational resources to achieve faster and more accurate natural language SQL queries.
[0007] The present application solves its technical problems by adopting the following technical solutions:
[0008] The present 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; and deploying the trained original large model as a natural language conversion module within the database management system, where 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.
[0009] In an alternative embodiment of the present application, each training record is a binary tuple consisting 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, and 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, where the first output is a JSON dictionary including multiple dictionary entries, and each dictionary entry is represented in the form of a key-value pair, with the key being the form name of a certain involved form and the value being the list of fields corresponding to the involved form; determining all candidate field values in the database according to the natural language query, where 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, and 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; and synthesizing a second prompt statement according to the natural language query, all the involved forms and involved fields, and the candidate field values and foreign key definition in all the involved fields.
[0010] 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, where 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, where the second low-rank adaptation layer is used to convert the input natural language query into an SQL query.
[0011] 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 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 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 largest 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 is composed 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 largest similarity to the input query vector and the corresponding similarity from the vector index library. The vector with the largest 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, outputting 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, it is determined that there is no approximate historical query; after determining that there is no approximate historical query, determining 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; synthesizing a first prompt statement according to the input query, all candidate field values, and all forms and fields in the database; using the first prompt statement as the input of the first low-rank adaptation layer and calling the first low-rank adaptation layer for processing to obtain a first output; determining the forms and fields involved in the first output, marking them as involved forms and involved fields, and determining the foreign key definition according to all involved forms and involved fields; synthesizing a second prompt statement according to the input query, all involved forms and fields, and the candidate field values and foreign key definitions in all involved fields; using the second prompt statement as the input of the second low-rank adaptation layer and calling the second low-rank adaptation layer for processing to obtain a second output, and the second output is an SQL query statement;Return the second output, as well as the answer set obtained by answering the second output in the database.
[0012] In an alternative embodiment of the present application, invoking the first low-rank adaptation layer processing to obtain a first output includes: the first low-rank adaptation layer processes the first prompt statement to obtain a first candidate output with a preset first candidate number; determines whether each first candidate output meets the first output specification, and 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.
[0013] In an alternative embodiment of the present application, invoking the second low-rank adaptation layer 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; selects SQL statements that meet the execution conditions from the second candidate outputs and marks them as candidate SQLs; if there is no candidate SQL, regenerate the second candidate output according to the second prompt statement; if there is a candidate SQL, divide all candidate SQLs into multiple equivalent classes according to the answer set in the database of the candidate SQL, 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; determine the target equivalent class from the equivalent classes, and the target equivalent class is the equivalent class with the most candidate SQLs among all equivalent classes, or, when there are multiple equivalent classes that all contain the most candidate SQLs, select any one of these equivalent classes with the most answers in the corresponding answer set as the target equivalent class; select any one candidate SQL from the target equivalent class and mark it as the second output.
[0014] In an alternative embodiment of the present application, before 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 the historical SQL query associated with the input query vector.
[0015] The present application also provides a computer device, including a processor and a memory: the processor is used to execute the computer program stored in the memory to implement the method as described above.
[0016] The present application also provides a computer-readable storage medium, storing a computer program, which implements the method as described above when executed by a processor.
[0017] Adopting the embodiments of the present application has the following beneficial effects:
[0018] The present application can achieve the following expected technical effects: (1) The computing resource requirements are not large, 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, at least an SQL query that can be actually executed in the database can be generated, and all data entities involved in the query are effectively restricted to exist in the database.
[0019] 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. 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. Description of the Drawings
[0020] 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, other drawings can be obtained based on these drawings without creative efforts.
[0021] Figure 1 It is a schematic flow chart of a large model training method for query conversion provided for an embodiment.
[0022] Figure 2 It is a schematic flow chart of a database query method provided for an embodiment.
[0023] Figure 3 It is a schematic block diagram of the structure of a computer device provided for an embodiment. Detailed Embodiments
[0024] The following will clearly and completely describe the technical solutions in the embodiments of the present application in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only some of the embodiments of the present application, rather than all of the embodiments. Based on the embodiments of 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.
[0025] Large model technology has now become the mainstream technology for natural language queries in databases, but there are still outstanding challenges with this technology itself. For example, the following problems exist: 1. High computational resource requirements, making it difficult to deploy and implement on a typical consumer-grade single GPU (with video memory not exceeding 24GB); 2. Low conversion rate, often requiring a long waiting time to convert natural language queries into SQL queries; 3. Insufficient accuracy, where either the conversion from natural language queries to SQL queries cannot be achieved, 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 the automatic conversion of natural language queries to SQL queries. It can not only be deployed on a consumer-grade single GPU, but also has a significant improvement in conversion rate and conversion reliability. Among them, the technical solution of this application is divided into a training stage and an application stage. The training stage is a large model training method for query conversion, and the application stage is a database query method. It should be noted that the solution provided by this application first needs to generate a module based on the large model in the training stage, and then use this module to support the processing in the query stage. It cannot be separately treated as two independent claims. The two are integrated.
[0026] 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 , which includes steps S110~S140.
[0027] Step S110: Obtain the original large model and the training record set. The training record set includes multiple training records.
[0028] 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 consumer-grade single GPU. A typical consumer-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 consumer-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 consumer-grade single GPU, it belongs to the original large model referred to in this application.
[0029] 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.
[0030] 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.
[0031] In one embodiment, when processing all training records one by one, the processing can be carried out in the following order.
[0032] 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.
[0033] 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, identify the nodes with the form name type or field name type as the forms and fields output in this step, and record them as the involved forms and involved fields respectively.
[0034] 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 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. Intuitively, this step outputs all candidate field values that match at least one substring in the natural language query with a high enough degree.
[0035] Fourth step, synthesize the first output according to all involved forms and involved fields. The first output is a JSON dictionary, including multiple dictionary items, and each dictionary item 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. A specific template example of the corresponding specification of the first output is shown in the following table.
[0036] Table 1
[0037]
[0038] 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.
[0039] 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 by the relevance of the natural language query from high to low. The forms and fields involved are represented by 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 ease 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.
[0040] Table 2
[0041]
[0042] Continued Table 2
[0043]
[0044] 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 by the relevance of the natural language query from high to low, it can be used in the technical solutions 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, which are used to achieve specific purposes to complete the functions required by this application.
[0045] Step 6: Determine the foreign key definition according to 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.
[0046] 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.
[0047] Table 3
[0048]
[0049] This application does not limit the specific template of the prompt statement for query conversion, i.e., the second prompt statement. As long as the template adopted has clear guidance and can prompt the large model to reply with an SQL query, it can be used in the technical solution of this application. It should be noted that the symbol group "" in the above code segment is not the comment symbol in the usual 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.
[0050] 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.
[0051] In one embodiment, after processing all 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.
[0052] 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.
[0053] 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.
[0054] The 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. This application does not limit the specific fine-tuning tool adopted. As long as the fine-tuning tool adopted supports the low-rank adaptation (LoRA) fine-tuning mechanism, it can be used in the technical solution of this 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.
[0055] Step S140: Deploy the trained original large model as a natural language conversion module within the database management system.
[0056] 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 steps S130 to train 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.
[0057] 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.
[0058] The database management system is newly added with a historical query cache module and a natural language conversion module.
[0059] The historical query cache module consists 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.
[0060] Text vectorization refers to converting the input natural language query into a numerical vector. The present application can use a multilingual embedding model such as paraphrase-multilingual-MiniLM-L12-v2 as the implementation model for text vectorization to support natural language queries expressed in different languages. The present application does not limit the specific implementation model for 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.
[0061] 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.
[0062] Nearest neighbor retrieval refers to finding the vector with the highest 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. 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.
[0063] 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.
[0064] To clearly describe the database query method proposed in this application, reference can be made to Figure 2 , which includes steps S210 to S290.
[0065] 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 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.
[0066] In an 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.
[0067] 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.
[0068] Step S220: Determine whether the similarity is greater than a preset similarity threshold.
[0069] 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.
[0070] In an 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.
[0071] If the similarity is less than or equal to the preset similarity threshold, 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.
[0072] In an 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.
[0073] 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.
[0074] In an 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.
[0075] The first prompt statement is used as the input of the first low-rank adaptation layer, and 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 conforms to 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.
[0076] If all the first candidate outputs do not conform to the first output specification, 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.
[0077] 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.
[0078] 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.
[0079] 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.
[0080] 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.
[0081] 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.
[0082] 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.
[0083] 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 outputs and mark them as candidate SQLs. The judgment process can be that the database management system in the system architecture of this application is used to determine whether the second candidate output can be executed normally; if the second candidate output can be parsed and executed by the database management system, mark the second candidate output as 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 outputs, regenerate the second candidate output according to the second prompt statement and execute the above selection step again until candidate SQLs are obtained.
[0084] 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.
[0085] Step S290: Return the second output and the answer set obtained by answering the second output in the database.
[0086] 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.
[0087] The above technical solution can achieve three technical effects that are difficult to achieve with existing technologies.
[0088] 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.
[0089] Secondly, a vector data engine is introduced to improve the efficiency of natural language query conversion. In this technical solution, 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 addition, both the vectorization of natural language queries and vector-based nearest neighbor retrieval can be completed within milliseconds. Therefore, in 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 degree of 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.
[0090] 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 the 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 the 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 law 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 scope of the data entities involved in the finally output SQL query and avoid the occurrence 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. After 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.
[0091] Figure 3 The internal structure diagram of a computer device in an embodiment is shown. This computer device can specifically be a terminal or a server. As Figure 3 shown, this 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 this 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 the 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 the database query method for query conversion. Those skilled in the art can understand that Figure 3 the structure shown in
[0092] 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 execute the steps of the method described in any of the foregoing embodiments.
[0093] Those of ordinary skill in the art can understand that all or part of the processes in the methods of 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. When the program is 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 memories can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memories 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.
[0094] 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.
[0095] The above-described embodiments merely represent several implementation manners of the present application. The description 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 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 should be subject to the appended claims.
Claims
1. A large model training method for query transformation, applied to a database management system, characterized in that, It includes the following steps: Obtain an original large model and a training record set, where the training record set includes multiple training records; Process 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; Train the original large model according to the first prompt statement, the second prompt statement, the first output, and the target SQL query; Deploy the trained original large model as a natural language conversion module within the database management system, where the natural language conversion module is used to collect forms and fields related to an input natural language query, and to convert the input natural language query into an SQL query; Among them, the training of the original large model according to the first prompt statement, the second prompt statement, the first output, and the target SQL query includes: 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 to fine-tune the original large model to obtain a first low-rank adaptation layer, where the first low-rank adaptation layer is used to collect the forms and fields involved according to the input query; 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 to fine-tune the original large model to obtain a second low-rank adaptation layer, where the second low-rank adaptation layer is used to convert the input natural language query into an SQL query.
2. The large model training method for query conversion according to claim 1, wherein, Each of the training records is a binary tuple composed of a natural language query and a target SQL query, The processing all the training records one by one includes: Convert the target SQL query into an abstract syntax tree, and determine the forms and fields involved in the target SQL query according to the abstract syntax tree, which are called involved forms and involved fields; Synthesize the first output according to all the involved forms and the involved fields. The first output is a JSON dictionary, including multiple dictionary items, and each dictionary item 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; 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 the character addition operation, the character deletion operation, and the character modification operation 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 the foreign key definition according to all the involved forms and the involved fields; synthesize the second prompt statement according to the natural language query, all the involved forms and the involved fields, and the candidate field values and the foreign key definition in all the involved fields.
3. A database query method, applied to a database management system for managing a database that stores multiple forms, where the rows of the forms are records and the columns are fields, and it is characterized in that, 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, which is used to perform text vectorization, vector indexing, and nearest neighbor retrieval according to the input query execution text; the text vectorization refers to converting the input query into a numerical vector; the 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; the nearest neighbor retrieval refers to finding the vector with the highest similarity to the numerical vector obtained by converting 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 consists of a large model including a first low-rank adaptation layer and a second low-rank adaptation layer, and the large model is trained according to the method described in any one of claims 1 to 2. 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, which is called the input query vector; calling 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, and the vector with the highest similarity to the input query vector is called the nearest neighbor of the input query vector; judging 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, and the unit costs of the character addition operation, character deletion operation, and character modification operation are preset by the user in advance; synthesize a first prompt statement according to the input query, all the candidate field values, and all the 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 to process 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 the involved fields; Synthesize a second prompt statement based on the input query, all the involved forms and fields, as well as the candidate field values and the foreign key definitions in all the involved fields; 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, which is an SQL query statement; Return the second output and the answer set obtained by answering the second output in the database.
4. The database query method according to claim 3, wherein The calling the first low-rank adaptation layer for processing to obtain a first output includes: The first low-rank adaptation layer processes the first prompt statement to obtain a first candidate output with a preset first candidate number; Determine whether each of the first candidate outputs meets the first output specification, which is determined by the natural language conversion module during the 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 are first candidate outputs that meet the first output specification, select the first candidate output with the most repeated times, and determine whether there are multiple first candidate outputs with the most repeated times; 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.
5. The database query method according to claim 3, wherein The 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 SQL statements that meet the execution conditions from the second candidate outputs and mark them as candidate SQLs; If there is no such candidate SQL, regenerate the second candidate output according to the second prompt statement; If there is such a candidate SQL, divide all the candidate SQLs into multiple equivalent classes according to the answer sets 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; determine the target equivalent class from the equivalent classes, where the target equivalent class is the equivalent class that contains the largest number of candidate SQLs among all the equivalent classes, or, when there are multiple equivalent classes that all contain the most candidate SQLs, select any one of these equivalent classes with the largest number of answers in the corresponding answer set as the target equivalent class; select any one candidate SQL from the target equivalent class and mark it as the second output.
6. The database query method according to claim 3, wherein Before returning the second output, the method further includes: Use the vector data engine to add the input query vector to the vector index library and save the second output as the historical SQL query associated with the input query vector.
7. A computer device, characterized in that, Includes 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 2, or the method according to any one of claims 3 to 6.
8. 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, it implements the method according to any one of claims 1 to 2, or the method according to any one of claims 3 to 6.
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