Data query methods, devices, equipment, and media based on natural language recognition

By training a target large language model and vector database, combined with power corpus and business terminology dictionary, multiple query statements are generated and evaluated, solving the problems of accuracy and security of data query in the power field, and realizing an automated power data query and decision-making closed loop.

CN121722904BActive Publication Date: 2026-06-30BEIJING JOIN BRIGHT DIGITAL POWER TECH CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
BEIJING JOIN BRIGHT DIGITAL POWER TECH CO LTD
Filing Date
2025-12-16
Publication Date
2026-06-30

AI Technical Summary

Technical Problem

Existing natural language recognition data query technologies suffer from semantic ambiguity, query structure errors, and the need for manual processing of query results in the power sector, leading to low efficiency in power business decision-making and security risks.

Method used

By training a target large language model based on an electricity corpus dataset and a hierarchical progressive fine-tuning strategy, and combining it with a pre-built business terminology dictionary and vector database, multiple candidate query statements are generated, and multi-dimensional scoring and verification are performed to ensure query accuracy and security.

Benefits of technology

It has improved the accuracy and security of data queries in the power sector, enabled automatic semantic verification and visualization, and reduced the risks and efficiency barriers to power business decision-making.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121722904B_ABST
    Figure CN121722904B_ABST
Patent Text Reader

Abstract

This application provides a data query method, apparatus, device, and medium based on natural language recognition, belonging to the field of data query technology. The method includes: obtaining extended keywords based on user natural language queries; obtaining pattern information and business knowledge based on the extended keywords, user natural language queries, and a vector database; generating target prompt words based on the pattern information, business knowledge, user natural language queries, and system prompt word templates; generating multiple candidate query statements through a target large language model based on the target prompt words and model generation parameters, and determining the optimal query statement; executing the optimal query statement in a database test environment, obtaining an execution result set and capturing abnormal information; validating the execution result set and obtaining a verification result; if the abnormal information is empty and the verification result is qualified, then generating a visualization chart based on the extended keywords, the optimal query statement, and the qualified result set. This application can improve the accuracy of power data queries.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application belongs to the field of data query technology, and more specifically, relates to data query methods, devices, equipment, and media based on natural language recognition. Background Technology

[0002] With the deep penetration of artificial intelligence technology into the power industry, data-driven decision-making has become a key support for core businesses such as power grid dispatching and operation, equipment maintenance, and energy efficiency management. The power system generates massive amounts of structured data every day, covering real-time power grid operating parameters, equipment ledgers, fault waveform records, and load forecasting results. This data is mostly stored in relational databases or data warehouses and needs to be queried and analyzed using Structured Query Language (SQL).

[0003] Natural Language Recognition (NRL) data query technology (with NL2SQL technology at its core) has emerged to automatically convert users' business needs expressed in natural language into executable query statements, achieving zero-code data interaction that is "what you ask is what you get," and solving the pain point that power business personnel generally lack database operation skills and find it difficult to efficiently release the value of data.

[0004] However, existing natural language processing (NLP) data query technologies have significant shortcomings when applied to the power industry: First, the power industry has a complex terminology system, and general-purpose language models are prone to semantic ambiguity and query structure errors when parsing semantics; second, existing technologies cannot effectively intercept erroneous queries caused by "pattern illusion," while scenarios such as power dispatching and fault diagnosis have stringent requirements for query accuracy, and erroneous results may lead to power grid safety risks; third, query results are mostly returned in the form of raw tables or numerical values, requiring users to manually process them, which seriously restricts decision-making efficiency.

[0005] Therefore, there is an urgent need for a natural language recognition data query technology that meets the needs of the power industry in order to improve the accuracy of data queries in the power sector. Summary of the Invention

[0006] The purpose of this application is to provide a data query method, apparatus, equipment, and medium based on natural language recognition, so as to improve the accuracy of data query in the power sector.

[0007] A first aspect of this application provides a data query method based on natural language recognition, including:

[0008] Based on user natural language queries, extended keywords are obtained through a target large language model and a pre-built business term dictionary. The target large language model is obtained by training an initial large language model based on an electric corpus dataset, an objective function, and fine-tuning parameters. The fine-tuning parameters are designed based on a hierarchical progressive fine-tuning strategy.

[0009] Parallel retrieval is performed based on extended keywords, user natural language queries, and a vector database to obtain pattern information and business knowledge, respectively; contextual hint blocks are obtained based on pattern information and business knowledge; the vector database is constructed based on pattern data and business knowledge data.

[0010] Target prompt words are generated based on contextual prompt blocks, user natural language queries, and system prompt word templates. Based on the target prompt words and model generation parameters, multiple candidate query statements are generated through the target large language model. The multidimensional score of each candidate query statement is calculated, and the candidate query statement with the highest multidimensional score is selected as the optimal query statement.

[0011] Execute the optimal query statement in the database test environment and obtain the execution result set; capture exception information when executing the optimal query statement; perform rule validation and semantic consistency validation on the execution result set and obtain the validation results;

[0012] If the exception information is empty and the verification result is qualified, the execution result set is taken as the qualified result set; multi-dimensional analysis results are obtained based on the extended keywords, the optimal query statement and the qualified result set; a visualization chart is generated based on the multi-dimensional analysis results and the qualified result set; the visualization chart and the qualified result set are used for display.

[0013] A second aspect of this application provides a data query device based on natural language recognition, comprising:

[0014] The user query keyword extraction module is used to obtain extended keywords based on user natural language queries through a target large language model and a pre-built business term dictionary. The target large language model is obtained by training an initial large language model based on an electric corpus dataset, an objective function, and fine-tuning parameters. The fine-tuning parameters are designed based on a hierarchical progressive fine-tuning strategy.

[0015] The vector retrieval enhanced context module is used to perform parallel retrieval based on extended keywords, user natural language queries, and a vector database to obtain pattern information and business knowledge, respectively; it generates contextual suggestion blocks based on pattern information and business knowledge; the vector database is constructed based on pattern data and business knowledge data.

[0016] The SQL statement generation module is used to generate target prompt words based on contextual prompt blocks, user natural language queries, and system prompt word templates; based on the target prompt words and model generation parameters, it generates multiple candidate query statements through the target large language model, calculates the multidimensional score of each candidate query statement, and selects the candidate query statement with the highest multidimensional score as the optimal query statement.

[0017] The SQL execution and structure verification module is used to execute the optimal query statement in the database test environment and obtain the execution result set; capture exception information when executing the optimal query statement; and perform rule verification and semantic consistency verification on the execution result set to obtain the verification result.

[0018] The visualization module is used to treat the execution result set as a qualified result set if the exception information is empty and the verification result is qualified; to obtain multi-dimensional analysis results based on extended keywords, optimal query statements and qualified result sets; to generate visualization charts based on multi-dimensional analysis results and qualified result sets; and to display the visualization charts and qualified result sets.

[0019] A third aspect of this application provides an electronic device, including a memory, a processor, and a computer program stored in the memory and running on the processor, wherein the processor executes the computer program to implement the steps of the above-described data query method based on natural language recognition.

[0020] A fourth aspect of this application provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the steps of the above-described data query method based on natural language recognition.

[0021] The beneficial effects of the data query method, apparatus, device, and medium based on natural language recognition provided in this application are as follows:

[0022] This application overcomes the limitations of existing technologies in power system applications, providing a data query and visualization method based on a large language model, combined with NL2SQL semantic parsing, prompt word engineering optimization strategies, and vector database retrieval mechanisms. Specifically, on the one hand, the trained target large language model improves the accuracy of power semantic-driven NL2SQL parsing, overcoming the challenge of accurately mapping professional terms to multi-level equipment relationships and reducing terminology mistranslation and structural mismatch rates. On the other hand, this application enables power-specific SQL semantic verification and anomaly monitoring, achieving automatic semantic verification and illusion interception before SQL execution. Furthermore, this application constructs a semantically aware intelligent visualization automatic adaptation capability, automatically matching the optimal chart type based on the keywords in the user query and the structural features of the returned execution result set data, achieving a closed-loop decision-making experience of query as view. Attached Figure Description

[0023] To more clearly illustrate the technical solutions in the embodiments of this application, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0024] Figure 1 A flowchart illustrating a data query method based on natural language recognition provided in an embodiment of this application;

[0025] Figure 2 A flowchart illustrating a data query method based on natural language recognition provided in another embodiment of this application;

[0026] Figure 3 A structural block diagram of a data query device based on natural language recognition provided in an embodiment of this application;

[0027] Figure 4 This is a schematic block diagram of an electronic device provided in an embodiment of this application. Detailed Implementation

[0028] In the following description, specific details such as particular system architectures and techniques are set forth for illustrative purposes and not for limitation, in order to provide a thorough understanding of the embodiments of this application. However, those skilled in the art will understand that this application may also be implemented in other embodiments without these specific details. In other instances, detailed descriptions of well-known systems, apparatuses, circuits, and methods have been omitted so as not to obscure the description of this application with unnecessary detail.

[0029] It is understood that in the embodiments of this application, data such as user information are involved. When the embodiments of this application are applied to specific products or technologies, user permission or consent is required, and the collection, use and processing of related data must comply with relevant laws, regulations and standards.

[0030] It should be noted that the terms "first," "second," etc., used in the specification, claims, and drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such use of data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented in sequences other than those illustrated or described herein.

[0031] Please refer to Figure 1 , Figure 1 This is a flowchart illustrating a data query method based on natural language recognition provided in an embodiment of this application. The method can be executed by an electronic device, and specifically, the method may include S101 to S105.

[0032] S101: Based on user natural language queries, extended keywords are obtained through the target large language model and a pre-built business term dictionary; the target large language model is obtained by training the initial large language model based on the power corpus dataset, the objective function and fine-tuning parameters, and the fine-tuning parameters are designed based on a hierarchical progressive fine-tuning strategy.

[0033] In this embodiment, based on the user's natural language query, extended keywords are obtained through a target large language model and a pre-built business terminology dictionary. Specifically, this includes: extracting keywords from the user's natural language query using the target large language model to obtain query keywords; and expanding the query keywords based on the pre-built business terminology dictionary to obtain extended keywords.

[0034] In this embodiment, the objective function is:

[0035] ;

[0036] ;

[0037] in, For the target loss value, This represents the i-th basic loss component, where i = {1, 2, 3}. for , for , for , The adaptive weight coefficients are the values ​​corresponding to the i-th basic loss component. For regularization terms, These are all trainable parameters in the initial large language model;

[0038] ;

[0039] ;

[0040] ;

[0041] ;

[0042] ;

[0043] ;

[0044] ;

[0045] ;

[0046] in, For NL2SQL master loss, For users to query in natural language, This is a sequence of actual SQL tags. T is the sequence length of the actual SQL label sequence. p is the real token at time step t. For the initial large language model based on The generated conditional probability distribution, Prediction tokens for the initial large language model

[0047] For structural consistency loss, For based on The resulting abstract syntax tree, For based on The resulting abstract syntax tree, From The set of all paths from the root node to the leaf node extracted from the data. From The set of all paths from the root node to the leaf node extracted from the data. For the accuracy between path sets, Recall rate among path sets The structure F1 score;

[0048] To resist interference and mitigate losses, Yes Adversarial examples generated by injecting noise, The initial large language model is based on adversarial examples The generated conditional probability distribution For adversarial examples The corresponding real token at time step t.

[0049] In this embodiment, the user's natural language query refers to the question data input by the user, such as a user's query request for electricity data expressed in everyday language, used to convey the specific query intent. The target large language model is a model obtained by training the initial large language model on an electricity corpus dataset, optimizing the objective function, and configuring parameters through hierarchical progressive fine-tuning, used to accurately extract keywords. The pre-built business terminology dictionary is a structured resource storing the mapping relationship between synonyms, abbreviations, and full names in the electricity industry, used for keyword expansion. Expanded keywords are the set formed by expanding query keywords through the terminology dictionary, used to improve the comprehensiveness of retrieval. Query keywords are the core semantic elements extracted by the target large language model from user queries. The electricity corpus dataset contains knowledge in areas such as electricity terminology and equipment relationships, used for model training. The objective function is an optimization objective that guides the model to learn electricity business and SQL generation rules. Fine-tuning parameters are training parameters adapted to the hierarchical progressive strategy. The initial large language model is a general large language model that has not undergone domain adaptation, and the hierarchical progressive fine-tuning strategy is a training method that differentiates the learning rates of each layer of the model.

[0050] This embodiment addresses the issues of inaccurate keyword extraction and incomplete semantic coverage in general-purpose models within the power industry context. The power industry terminology is complex and contains multiple expressions; general-purpose models lack domain knowledge, easily overlooking core semantics or extracting incorrect keywords. This embodiment trains the model using a power industry corpus dataset and an objective function, allowing the model to internalize domain knowledge and improve keyword extraction accuracy. A layered, progressive fine-tuning strategy balances the model's general capabilities with domain adaptability, avoiding catastrophic forgetting. A pre-built business terminology dictionary expands keywords, covering synonyms, abbreviations, and other expressions, resolving issues of vague or non-standard user query expressions. This lays the foundation for subsequent accurate retrieval and query statement generation, ensuring the accuracy of the entire query process.

[0051] like Figure 2 As shown, exemplarily, the training process of the initial large language model in this embodiment is as follows:

[0052] This embodiment provides a fine-tuning strategy for large language models, aiming to overcome problems such as schema illusion, syntax errors, and detail neglect in general-purpose large language models (LLMs) for NL2SQL tasks. The fine-tuning strategy in this embodiment is based on the open-source Qwen3 model. Through the designed objective function and fine-tuning parameters, the model deeply internalizes database schema knowledge and accurately grasps the conversion rules from natural language to SQL.

[0053] The core idea behind the objective function design is to coordinate multiple complementary sub-losses with dynamic weights, guiding the model to focus on improving different capabilities at different training stages.

[0054] Objective function (i.e., joint loss function) The definition is as follows:

[0055]

[0056] in, This is a regularization term, typically using L2 regularization, i.e., weight decay. Loss component. It consists of three main parts: NL2SQL master loss Structural consistency loss and anti-interference to combat losses .

[0057] NL2SQL main loss We employ the standard tokenizer-level cross-entropy loss function, which measures the token-level difference between the SQL sequence generated by the model and the actual labels. Let the input natural language problem be... The model parameters are The actual SQL label sequence is Where T is the sequence length, This represents the actual token at time step t. The model is based on... The generated conditional probability distribution is p , If the token is predicted by the model, then the NL2SQL main loss is defined as the generated sequence. The negative log-likelihood relative to the true sequence y:

[0058]

[0059] Structural consistency loss Instead of focusing on specific table or column names, this approach focuses on the Abstract Syntax Tree (AST) structure of SQL. First, it... and Each is parsed into an Abstract Syntax Tree (AST), and its specific identifiers (such as table names, column names, and values) are replaced with generalized placeholders (such as [TABLE], [COLUMN], [VALUE]), resulting in an Abstract Syntax Tree. Extract the set of all paths from the root node to the leaf node in each abstract syntax tree. and Then, the precision and recall rates among the path sets are calculated:

[0060]

[0061]

[0062] This leads to the structure F1 score:

[0063]

[0064] The final structural consistency loss is defined as:

[0065]

[0066] This structural consistency loss The model is forced to learn the correct SQL syntax skeleton, which effectively reduces structural errors such as missing [SELECT*] conditions, missing [WHERE] conditions, and nesting errors.

[0067] Anti-interference and counter-loss The aim is to improve the robustness of the model. During training, the input natural language questions are randomly selected. Injecting noise, such as synonym substitution, inserting irrelevant function words, and simulating spelling errors, generates adversarial examples. Adversarial examples And its corresponding actual SQL answer As the training pair, input the model and compute its cross-entropy loss. :

[0068]

[0069] This anti-interference adversarial loss forces the model to maintain stable performance when faced with noisy, imperfect, or colloquial queries, enhancing its generalization ability in practical applications.

[0070] Weight Instead of fixing the hyperparameters, they are dynamically adjusted based on the relative convergence rate of each loss component on the validation set. This embodiment can set an initial weight, and after each training epoch, calculate the percentage decrease of each loss component relative to the previous epoch. For components that decrease slowly, their weights are adjusted accordingly. The structural consistency loss can be appropriately increased to give it more attention in subsequent training; conversely, it can be slightly decreased. For example, in the 10th training cycle, the structural consistency loss... It decreased from 0.42 to 0.38 (a decrease of approximately 9.5%), while combating losses It only decreased from 0.50 to 0.49 (a decrease of approximately 2.0%). Due to... The convergence is slower, so this embodiment automatically increases its weights from 0.3 to 0.4 to enhance the model's robustness to noisy inputs during training; at the same time, The weights were adjusted from 0.7 to 0.6 to prevent them from dominating the training process. This mechanism ensures the balanced development of all aspects of the model's capabilities, preventing any one capability from overfitting prematurely and thus limiting overall performance.

[0071] To prevent overfitting during fine-tuning, this embodiment introduces L2 weight decay into the total loss function, applying it to all trainable parameters of the model. Apply L2 norm penalty. For example, the L2 weight decay coefficient is set to 0.001, meaning the regularization term in the total loss function is:

[0072]

[0073] This embodiment adopts a layered, progressive fine-tuning strategy and makes targeted configurations for the key hyperparameters of the large language model.

[0074] First, this embodiment employs a tiered learning rate approach, rather than using a uniform learning rate across all model layers. For the lower layers inherited from the pre-trained model (closer to the input), this embodiment uses a smaller learning rate (e.g., 5e-6 to 1e-5) to preserve their strong general language understanding and generation capabilities and prevent catastrophic forgetting. For the top layers of the model (closer to the output) and newly added task-specific layers (e.g., the Header for classification), this embodiment uses a larger learning rate (e.g., 3e-5 to 5e-5) to encourage rapid adaptation to new NL2SQL tasks. This setup in this embodiment allows for fine-tuning of both the lower and upper layers.

[0075] Next, this embodiment employs mini-batch training, with the batch size set to 8 to 32 based on GPU memory. Simultaneously, this embodiment uses gradient accumulation technology to simulate the effect of large-batch training, improving training stability, especially in resource-constrained environments.

[0076] Secondly, the learning rate scheduler in this embodiment employs a cosine annealing decay strategy with linear warm-up. In the first 10% of training steps, the learning rate is linearly increased from 0 to a preset maximum, allowing the model to stably enter the optimization process in the early stages of training. In the remaining 90% of steps, the learning rate decays from its maximum value to near 0 according to a cosine function curve, helping the model converge to a better local optimum in the later stages of training.

[0077] Finally, the number of training epochs is set to 5 to 10. Simultaneously, this embodiment monitors the loss and execution accuracy on the validation set. If the validation metric no longer improves after three consecutive epochs, an early stopping mechanism is triggered to immediately terminate training and avoid overfitting.

[0078] like Figure 2As illustrated, for example, considering that the general-purpose large language model lacks knowledge of specific business domains and cannot directly access the database schema, it is prone to "schema illusion," i.e., generating non-existent table or field names. This embodiment solves this problem by constructing a dynamic, semantically enhanced context-aware retrieval mechanism. The core lies in utilizing a vector database to achieve accurate semantic indexing and recall, thereby providing the most relevant contextual information for the LLM. Specifically, this embodiment can construct a vector database and semantic index, that is, to fuse and vectorize structured schema information with unstructured business knowledge. For data source schema information, this embodiment can extract metadata from the connected database, including but not limited to: table names, field names, field data types, field comments, and primary / foreign key relationships, and combine this metadata into natural language fragments in JSON format. For business knowledge information, such as historical successful Q&A records, business terminology dictionaries, indicator definition documents, and data warehouse model descriptions, these are also combined into key-value pairs in JSON format. This knowledge is key to understanding aliases, slang, and business jargon in user queries.

[0079] This embodiment can use the bge-large-zh-v1.5 text embedding model to convert the above JSON format key-value pairs into vector data. The generated vector embeddings, along with the corresponding original text and metadata (such as the table name and type), are stored in the Milvus vector database, with each record forming a "semantic node".

[0080] For example, considering that directly using the user's original question for retrieval might lead to inaccurate recall due to ambiguity, this embodiment performs in-depth analysis and expansion of the user query before retrieval. First, this embodiment utilizes a trained target large language model to analyze and expand the user query from the user question... Extract core noun phrases and verb phrases from the user's natural language query as the initial set of query keywords. Specifically, the user's natural language query is input into the target large language model, and the target large language model is guided to output structured semantic elements through prompt word engineering, including but not limited to: core operation verbs (such as "query", "statistics", "comparison"), target object nouns (such as "load", "transformer area", "trip record"), and limiting conditions (such as "yesterday", "Chengdong substation", "voltage greater than 220V").

[0081] Secondly, this embodiment is based on a pre-built business terminology dictionary, for Each keyword in the dictionary has its synonyms, abbreviations, full names, and common alternative names found. The business terminology dictionary is a structured semantic resource tailored to the power system vertical industry. Its content is manually compiled based on historical experience, covering semantic equivalence mappings and industry abbreviations to support semantic expansion and intent alignment for user queries. For example, it includes the following types: synonyms / near-synonyms (such as "customer," "customer," "user"), abbreviations and full names (such as "PT" corresponding to "voltage transformer," "PMS" corresponding to "production management system"). Finally, an expanded keyword set is formed. .

[0082] S102: Parallel retrieval is performed based on extended keywords, user natural language queries, and vector databases to obtain pattern information and business knowledge, respectively; contextual hint blocks are obtained based on pattern information and business knowledge; the vector database is constructed based on pattern data and business knowledge data.

[0083] In this embodiment, obtaining a contextual hint block based on pattern information and business knowledge specifically includes: removing redundancy from pattern information and business knowledge according to semantic similarity; and concatenating the removed redundancy pattern information and business knowledge into a context according to a preset template to obtain a contextual hint block.

[0084] In this embodiment, pattern information refers to the structured metadata of the database retrieved from the vector database that is relevant to the query. This metadata clarifies the basic structure of the data query, including tables, fields, etc., such as table names, field names, field data types, and primary / foreign key relationships. Business knowledge refers to the unstructured information in the power sector retrieved from the vector database that is relevant to the query. This business knowledge assists in understanding the query semantics and may include historical successful Q&A records, business terminology definitions, and indicator calculation logic. The vector database stores pattern data and business knowledge data in vector form, enabling semantic similarity matching and rapid information retrieval. Pattern data is the foundational structured data for building the vector database, derived from database metadata. Business knowledge data is the foundational unstructured data for building the vector database, derived from power business documents and historical interaction records. Semantic similarity is an indicator that measures the relevance between the retrieved information and the user's query intent, used to filter relevant information and remove redundancy. Redundancy removal is the operation of eliminating duplicate or overlapping information based on semantic similarity, used to simplify the context. The preset template is a predefined context concatenation format used to standardize the presentation structure of pattern information and business knowledge. The context hint block provides accurate contextual support for the generation of subsequent query statements.

[0085] This embodiment addresses the "pattern illusion" and semantic comprehension bias issues caused by the lack of precise context in general models. Considering that power data queries rely on accurate database structures and domain knowledge, directly using unfiltered search results can lead to redundant information interfering with model judgment and generating incorrect queries. Furthermore, context without a standardized format reduces the model's utilization of information. Therefore, this embodiment removes redundancy through semantic similarity, ensuring concise contextual information and avoiding repetition of core semantics, thus preventing model confusion. The embodiment also uses a pre-defined template for concatenation, ensuring clear classification and structural consistency of pattern information and business knowledge. This allows the model to quickly locate key information, accurately understand power business logic and data structures, and lay the foundation for generating correct and compliant queries, guaranteeing the accuracy and efficiency of data queries.

[0086] like Figure 2 As shown, exemplarily, this embodiment can dynamically construct a prompt context with the richest information for an LLM. Firstly, this embodiment employs a multi-path vector retrieval and ranking strategy to expand the keyword set... Issues with original users Each element is converted into a vector, and then retrieved in parallel within a vector database.

[0087] (1) Pattern information retrieval: Retrieve the top-K table name and field name nodes that are most similar to the query vector from the “pattern information” vector set in the vector database.

[0088] (2) Business knowledge retrieval: Retrieve the top-M historical question and answer and term definition nodes that are most similar to the query vector from the "business knowledge" vector set in the vector database.

[0089] This embodiment merges the results from the two search paths, sorts them uniformly based on the semantic similarity scores of the query results, removes redundant information, and generates a final candidate information list sorted in descending order of relevance. This embodiment will include a candidate list. The information is dynamically assembled into a structured context prompt block, Context_Prompt, according to a preset template. The template clearly distinguishes different information types, and the preset template is as follows:

[0090] Relevant database table structure information: {schema} schema;

[0091] Relevant business definition: {business} business;

[0092] Example of a similar historical question: {history} history.

[0093] The dynamically concatenated contextual cue words (i.e., contextual cue blocks) are as follows:

[0094] Relevant database table structure information:

[0095] Table name: User account table (usr_account);

[0096] Field: Account ID (acct_id), Data type: bigint, Note: Uniquely identifies an account;

[0097] Field: Account Balance (acct_balance), Data Type: decimal(20, 2), Comment: The user's current account balance;

[0098] The relevant business definition: "Total assets" refers to the sum of the balances in all of a user's accounts.

[0099] Example of a similar question from the past: Q: "Can you check my balance?"

[0100] A: SELECT acct_balance FROM usr_account WHERE user_id=xxx.

[0101] S103: Generate target prompt words based on contextual prompt blocks, user natural language queries, and system prompt word templates; based on the target prompt words and model generation parameters, generate multiple candidate query statements through the target large language model, calculate the multidimensional score of each candidate query statement, and select the candidate query statement with the highest multidimensional score as the optimal query statement.

[0102] In this embodiment, the system prompt word template is a predefined fixed text framework containing information such as role positioning and output format requirements, used to standardize the structure of the target prompt words and the direction of model output. The target prompt words drive the target large language model to generate query statements. Model generation parameters are configuration items that control the diversity of the model's generated results, used to obtain multiple logically equivalent query statements. Candidate query statements are multiple query statements generated by the target large language model based on the target prompt words, with different expressions but consistent core logic. Multidimensional scoring is a quantitative evaluation score of candidate query statements from dimensions such as syntax, security, and performance, used to select the optimal statement. The optimal query statement is the candidate query statement with the highest multidimensional score, and is the final statement executed subsequently.

[0103] In this embodiment, the uncertainty and risk associated with generating a single query statement are addressed, adapting to the stringent reliability requirements of power scenarios. In scenarios such as power dispatching and fault diagnosis, erroneous queries can lead to power grid safety issues, and single-statement generation is prone to syntax errors or inefficient statements due to model bias. System prompt word templates can constrain the model's output format, preventing redundant content from interfering with execution; the multi-candidate generation in this embodiment can cover more possible expressions, reducing the occasional bias of a single result. Multi-dimensional scoring comprehensively evaluates the validity, security, and efficiency, ensuring the selection of statements without syntax errors, dangerous operations, and high execution efficiency, providing a reliable foundation for subsequent test environment execution and result verification, and guaranteeing the accuracy and security of power data queries.

[0104] like Figure 2 As shown, exemplarily, this embodiment utilizes the powerful generation capabilities of a large language model to convert natural language queries into correct, secure, and efficient SQL statements, and ensures the optimality of the output results through a multi-candidate generation and weighted evaluation mechanism.

[0105] This embodiment uses the target large language model as the core parsing engine to perform the sentence transformation task. In this embodiment, dynamic contextual hints (containing relevant table structures and business knowledge) are input together with the user's original question into the designed system hint template. The system hint template clearly defines the role of the instruction model, the output format requirements, and provides a few examples of chain-like thinking to guide the model in logical reasoning. For example, the system hint template is:

[0106] Role: You are a professional database query engineer, proficient in SQL syntax and database structure. Your task is to generate accurate and executable SQL query statements based on natural language questions posed by users and the provided database table structure information.

[0107] Output requirements:

[0108] Output only the SQL statement; do not include any explanations, comments, Markdown formatting, or additional text.

[0109] The provided table and field names must be used strictly; they must not be made up or misspelled.

[0110] The query field output must be a Chinese alias; do not output the English field directly.

[0111] SQL statements must be syntactically correct, logically complete, and executable directly in the target database.

[0112] Relevant database table structure information: {schema};

[0113] Relevant business definition: {business};

[0114] Example of a similar historical question: {history};

[0115] User input: {user_input};

[0116] The dynamically concatenated context prompts are:

[0117] Role: You are a professional database query engineer, proficient in SQL syntax and database structure. Your task is to generate accurate and executable SQL query statements based on natural language questions posed by users and the provided database table structure information.

[0118] Output requirements:

[0119] Output only the SQL statement; do not include any explanations, comments, Markdown formatting, or additional text.

[0120] The provided table and field names must be used strictly; they must not be made up or misspelled.

[0121] The query field output must be a Chinese alias; do not output the English field directly.

[0122] SQL statements must be syntactically correct, logically complete, and executable directly in the target database.

[0123] Relevant database table structure information:

[0124] Table name: User account table (usr_account);

[0125] Field: Account ID (acct_id), Data type: bigint, Note: Uniquely identifies an account;

[0126] Field: Account Balance (acct_balance), Data Type: decimal(20, 2), Comment: The user's current account balance;

[0127] The relevant business definition: "Total assets" refers to the sum of the balances in all of a user's accounts.

[0128] Example of a similar question from the past: Q: "Can you check my balance?"

[0129] A: SELECT acct_balance FROM usr_account WHERE user_id=xxx;

[0130] User input: {{user_input}}.

[0131] For example, considering the uncertainty and risk inherent in generating a single SQL statement, this embodiment differs from the traditional single-generation strategy. Instead, it adopts a generation-evaluation-selection approach. By adjusting the generation parameters (temperature value) of the large language model, it generates N (typically N=3~5) candidate SQL statements {SQL_1, SQL_2, ..., SQL_N} with subtle differences in expression for the same input problem in parallel. These candidate SQL statements are logically equivalent but differ in their specific implementations, providing a basis for subsequent optimization and selection. For the multiple candidate SQL sets, this embodiment designs a multi-dimensional evaluator with configurable weights to automatically evaluate and score each candidate SQL statement, and outputs the best one based on the total score. The evaluation dimensions and weights are configured as follows:

[0132] First, this embodiment uses the standard SQL parser JSqlParser to parse the generated SQL. If the parsing is successful, it proves that the syntax is correct. If the check passes, the full score (40 points) is obtained for this dimension; otherwise, 0 points are obtained. This dimension has the highest weight to ensure the legality of the output results.

[0133] Then, this embodiment establishes a blacklist of risky operation keywords: BLACKLIST={"DROP","DELETE","UPDATE","INSERT","ALTER","CREATE","TRUNCATE","GRANT","REVOKE"}. This embodiment performs a case-insensitive scan and match on candidate SQL statements. If a candidate SQL statement contains any keyword from the blacklist, it receives 0 points; otherwise, it receives full marks (30 points). This mechanism vetoes any dangerous operation that could potentially corrupt or alter the database structure, fundamentally ensuring the system's data security.

[0134] This embodiment evaluates performance and efficiency using a set of heuristic rules. Specific rules include: prohibiting the use of 'SELECT *'; checking if required fields are explicitly listed (points are awarded for explicit listing, points are deducted for using '*'); checking for necessary filtering conditions and ensuring the WHERE clause contains valid filtering conditions to avoid full table scans; optimizing join queries and checking if join conditions are valid and if indexed fields (such as primary / foreign keys) are used; and assessing subquery complexity by evaluating the nesting level of subqueries, with lower nesting levels resulting in higher scores.

[0135] This embodiment deducts points from the 30 points based on the number and severity of violations of the above rules. Specifically, violating rule 1 or 2 deducts 5 points, violating rule 3 deducts 6 points, and violating rule 4 deducts 2 points for each nested level, up to a maximum of 8 points. This dimension guides the model to generate more efficient queries, reducing database load. For example, assuming a generated SQL statement is "SELECT * FROM orders o JOIN customers c ON o.customer_id = c.id WHERE o.amount>1000", this embodiment will evaluate each statement individually:

[0136] Rule 1: Using "SELECT*" is a violation and will result in a deduction of 5 points.

[0137] Rule 2: If the WHERE clause contains the valid filtering condition "o.amount>1000", it meets the requirement and no points will be deducted;

[0138] Rule 3: The join condition "o.customer_id=c.id" is a foreign key association, which meets the requirements for indexed fields, so no points will be deducted;

[0139] Rule 4: No subqueries, which meets the optimal condition, will not result in a penalty.

[0140] Based on the above rules, the statement scores 30-5=25 points in the performance dimension.

[0141] Finally, calculate the total score for each candidate SQL statement and select the candidate with the highest total score as the final output. In case of a tie, prioritize the one with the higher performance and efficiency scores.

[0142] S104: Execute the optimal query statement in the database test environment and obtain the execution result set; capture exception information when executing the optimal query statement; perform rule verification and semantic consistency verification on the execution result set and obtain the verification results.

[0143] In this embodiment, the database testing environment is a database environment that is consistent with and completely isolated from the production environment's data model and sample data, used for secure execution of query statements. The execution result set is a structured data collection returned after the optimal query statement is executed in the testing environment. Anomaly information includes error signals captured during execution, such as syntax errors, timeouts, and abnormal result row counts. Rule validation is an operation that checks the reasonableness of the result set data using a pre-built business rule base. Semantic consistency validation is an operation that compares the semantic matching degree between the user query and the result set summary. The validation result is a pass / fail conclusion output after rule validation and semantic consistency validation.

[0144] This embodiment avoids the risks of power data queries to the production system and ensures that the results conform to business realities and user intent. The power system database stores critical data on power grid operation; directly executing queries in the production environment could lead to data security or system performance issues. An isolated testing environment completely avoids this risk. Capturing anomalies during execution allows for timely interception of syntax errors, inefficient queries, and other problems, preventing ineffective execution from wasting resources. Rule validation filters out unreasonable data based on power business characteristics (e.g., load rate must be between 0-150%), while semantic consistency validation prevents queries with correct syntax but deviating from intended intent. The combination of these two measures meets the zero-tolerance requirements for result accuracy in scenarios such as power dispatching and fault diagnosis, ensuring reliable query results without security risks.

[0145] like Figure 2 As shown, exemplarily, the core of this embodiment lies in intercepting potential errors, verifying the rationality of the results, and initiating an automated semantic error correction mechanism when a problem is detected, thereby forming a complete technical closed loop that can self-verify and self-optimize.

[0146] This embodiment does not directly apply the obtained optimal query statement to the production database. Instead, it executes it in a completely isolated database test environment. This test environment has the same data schema and sample data as the production environment. During execution, this embodiment monitors and captures the following anomalies in real time:

[0147] (1) Syntax and runtime exceptions: capture errors directly thrown by the database engine, such as: field does not exist, table does not exist, function error, etc. These are the most direct error signals.

[0148] (2) Performance anomaly monitoring: Set an execution timeout threshold (e.g., 5 seconds). If the query execution time exceeds this threshold, the execution will be terminated immediately and marked as a candidate for performance failure to prevent malicious or inefficient queries from dragging down the database.

[0149] (3) Abnormal result set size: Check the number of rows in the returned result set. If the number of rows in the result set is abnormally high (e.g., more than 100 rows) or empty, a warning will be triggered, as this often indicates that the query conditions are missing or incorrect.

[0150] Even if the SQL executes successfully without throwing an exception, the result may not reflect the user's true intent in the query. Therefore, this embodiment also introduces a reasonableness verification step based on rules and semantic consistency.

[0151] For the rule validation part, this embodiment has a built-in configurable business rule library. For example: "Account balance cannot be negative", "Age field range should be between 0-150", "Daily month-on-month growth rate of a certain indicator usually does not exceed 100%", etc. This embodiment scans the result set returned by the execution, and once it finds that the data violates the predefined business rules, it determines that the query result is unreasonable.

[0152] For the semantic consistency verification, this embodiment uses the lightweight text similarity model Sentence-BERT to compare the semantic consistency between the textual summary of the user's original question (Q) and the SQL execution result (R). Specifically, the core information of the result set R (such as statistical values ​​and key field values) is summarized into a natural language description, for example: "The query result is: 45 records, with an average balance of 12,500 yuan." Then, this embodiment calculates the semantic similarity between this description and the user's original question Q. If the similarity is lower than a set threshold, it indicates that although the generated SQL is syntactically correct and can be executed, it does not truly meet the user's query intent, and the result is judged to be unreasonable.

[0153] S105: If the exception information is empty and the verification result is qualified, the execution result set is taken as the qualified result set; multi-dimensional analysis results are obtained based on the extended keywords, the optimal query statement and the qualified result set; a visualization chart is generated based on the multi-dimensional analysis results and the qualified result set; the visualization chart and the qualified result set are used for display.

[0154] In this embodiment, before obtaining multi-dimensional analysis results based on expanded keywords, optimal query statements, and qualified result sets, the following steps are also included:

[0155] If the exception information is not empty and / or the verification result is that the verification is unqualified, the optimal query statement is intercepted, and the error information is determined based on the exception information and / or the verification result.

[0156] Based on the optimal query statement, the user's natural language query, and error information, the query statement is regenerated using error correction instructions.

[0157] Execute the regenerated query to obtain a valid result set.

[0158] In this embodiment, multi-dimensional analysis results are obtained based on expanded keywords, optimal query statements, and qualified result sets, including:

[0159] Extract keyword features from expanded keywords, extract statement structure features from the optimal query statement, and extract result set data features from the qualified result set;

[0160] Keyword features, sentence structure features, and result set data features are used as the results of multi-dimensional analysis.

[0161] In this embodiment, generating a visualization chart based on multi-dimensional analysis results and a qualified result set specifically includes: concatenating the multi-dimensional analysis results into structured text features; generating visualization type labels based on the structured text features; matching and mapping chart types from the rule knowledge base based on the visualization type labels; determining configuration parameters based on the mapping chart types and the qualified result set; and generating a visualization chart based on the configuration parameters and the qualified result set.

[0162] In this embodiment, the qualified result set is the execution result set when the exception information is empty and the verification result is qualified. It serves as the foundational data for subsequent multi-dimensional analysis and visualization. Multi-dimensional analysis results support visualization intent recognition. Keyword features are core semantic elements extracted from extended keywords, such as regional equipment indicators in the power sector, used to identify user query intent. Statement structure features are syntactic structure information extracted from the optimal query statement, such as whether it contains clauses like GROUP BY JOIN. Result set data features are data attributes extracted from the qualified result set, such as field types, numerical distribution, classification cardinality, and time intervals. Error correction instructions are instructions containing error information, used to drive the target large language model to regenerate the query statement. Structured text features are text formed by concatenating multi-dimensional analysis results, used to generate visualization type labels. Visualization type labels are labels that guide chart selection, such as time-series trend ranking comparisons. The rule knowledge base is a resource library storing the prerequisite data for chart application scenarios. Mapped chart types are specific chart types matched from the rule knowledge base. Configuration parameters are parameters such as field mapping colors required to generate visualization charts.

[0163] This embodiment constructs a reliable and efficient query loop, adapting to the needs of the power industry. Interception and error correction in case of anomalies or verification failures prevent erroneous results from being transmitted to users, meeting the zero-tolerance requirements for accuracy in scenarios such as power dispatch fault diagnosis and ensuring data security. Multi-dimensional analysis integrates keyword, sentence structure, and result set features, comprehensively reflecting user intent and data characteristics, laying the foundation for accurate matching of visual charts. Based on a rule-based knowledge base, chart matching and configuration parameter generation automatically outputs visual charts adapted to business semantics, solving the pain point of traditional queries requiring manual result processing. This allows power business personnel to directly gain insights through intuitive charts, improving decision-making efficiency.

[0164] like Figure 2As illustrated, for example, if an execution exception or unreasonable validation result is captured, this embodiment determines that a "pattern illusion" SQL or erroneous SQL has been generated and immediately intercepts it. This embodiment does not return the result to the user but triggers an error correction mechanism. If everything is normal, the result set is returned to the user through a subsequent visualization module. When the SQL is intercepted, this embodiment does not simply report an error but initiates an automated error correction process. The user's original question (Q), the intercepted SQL, the execution error information, or the reasonableness validation conclusion are all used as new inputs and resent to the NL2SQL parsing engine. The re-parsed prompt will include an additional instruction: "The previously generated SQL failed to execute. The error reason is: 'Field name xxx does not exist'. Please avoid this error and regenerate the correct SQL based on the above context."

[0165] Through the above self-diagnosis and feedback loop, the model generates new data based on error information, which often produces correct SQL, greatly improving the success rate of the system and the user experience.

[0166] For example, this embodiment introduces a semantically driven intelligent visualization adaptation process to automatically convert SQL execution results into visual charts that best match the user's query intent and data structure characteristics, greatly reducing the threshold for data interpretation and improving decision-making efficiency. Specifically, this embodiment comprehensively analyzes the following multiple input signals to accurately capture user intent:

[0167] First, this embodiment identifies the user's query intent based on the user's question keyword information (i.e., extended keywords) using a target large language model. For example, if the question contains words such as "trend," "growth," and "change over time," it suggests that the user's intent is time series analysis; if the question contains words such as "percentage," "distribution," and "share," it suggests that the user's intent is composition analysis; if the question contains words such as "comparison," "different," "year-on-year," and "month-on-month," it suggests that the user's intent is comparative analysis.

[0168] Secondly, this embodiment analyzes the generated SQL statement itself. For example, a query containing a GROUP BY clause usually means that aggregation and grouping are required; a query containing ORDER BY...DESC and LIMIT clauses means that ranking is required; a query involving multi-table JOIN requires visualization of the relationships.

[0169] Furthermore, this embodiment performs statistical analysis on the query result set to identify its data characteristics. The specific implementation steps are as follows:

[0170] (1) Field structure analysis: In this embodiment, the metadata of the result set is traversed to record the number of fields, field names and data types returned by the database (such as VARCHAR, INT and DATETIME, etc.), and mapped to semantic types: text, numeric and time.

[0171] (2) Numerical distribution analysis: For numerical fields, calculate basic statistics (mean, standard deviation, maximum value, minimum value and quartiles). If the standard deviation is close to 0 or the proportion of extreme values ​​is too high, it is marked as "low discrimination".

[0172] (3) Categorical field analysis: For text fields, count the number of unique values ​​(cardinality). If the cardinality is ≤5, it is marked as a "low cardinality categorical field", which is suitable for grouping or legends; if the cardinality is >20 and there is no obvious clustering, it is marked as a "high cardinality unordered field", which is not suitable as a grouping dimension.

[0173] (4) Time field recognition and pattern detection: If there is a time field, detect whether its value has regular intervals (such as daily or monthly), and determine whether it is an equally spaced sequence by the difference method to support time series chart recommendations;

[0174] (5) Preliminary judgment of multi-field relationship: If there is one time field and one numerical field in the result set, "time series trend" is recommended; if there is one category field and one numerical field and the category base is ≤5, "proportion distribution" or "bar comparison" is recommended; if there are two numerical fields, "may be related" is initially marked for subsequent correlation chart recommendations.

[0175] Finally, based on the above analysis, this embodiment uses the FastText classification model to map user intent to predefined visualization type labels, such as time-series trends, percentage distributions, geographic locations, ranking comparisons, and relevance, providing clear guidance for the next step of chart matching. The specific implementation is as follows:

[0176] (1) Input feature construction: In this embodiment, the aforementioned analysis results are structured and concatenated into text features, including: keywords in the user's original question, such as "trend", "comparison", and "proportion"; SQL statement structure features, such as "containing GROUP BY" and "containing JOIN"; and result set data feature descriptions, such as "containing time field", "standard deviation of numerical field > 1000", and "cardinality of categorical field = 3". The above content is concatenated into a structured text, for example: "Keywords = trend, comparison; SQL structure = GROUP BY; data features = time field exists, high variance of numerical field, low cardinality of categorical field".

[0177] (2) Model training and deployment: In this embodiment, the training dataset is constructed using historical user query-chart selection logs. Each sample contains the above-mentioned structured text features and manually labeled or user-selected visualization type labels. The FastText supervised text classification mode is used for training, and the model automatically learns the mapping relationship between text features and visualization labels.

[0178] (3) Model output and decision: FastText outputs top-k labels for subsequent chart matching module sorting, realizing end-to-end mapping from "semantics + structure + data features" to "visual intent".

[0179] This embodiment also constructs a visual matching rule knowledge base, which stores the applicable scenarios, data prerequisites, and Echarts configuration examples for various chart types (such as line charts, bar charts, pie charts, scatter plots, and maps). The large model's inference engine can infer based on the rules. If the intent is "time-series trend" and includes one time field and one or more numerical fields, a line chart is recommended. If the intent is "proportional distribution" and includes one categorical field and one numerical field, a pie chart or donut chart is recommended. If the intent is "comparative analysis" and includes two categorical fields and one numerical field, a clustered bar chart or grouped bar chart is recommended. Based on the above matching rules, the large model's inference engine outputs one or more suggestions for the most suitable chart type and their Echarts configuration parameters, including X-axis and Y-axis mapping fields and color mapping fields.

[0180] For example, this embodiment describes a power company's operation monitoring center's desire to quickly understand the load status of main transformers in substations within a specific area in order to adjust operating modes and predict loads.

[0181] Step 1: User inputs natural language query. The user inputs the following natural language query: "Please list the maximum load rate of the main transformers of all 220 kV substations in Shijiazhuang in the past week, and sort them from highest to lowest load rate."

[0182] Step Two: Semantic Enhancement and Contextual Retrieval. Upon receiving a user query, this embodiment first performs keyword extraction and semantic expansion. Core entities are identified: "Shijiazhuang" (region), "220 kV" (voltage level), "substation," "main transformer" (equipment type), "maximum load rate" (indicator), and "last week" (time range). This embodiment converts these keywords into vectors and retrieves them from a pre-built power industry vector knowledge base. The search results successfully retrieved the following relevant information:

[0183] The substation information table and its fields: sub_id (station ID), sub_name (station name), vol_level (voltage level), district (region);

[0184] The transformer (main transformer information table) and its fields: trans_id (transformer ID), sub_id (substation ID), rated_capacity (rated capacity);

[0185] load_data (load data table) and its fields: trans_id (transform ID), load_value (load value), data_time (data time point);

[0186] Business terminology definition: "Load rate" = (Actual load / Rated capacity) * 100%;

[0187] Examples of similar historical queries and their successfully executed SQL statements.

[0188] Step 3: NL2SQL Parsing and SQL Generation. The following are the NL2SQL prompts:

[0189] Role: You are a professional database query engineer, proficient in SQL syntax and database structure. Your task is to generate accurate and executable SQL query statements based on natural language questions posed by users and the provided database table structure information.

[0190] Output requirements:

[0191] Output only the SQL statement; do not include any explanations, comments, Markdown formatting, or additional text.

[0192] The provided table and field names must be used strictly; they must not be made up or misspelled.

[0193] The query field output must be a Chinese alias; do not output the English field directly.

[0194] SQL statements must be syntactically correct, logically complete, and executable directly in the target database.

[0195] Related table structure:

[0196] Table name: substation (substation information table);

[0197] Fields: sub_id (integer, primary key), sub_name (varchar), vol_level (varchar), district (varchar);

[0198] Table name: transformer (main transformer information table);

[0199] Fields: trans_id (integer, primary key), sub_id (integer, foreign key), rated_capacity (float);

[0200] Table name: load_data (load data table);

[0201] Fields: data_id (bigint, primary key), trans_id (integer, foreign key), load_value (float), data_time (timestamp);

[0202] Business rule: The formula for calculating "load rate" is: (load_value / rated_capacity)*100;

[0203] User question: Please list the maximum load rate of the main transformers of all 220 kV substations in Shijiazhuang for the most recent week, and sort them from highest to lowest load rate.

[0204] The target large language model generated three SQL candidates based on the prompts. The evaluator then assigned a weighted score to each candidate.

[0205] Candidate A: Correct syntax, no dangerous operations, good performance (fields are clearly listed, and efficient aggregation is used), total score 100 points.

[0206] Candidate B: Syntactically correct and without dangerous operations, but it uses SELECT*, resulting in a slightly lower performance score, with a total score of 95.

[0207] Candidate C: The syntax is correct, but the field name (load_val) is misspelled, which will cause an error during execution. The syntax score is 0, and the total score is 0.

[0208] In this embodiment, candidate A is ultimately selected as the execution target, and its SQL is:

[0209] SELECT

[0210] s.sub_name,

[0211] t.trans_id,

[0212] MAX((l.load_value / t.rated_capacity) * 100) AS max_load_rate

[0213] FROM substations

[0214] JOIN transformer t ON s.sub_id = t.sub_id

[0215] JOIN load_data l ON t.trans_id = l.trans_id

[0216] WHERE s.district = 'Shijiazhuang'

[0217] AND s.vol_level = '220kV'

[0218] AND l.data_time>= NOW() - INTERVAL '7 days'

[0219] GROUP BY s.sub_name, t.trans_id

[0220] ORDER BY max_load_rate DESC;

[0221] Step 4: SQL Execution and Result Verification. In this example, the SQL for candidate A is executed in a test sandbox environment. Execution is successful, no exception is thrown, and a result set containing the substation name, main transformer ID, and maximum load rate is returned.

[0222] This embodiment undergoes a plausibility check:

[0223] Business rule verification: The check found that all load rate values ​​were within a reasonable range (0-150%), and passed.

[0224] Semantic consistency check: The similarity between the result summary ("The query result is: the maximum load rate of 30 main transformers in 15 220kV substations in Shijiazhuang is 85.7%) and the user's original question was calculated, and the score was as high as 0.92, which passed.

[0225] All checks passed, and the result was deemed reasonable and valid.

[0226] Step 5: Intelligent Visualization Adaptation. This embodiment analyzes the user query and SQL results. It identifies the user intent as "ranking comparison," and the results contain the key fields "name" and "value." Based on the "ranking comparison" intent, the chart matching engine determines that a horizontal bar chart is the optimal choice from the rule base, clearly and intuitively displaying the ranking and comparison of different main transformer load rates. This embodiment automatically generates a visual chart and returns it to the user's front-end interface along with the original data.

[0227] As can be seen from the above, this embodiment, by deeply integrating large-scale model technology with power system data query and display, forms a highly targeted and efficient solution, which has the following significant advantages compared to existing technologies:

[0228] (1) Improved the accuracy and professionalism of power data queries. This embodiment adopts a targeted fine-tuning strategy of multi-task learning and uses power industry business knowledge corpus for training, enabling the model to deeply internalize the unique logic and terminology system of power business. Compared with using a general model, the generated SQL statements have significantly improved in terms of business semantic accuracy, avoiding query errors caused by ambiguity in industry terminology.

[0229] (2) Accurate data retrieval and context construction based on semantic understanding were achieved. This embodiment constructs a vector knowledge base that integrates power equipment information, business indicators and historical queries. It can accurately understand the user's true intention from the user's vague and colloquial natural language descriptions and automatically recall the most relevant data table structure information, providing a rich and high-quality context for the model to generate SQL, thus solving the "pattern illusion" problem.

[0230] (3) A comprehensive data security and reliability barrier has been constructed. This embodiment intercepts dangerous operation statements through a multi-candidate SQL generation and weighted evaluation mechanism, reducing the risk of power grid data being tampered with or deleted due to misoperation.

[0231] (4) Improved efficiency of data analysis and decision-making. This embodiment realizes end-to-end automation from "natural language questioning" to "visual insights". Business personnel do not need to write complex SQL or manually create charts. They only need to describe their needs in natural language to automatically generate SQL, query data and return the most suitable visualization results, which liberates productivity and helps to make power decision-making more agile.

[0232] Corresponding to the data query method based on natural language recognition in the above embodiments, Figure 3 This is a structural block diagram of a data query device based on natural language recognition, provided as an embodiment of this application. For ease of explanation, only the parts relevant to the embodiment of this application are shown. References Figure 3 The data query device 20 based on natural language recognition includes: a user query keyword extraction module 21, a vector retrieval enhanced context module 22, an SQL statement generation module 23, an SQL execution and structure verification module 24, and a visualization module 25.

[0233] Among them, the user query keyword extraction module 21 is used to obtain extended keywords based on the user's natural language query through the target large language model and the pre-built business term dictionary; the target large language model is obtained by training the initial large language model based on the power corpus dataset, the objective function and the fine-tuning parameters, and the fine-tuning parameters are designed based on the hierarchical progressive fine-tuning strategy;

[0234] The vector retrieval enhanced context module 22 is used to perform parallel retrieval based on extended keywords, user natural language queries, and a vector database to obtain pattern information and business knowledge, respectively; it also obtains contextual suggestion blocks based on the pattern information and business knowledge; the vector database is constructed based on pattern data and business knowledge data.

[0235] SQL statement generation module 23 is used to generate target prompt words based on context prompt blocks, user natural language queries and system prompt word templates; based on target prompt words and model generation parameters, multiple candidate query statements are generated through the target large language model, multi-dimensional scores of each candidate query statement are calculated, and the candidate query statement with the highest multi-dimensional score is taken as the optimal query statement.

[0236] The SQL execution and structure verification module 24 is used to execute the optimal query statement in the database test environment and obtain the execution result set; capture exception information when executing the optimal query statement; and perform rule verification and semantic consistency verification on the execution result set to obtain the verification result.

[0237] The visualization module 25 is used to take the execution result set as the qualified result set if the exception information is empty and the verification result is qualified; to obtain multi-dimensional analysis results based on extended keywords, optimal query statements and qualified result sets; to generate visualization charts based on multi-dimensional analysis results and qualified result sets; and to display the visualization charts and qualified result sets.

[0238] In one embodiment of this application, when the user query keyword extraction module 21 obtains extended keywords based on the user's natural language query through the target large language model and the pre-built business term dictionary, it is specifically used to: extract keywords from the user's natural language query through the target large language model to obtain query keywords; and expand the query keywords based on the pre-built business term dictionary to obtain extended keywords.

[0239] In one embodiment of this application, when the vector retrieval enhanced context module 22 obtains a context hint block based on pattern information and business knowledge, it is specifically used to: remove redundancy from the pattern information and business knowledge according to semantic similarity; and concatenate the pattern information and business knowledge after removing redundancy according to a preset template to obtain a context hint block.

[0240] In one embodiment of this application, the objective function is:

[0241] ;

[0242] ;

[0243] in, For the target loss value, This represents the i-th basic loss component, where i = {1, 2, 3}. for , for , for , The adaptive weight coefficients are the values ​​corresponding to the i-th basic loss component. For regularization terms, These are all trainable parameters in the initial large language model;

[0244] ;

[0245] ;

[0246] ;

[0247] ;

[0248] ;

[0249] ;

[0250] ;

[0251] ;

[0252] in, For NL2SQL master loss, For users to query in natural language, This is a sequence of actual SQL tags. T is the sequence length of the actual SQL label sequence. p is the real token at time step t. For the initial large language model based on The generated conditional probability distribution, Prediction tokens for the initial large language model

[0253] For structural consistency loss, For based on The resulting abstract syntax tree, For based on The resulting abstract syntax tree, From The set of all paths from the root node to the leaf node extracted from the data. From The set of all paths from the root node to the leaf node extracted from the data. For the accuracy between path sets, Recall rate among path sets The structure F1 score;

[0254] To resist interference and mitigate losses, Yes Adversarial examples generated by injecting noise, The initial large language model is based on adversarial examples The generated conditional probability distribution For adversarial examples The corresponding real token at time step t.

[0255] In one embodiment of this application, the data query device 20 based on natural language recognition further includes: an illusion interception and semantic error correction module, used to: intercept the optimal query statement if the abnormal information is not empty and / or the verification result is unqualified; determine the error information based on the abnormal information and / or the verification result; regenerate the query statement through error correction instructions based on the optimal query statement, the user's natural language query and the error information; and execute the regenerated query statement to obtain a qualified result set.

[0256] In one embodiment of this application, when the visualization module 25 obtains multi-dimensional analysis results based on extended keywords, optimal query statements, and qualified result sets, it is specifically used to: extract keyword features from extended keywords, extract statement structure features from optimal query statements, and extract result set data features from qualified result sets; and use the keyword features, statement structure features, and result set data features as multi-dimensional analysis results.

[0257] In one embodiment of this application, when the visualization module 25 generates a visualization chart based on the multi-dimensional analysis results and the qualified result set, it is specifically used to: concatenate the multi-dimensional analysis results into structured text features, generate visualization type labels based on the structured text features; match the mapping chart type from the rule knowledge base based on the visualization type labels; determine the configuration parameters based on the mapping chart type and the qualified result set; and generate the visualization chart based on the configuration parameters and the qualified result set.

[0258] See Figure 4 , Figure 4 This is a schematic block diagram of an electronic device provided according to an embodiment of this application. Figure 4The electronic device 300 in this embodiment may include one or more processors 301, one or more input devices 302, one or more output devices 303, and one or more memories 304. The processors 301, input devices 302, output devices 303, and memories 304 communicate with each other via a communication bus 305. The memories 304 store computer programs, including program instructions. The processors 301 execute the program instructions stored in the memories 304. Specifically, the processors 301 are configured to invoke the program instructions to perform the functions of the modules in the aforementioned device embodiments, for example... Figure 3 The functions of the user query keyword extraction module 21, vector retrieval enhanced context module 22, SQL statement generation module 23, SQL execution and structure verification module 24, and visualization module 25 are shown.

[0259] It should be understood that, in the embodiments of this application, the processor 301 may be a central processing unit (CPU), but it may also be other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor.

[0260] Input device 302 may include a touchpad, a fingerprint sensor (for collecting the user's fingerprint information and fingerprint orientation information), a microphone, etc., and output device 303 may include a display (LCD, etc.), a speaker, etc.

[0261] The memory 304 may include read-only memory and random access memory, and provides instructions and data to the processor 301. A portion of the memory 304 may also include non-volatile random access memory. For example, the memory 304 may also store information about query statements.

[0262] In specific implementations, the processor 301, input device 302, and output device 303 described in the embodiments of this application can execute the implementation methods described in the embodiments of the data query method based on natural language recognition provided in the embodiments of this application, or they can execute the implementation methods of the electronic device 300 described in the embodiments of this application, which will not be repeated here.

[0263] In another embodiment of this application, a computer-readable storage medium is provided. This computer-readable storage medium stores a computer program, which includes program instructions. When executed by a processor, the program instructions implement all or part of the processes in the methods described above. Alternatively, the computer program can instruct related hardware to complete the process. The computer program can be stored in a computer-readable storage medium, and when executed by a processor, it can implement the steps of the various method embodiments described above. The computer program includes computer program code, which can be in the form of source code, object code, executable files, or certain intermediate forms. The computer-readable medium can include any entity or device capable of carrying computer program code, a recording medium, a USB flash drive, a portable hard drive, a magnetic disk, an optical disk, a computer memory, a read-only memory (ROM), a random access memory (RAM), an electrical carrier signal, a telecommunication signal, and a software distribution medium, etc.

[0264] The computer-readable storage medium can be an internal storage unit of the electronic device in any of the foregoing embodiments, such as a hard disk or memory of the electronic device. The computer-readable storage medium can also be an external storage device of the electronic device, such as a plug-in hard disk, smart media card (SMC), secure digital card (SD) card, flash card, etc., equipped on the electronic device. Furthermore, the computer-readable storage medium can include both internal and external storage units of the electronic device. The computer-readable storage medium is used to store computer programs and other programs and data required by the electronic device. The computer-readable storage medium can also be used to temporarily store data that has been output or will be output.

[0265] Those skilled in the art will recognize that the modules / units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementations should not be considered beyond the scope of this application.

[0266] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working process of the electronic devices and units described above can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.

[0267] In the several embodiments provided in this application, it should be understood that the disclosed electronic devices and methods can be implemented in other ways. For example, the device embodiments described above are merely illustrative. For instance, the division of modules / units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple modules, units, or components may be combined or integrated into another system, or some features may be ignored or not executed. In addition, the mutual coupling or direct coupling or communication connection shown or discussed may be indirect coupling or communication connection through some interfaces or modules / units, or it may be an electrical, mechanical, or other form of connection.

[0268] The modules / units described as separate components may or may not be physically separate. Similarly, the components shown as modules / units may or may not be physical modules / units; they may be located in one place or distributed across multiple network modules / units. Some or all of the modules / units can be selected to achieve the purpose of the embodiments of this application, depending on actual needs.

[0269] Furthermore, the functional modules / units in the various embodiments of this application can be integrated into one processing module / unit, or each module / unit can exist physically separately, or two or more modules / units can be integrated into one module / unit. The integrated modules / units described above can be implemented in hardware or in the form of software functional modules / units.

[0270] The above are merely specific embodiments of this application, but the scope of protection of this application is not limited thereto. Any person skilled in the art can easily conceive of various equivalent modifications or substitutions within the technical scope disclosed in this application, and these modifications or substitutions should all be covered within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.

Claims

1. A data query method based on natural language recognition, characterized in that, include: Based on user natural language queries, extended keywords are obtained through a target large language model and a pre-built business term dictionary; the target large language model is obtained by training an initial large language model based on an electric corpus dataset, an objective function, and fine-tuning parameters, and the fine-tuning parameters are designed based on a hierarchical progressive fine-tuning strategy. The objective function is: ; ; in, For the target loss value, This represents the i-th basic loss component, where i = {1, 2, 3}. for , for , for , The adaptive weight coefficients are the values ​​corresponding to the i-th basic loss component. For regularization terms, These are all trainable parameters in the initial large language model; ; ; ; ; ; ; ; ; in, For NL2SQL master loss, For users to query in natural language, This is a sequence of actual SQL labels. T is the sequence length of the actual SQL label sequence. p is the real token at time step t. For the initial large language model based on The generated conditional probability distribution, The prediction token for the initial large language model; For structural consistency loss, For based on The abstract syntax tree obtained from parsing, For based on The abstract syntax tree obtained from parsing, From The set of all paths from the root node to the leaf node extracted from the data. From The set of all paths from the root node to the leaf node extracted from the data. For the accuracy between path sets, Recall rate among path sets The structure F1 score; To resist interference and mitigate losses, Yes Adversarial examples generated by injecting noise, This indicates that the initial large language model is based on adversarial examples. The generated conditional probability distribution, For adversarial examples The corresponding real token at time step t; Parallel retrieval is performed based on the extended keywords, the user's natural language query, and the vector database to obtain pattern information and business knowledge, respectively; a contextual suggestion block is obtained based on the pattern information and the business knowledge; the vector database is constructed based on pattern data and business knowledge data. Based on the contextual prompt block, the user's natural language query, and the system prompt word template, a target prompt word is generated; based on the target prompt word and the model generation parameters, multiple candidate query statements are generated through the target large language model, and a multidimensional score is calculated for each candidate query statement. The candidate query statement with the highest multidimensional score is selected as the optimal query statement. The optimal query statement is executed in a database test environment to obtain an execution result set; exception information is captured during the execution of the optimal query statement; rule validation and semantic consistency validation are performed on the execution result set to obtain the validation results. If the abnormal information is empty and the verification result is qualified, then the execution result set is taken as the qualified result set; multi-dimensional analysis results are obtained based on the extended keywords, the optimal query statement, and the qualified result set; a visualization chart is generated based on the multi-dimensional analysis results and the qualified result set; the visualization chart and the qualified result set are used for display.

2. The data query method based on natural language recognition as described in claim 1, characterized in that, The expanded keywords, derived from the user's natural language query using a target large language model and a pre-built business terminology dictionary, include: The target large language model is used to extract keywords from the user's natural language query to obtain the query keywords; The query keywords are expanded based on a pre-built business terminology dictionary to obtain expanded keywords.

3. The data query method based on natural language recognition as described in claim 1, characterized in that, The context suggestion block obtained based on the pattern information and the business knowledge includes: Redundancy is removed from the pattern information and the business knowledge based on semantic similarity. The redundant pattern information and business knowledge are concatenated in context according to the preset template to obtain the context hint block.

4. The data query method based on natural language recognition as described in claim 1, characterized in that, Before obtaining the multi-dimensional analysis results based on the expanded keywords, the optimal query statement, and the qualified result set, the method further includes: If the abnormal information is not empty and / or the verification result is a verification failure, then the optimal query statement is intercepted, and an error message is determined based on the abnormal information and / or the verification result. Based on the optimal query statement, the user's natural language query, and the error information, the query statement is regenerated using error correction instructions; Execute the regenerated query to obtain a valid result set.

5. The data query method based on natural language recognition as described in claim 1, characterized in that, The multi-dimensional analysis results obtained based on the expanded keywords, the optimal query statement, and the qualified result set include: Extract keyword features from the expanded keywords, extract statement structure features from the optimal query statement, and extract result set data features from the qualified result set; The keyword features, the sentence structure features, and the result set data features are used as the results of the multi-dimensional analysis.

6. The data query method based on natural language recognition as described in claim 1, characterized in that, The generation of visualization charts based on the multi-dimensional analysis results and the qualified result set includes: The multi-dimensional analysis results are concatenated into structured text features, and visual type labels are generated based on the structured text features. Based on the visualization type label, match and map the chart type from the rule knowledge base; The configuration parameters are determined based on the mapping chart type and the qualified result set; A visualization chart is generated based on the configuration parameters and the qualified result set.

7. A data query device based on natural language recognition, characterized in that, include: The user query keyword extraction module is used to obtain extended keywords based on user natural language queries through a target large language model and a pre-built business term dictionary. The target large language model is obtained by training an initial large language model based on an electric corpus dataset, an objective function, and fine-tuning parameters. The fine-tuning parameters are designed based on a hierarchical progressive fine-tuning strategy. The objective function is: ; ; in, For the target loss value, This represents the i-th basic loss component, where i = {1, 2, 3}. for , for , for , The adaptive weight coefficients are the values ​​corresponding to the i-th basic loss component. For regularization terms, These are all trainable parameters in the initial large language model; ; ; ; ; ; ; ; ; in, For NL2SQL master loss, For users to query in natural language, This is a sequence of actual SQL labels. T is the sequence length of the actual SQL label sequence. p is the real token at time step t. For the initial large language model based on The generated conditional probability distribution, The prediction token for the initial large language model; For structural consistency loss, For based on The abstract syntax tree obtained from parsing, For based on The abstract syntax tree obtained from parsing, From The set of all paths from the root node to the leaf node extracted from the data. From The set of all paths from the root node to the leaf node extracted from the data. For the accuracy between path sets, Recall rate among path sets The structure F1 score; To resist interference and mitigate losses, Yes Adversarial examples generated by injecting noise, This indicates that the initial large language model is based on adversarial examples. The generated conditional probability distribution, For adversarial examples The corresponding real token at time step t; The vector retrieval enhanced context module is used to perform parallel retrieval based on the extended keywords, the user's natural language query, and the vector database to obtain pattern information and business knowledge, respectively; and to obtain a contextual suggestion block based on the pattern information and the business knowledge; the vector database is constructed based on pattern data and business knowledge data. The SQL statement generation module is used to generate target prompt words based on the context prompt block, the user's natural language query, and the system prompt word template; based on the target prompt words and model generation parameters, it generates multiple candidate query statements through the target large language model, calculates the multidimensional score of each candidate query statement, and selects the candidate query statement with the highest multidimensional score as the optimal query statement. The SQL execution and structure verification module is used to execute the optimal query statement in a database test environment to obtain an execution result set; capture exception information when executing the optimal query statement; and perform rule verification and semantic consistency verification on the execution result set to obtain verification results. The visualization module is used to: if the exception information is empty and the verification result is qualified, then take the execution result set as the qualified result set; obtain multi-dimensional analysis results based on the extended keywords, the optimal query statement, and the qualified result set; generate a visualization chart based on the multi-dimensional analysis results and the qualified result set; and display the visualization chart and the qualified result set.

8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and running on the processor, characterized in that, When the processor executes the computer program, it implements the steps of the method as described in any one of claims 1 to 6.

9. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method as described in any one of claims 1 to 6.