CN-121979908-A - Natural language SQL conversion method and system based on multi-language model collaboration and metadata map injection
Abstract
The application discloses a natural language to SQL conversion method and system based on multi-language model collaboration and metadata map injection. The method comprises the steps of receiving a natural language query text input by a user, carrying out character level error correction on the natural language query text to ensure the accuracy of the input text, carrying out synonym recognition and suffix alias mapping of database fields by utilizing an elastic search engine in combination with a word vector model to solve the problem of database field naming difference, carrying out multi-round interaction through a first large language model, giving out a clarification problem to the user and further defining query intention based on user feedback, injecting a metadata map into a second large language model, wherein the metadata map comprises table structure information and business association information of the database, carrying out semantic understanding on the natural language query text or the defined query intention by the second large language model based on the injected metadata map, and generating a preliminary SQL query statement. The application improves the accuracy, flexibility and expandability of the natural language to SQL system.
Inventors
- WANG XI
- SU HAN
- GENG LUJING
- CHEN WENBIN
- LIN FENGCHENG
- Cheng Moquan
Assignees
- 中国移动通信集团福建有限公司
- 中国移动通信集团有限公司
Dates
- Publication Date
- 20260505
- Application Date
- 20251225
Claims (10)
- 1. The natural language to SQL conversion method based on the cooperation of the multiple language models and metadata map injection is characterized by comprising the following steps: S1, receiving a natural language query text input by a user, and performing character level correction on the natural language query text to ensure the accuracy of the input text; S2, carrying out synonym recognition and suffix alias mapping of the database fields by combining a word vector model through an elastic search engine on the natural language query text processed in the step S1 so as to solve the problem of naming difference of the database fields; S3, if the natural language query text processed in the step S2 still has ambiguity or ambiguity, carrying out multi-round interaction through the first large language model, giving out clarification problems to the user, and further defining the query intention based on user feedback; S4, inputting the natural language query text processed in the step S2 or the clear query intention clarified in the step S3 into a second large language model, and simultaneously, injecting a metadata map into the second large language model, wherein the metadata map comprises table structure information and business association information of a database; S5, carrying out double-track verification on the preliminary SQL query statement generated in the step S4: S51, verifying whether the preliminary SQL query statement accords with grammar rules, authority requirements and database table structure constraints of a MySQL database; s52, carrying out semantic verification on the preliminary SQL query statement by using a third large language model, and judging whether the preliminary SQL query statement accurately reflects the original query intention of the user; s6, if the two-track verification in the step S5 is passed, outputting a final SQL query statement, and if any verification is not passed, returning to the step S4 to regenerate the SQL query statement, or returning to the step S3 to further clarify the intention of the user.
- 2. The method for converting natural language to SQL based on multi-language model collaboration and metadata map injection of claim 1, wherein the natural language query text is subjected to a pre-training model BERT based on a Transformer for character level error correction Chinese, pre-trained through an MLM mask language model, is used for learning context representation of character level and word level, and performs embedding calculation on characters/words corresponding to an original sequence input by a user as follows: Wherein the method comprises the steps of Representing the length of the input sequence, i.e. the number of words, Represent the first An initial embedding vector of the individual word; The BERT-Chinese comprises an embedded layer, a multi-layer transducer encoder, a pooling layer and an output layer, by the multi-layer transducer encoder of the BERT-Chinese, Indicating that BERT encoder is at the first The context vector of the individual position outputs, resulting in a context representation, is calculated as follows: the sensitivity based on the shape-near word errors is reduced, but the model cannot be corrected correctly, in order to further improve the adaptability to the field and place name error correction tasks, Represent the first The name of the individual place is given, Is the total amount of the place name dataset, Representing a place name dataset, the error correction dataset is set as follows: Fine tuning by error correction data sets, wherein For sentences containing misspellings or place names, Corresponding corrected sentences; the fine tuning objective is to maximize the conditional probability of correcting text, equivalent to minimizing cross entropy loss, calculated as follows: Wherein, the As a function of the loss, As a conditional probability function, where For sentences containing misspellings or place names, For the corresponding corrected sentence, the sentence is corrected, Represent the first The name of the individual place is given, Is the total amount of place name dataset.
- 3. The method for converting natural language to SQL based on multi-language model collaboration and metadata map injection according to claim 1, wherein the multi-round interaction comprises starting a multi-round interaction flow if the semantic ambiguity score of step S1 exceeds a preset threshold or more than or equal to 2 high-similarity candidate fields exist in the dynamic synonym-alias mapping result of step S2; carrying out structural analysis on the preprocessed text and the dynamic mapping result through a first large language model, identifying the type of the fuzzy point and marking the position, wherein the first large language model calls a preset clarification problem template library based on the type of the fuzzy point, and the template comprises four types of entity clarification, relation clarification, constraint clarification and term clarification; Filling accurate candidate information by combining the dynamic synonym-alias mapping result and the basic business knowledge of the metadata map in the step S2 as a template, calculating the influence weight of each fuzzy point on the query intention through an information gain algorithm, preferentially clarifying the high-weight fuzzy points, outputting the first clarification problem after sequencing to a user, and receiving a feedback text through a user interaction interface; Updating the structural representation of the query intention based on the analysis result, calling the semantic understanding model of the step S1 to recalculate the ambiguity score, and taking the structural intention checked by the termination condition as the optimized query intention.
- 4. The method for converting natural language to SQL based on multi-language model collaboration and metadata map injection according to claim 1, wherein the SQL query statement comprises metadata map structured representation and injection preparation, physical layer JSON and business layer triples of the metadata map are spliced into structured prompt prefixes, two-way mapping with semantic-metadata is constructed through second large language model input, the structured prompt prefixes, user-defined query intention and SQL generating instructions are spliced into model input sequences, SQL types are judged based on query intention, service query defaults to generate SELECT statements, further fine molecular types, each clause is spliced according to SQL grammar rules to generate complete preliminary SQL, the second large language model calls an internal SQL grammar checker to verify grammar correctness of the generated SQL, the physical layer information of the metadata map is combined to check the matching of the SQL and a database structure, if grammar or consistency errors exist, the second large language model automatically corrects based on check feedback, and the SQL is regenerated until verification is passed.
- 5. The method for converting natural language to SQL based on multi-language model collaboration and metadata map injection according to claim 1, wherein the metadata map drives SQL generation to organize tables, fields and main foreign keys, synonyms and business concept information of the whole database into a graph, and the graph set is established as follows: wherein the node Including all tables and fields, edges Then their association is represented; After receiving the user intention text subjected to the error correction and normalization, searching a representative graph through a subgraph searching function, and calculating as follows: Wherein, the The sub-graph retrieves the set of graphs, To quickly locate the most relevant table and field atlases, And searching the area for the target.
- 6. The method for converting natural language into SQL based on multi-language model collaboration and metadata map injection according to claim 1, wherein the double-track verification comprises obtaining a preliminary SQL query statement generated in the step S4 and natural language text of a user original query intention, wherein the original query intention comprises a query target expressed by a natural language by a user, screening conditions, a data association relationship and an output requirement; Constructing the preliminary SQL query statement and the original query intention Q into model input sequence classification marks; Inputting the input sequence into the third large language model, and analyzing an SQL sentence through a grammar tree by an encoder of the model to generate an SQL semantic vector; Calculating cosine similarity of the SQL semantic vector and the intention semantic vector through a decoder of the third large language model to obtain a semantic matching degree score, and positioning a semantic conflict point based on vector difference, wherein the method comprises the following steps: if the query target column of SQL is inconsistent with the intended target in Q, marking that the target column is not matched; if the WHERE condition of SQL is inconsistent with the time constraint in Q, marking as screening condition conflict; If the clear association relation in the Q is omitted in SQL, marking the association relation as missing; presetting a semantic matching degree threshold T, and dynamically adjusting according to the historical verification accuracy, wherein if the semantic matching degree score is more than or equal to T and the number of semantic conflict points is less than or equal to 1, the preliminary SQL query statement is judged to accurately reflect the original query intention of the user; otherwise, judging that the conflict point is not accurately reflected, and outputting a semantic verification report containing conflict point positioning and correction suggestions.
- 7. The method for converting natural language to SQL based on multi-language model collaboration and metadata map injection according to claim 1, wherein the dual-track verification starts a GLM semantic verifier after rule verification is passed to compare user intention text Semantically-induced with SQL original query intent Q, GLM computes a confidence of match internally The calculation is as follows: And is in accordance with a preset threshold value Comparison is performed: If it is The semantics are considered to be consistent, and verification is passed; Otherwise, the GLM will present a clear to the user in a conversational form, detect precisely differentiated conditions that are missed by the SQL query, and determine if an adjustment is needed.
- 8. A natural language to SQL system based on multi-language model collaboration and metadata map injection of the natural language to SQL method based on multi-language model collaboration and metadata map injection as claimed in any one of claims 1 to 7, comprising: the preprocessing and ambiguity assessment module is used for receiving a natural language query text, executing character-level error correction through a bidirectional coding model and calculating a text semantic ambiguity score; The dynamic mapping module is used for calculating the synonym similarity and suffix alias mapping weight of the entity and the database field in the text after error correction based on the elastic search engine index library and the word vector model, and outputting a dynamic mapping result; the intention clarifying module is used for calling the first large language model to generate a context-dependent clarification problem if the semantic ambiguity score exceeds a preset threshold value, and optimizing the query intention through multiple rounds of interaction; The metadata map enhancement generation module dynamically injects a metadata map into the second large language model, wherein the metadata map comprises a table structure triplet relation, external key association weight and business semantic attribute, and a preliminary SQL query statement is generated through structured knowledge reasoning; And the double-track verification and iteration optimization module is used for executing MySQL grammar rule verification and third large language model semantic intention verification on the preliminary SQL, adjusting a pattern injection mode or triggering intention reclassification based on verification feedback, and outputting a final SQL query statement.
- 9. An electronic device, comprising: at least one memory non-transitory storing computer-executable instructions; At least one processor configured to execute the computer-executable instructions, Wherein the computer executable instructions, when executed by the processor, implement a natural language to SQL method based on multi-language model collaboration and metadata map injection according to any of claims 1-7.
- 10. A computer-readable storage medium storing computer-executable instructions that when executed by at least one processor implement the natural language to SQL method based on multi-language model collaboration and metadata map injection of any of claims 1-7.
Description
Natural language SQL conversion method and system based on multi-language model collaboration and metadata map injection Technical Field The application relates generally to the technical field of artificial intelligence data conversion, and in particular relates to a natural language SQL (structured query language) conversion method and system based on multi-language model collaboration and metadata map injection. Background With the rapid development of information technology and the popularization of database applications, the demands of users for data query and analysis on databases are increasing. The Structured Query Language (SQL) is used as a standard language for operating a relational database, has higher mastering and using thresholds, and has great difficulty for non-professional users to directly write SQL sentences for data query. Therefore, a natural language to SQL (NaturalLanguagetoSQL, NL SQL) technology has been developed, aiming at allowing users to input query demands in a natural language form, and the system automatically converts the query demands into executable SQL sentences, so that the database use threshold is reduced, and the data query efficiency is improved. The natural language to SQL method in the prior art mainly depends on a rule engine or a template-based generation mode. These methods typically predefine a large number of grammar rules or SQL templates to generate corresponding SQL statements by matching natural language queries entered by a user with predefined rules or templates. In the simple and fixed scene query task, the method can achieve a certain effect. However, existing rule or template based traditional approaches expose significant limitations when faced with complex query environments: first, the adaptability is insufficient. When the database structure is complex (such as a large number of tables, complex relationships), the user needs are changeable or the query intention is ambiguous, the predefined rules and templates are difficult to fully cover all possible situations, so that the system cannot flexibly cope with. Second, accuracy is limited. The traditional method is difficult to accurately process fuzzy query, synonym difference, ambiguous word ambiguity and irregularities (such as wrongly written words and spoken language expressions) of user input in natural language, which directly leads to that the generated SQL query statement is different from the true intention of the user, and even generates an incorrect query result. In order to solve the above problems, the present application provides a natural language to SQL system (Multi-ModelCollaborativeandMeta-KnowledgeGraphInjection-basedText-to-SQLSYSTEM) based on Multi-language model collaboration and metadata map injection. The system realizes more accurate, robust and intelligent natural language to SQL task by cooperative work of multiple models and combining Large Language Model (LLM), multi-language model cooperation (MMC) and metadata Map (MKG) technology. The system firstly carries out semantic understanding on user inquiry through a large language model and provides a database structure and a business knowledge background for the model by combining a metadata map, then further optimizes understanding of diversified inquiry through a multi-language model collaboration mechanism to ensure more accurate SQL generation, and finally ensures that the generated SQL inquiry accords with the database rule and user intention through a double-track verification mechanism (comprising MySQL rule verification and GLM semantic verification), thereby reducing risks of generating wrong and invalid inquiry. Disclosure of Invention In order to achieve the above purpose, the invention adopts the following technical scheme: In one aspect of the present invention, a method for converting natural language into SQL based on multi-language model collaboration and metadata map injection is provided, comprising the steps of: S1, receiving a natural language query text input by a user, and performing character level correction on the natural language query text to ensure the accuracy of the input text; S2, carrying out synonym recognition and suffix alias mapping of the database fields by combining a word vector model through an elastic search engine on the natural language query text processed in the step S1 so as to solve the problem of naming difference of the database fields; S3, if the natural language query text processed in the step S2 still has ambiguity or ambiguity, carrying out multi-round interaction through the first large language model, giving out clarification problems to the user, and further defining the query intention based on user feedback; S4, inputting the natural language query text processed in the step S2 or the clear query intention clarified in the step S3 into a second large language model, and simultaneously, injecting a metadata map into the second large language model, wherein the me