Method and system for converting natural language into SQL (Structured Query Language) based on multi-language model collaboration and metadata atlas injection

By employing multilingual model collaboration and metadata graph injection technology, the adaptability and accuracy issues of existing natural language to SQL conversion methods in complex query environments have been resolved, achieving efficient and reliable SQL generation and improving user experience and system adaptability.

CN121979908APending Publication Date: 2026-05-05CHINA MOBILE GRP FUJIAN CO LTD +1
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
CHINA MOBILE GRP FUJIAN CO LTD
Filing Date
2025-12-25
Publication Date
2026-05-05

AI Technical Summary

Technical Problem

Existing natural language to SQL methods are not adaptable and have limited accuracy in complex query environments. They struggle to handle fuzzy queries, differences in synonyms, and non-standard user input, resulting in generated SQL queries that do not match the user's intent.

Method used

Employing multilingual model collaboration and metadata graph injection technology, semantic understanding is achieved through a large language model, database structure and business knowledge are provided by combining metadata graphs, query intent is clarified through multi-round interactions, and the accuracy and security of SQL generation are ensured through dual-track verification.

Benefits of technology

It significantly improves the accuracy and robustness of natural language to SQL conversion systems, enabling them to better understand user intent and generate high-quality SQL queries, ensuring high reliability and transparency of generated results, and enhancing user experience and operational efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121979908A_ABST
    Figure CN121979908A_ABST
Patent Text Reader

Abstract

The invention discloses a natural language-to-SQL (Structured Query Language) conversion method and system based on multi-language model collaboration and metadata atlas injection. Comprising the following steps: receiving a natural language query text input by a user, and performing character-level error correction on the natural language query text to ensure the accuracy of the input text; synonym recognition and suffix alias mapping of database fields are performed by combining an elastic search engine with a word vector model, so that the problem of naming difference of the database fields is solved; performing multiple rounds of interaction through the first large language model, proposing a clarified question to the user, and further clarifying a query intention based on user feedback; injecting a metadata atlas into the second large language model, wherein the metadata atlas comprises table structure information and business association information of the database; the second large language model performs semantic understanding on a natural language query text or a clear query intention based on the injected metadata graph, and generates a preliminary SQL query statement. According to the method, the accuracy, the flexibility and the expandability of the system for converting the natural language into the SQL are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] Applications generally involve the field of artificial intelligence data conversion technology, and in particular, a natural language to SQL method and system based on multilingual model collaboration and metadata graph injection. Background Technology

[0002] With the rapid development of information technology and the widespread application of databases, users' demand for data querying and analysis is increasing daily. Structured Query Language (SQL), as the standard language for manipulating relational databases, has a high learning curve, making it difficult for non-professional users to directly write SQL statements for data queries. Therefore, Natural Language to SQL (NL2SQL) technology has emerged, aiming to allow users to input query requirements in natural language, which the system automatically converts into executable SQL statements, thereby lowering the barrier to database use and improving data query efficiency.

[0003] Existing natural language to SQL conversion methods primarily rely on rule engines or template-based generation. These methods typically predefine a large number of syntax rules or SQL templates, generating corresponding SQL statements by matching the user's input natural language query with the predefined rules or templates. Such methods can achieve certain results in simple, fixed-scenario query tasks.

[0004] However, when faced with complex query environments, existing rule-based or template-based traditional methods reveal significant limitations: First, there is insufficient adaptability. When the database structure is complex (e.g., many tables, complex relationships), user needs are varied, or query intent is ambiguous, predefined rules and templates cannot fully cover all possible situations, resulting in the system's inability to respond flexibly.

[0005] Secondly, accuracy is limited. Traditional methods struggle to accurately handle fuzzy queries, synonym differences, polysemous ambiguities, and non-standard user input (such as typos and colloquial expressions) in natural language. This directly leads to generated SQL queries that do not match the user's true intent, and may even produce incorrect query results.

[0006] To address the aforementioned issues, this application proposes a natural language to SQL system based on multi-model collaborative and metadata graph injection (MKG). This system achieves more accurate, robust, and intelligent natural language to SQL conversion by leveraging the collaborative work of multiple models, combining Large Language Model (LLM), Multi-language Model Collaboration (MMC), and Meta-Knowledge Graph (MKG) technologies. First, the system performs semantic understanding of user queries using the Large Language Model, and then uses the metadata graph to provide the model with database structure and business knowledge context. Next, the system further optimizes the understanding of diverse queries through a multi-language model collaborative mechanism, ensuring more accurate SQL generation. Finally, a dual-track verification mechanism (including MySQL rule verification and GLM semantic verification) ensures that the generated SQL queries conform to database rules and user intent, reducing the risk of generating incorrect and invalid queries. Summary of the Invention

[0007] To achieve the above objectives, the present invention adopts the following technical solution: One aspect of the present invention provides a natural language to SQL method based on multi-language model collaboration and metadata graph injection, comprising the following steps: S1: Receive the natural language query text input by the user, and perform character-level error correction on the natural language query text to ensure the accuracy of the input text; S2: For the natural language query text processed in step S1, use an elastic search engine combined with a word vector model to identify synonyms and map suffix aliases of database fields to solve the problem of database field naming differences. S3: If the natural language query text after step S2 is still unclear or ambiguous, multiple rounds of interaction are conducted through the first language model to raise clarification questions to the user and further clarify the query intent based on user feedback; S4: Input the natural language query text processed in step S2, or the explicit query intent clarified in step S3, into the second language model; at the same time, inject a metadata graph into the second language model, the metadata graph containing the table structure information and business association information of the database; based on the injected metadata graph, the second language model performs semantic understanding on the natural language query text or explicit query intent, and generates a preliminary SQL query statement; S5: Perform dual-track verification on the preliminary SQL query statement generated in step S4: S51: Verify whether the preliminary SQL query statement conforms to the syntax rules, permission requirements, and database table structure constraints of the MySQL database; S52: Use the third language model to perform semantic verification on the preliminary SQL query statement to determine whether it accurately reflects the user's original query intent; S6. If both verifications in step S5 pass, the final SQL query statement is output; if either verification fails, the process returns to step S4 to regenerate the SQL query statement, or returns to step S3 for further clarification of user intent.

[0008] In one optional implementation, the natural language query text undergoes character-level error correction based on the Transformer pre-trained model BERT. For Chinese, the character / word embeddings corresponding to the original sequence of user input are calculated as follows, using a pre-trained MLM masked language model to learn character-level and word-level contextual representations:

[0009] in This indicates the length of the input sequence, i.e., the number of words. Indicates the first The initial embedding vectors of each word; BERT-Chinese consists of an embedding layer, a multi-layer Transformer encoder, a pooling layer, and an output layer. Through BERT-Chinese's multi-layer Transformer encoder... Indicates the BERT encoder at the 1st The context vector output at each position is used to obtain the context representation, which is calculated as follows:

[0010] While the sensitivity to errors caused by similar-looking characters decreases, the model still cannot correct them correctly. To further improve its adaptability to domain-specific and place name error correction tasks, Indicates the first A place name, It is the total amount of the place name dataset. To represent the place name dataset, the error correction dataset is set up as follows:

[0011] Fine-tuning was performed using the error correction dataset, where For sentences containing spelling or place name errors, The corresponding corrected sentence; The fine-tuning objective is to maximize the conditional probability of the corrected text, which is equivalent to minimizing the cross-entropy loss as follows:

[0012] in, For loss function, Let be a conditional probability function, where For sentences containing spelling or place name errors, The corresponding corrected sentence, Indicates the first A place name, This represents the total amount of the place name dataset.

[0013] In one optional implementation, the multi-round interaction includes initiating the multi-round interaction process if the semantic ambiguity score in step S1 exceeds a preset threshold, or if there are ≥2 highly similar candidate fields in the dynamic synonym-alias mapping result in step S2. The first language model is used to perform structured analysis on the preprocessed text and dynamic mapping results, identify the types of ambiguities and mark their locations. The first language model calls the preset clarification question template library based on the ambiguity type. The templates include four categories: entity clarification, relation clarification, constraint clarification and terminology clarification. Combining the dynamic synonym-alias mapping results from step S2 with the basic business knowledge of the metadata graph, the template is filled with accurate candidate information. The influence weight of each ambiguous point on the query intent is calculated using the information gain algorithm. High-weight ambiguous points are clarified first, and the first clarification question after sorting is output to the user. Feedback text is received through the user interaction interface. The structured representation of the query intent is updated based on the parsing results, and the semantic understanding model in step S1 is called to recalculate the fuzziness score. The structured intent that passes the termination condition verification is used as the optimized query intent.

[0014] In one optional implementation, the SQL query statement includes a structured representation and injection preparation of the metadata graph. The physical layer JSON of the metadata graph is concatenated with the business layer triples to form a structured hint prefix. Through the input construction and semantic-metadata bidirectional mapping of the second language model, the structured hint prefix + the user's explicit query intent + the SQL generation instruction are concatenated into a model input sequence. The SQL type is determined based on the query intent. The business query generates a SELECT statement by default, which is further subdivided into subtypes. Each clause is concatenated according to the SQL syntax rules to generate a complete preliminary SQL. The second language model calls the built-in SQL syntax checker to verify the syntactic correctness of the generated SQL. Combined with the physical layer information of the metadata graph, the matching between the SQL and the database structure is checked. If there are syntax or consistency errors, the second language model automatically corrects them based on the verification feedback and regenerates the SQL until it passes the verification.

[0015] In one optional implementation, the metadata graph-driven SQL generation organizes the tables, fields, primary and foreign keys, synonyms, and business concept information of the entire database into a graph, creating the graph atlas as follows:

[0016] Among the nodes Includes all tables and fields, edges This indicates their relationship; After receiving the user intent text that has undergone the aforementioned error correction and normalization, the following calculation is performed using the subgraph retrieval function, where Retrieve represents graph retrieval:

[0017] in, Subgraph retrieval atlas, To quickly locate the most relevant tables and fields in the graph, This is the target search area.

[0018] In one optional implementation, the dual-track verification includes obtaining the preliminary SQL query statement generated in step S4, and the natural language text of the user's original query intent, wherein the original query intent includes the user's query goal, filtering conditions, data association relationships and output requirements expressed in natural language; The initial SQL query statement and the original query intent Q are used to construct a model input sequence classification label; The input sequence is input into the third language model, and the model's encoder performs syntax tree parsing on the SQL statement to generate an SQL semantic vector; at the same time, the original query intent Q is extracted to generate an intent semantic vector. The cosine similarity between the SQL semantic vector and the intent semantic vector is calculated using the decoder of the third language model to obtain a semantic matching score. Semantic conflict points are then located based on vector differences, including: If the target column of the SQL query does not match the intent target in Q, it is marked as a target column mismatch; If the WHERE condition in the SQL statement is inconsistent with the time constraint in Q, it will be marked as a filter condition conflict. If the SQL statement omits an explicit relationship in Q, it is marked as having a missing relationship. A preset semantic matching threshold T is set and dynamically adjusted based on historical verification accuracy. If the semantic matching score is ≥ T and the number of semantic conflict points is ≤ 1, then the initial SQL query statement is determined to accurately reflect the user's original query intent. Otherwise, it is judged as not accurately reflecting the situation, and a semantic verification report containing conflict point location and correction suggestions is output.

[0019] In one optional implementation, after the rule verification passes, the dual-track verification activates the GLM semantic validator to compare the user intent text. To ensure semantic consistency with the original SQL query intent Q, GLM internally calculates a match confidence score. The calculation is as follows:

[0020] and with preset threshold Comparison: like If the semantics are consistent, the verification is considered successful. Otherwise, GLM will provide clarification to the user in a dialogic manner, detect any precise distinctions missed in the SQL query, and determine whether adjustments are needed.

[0021] Another aspect of the present invention provides a natural language to SQL system based on multi-language model collaboration and metadata graph injection, comprising: The preprocessing and ambiguity assessment module receives natural language query text, performs character-level error correction through a bidirectional encoding model, and calculates the semantic ambiguity score of the text. The dynamic mapping module, based on the elastic search engine index library and word vector model, calculates the similarity of synonyms between entities in the corrected text and database fields, as well as the mapping weight of suffix aliases, and outputs the dynamic mapping results. The intent clarification module includes calling the first language model to generate context-dependent clarification questions if the semantic ambiguity score exceeds a preset threshold, and optimizing the query intent through multiple rounds of interaction; The metadata graph enhancement generation module dynamically injects metadata graphs into the second largest language model. The metadata graphs include table structure triple relationships, foreign key association weights, and business semantic attributes, and generate preliminary SQL query statements through structured knowledge reasoning. The dual-track verification and iterative optimization module performs MySQL syntax rule verification and semantic intent verification of the initial SQL query. Based on the verification feedback, it adjusts the graph injection method or triggers intent re-clarification and outputs the final SQL query statement.

[0022] In another aspect, the present invention provides an electronic device comprising: At least one memory stores computer-executable instructions non-transitory; At least one processor, configured to run the computer-executable instructions, The computer-executable instructions are implemented by the processor at runtime according to the natural language to SQL method based on multi-language model collaboration and metadata graph injection described above.

[0023] In another aspect, the present invention provides 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 multilingual model collaboration and metadata graph injection as described above.

[0024] This invention, by combining traditional methods with advanced data-driven technologies, not only significantly improves the accuracy and robustness of the natural language to SQL conversion system, but also enhances the adaptability and scalability of the model in different network environments (rapidly adapting to different environments) and complex business scenarios through the thesaurus mapping and normalization module and the metadata graph SQL generation module, via the Elasticsearch search engine and external knowledge base. The introduction of multilingual model collaboration and knowledge graph injection technologies enables the system to better understand user intent and generate high-quality SQL queries, overcoming the limitations of existing methods. Furthermore, through dual-track verification and interpretability analysis, the high reliability and transparency of the generated SQL query results are ensured, allowing direct display of the data corresponding to the SQL statements, greatly improving the user experience and operational efficiency. Attached Figure Description

[0025] Figure 1 This is a flowchart of a natural language to SQL method based on multi-language model collaboration and metadata graph injection, as provided in Embodiment 1 of the present invention; Figure 2 This is a framework diagram of a natural language to SQL system based on multi-language model collaboration and metadata graph injection, provided in Embodiment 3 of the present invention. Figure 3 This is a block diagram of the electronic device provided in Embodiment 4 of the present invention; Figure 4 This is a block diagram of a computer-readable storage medium provided in Embodiment 4 of the present invention. Detailed Implementation

[0026] The present application / disclosure will now be described in further detail with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the present application / disclosure and are not intended to limit the scope of the present application / disclosure. Furthermore, it should be noted that, for ease of description, only the parts relevant to the present application / disclosure are shown in the accompanying drawings, not the entire structure.

[0027] In this invention, unless otherwise explicitly specified and limited, the term "connection" should be interpreted broadly. For example, "connection" can be a fixed mechanical connection, a detachable mechanical connection, or an integral part; or, "connection" can be a direct connection or an indirect connection through an intermediate medium. Furthermore, unless otherwise explicitly specified and limited, the term "coupling" should be interpreted broadly. For example, "coupling" can be a direct electrical connection, such as physical contact and electrical conduction between two components; it can also be understood as an electrical connection between different components in a circuit structure through physical lines capable of transmitting electrical signals, such as copper foil or wires on a printed circuit board (PCB), to transmit electrical signals; or, "coupling" can be an indirect electrical connection between two components through an intermediate medium; or, "coupling" can be an electrical connection between two components in a non-contact manner, such as an electrical connection between two components using capacitive coupling to transmit electrical signals.

[0028] Example 1: like Figure 1 As shown, this embodiment of the invention provides a natural language to SQL method based on multi-language model collaboration and metadata graph injection, comprising the following steps: S1: Receive the natural language query text input by the user, and perform character-level error correction on the natural language query text to ensure the accuracy of the input text; S2: For the natural language query text processed in step S1, use an elastic search engine combined with a word vector model to identify synonyms and map suffix aliases of database fields to solve the problem of database field naming differences. S3: If the natural language query text after step S2 is still unclear or ambiguous, multiple rounds of interaction are conducted through the first language model to raise clarification questions to the user and further clarify the query intent based on user feedback; S4: Input the natural language query text processed in step S2, or the explicit query intent clarified in step S3, into the second language model; at the same time, inject a metadata graph into the second language model, the metadata graph containing the table structure information and business association information of the database; based on the injected metadata graph, the second language model performs semantic understanding on the natural language query text or explicit query intent, and generates a preliminary SQL query statement; S5: Perform dual-track verification on the preliminary SQL query statement generated in step S4: S51: Verify whether the preliminary SQL query statement conforms to the syntax rules, permission requirements, and database table structure constraints of the MySQL database; S52: Use the third language model to perform semantic verification on the preliminary SQL query statement to determine whether it accurately reflects the user's original query intent; S6. If both verifications in step S5 pass, the final SQL query statement is output; if either verification fails, the process returns to step S4 to regenerate the SQL query statement, or returns to step S3 for further clarification of user intent.

[0029] The above embodiments address the shortcomings of existing rule-based methods in adaptability and accuracy under complex query environments. Existing natural language to SQL methods primarily rely on rule engines or template-based generation. These methods often fail to accurately handle fuzzy queries, synonym differences, and non-standard user input when faced with diverse user queries, resulting in generated SQL queries that do not match the user's intent. This is especially true when dealing with complex database structures or changing user needs, where traditional methods exhibit poor flexibility and accuracy.

[0030] Example 2 The natural language query text undergoes character-level error correction based on the Transformer pre-trained model BERT. For Chinese, the character / word embeddings corresponding to the original sequence of user input are calculated as follows, using a pre-trained MLM masked language model to learn character-level and word-level contextual representations:

[0031] in This indicates the length of the input sequence, i.e., the number of words. Indicates the first The initial embedding vectors of each word; BERT-Chinese consists of an embedding layer, a multi-layer Transformer encoder, a pooling layer, and an output layer. Through BERT-Chinese's multi-layer Transformer encoder... Indicates the BERT encoder at the 1st The context vector output at each position is used to obtain the context representation, which is calculated as follows:

[0032] While the sensitivity to errors caused by similar-looking characters decreases, the model still cannot correct them correctly. To further improve its adaptability to domain-specific and place name error correction tasks, Indicates the first A place name, It is the total amount of the place name dataset. To represent the place name dataset, the error correction dataset is set up as follows:

[0033] Fine-tuning was performed using the error correction dataset, where For sentences containing spelling or place name errors, The corresponding corrected sentence; The fine-tuning objective is to maximize the conditional probability of the corrected text, which is equivalent to minimizing the cross-entropy loss as follows:

[0034] in, For loss function, Let be a conditional probability function, where For sentences containing spelling or place name errors, The corresponding corrected sentence, Indicates the first A place name, This represents the total amount of the place name dataset.

[0035] The multi-round interaction includes starting the multi-round interaction process if the semantic ambiguity score in step S1 exceeds a preset threshold, or if there are ≥2 highly similar candidate fields in the dynamic synonym-alias mapping result in step S2. The first language model is used to perform structured analysis on the preprocessed text and dynamic mapping results, identify the types of ambiguities and mark their locations. The first language model calls the preset clarification question template library based on the ambiguity type. The templates include four categories: entity clarification, relation clarification, constraint clarification and terminology clarification. Combining the dynamic synonym-alias mapping results from step S2 with the basic business knowledge of the metadata graph, the template is filled with accurate candidate information. The influence weight of each ambiguous point on the query intent is calculated using the information gain algorithm. High-weight ambiguous points are clarified first, and the first clarification question after sorting is output to the user. Feedback text is received through the user interaction interface. The structured representation of the query intent is updated based on the parsing results, and the semantic understanding model in step S1 is called to recalculate the fuzziness score. The structured intent that passes the termination condition verification is used as the optimized query intent.

[0036] The SQL query statement includes a structured representation and injection preparation of the metadata graph. The physical layer JSON of the metadata graph is concatenated with the business layer triples to form a structured hint prefix. Through the input construction and semantic-metadata bidirectional mapping of the second language model, the structured hint prefix + the user's explicit query intent + SQL generation instructions are concatenated into the model input sequence. Based on the query intent, the SQL type is determined. Business queries generate a SELECT statement by default, which is further subdivided into subtypes. Each clause is concatenated according to the SQL syntax rules to generate a complete preliminary SQL. The second language model calls the built-in SQL syntax checker to verify the syntactic correctness of the generated SQL. Combined with the physical layer information of the metadata graph, the matching between the SQL and the database structure is checked. If there are syntax or consistency errors, the second language model automatically corrects them based on the verification feedback and regenerates the SQL until it passes the verification.

[0037] The metadata graph-driven SQL generation organizes the tables, fields, primary and foreign keys, synonyms, and business concept information of the entire database into a graph, creating the graph set as follows:

[0038] Among the nodes Includes all tables and fields, edges This indicates their relationship; After receiving the user intent text that has undergone the aforementioned error correction and normalization, the following calculation is performed using the subgraph retrieval function, where Retrieve represents graph retrieval:

[0039] in, Retrieve the atlas for subgraphs. To quickly locate the most relevant tables and field graphs, This is the target search area.

[0040] The dual-track verification includes obtaining the preliminary SQL query statement generated in step S4, as well as the natural language text of the user's original query intent. The original query intent includes the user's query goal, filtering conditions, data association relationships, and output requirements expressed in natural language. The initial SQL query statement and the original query intent Q are used to construct a model input sequence classification label; The input sequence is input into the third language model, and the model's encoder performs syntax tree parsing on the SQL statement to generate an SQL semantic vector; at the same time, the original query intent Q is extracted to generate an intent semantic vector. The cosine similarity between the SQL semantic vector and the intent semantic vector is calculated using the decoder of the third language model to obtain a semantic matching score. Semantic conflict points are then located based on vector differences, including: If the target column of the SQL query does not match the intent target in Q, it is marked as a target column mismatch; If the WHERE condition in the SQL statement is inconsistent with the time constraint in Q, it will be marked as a filter condition conflict. If the SQL statement omits an explicit relationship in Q, it is marked as having a missing relationship. A preset semantic matching threshold T is set and dynamically adjusted based on historical verification accuracy. If the semantic matching score is ≥ T and the number of semantic conflict points is ≤ 1, then the initial SQL query statement is determined to accurately reflect the user's original query intent. Otherwise, it is judged as not accurately reflecting the situation, and a semantic verification report containing conflict point location and correction suggestions is output.

[0041] The dual-track verification process, after the rule verification passes, activates the GLM semantic validator to compare the user intent text. To ensure semantic consistency with the original SQL query intent Q, GLM internally calculates a match confidence score. The calculation is as follows:

[0042] and with preset threshold Comparison: like If the semantics are consistent, the verification is considered successful. Otherwise, GLM will provide clarification to the user in a dialogic manner, detect any precise distinctions missed in the SQL query, and determine whether adjustments are needed.

[0043] Example 3 This invention provides a natural language to SQL system based on multi-language model collaboration and metadata graph injection, comprising: The preprocessing and ambiguity assessment module receives natural language query text, performs character-level error correction through a bidirectional encoding model, and calculates the semantic ambiguity score of the text. The dynamic mapping module, based on the elastic search engine index library and word vector model, calculates the similarity of synonyms between entities in the corrected text and database fields, as well as the mapping weight of suffix aliases, and outputs the dynamic mapping results. The intent clarification module includes calling the first language model to generate context-dependent clarification questions if the semantic ambiguity score exceeds a preset threshold, and optimizing the query intent through multiple rounds of interaction; The metadata graph enhancement generation module dynamically injects metadata graphs into the second largest language model. The metadata graphs include table structure triple relationships, foreign key association weights, and business semantic attributes, and generate preliminary SQL query statements through structured knowledge reasoning. The dual-track verification and iterative optimization module performs MySQL syntax rule verification and semantic intent verification of the initial SQL query. Based on the verification feedback, it adjusts the graph injection method or triggers intent re-clarification and outputs the final SQL query statement.

[0044] By leveraging the collaborative work of multiple models, combining Large Language Model (LLM), Multilingual Model Collaboration (MMC), and Metadata Graph (MKG) technologies, the system addresses the SQL generation problems caused by the lack of contextual understanding and flexible adaptability in traditional methods. The system first performs semantic understanding of user queries using the Large Language Model and then utilizes the Metadata Graph to provide the model with background information on database structure and business knowledge. Next, the multilingual model collaboration mechanism further optimizes the understanding of diverse queries, ensuring more accurate SQL generation. Finally, the system employs a dual-track verification mechanism (MySQL rule verification and GLM semantic verification) to ensure that the generated SQL queries conform to database rules and user intent, reducing the risk of generating incorrect and invalid queries.

[0045] The system comprises the following key modules: First, the BERT-Chinese model (BERT bidirectional encoder) performs character-level error correction on user input to ensure the accuracy of the input text; then, it uses ElasticSearch (elastic search engine) and word vector models to handle synonym and suffix alias mapping, resolving database field naming differences; next, the GLM-6B model (ChatGLM large language model, such as...) Figure 1 The SQL statement (as shown in the diagram) is used to interact with the user in multiple rounds to clarify fuzzy queries. Subsequently, the metadata graph (MKG) provides the DeepSeek with database table structure and business relationship information, making SQL generation more accurate. Finally, MySQL rule validation and GLM semantic validation are used to ensure the correctness and security of the SQL statement.

[0046] By combining traditional methods with advanced data-driven technologies, the accuracy and robustness of the natural language to SQL system are significantly improved. The synonym mapping and normalization module and the SQL generation module based on metadata graphs, enhanced by the Elasticsearch search engine and external knowledge base, improve the model's adaptability and scalability in different network environments (rapidly adapting to different environments) and complex business scenarios. The introduction of multilingual model collaboration and knowledge graph injection technologies enables the system to better understand user intent and generate high-quality SQL queries, overcoming the limitations of existing methods. Furthermore, through dual-track verification and interpretability analysis, the high reliability and transparency of the generated SQL query results are ensured, allowing direct display of the data corresponding to the SQL statements, greatly improving the user experience and operational efficiency.

[0047] In one embodiment, the user's original input often contains noise such as spelling errors, confusion of similar-looking characters, omission of place names, or inconsistent suffixes. If these problems are not corrected, they will be amplified in the subsequent thesaurus mapping, SQL generation, and validation stages, leading to generated errors or unexecutable queries. Therefore, this module can eliminate input noise at the source through character and word-level correction, improving the overall robustness and accuracy of the system.

[0048] The text preprocessing and error correction module first uses the Transformer-based pre-trained model BERT. This model, pre-trained on a large-scale Chinese corpus using Masked Language Modeling (MLM), learns character-level and word-level contextual representations. Let the character / word embeddings corresponding to the original sequence of user input be:

[0049] in This indicates the length of the input sequence, i.e., the number of words. Indicates the first The initial embedding vector of each word.

[0050] BERT-Chinese consists of an embedding layer, a multi-layer Transformer encoder, a pooling layer, and an output layer. Through BERT-Chinese's multi-layer Transformer encoder... Indicates the BERT encoder at the 1st The contextualized embeddings output at each position yield the context representation:

[0051] The self-attention mechanism of each layer enables the model to capture simple spelling mistakes and similar-character mistakes in the user input. However, BERT-Chinese's sensitivity to similar-character mistakes of certain specific place names will decrease (for example, similar-character confusion: Fuzhou Gulou (the correct writing should be Fuzhou Drum Tower)), but the model cannot correct it correctly. Therefore, in order to further improve the adaptability to domain and place name error correction tasks represents the th place name, is the total amount of the place name dataset, represents the place name dataset. This module is in the domain error correction dataset:

[0052] for fine-tuning. Among them is a sentence containing spelling or place name errors, is the corresponding corrected sentence. The fine-tuning objective is to maximize the conditional probability of the corrected text, which is equivalent to minimizing the cross-entropy loss:

[0053] After fine-tuning the model, the model not only maintains the error correction ability for common Chinese, but also learns to correct key place names such as "Fuzhou Gulou District" to "Gulou District, Fuzhou City".

[0054] After completing the fine-tuning, the model decodes the new input during inference, that is, taking the maximum probability:

[0055] For example, for the input "Query the RSRP level value of Gulou District, Fuzhou, Fujian Province", the model will first select "Gulou District, Fuzhou City" according to the context and keep the "RSRP level value" field unchanged, and output "Query the RSRP level value of Gulou District, Fuzhou City, Fujian Province". The "correct first, then generate" method effectively eliminates downstream errors caused by typos or miswritten place names, providing a high-precision and low-noise input benchmark for the synonym mapping and SQL generation modules.

[0056] Synonym mapping and normalization: The synonym mapping and normalization module can solve the inconsistency problem caused by aliases, suffixes, or industry terms in user input, such as "Fuzhou City" and "Fuzhou," or "Mobile Company" and "China Mobile"—if these noises are not processed, they will directly lead to the SQL generated by the subsequent large language model not matching the database fields. To solve this problem, this module integrates ElasticSearch's efficient candidate recall (quickly retrieving standardized names by building an inverted index, synonym filter, and fuzzy matching function) with semantic similarity calculation of word vectors to achieve automatic normalization of input terms. First, let the input sequence after error correction be:

[0057] For any one of them Retrieves a set of candidate sets that are synonyms or similar to it using ElasticSearch.

[0058] This includes standardized names that actually exist in the database, such as "Fuzhou" corresponding to "Fuzhou City" or "China Mobile" corresponding to "China Mobile Company". Then, using a word vector model finely tuned on a large-scale Chinese corpus and business texts, each word to be replaced... and replacement word options Mapped to the vector to be replaced With replacement word candidate vector And calculate their cosine similarity:

[0059] If cosine similarity has the following relationship This refers to a specific probability value; Maxcosim represents the maximum cosine similarity.

[0060] The candidate with the highest similarity is selected as the replacement result, and then the replacement is performed.

[0061]

[0062] Otherwise keep In addition, this module also uses regular expression-based suffix stripping rules to clean up common place name suffixes ("city", "district") or company name suffixes ("limited company", "company"):

[0063] For example, after removing the suffix from "Guangzhou City", we get "Guangzhou"; after removing "Mobile Company", we get "China Mobile". Further combining this with similarity mapping can improve accuracy. After completing the above mapping and removal, we obtain the normalized sequence:

[0064] This sequence strictly corresponds to the database table and field name standards, providing an executable input basis for subsequent knowledge graph injection and SQL generation from large language models.

[0065] Metadata graph drives DeepSeek's SQL generation: The fourth module, driven by a Meta-Knowledge Graph (MKG), injects a structured database context into the DeepSeek large language model, effectively preventing the model from fabricating fields or joining tables in a "phantom" manner. We first organize the entire database's tables, fields, primary and foreign keys, synonyms, business concepts, and other information into a graph.

[0066]

[0067] Among the nodes Includes all tables and fields, edges This indicates their relationship. After receiving the user intent text (such as "Query the RSRP level value of Cangshan District, Fuzhou City, Fujian Province") after the aforementioned error correction and normalization, the system retrieves the subgraph using the subgraph retrieval function. Representative graph retrieval:

[0068] Quickly locate the most relevant tables and fields (province, city, district, rsrp). Then, serialize the subgraph information into concise knowledge summary paragraphs. And together with system role instructions and user input, a complete Prompt is constructed. To concatenate on behalf of Prompt:

[0069] in: "You are an SQL expert. Please generate a valid SQL query based on the following information." The above diagram serves as a hint; "Query the RSRP level value in Cangshan District, Fuzhou City, Fujian Province" After receiving the prompt, DeepSeek combines self-attention mechanism with graph priors to generate accurate and unambiguous SQL statements.

[0070] By injecting graph knowledge, DeepSeek can directly reference real table fields and correctly organize SQL statements' WHERE and aggregation conditions, fundamentally eliminating "phantom fields" and "incorrect joins".

[0071] Dual-track verification and intelligent feedback After generating the initial SQL query provided by DeepSeek, this module rigorously checks the query results through two independent paths: "rule validation" and "semantic validation." When inconsistencies are found, it quickly corrects them through intelligent interaction between GLM and the user, thereby completely eliminating syntax errors and intent biases.

[0072] The system will The initial SQL statement is submitted to the MySQL rule validation engine, which then performs the following steps: Syntax parsing—constructing an Abstract Syntax Tree (AST) and checking for illegal keywords or spelling errors; Permissions verification – confirms that the user has execute permissions on the signal_data (corresponding SQL table) and its fields; Security detection – blocking high-risk operations such as DROP / TRUNCATE (deleting / clearing data) and performing static scanning for possible SQL injection patterns.

[0073] If rule validation fails, a diagnostic message similar to "Syntax error: Unknown field fuzhou; Please check if the table name or field name is correct" is immediately returned, guiding DeepSeek to correct the relevant fragment in the next generation. Once rule validation passes, the system starts the GLM semantic validator to compare the user's intent text. (e.g., "Query RSRP level value in Cangshan District, Fuzhou City") and SQL statement Semantic consistency. GLM internally calculates a match confidence score. :

[0074] and with preset threshold ( (The threshold can be adjusted according to the specific scenario) for comparison: like If the semantics are consistent, the verification is considered successful. Otherwise, GLM will clarify with the user via a dialog: "The SQL query may have missed the condition that precisely distinguishes between 'Cangshan District' and 'Gulou District.' Does this need to be adjusted?" Users can directly reply with "yes" or "no" and can add or modify the conditions. The system updates its intent accordingly. DeepSeek is rescheduled to generate a new version of SQL, thus forming a closed-loop selection process.

[0075] Finally, when both track verifications pass, the system executes... And return the results. For example: Query result: RSRP level value is -78dBm. Through this dual verification of "rules first, semantics second", combined with real-time human-computer dialogue feedback driven by GLM, this module not only ensures the syntax security and field validity of SQL, but also ensures that the query results are highly consistent with the user's actual needs, greatly improving the reliability and interpretability of the entire TexttoSQL system.

[0076] This invention discloses a natural language to SQL method and system based on multi-language model collaboration and metadata graph injection. The method includes receiving natural language query text input by a user, performing character-level error correction on the query text to ensure accuracy; utilizing an elastic search engine combined with a word vector model for synonym recognition and mapping of database field suffixes and aliases to resolve database field naming discrepancies; conducting multi-round interactions through a first major language model to raise clarifying questions to the user and further clarify the query intent based on user feedback; injecting a metadata graph into a second major language model, the metadata graph containing database table structure information and business relationship information; and the second major language model performing semantic understanding of the natural language query text or the clarified query intent based on the injected metadata graph, and generating a preliminary SQL query statement. This application improves the accuracy, flexibility, and scalability of the natural language to SQL system.

[0077] Example 4 Figure 3 A block diagram of an exemplary electronic device suitable for implementing embodiments of the present invention is shown.

[0078] The electronic device may include a central processing unit / microprocessor / main control chip, etc. 4; and a storage medium 5, coupled to the central processing unit / microprocessor / main control chip, etc. 4, and storing computer-executable instructions therein for performing the steps of various methods of embodiments of the present invention when executed by the processor.

[0079] The central processing unit / microprocessor / main control chip, etc., can include, but are not limited to, one or more processors or microprocessors.

[0080] Storage medium 5 may include, but is not limited to, random access memory (RAM), read-only memory (ROM), flash memory, EPROM memory, EEPROM memory, registers, computer storage media (e.g., hard disk, floppy disk, solid-state drive, removable disk, CD-ROM, DVD-ROM, Blu-ray disc, etc.).

[0081] In addition, the electronic device may also include (but is not limited to) a data bus 6, an input / output bus / external bus / device bus 7, a display 8, and input / output devices 9 (e.g., keyboard, mouse, speaker, etc.).

[0082] The central processing unit / microprocessor / main control chip, etc. 4 can communicate with external devices (8, 9, etc.) via I / O bus 7 through wired or wireless network (not shown).

[0083] The storage medium 5 may also store at least one computer-executable instruction for performing the steps of various functions and / or methods in the embodiments described herein when the central processing unit / microprocessor / main control chip, etc., 4 is running.

[0084] In one embodiment, the at least one computer-executable instruction may also be compiled into or comprise a software product, wherein one or more computer-executable instructions are executed by a processor to perform the steps of the various functions and / or methods in the embodiments described herein.

[0085] Figure 4 A schematic diagram of a computer-readable storage medium according to an embodiment of the present invention is shown.

[0086] like Figure 4 As shown, the non-transitory computer-readable storage medium 11 stores instructions, such as computer-readable instructions 10. When the computer-readable instructions 10 are executed by a processor, the various methods described above can be performed. The non-transitory computer-readable storage medium includes, but is not limited to, volatile memory and / or non-volatile memory. Volatile memory may include, for example, random access memory (RAM) and / or cache memory. Non-transitory non-volatile memory may include, for example, read-only memory (ROM), hard disk, flash memory, etc. For example, the non-transitory computer-readable storage medium 11 can be connected to a computing device such as a computer, and then, when the computing device executes the computer-readable instructions 10 stored on the computer-readable storage medium 11, the various methods described above can be performed.

[0087] In the several embodiments provided by this invention, it should be understood that the disclosed apparatus and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or units may be electrical, mechanical, or other forms.

[0088] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.

[0089] Furthermore, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.

[0090] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions for executing all or part of the steps of the methods of the various embodiments of this invention through a computer device (which may be a personal computer, server, or network device, etc.). The aforementioned storage medium includes: USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, optical disks, and other media capable of storing program code.

[0091] The above embodiments are only used to illustrate the technical solutions of the present invention, and are not intended to limit it. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A natural language to SQL method based on multi-language model collaboration and metadata graph injection, characterized in that, Includes the following steps: S1: Receive the natural language query text input by the user, and perform character-level error correction on the natural language query text to ensure the accuracy of the input text; S2: For the natural language query text processed in step S1, use an elastic search engine combined with a word vector model to identify synonyms and map suffix aliases of database fields to solve the problem of database field naming differences. S3: If the natural language query text after step S2 is still unclear or ambiguous, multiple rounds of interaction are conducted through the first language model to raise clarification questions to the user and further clarify the query intent based on user feedback; S4: Input the natural language query text processed in step S2, or the explicit query intent clarified in step S3, into the second language model; at the same time, inject a metadata graph into the second language model, the metadata graph containing the table structure information and business association information of the database; based on the injected metadata graph, the second language model performs semantic understanding on the natural language query text or explicit query intent, and generates a preliminary SQL query statement; S5: Perform dual-track verification on the preliminary SQL query statement generated in step S4: S51: Verify whether the preliminary SQL query statement conforms to the syntax rules, permission requirements, and database table structure constraints of the MySQL database; S52: Use the third language model to perform semantic verification on the preliminary SQL query statement to determine whether it accurately reflects the user's original query intent; S6. If both verifications in step S5 pass, the final SQL query statement is output; if either verification fails, the process returns to step S4 to regenerate the SQL query statement, or returns to step S3 for further clarification of user intent.

2. The natural language to SQL method based on multi-language model collaboration and metadata graph injection as described in claim 1, characterized in that, The natural language query text undergoes character-level error correction based on the Transformer pre-trained model BERT. For Chinese, the character / word embeddings corresponding to the original sequence of user input are calculated as follows, using a pre-trained MLM masked language model to learn character-level and word-level contextual representations: in This indicates the length of the input sequence, i.e., the number of words. Indicates the first The initial embedding vectors of each word; BERT-Chinese consists of an embedding layer, a multi-layer Transformer encoder, a pooling layer, and an output layer. Through BERT-Chinese's multi-layer Transformer encoder... Indicates the BERT encoder at the 1st The context vector output at each position is used to obtain the context representation, which is calculated as follows: While the sensitivity to errors caused by similar-looking characters decreases, the model still cannot correct them correctly. To further improve its adaptability to domain-specific and place name error correction tasks, Indicates the first A place name, It is the total amount of the place name dataset. To represent the place name dataset, the error correction dataset is set up as follows: Fine-tuning was performed using the error correction dataset, where For sentences containing spelling or place name errors, The corresponding corrected sentence; The fine-tuning objective is to maximize the conditional probability of the corrected text, which is equivalent to minimizing the cross-entropy loss as follows: in, For loss function, Let be a conditional probability function, where For sentences containing spelling or place name errors, The corresponding corrected sentence, Indicates the first A place name, This represents the total amount of the place name dataset.

3. The natural language to SQL method based on multi-language model collaboration and metadata graph injection as described in claim 1, characterized in that, The multi-round interaction includes starting the multi-round interaction process if the semantic ambiguity score in step S1 exceeds a preset threshold, or if there are ≥2 highly similar candidate fields in the dynamic synonym-alias mapping result in step S2. The first language model is used to perform structured analysis on the preprocessed text and dynamic mapping results, identify the types of ambiguities and mark their locations. The first language model calls the preset clarification question template library based on the ambiguity type. The templates include four categories: entity clarification, relation clarification, constraint clarification and terminology clarification. Combining the dynamic synonym-alias mapping results from step S2 with the basic business knowledge of the metadata graph, the template is filled with accurate candidate information. The influence weight of each ambiguous point on the query intent is calculated using the information gain algorithm. High-weight ambiguous points are clarified first, and the first clarification question after sorting is output to the user. Feedback text is received through the user interaction interface. The structured representation of the query intent is updated based on the parsing results, and the semantic understanding model in step S1 is called to recalculate the fuzziness score. The structured intent that passes the termination condition verification is used as the optimized query intent.

4. The natural language to SQL method based on multi-language model collaboration and metadata graph injection as described in claim 1, characterized in that, The SQL query statement includes a structured representation and injection preparation of the metadata graph. The physical layer JSON of the metadata graph is concatenated with the business layer triples to form a structured hint prefix. Through the input construction and semantic-metadata bidirectional mapping of the second language model, the structured hint prefix + the user's explicit query intent + SQL generation instructions are concatenated into the model input sequence. Based on the query intent, the SQL type is determined. Business queries generate a SELECT statement by default, which is further subdivided into subtypes. Each clause is concatenated according to the SQL syntax rules to generate a complete preliminary SQL. The second language model calls the built-in SQL syntax checker to verify the syntactic correctness of the generated SQL. Combined with the physical layer information of the metadata graph, the matching between the SQL and the database structure is checked. If there are syntax or consistency errors, the second language model automatically corrects them based on the verification feedback and regenerates the SQL until it passes the verification.

5. The natural language to SQL method based on multi-language model collaboration and metadata graph injection as described in claim 1, characterized in that, The metadata graph-driven SQL generation organizes the tables, fields, primary and foreign keys, synonyms, and business concept information of the entire database into a graph, creating the graph set as follows: Among the nodes Includes all tables and fields, edges This indicates their relationship; After receiving the user intent text that has undergone the aforementioned error correction and normalization, the following calculation is performed using the subgraph retrieval function, where Retrieve represents graph retrieval: in, Subgraph retrieval atlas, To quickly locate the most relevant tables and fields in the graph, This is the target search area.

6. The natural language to SQL method based on multi-language model collaboration and metadata graph injection as described in claim 1, characterized in that, The dual-track verification includes obtaining the preliminary SQL query statement generated in step S4, as well as the natural language text of the user's original query intent. The original query intent includes the user's query goal, filtering conditions, data association relationships, and output requirements expressed in natural language. The initial SQL query statement and the original query intent Q are used to construct a model input sequence classification label; The input sequence is input into the third language model, and the model's encoder performs syntax tree parsing on the SQL statement to generate an SQL semantic vector; at the same time, the original query intent Q is extracted to generate an intent semantic vector. The cosine similarity between the SQL semantic vector and the intent semantic vector is calculated using the decoder of the third language model to obtain a semantic matching score. Semantic conflict points are then located based on vector differences, including: If the target column of the SQL query does not match the intent target in Q, it is marked as a target column mismatch; If the WHERE condition in the SQL statement is inconsistent with the time constraint in Q, it will be marked as a filter condition conflict. If the SQL statement omits an explicit relationship in Q, it is marked as having a missing relationship. A preset semantic matching threshold T is set and dynamically adjusted based on historical verification accuracy. If the semantic matching score is ≥ T and the number of semantic conflict points is ≤ 1, then the initial SQL query statement is determined to accurately reflect the user's original query intent. Otherwise, it is judged as not accurately reflecting the situation, and a semantic verification report containing conflict point location and correction suggestions is output.

7. The natural language to SQL method based on multi-language model collaboration and metadata graph injection as described in claim 1, characterized in that, The dual-track verification process, after the rule verification passes, activates the GLM semantic validator to compare the user intent text. To ensure semantic consistency with the original SQL query intent Q, GLM internally calculates a match confidence score. The calculation is as follows: and with preset threshold Comparison: like If the semantics are consistent, the verification is considered successful. Otherwise, GLM will provide clarification to the user in a dialogic manner, detect any precise distinctions missed in the SQL query, and determine whether adjustments are needed.

8. A natural language to SQL system based on multilingual model collaboration and metadata graph injection, as described in any one of claims 1 to 7, characterized in that, include: The preprocessing and ambiguity assessment module receives natural language query text, performs character-level error correction through a bidirectional encoding model, and calculates the semantic ambiguity score of the text. The dynamic mapping module, based on the elastic search engine index library and word vector model, calculates the similarity of synonyms between entities in the corrected text and database fields, as well as the mapping weight of suffix aliases, and outputs the dynamic mapping results. The intent clarification module includes calling the first language model to generate context-dependent clarification questions if the semantic ambiguity score exceeds a preset threshold, and optimizing the query intent through multiple rounds of interaction; The metadata graph enhancement generation module dynamically injects metadata graphs into the second largest language model. The metadata graphs include table structure triple relationships, foreign key association weights, and business semantic attributes, and generate preliminary SQL query statements through structured knowledge reasoning. The dual-track verification and iterative optimization module performs MySQL syntax rule verification and semantic intent verification of the initial SQL query. Based on the verification feedback, it adjusts the graph injection method or triggers intent re-clarification and outputs the final SQL query statement.

9. An electronic device, comprising: At least one memory stores computer-executable instructions non-transitory; At least one processor, configured to run the computer-executable instructions, The computer-executable instructions are implemented by the processor at runtime according to any one of claims 1-7, which describes the natural language to SQL method based on multilingual model collaboration and metadata graph injection.

10. A computer-readable storage medium, wherein, The computer-readable storage medium stores computer-executable instructions, which, when executed by at least one processor, implement the natural language to SQL method based on multilingual model collaboration and metadata graph injection according to any one of claims 1-7.