Query statement generation method and device, equipment, medium and program product

By combining knowledge graphs and large language models, we can generate SQL query statements that conform to grammatical norms and meet user needs, thus solving the problem of insufficient accuracy in existing technologies and achieving efficient generation and correction of complex queries.

CN121524328APending Publication Date: 2026-02-13INNOVATION QIZHI TECH GRP CO LTD

Patent Information

Application Number
CN202511774048.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-11-28
Publication Date
2026-02-13

AI Technical Summary

Technical Problem

Existing methods fail to deeply understand the semantic relationships between database tables and the specific business context of users when generating complex SQL queries, resulting in insufficient accuracy and poor adaptability of the generated query statements, making it difficult to meet actual business needs.

Method used

By constructing a knowledge graph to obtain a candidate data table for context, and combining it with a large language model for personalized filtering and verification, query statements that conform to grammatical norms and meet requirements are generated, and error correction is performed using a database simulator.

Benefits of technology

It improves the accuracy and adaptability of SQL queries, reduces the burden of manual debugging for users, and enhances the user experience and the reliability of query results.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121524328A_ABST
    Figure CN121524328A_ABST
Patent Text Reader

Abstract

The invention discloses a query statement generation method and device, equipment, a medium and a program product. The method comprises the following steps: receiving a natural language query text input by a target user; obtaining a context candidate data table matched with the natural language query text according to a pre-constructed knowledge graph; performing personalized screening on the context candidate data table to obtain a personalized data table; processing the personalized data table and the natural language query text through a large language model to generate a preliminary query statement; and when the preliminary query statement is verified to have no error, determining the preliminary query statement as a final target query statement and outputting the final target query statement. According to the method, the problem that the accuracy is insufficient when a complex query is generated due to the lack of understanding of database semantics and user context can be solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of database query technology, specifically to a query statement generation method, apparatus, electronic device, readable storage medium, and computer program product. Background Technology

[0002] In recent years, with the surge in enterprise data volume and the increasing complexity of database systems, tools capable of automatically generating accurate SQL queries have become crucial in data analysis and system testing scenarios. Existing methods typically convert user-input natural language directly into SQL query statements. However, these methods have significant limitations when dealing with multi-table joins or complex business logic. Because they cannot deeply understand the semantic relationships between database tables and the user's specific business context, the generated queries often lack accuracy and adaptability, failing to meet the precise query requirements of complex scenarios in real-world business operations. Summary of the Invention

[0003] In view of the above problems, this application provides a query statement generation method, apparatus, electronic device, readable storage medium, and computer program product, which can solve the problem of insufficient accuracy in generating complex queries due to a lack of understanding of database semantics and user context.

[0004] Firstly, this application provides a method for generating query statements, including: Receive natural language query text input by the target user; Based on a pre-constructed knowledge graph, obtain a contextual candidate data table that matches the natural language query text; Personalized filtering is performed on the context candidate data table to obtain a personalized data table; The personalized data table and the natural language query text are processed using a large language model to generate a preliminary query statement; Once it is verified that the preliminary query statement does not contain any errors, the preliminary query statement is determined as the final target query statement and output.

[0005] In the above technical solution, this method can accurately capture the user's query intent and, relying on the semantic association capabilities of the knowledge graph, efficiently locate candidate data tables related to the query requirements, avoiding interference from irrelevant data. It can also filter data based on the user's personalized needs and business scenarios, ensuring a high degree of fit between the personalized data tables and the actual query scenarios. Furthermore, it can leverage the natural language understanding and SQL generation capabilities of the large language model and the accurately filtered personalized data tables to output preliminary query statements that conform to grammatical norms and fit the requirements. Finally, it can ensure the correctness and usability of the query statements through an error verification process, thereby enabling the target query statements to effectively adapt to complex business scenarios.

[0006] In some implementations, the method further includes, prior to receiving the natural language query text input by the target user: Obtain the original database, and based on the original database, obtain entities, relationships between entities, metadata, query logs, and domain knowledge; A knowledge graph is constructed based on the entities, the relationships between the entities, the metadata, the query logs, and the domain knowledge.

[0007] In the above technical solution, the method can construct a multi-dimensional semantic knowledge graph that integrates database entities, entity relationships, metadata, user query habits and domain business rules, so that the knowledge graph can deeply characterize the intrinsic relationship between the underlying data logic of the database and the upper-level business scenario.

[0008] In some implementations, the nodes of the knowledge graph include the entities, the connecting edges of the knowledge graph include the associations, and the node attributes of the knowledge graph include the metadata, the query logs, and the domain knowledge.

[0009] In the above technical solution, the method can accurately anchor core data entities through nodes, intuitively present the relationship logic between entities through connecting edges, and enrich the business background and user query characteristics of data through node attributes, thereby providing accurate and comprehensive semantic basis for the rapid matching and personalized filtering of subsequent context candidate data tables.

[0010] In some implementations, obtaining a context candidate data table that matches the natural language query text based on a pre-constructed knowledge graph includes: Generate a query text vector based on the natural language query text; Based on the nodes and node attributes in the knowledge graph, generate multiple node vectors; Calculate the vector similarity between the query text vector and each node vector; Based on the vector similarity, a semantically relevant context candidate data table is obtained from the knowledge graph.

[0011] In the above technical solution, the method can transform natural language queries and knowledge graph elements into quantifiable vector forms, and achieve accurate semantic matching through vector similarity calculation. It can efficiently filter out context candidate data tables that are highly relevant to the query requirements from the knowledge graph, avoiding the problem of missed or incorrect selection caused by insufficient semantic understanding in traditional matching methods.

[0012] In some implementations, the personalized filtering of the context candidate data table to obtain a personalized data table includes: Obtain the user information of the target user; wherein, the user information includes at least the user's organizational structure and historical data access records; The context candidate data table is filtered based on the user information to obtain a personalized data table.

[0013] In the above technical solution, the method can combine the target user's organizational structure, historical data access records and other core user information to perform targeted filtering of context candidate data tables, so as to obtain personalized data tables that fit the user's business scenarios and usage habits, avoid interference from irrelevant data tables, and ensure that the subsequent query statements generated are more in line with the user's actual business needs.

[0014] In some embodiments, the method further includes: The preliminary query statement is verified using a database simulator. If the preliminary query statement is found to be error-free, the step of determining the preliminary query statement as the final target query statement and outputting it is executed.

[0015] In the above technical solution, the method can use a database simulator to perform targeted verification on the initial query statement, accurately identify problems such as syntax errors and abnormal data association, and ensure that the output target query statement has syntactic correctness and execution feasibility.

[0016] In some embodiments, the method further includes: When an error is detected in the initial query statement, error information is obtained based on the verification result; Based on the error message, the initial query statement, the natural language query text, and the personalized data table, construct the corrected data; The corrected data is input into the large language model to regenerate the initial query statement, and the step of verifying the initial query statement through the database simulator to obtain the verification result is executed.

[0017] In the above technical solution, when the initial query statement is found to have errors, the method can accurately capture error information and integrate relevant core data to construct corrective data. It can also achieve iterative optimization and regeneration of the query statement through a large language model, and continuously correct syntax, data association and other issues by combining the loop verification mechanism of the database simulator. This ensures that the final output target query statement fully complies with the syntax specifications and business requirements, and greatly improves the fault tolerance and accuracy of query statement generation in complex query scenarios.

[0018] In some implementations, after inputting the corrected data into the large language model and regenerating the initial query statement, the method further includes: When the number of times the initial query statement is regenerated reaches a preset threshold, the last generated initial query statement is determined as the final target query statement, and the target query statement is output.

[0019] In the above technical solution, the method can limit the number of times the query statement is regenerated by setting a preset threshold, thus avoiding the iterative correction process from getting stuck in an infinite loop; at the same time, the final generated preliminary query statement is used as the target query statement for output, ensuring that usable query results are provided within a reasonable time.

[0020] Secondly, this application provides a query statement generation apparatus, comprising: The receiving unit is used to receive natural language query text input by the target user; The acquisition unit is used to acquire a context candidate data table that matches the natural language query text based on a pre-constructed knowledge graph; A filtering unit is used to perform personalized filtering on the context candidate data table to obtain a personalized data table. The generation unit is used to process the personalized data table and the natural language query text through a large language model to generate a preliminary query statement; The output unit is used to determine the preliminary query statement as the final target query statement and output it when it is verified that the preliminary query statement does not contain any errors.

[0021] In the above technical solution, the device can accurately capture the user's query intent and, relying on the semantic association capabilities of the knowledge graph, efficiently locate candidate data tables related to the query requirements, avoiding interference from irrelevant data. It can also filter data based on the user's personalized needs and business scenarios, ensuring a high degree of adaptation between the personalized data tables and the actual query scenarios. Furthermore, it can leverage the natural language understanding and SQL generation capabilities of the large language model and the accurately filtered personalized data tables to output preliminary query statements that conform to grammatical norms and fit the requirements. Finally, it can ensure the correctness and usability of the query statements through an error verification process, thereby enabling the target query statements to effectively adapt to complex business scenarios.

[0022] Thirdly, this application provides an electronic device, the electronic device including a memory and a processor, the memory for storing a computer program, the processor running the computer program to cause the electronic device to perform the query statement generation method described in any one of the first aspects.

[0023] Fourthly, this application provides a readable storage medium storing a computer program, which, when executed by a processor, performs the query statement generation method described in any one of the first aspects.

[0024] Fifthly, this application provides a computer program product comprising a computer program that, when executed by a processor, performs the query statement generation method described in any one of the first aspects.

[0025] The beneficial effects of this application are as follows: it can more accurately understand the user's query intent and the semantic relationships of the dataset through the semantic enhancement of knowledge graphs and the natural language processing capabilities of large language models, thereby generating more accurate SQL query statements; at the same time, the user interaction and feedback loop mechanism enables users to provide real-time feedback during the query statement generation process, thereby adjusting the generation of query statements through feedback, which improves user participation and satisfaction; finally, the self-correction mechanism can also automatically detect and correct errors in SQL queries, ensuring that the generated SQL is not only syntactically correct, but also can be executed efficiently and reliably. Attached Figure Description

[0026] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments of this application will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.

[0027] Figure 1 This is a flowchart illustrating the query statement generation method in some embodiments of this application; Figure 2 This is a schematic diagram of the structure of a query statement generation device in some embodiments of this application; Figure 3 This is a schematic diagram of the structure of an electronic device in some embodiments of this application. Detailed Implementation

[0028] The embodiments of the technical solution of this application will now be described in detail with reference to the accompanying drawings. These embodiments are only used to more clearly illustrate the technical solution of this application and are therefore merely examples, and should not be used to limit the scope of protection of this application.

[0029] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application pertains; the terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit the application; the terms “comprising” and “having”, and any variations thereof, in the specification, claims, and foregoing description of the drawings are intended to cover non-exclusive inclusion.

[0030] In the description of the embodiments of this application, technical terms such as "first" and "second" are used only to distinguish different objects and should not be construed as indicating or implying relative importance or implicitly specifying the number, specific order, or primary and secondary relationship of the indicated technical features. In the description of the embodiments of this application, "multiple" means two or more (including two), similarly, "multiple sets" refers to two or more sets (including two sets), and "multiple pieces" refers to two or more pieces (including two pieces) unless otherwise explicitly defined.

[0031] In this document, the term "embodiment" means that a particular feature, structure, or characteristic described in connection with an embodiment may be included in at least one embodiment of this application. The appearance of this phrase in various places throughout the specification does not necessarily refer to the same embodiment, nor is it a separate or alternative embodiment mutually exclusive with other embodiments. It will be explicitly and implicitly understood by those skilled in the art that the embodiments described herein can be combined with other embodiments.

[0032] In the description of the embodiments in this application, the term "and / or" is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, and B existing alone. Additionally, the character " / " in this document generally indicates that the preceding and following related objects have an "or" relationship.

[0033] Currently, most methods for generating SQL query statements directly convert user-input natural language into SQL query statements. However, existing methods have significant limitations when dealing with multi-table joins or complex business logic. Because they cannot deeply understand the semantic relationships between database tables and the user's specific business context, the generated query statements often lack accuracy and adaptability, making it difficult to meet the precise query requirements of complex business scenarios.

[0034] To address the aforementioned technical issues, this application provides a query statement generation method. This method can provide rich contextual and semantic information through knowledge graphs, enabling the system to understand complex data relationships and business logic. It can also process complex natural language input using a large language model, accurately converting it into SQL queries, thus lowering the technical threshold for users. Furthermore, it can reduce the burden of manual debugging and correction for users by automatically correcting SQL errors, thereby improving work efficiency. Finally, it can enhance the user experience through an interactive interface and feedback mechanism, allowing non-technical personnel to easily use the system to generate SQL queries.

[0035] like Figure 1 As shown, some embodiments of this application provide a query statement generation method, which includes: S101, Receive the natural language query text input by the target user; S102. Based on the pre-constructed knowledge graph, obtain a context candidate data table that matches the natural language query text; S103. Perform personalized filtering on the context candidate data table to obtain a personalized data table; S104. Process the personalized data table and natural language query text using a large language model to generate preliminary query statements; S105. When it is verified that there are no errors in the preliminary query statement, the preliminary query statement is determined as the final target query statement and output.

[0036] In some embodiments, natural language query text refers to text content entered by the target user in everyday conversational language to express data query needs.

[0037] In some embodiments, a knowledge graph refers to a structured knowledge network built upon entities, entity relationships, metadata, query logs, and domain knowledge in the original database, used to characterize data semantics and business logic.

[0038] In some embodiments, the context candidate data table refers to a collection of database tables that are semantically associated with the natural language query text, selected through knowledge graph matching.

[0039] In some embodiments, a personalized data table refers to a database table that is selected from a contextual candidate data table by combining user information such as the target user's organizational structure and historical data access records, and that fits the user's business scenario and usage habits.

[0040] In some embodiments, the preliminary query statement refers to the query statement generated by the large language model based on the personalized data table and natural language query text, which conforms to the SQL syntax specification and initially meets the user's query needs.

[0041] In some embodiments, the target query statement refers to a query statement that has been verified to be error-free or has been determined through iterative correction, and that can be directly executed and return accurate results.

[0042] In some embodiments, large language models (such as the GPT series) possess powerful natural language understanding and generation capabilities. These models can deeply understand the user's natural language and generate SQL statements that conform to grammar and semantics.

[0043] In some embodiments, the method also allows users to personalize system behavior by defining datasets, providing custom instructions and example queries, thereby enabling users to adjust the system's query generation and optimization strategies according to specific needs, thus avoiding dependence on the platform team.

[0044] In the above embodiments, this method can accurately capture the user's query intent and, relying on the semantic association capabilities of the knowledge graph, efficiently locate candidate data tables related to the query requirements, avoiding interference from irrelevant data. It can also filter data based on the user's personalized needs and business scenarios, ensuring a high degree of fit between the personalized data tables and the actual query scenarios. Furthermore, it can leverage the natural language understanding and SQL generation capabilities of the large language model and the accurately filtered personalized data tables to output preliminary query statements that conform to grammatical norms and meet the requirements. Finally, it can ensure the correctness and usability of the query statements through an error verification process, thereby enabling the target query statements to effectively adapt to complex business scenarios.

[0045] In some embodiments, the method further includes, before receiving the natural language query text input by the target user: Obtain the raw database, and extract entities, relationships between entities, metadata, query logs, and domain knowledge based on the raw database; A knowledge graph is constructed based on entities, relationships between entities, metadata, query logs, and domain knowledge.

[0046] In some embodiments, knowledge graphs provide rich contextual information by modeling entities (such as users, tables, fields, etc.) and their relationships in a database as a graph structure. This structured semantic network enables the system to better understand the complex relationships and business semantics between data, thereby providing more accurate contextual support when generating SQL queries.

[0047] In the above embodiments, the method can construct a multi-dimensional semantic knowledge graph that integrates database entities, entity relationships, metadata, user query habits, and domain business rules, so that the knowledge graph can deeply characterize the intrinsic relationship between the underlying data logic of the database and the upper-level business scenarios.

[0048] In some embodiments, the nodes of a knowledge graph include entities, the connecting edges of a knowledge graph include relationships, and the node attributes of a knowledge graph include metadata, query logs, and domain knowledge.

[0049] In some embodiments, the method can construct and utilize a knowledge graph, treating users, table clusters, tables, and fields as nodes; where node attributes are derived from metadata, query logs, and crowdsourced domain knowledge. Based on this, a deeper semantic understanding of datasets and concepts can be enhanced, which is beneficial for improving the accuracy of SQL queries.

[0050] In the above embodiments, the method can accurately anchor core data entities through nodes, intuitively present the relationship logic between entities through connecting edges, and enrich the business background and user query characteristics of data through node attributes, thereby providing accurate and comprehensive semantic basis for the rapid matching and personalized filtering of subsequent context candidate data tables.

[0051] In some embodiments, a contextual candidate data table matching the natural language query text is obtained based on a pre-constructed knowledge graph, including: Generate query text vectors from natural language query text; Generate multiple node vectors based on the nodes and node attributes in the knowledge graph; Calculate the vector similarity between the query text vector and the vector of each node; Obtain semantically relevant context candidate data tables from the knowledge graph based on vector similarity.

[0052] In some embodiments, the method can use embedded retrieval techniques to convert user input and database metadata into vectors in a high-dimensional vector space.

[0053] In some embodiments, the method can quickly retrieve contextual information and data tables related to user queries by calculating the similarity between vectors, thereby improving the efficiency and accuracy of query generation.

[0054] In the above embodiments, the method can transform natural language queries and knowledge graph elements into quantifiable vector forms, achieve accurate semantic matching through vector similarity calculation, and efficiently filter out contextual candidate data tables that are highly relevant to the query requirements from the knowledge graph, avoiding the problem of missed or incorrect selection caused by insufficient semantic understanding in traditional matching methods.

[0055] In some embodiments, the context candidate data table is individually filtered to obtain a personalized data table, including: Obtain user information of the target user; the user information includes at least the user's organizational structure and historical data access records; The contextual candidate data table is filtered based on user information to obtain a personalized data table.

[0056] In some embodiments, this method can use embedded-based retrieval technology to retrieve contextual information based on the semantic relevance of the user's question, and combine this with the user's organizational structure and browsing history to make personalized recommendations. This narrows down the candidate data table and improves query generation efficiency.

[0057] In the above embodiments, the method can combine the target user's organizational structure, historical data access records and other core user information to perform targeted filtering of context candidate data tables, so as to obtain personalized data tables that fit the user's business scenarios and usage habits, avoid interference from irrelevant data tables, and ensure that the subsequent query statements generated are more in line with the user's actual business needs.

[0058] In some embodiments, the method further includes: The initial query statement is validated using a database simulator. Once it is confirmed that the initial query statement does not contain any errors, the process of determining the initial query statement as the final target query statement and outputting it is executed.

[0059] In some embodiments, the method can perform a verification step after generating SQL, using a database simulator for simulated execution or static analysis to detect potential errors in the generated SQL, and automatically correct them based on the feedback obtained, ensuring the correctness and execution efficiency of the SQL.

[0060] In the above embodiments, the method can use a database simulator to perform targeted verification on the initial query statement, accurately identify problems such as syntax errors and abnormal data associations, and ensure that the output target query statement has grammatical correctness and execution feasibility.

[0061] In some embodiments, the method further includes: When an error is detected in the initial query statement, the error information is obtained based on the verification results; Based on the error message, the initial query statement, the natural language query text, and the personalized data table, construct the corrected data; The corrected data is input into the large language model, the initial query statement is regenerated, and the initial query statement is verified through a database simulator to obtain the verification results.

[0062] In some embodiments, the method may utilize a large language model (LLM) for query generation and sorting, and gradually construct the final SQL query through iteration.

[0063] In some embodiments, a self-correction mechanism is introduced, which can also automatically detect and correct errors in the query through verification and correction steps, ensuring that the generated SQL syntax is correct and conforms to business logic.

[0064] In the above embodiments, when errors are found in the initial query statement during verification, the method can accurately capture error information and integrate relevant core data to construct corrective data. It can iteratively optimize and regenerate the query statement through a large language model, and continuously correct syntax, data association and other issues by combining the loop verification mechanism of the database simulator. This ensures that the final output target query statement fully complies with the syntax specifications and business requirements, and greatly improves the fault tolerance and accuracy of query statement generation in complex query scenarios.

[0065] In some embodiments, after inputting the corrected data into a large language model and regenerating the initial query statement, the method further includes: When the number of times the initial query statement is regenerated reaches a preset threshold, the last generated initial query statement is determined as the final target query statement, and the target query statement is output.

[0066] In the above embodiments, the method can limit the number of times the query statement is regenerated by setting a preset threshold, thereby avoiding the iterative correction process from getting stuck in an infinite loop; at the same time, the final generated preliminary query statement is used as the target query statement for output, ensuring that usable query results are provided within a reasonable time.

[0067] Figure 2 A schematic diagram of a query statement generation device is shown. It should be understood that this device is related to... Figure 1 The method executed in the middle corresponds to the steps involved in the aforementioned method. The specific functions and effects of the device can be found in the description above. To avoid repetition, detailed descriptions are omitted here.

[0068] The query statement generation device includes: The receiving unit 210 is used to receive natural language query text input by the target user; The acquisition unit 220 is used to acquire a context candidate data table that matches the natural language query text based on a pre-built knowledge graph; The filtering unit 230 is used to perform personalized filtering on the context candidate data table to obtain a personalized data table. The generation unit 240 is used to process the personalized data table and natural language query text through a large language model to generate preliminary query statements; Output unit 250 is used to determine the preliminary query statement as the final target query statement and output it when it is verified that there are no errors in the preliminary query statement.

[0069] In some embodiments, the query statement generation apparatus further includes: The acquisition unit 220 is also used to acquire the original database before the receiving unit 210 receives the natural language query text input by the target user, and to acquire entities, relationships between entities, metadata, query logs and domain knowledge based on the original database; The first building unit 260 is used to build a knowledge graph based on entities, relationships between entities, metadata, query logs, and domain knowledge.

[0070] In some embodiments, the nodes of a knowledge graph include entities, the connecting edges of a knowledge graph include relationships, and the node attributes of a knowledge graph include metadata, query logs, and domain knowledge.

[0071] In some embodiments, the acquisition unit 220 includes: Generating subunit 221 is used to generate query text vectors based on natural language query text; The generation subunit 221 is also used to generate multiple node vectors based on the nodes and node attributes in the knowledge graph; Computation subunit 222 is used to calculate the vector similarity between the query text vector and each node vector; The first acquisition subunit 223 is used to acquire a semantically related context candidate data table from the knowledge graph based on vector similarity.

[0072] In some embodiments, the filtering unit 230 includes: The second acquisition subunit 231 is used to acquire user information of the target user; wherein, the user information includes at least the user's organizational structure and historical data access records; The filtering subunit 232 is used to filter the context candidate data table based on user information to obtain a personalized data table.

[0073] In some embodiments, the output unit 250 is specifically used to verify the preliminary query statement through a database simulator, and when it is verified that the preliminary query statement has no errors, to determine the preliminary query statement as the final target query statement and output it.

[0074] In some embodiments, the query statement generation apparatus further includes: The acquisition unit 220 is used to obtain error information based on the verification result when an error is found in the preliminary query statement. The second construction unit 270 is used to construct corrected data based on error messages, preliminary query statements, natural language query text, and personalized data tables; The output unit 250 is also used to input the corrected data into the large language model, regenerate the initial query statement, and verify the initial query statement through the database simulator to obtain the verification result.

[0075] In some embodiments, after inputting the corrected data into the large language model and regenerating the preliminary query statement, the output unit 250 determines the final generated preliminary query statement as the final target query statement and outputs the target query statement when the number of times the preliminary query statement is regenerated reaches a preset threshold.

[0076] like Figure 3 As shown, this application provides an electronic device 300, which includes a processor 301 and a memory 302. The processor 301 and the memory 302 are interconnected and communicate with each other through a communication bus 303 and / or other forms of connection mechanism (not shown). The memory 302 stores a computer program that can be executed by the processor 301. When the computing device is running, the processor 301 executes the computer program to perform the method in any of the aforementioned optional implementations.

[0077] This application provides a computer-readable storage medium storing a computer program, which, when executed by a processor, performs the method in any of the aforementioned optional implementations.

[0078] The computer-readable storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as Static Random Access Memory (SRAM), Electrically Erasable Programmable Read-Only Memory (EEPROM), Erasable Programmable Read Only Memory (EPROM), Programmable Red-Only Memory (PROM), Read-Only Memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk.

[0079] This application provides a computer program product, which includes a computer program that, when run by a processor, executes the method in any of the aforementioned optional implementations.

[0080] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and not to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features therein. These modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of this application, and they should all be covered within the scope of the claims and specification of this application. In particular, as long as there is no conflict, the various technical features mentioned in the embodiments can be combined in any way. This application is not limited to the specific embodiments disclosed herein, but includes all technical solutions falling within the scope of the claims.

Claims

1. A method for generating query statements, characterized in that, include: Receive natural language query text input by the target user; Based on a pre-constructed knowledge graph, obtain a contextual candidate data table that matches the natural language query text; Personalized filtering is performed on the context candidate data table to obtain a personalized data table; The personalized data table and the natural language query text are processed using a large language model to generate a preliminary query statement; Once it is verified that the preliminary query statement does not contain any errors, the preliminary query statement is determined as the final target query statement and output.

2. The query statement generation method according to claim 1, characterized in that, Before receiving the natural language query text input by the target user, the method further includes: Obtain the original database, and based on the original database, obtain entities, relationships between entities, metadata, query logs, and domain knowledge; A knowledge graph is constructed based on the entities, the relationships between the entities, the metadata, the query logs, and the domain knowledge.

3. The query statement generation method according to claim 2, characterized in that, The nodes of the knowledge graph include the entities, the connecting edges of the knowledge graph include the associations, and the node attributes of the knowledge graph include the metadata, the query logs, and the domain knowledge.

4. The query statement generation method according to claim 1, characterized in that, The step of obtaining a context candidate data table that matches the natural language query text based on a pre-constructed knowledge graph includes: Generate a query text vector based on the natural language query text; Based on the nodes and node attributes in the knowledge graph, generate multiple node vectors; Calculate the vector similarity between the query text vector and each node vector; Based on the vector similarity, a semantically relevant context candidate data table is obtained from the knowledge graph.

5. The query statement generation method according to claim 1, characterized in that, The personalized filtering of the context candidate data table to obtain a personalized data table includes: Obtain the user information of the target user; wherein, the user information includes at least the user's organizational structure and historical data access records; The context candidate data table is filtered based on the user information to obtain a personalized data table.

6. The query statement generation method according to claim 1, characterized in that, The method further includes: The preliminary query statement is verified using a database simulator. If the preliminary query statement is found to be error-free, the step of determining the preliminary query statement as the final target query statement and outputting it is executed.

7. The query statement generation method according to claim 6, characterized in that, The method further includes: When an error is detected in the initial query statement, error information is obtained based on the verification result; Based on the error message, the initial query statement, the natural language query text, and the personalized data table, construct the corrected data; The corrected data is input into the large language model to regenerate the initial query statement, and the step of verifying the initial query statement through the database simulator to obtain the verification result is executed.

8. The query statement generation method according to claim 7, characterized in that, After inputting the corrected data into the large language model and regenerating the initial query statement, the method further includes: When the number of times the initial query statement is regenerated reaches a preset threshold, the last generated initial query statement is determined as the final target query statement, and the target query statement is output.

9. A query statement generation device, characterized in that, The query statement generation device includes: The receiving unit is used to receive natural language query text input by the target user; The acquisition unit is used to acquire a context candidate data table that matches the natural language query text based on a pre-constructed knowledge graph; A filtering unit is used to perform personalized filtering on the context candidate data table to obtain a personalized data table. The generation unit is used to process the personalized data table and the natural language query text through a large language model to generate a preliminary query statement; The output unit is used to determine the preliminary query statement as the final target query statement and output it when it is verified that the preliminary query statement does not contain any errors.

10. An electronic device, characterized in that, The electronic device includes a memory and a processor, the memory being used to store a computer program, and the processor running the computer program to cause the electronic device to perform the query statement generation method according to any one of claims 1 to 8.

11. A readable storage medium, characterized in that, The readable storage medium stores a computer program, which, when executed by a processor, performs the query statement generation method according to any one of claims 1 to 8.

12. A computer program product, characterized in that, The computer program product includes a computer program that, when executed by a processor, performs the query statement generation method according to any one of claims 1 to 8.

Citation Information

Patent Citations

  • Outbound scene data query method and device, computer equipment and storage medium

    CN114153865A

  • Structured query language generation method and device, electronic equipment and storage medium

    CN118093621A

  • Method and system for realizing Text2SQL (Structured Query Language)

    CN120470020A

  • RAG enhanced Text-to-SQL query method and system for large-scale database environment

    CN120492494A

  • Query statement conversion method and device based on large model, equipment and medium

    CN120578683A

Cited By

  • Natural language to SQL (Structured Query Language) conversion method and device applied to security industry, and storage medium

    CN121833757A