Text-to-structured query language generation method and device based on self-evolution rule base
By using a text-to-structured query language generation method based on a self-evolving rule base, the problems of database structure changes and complex queries are solved, achieving efficient and secure text-to-SQL conversion, reducing maintenance costs and improving real-time performance.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-18
- Publication Date
- 2026-03-27
AI Technical Summary
Existing Text-to-SQL methods are prone to syntax errors, inaccurate field mapping, and unauthorized access when the database structure changes frequently, field naming is not standardized, or query intent is complex. Furthermore, pure model solutions have high maintenance costs and poor real-time performance, making it difficult to stably support the diverse query needs of teachers and students in the long term.
A text-to-structured query language generation method based on a self-evolving rule base is adopted. By acquiring a hybrid parsing strategy, natural language and structured query language are dynamically mapped, query statements are completed using a preset language model, and feedback is executed in an isolated environment to automatically update the mapping relationship, configure permission filtering conditions, and construct a hybrid decision-making mechanism that prioritizes rules and is assisted by models.
It significantly reduces syntax errors and field mapping inaccuracies, ensures data security, lowers maintenance costs, improves query real-time performance and maintainability, and adapts to database structure changes and new query requirements.
Smart Images

Figure CN121743340A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, and in particular to a method and apparatus for generating text-to-structured query language based on a self-evolving rule base. Background Technology
[0002] Existing Text-to-SQL methods mostly rely on large language models to generate SQL (Structured Query Language) online.
[0003] When database structures change frequently, field naming is not standardized, or query intent is complex, large language models are prone to problems such as syntax errors, inaccurate field mapping, and unauthorized access. In educational scenarios, semester updates often bring new fields, table structure adjustments, and permission refinements. Pure model solutions require repeated manual annotation or retraining, resulting in high maintenance costs and poor real-time performance, making it difficult to stably support the diverse query needs of teachers and students in the long term. Summary of the Invention
[0004] This invention provides a method and apparatus for generating text-to-structured query language based on a self-evolving rule base. It addresses the shortcomings of existing technologies, such as the tendency of large language models to generate syntax errors, inaccurate field mapping, and unauthorized access when the database structure changes frequently, field naming is not standardized, or the query intent is complex. Furthermore, pure model solutions require repeated manual annotation or retraining, resulting in high maintenance costs and poor real-time performance, making it difficult to stably support the diverse query needs of teachers and students in the long term.
[0005] This invention provides a method for generating text-to-structured query language based on a self-evolving rule base, comprising the following steps: Obtain a hybrid parsing strategy; the hybrid parsing strategy includes a dynamic mapping relationship between intent phrases in natural language and structured query language; In response to a user's natural language query, the natural language query is parsed based on the hybrid parsing strategy to obtain an initial parsing result. If the initial parsing result is incomplete, a preset language model is invoked to complete the natural language query to obtain a completed query statement. Execute the completion query statement in an isolated, secure verification environment to obtain the execution feedback information of the completion query statement; If the execution feedback information indicates successful execution and the initial parsing result is incomplete, the fragment obtained by completing the natural language query is mapped to a new mapping relationship, and the new mapping relationship is added to the dynamic mapping relationship.
[0006] According to the present invention, a method for generating text-to-structured query language based on a self-evolving rule base is provided, wherein the attributes of each mapping relationship in the dynamic mapping relationship include confidence score and context information; The process of parsing the natural language query based on the hybrid parsing strategy to obtain an initial parsing result includes: From the dynamic mapping relationship, candidate mapping relationships corresponding to each intent phrase in the natural language query are matched, and when there are multiple candidate mapping relationships, the target mapping relationship is determined from the candidate mapping relationships based on the confidence score of each candidate mapping relationship and the context information. Based on the target mapping relationship, the natural language query is parsed to obtain the initial parsing result.
[0007] According to the present invention, a method for generating text-to-structured query language based on a self-evolving rule base is provided, the method further comprising: If the execution feedback information indicates that the execution has failed, determine the dynamic mapping relationship of the failure of the completion query statement. Lower the confidence score of the failed dynamic mapping relationship or mark the failed dynamic mapping relationship as pending review.
[0008] According to the present invention, a method for generating text-to-structured query language based on a self-evolving rule base is provided, wherein each mapping relationship in the dynamic mapping relationship includes field mapping relationships and operator mapping relationships; The field mapping relationship is used to characterize the correspondence between the components of the intent phrase and a table or field in a preset database; The operator mapping relationship is used to map the logical intent in the intent phrase to the operators in the structured query language.
[0009] According to the present invention, a method for generating text-to-structured query language based on a self-evolving rule base is provided, the method further comprising: Configure permission filtering conditions for the completion query statement based on the user's role information; wherein, the permission filtering conditions are used to limit the range of rows or columns of data that the completion query statement can access.
[0010] According to the present invention, a method for generating text-to-structured query language based on a self-evolving rule base is provided, wherein the step of calling a preset language model to complete the natural language query to obtain a completed query statement includes: Based on the incompleteness level of the initial parsing result, the target language model of the initial parsing result is determined from a preset language model pool; The target language model is invoked to complete the natural language query, resulting in a completed query statement.
[0011] According to the present invention, a method for generating text-to-structured query language based on a self-evolving rule base is provided, wherein adding the new mapping relationship to the dynamic mapping relationship includes: Generate a natural language interpretation to explain the query intent of the completed query statement; The natural language definition is displayed to the user, and upon receiving confirmation from the user, the new mapping relationship is added to the dynamic mapping relationship.
[0012] The present invention also provides a text-to-structured query language generation device based on a self-evolving rule base, comprising the following units: An acquisition unit is used to acquire a hybrid parsing strategy; the hybrid parsing strategy includes a dynamic mapping relationship between intent phrases in natural language and structured query language; The parsing unit is used to respond to the user's natural language query, parse the natural language query based on the hybrid parsing strategy, obtain an initial parsing result, and, if the initial parsing result is incomplete, call a preset language model to complete the natural language query to obtain a completed query statement; An execution unit is used to execute the completion query statement in an isolated security verification environment and obtain the execution feedback information of the completion query statement; The mapping unit is used to map the fragment obtained by completing the natural language query into a new mapping relationship when the execution feedback information indicates that the execution was successful and the initial parsing result is incomplete, and to add the new mapping relationship to the dynamic mapping relationship.
[0013] The present invention also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the text-to-structured query language generation method based on any of the above-described self-evolving rule bases.
[0014] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the text-to-structured query language generation method based on a self-evolving rule base as described above.
[0015] The present invention also provides a computer program product, including a computer program that, when executed by a processor, implements the text-to-structured query language generation method based on a self-evolving rule base as described above.
[0016] The present invention provides a text-to-structured query language generation method and apparatus based on a self-evolving rule base. On the one hand, it acquires a hybrid parsing strategy that includes dynamic mapping relationships between intent phrases in natural language and structured query language, and parses user queries based on this strategy. Only when the initial parsing result is incomplete is a preset language model invoked for completion. This constructs a rule-first, model-assisted hybrid decision-making mechanism, which significantly reduces syntax errors and field mapping inaccuracies caused by direct reliance on the model through controllable dynamic mapping relationships, while ensuring the parsing capability of complex queries. On the other hand, by executing the completed query statement in an isolated, secure verification environment and obtaining feedback, it effectively intercepts potential errors or unauthorized operations, ensuring data security. Furthermore, when execution is successful and the initial parsing result is incomplete, the completed valid fragments are automatically converted into new mapping relationships and added to the dynamic mapping relationships. This self-learning mechanism eliminates the need for repeated manual intervention or model retraining when facing database structure changes or new query requirements, thus significantly improving the maintainability of text-to-structured query language generation and fundamentally overcoming the shortcomings of pure model solutions, such as high maintenance costs and poor real-time performance. Attached Figure Description
[0017] To more clearly illustrate the technical solutions in this invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0018] Figure 1 This is a flowchart illustrating the text-to-structured query language generation method based on a self-evolving rule base provided by the present invention.
[0019] Figure 2 This is a schematic diagram of the structure of the text-to-structured query language generation device based on a self-evolving rule base provided by the present invention.
[0020] Figure 3 This is a schematic diagram of the structure of the electronic device provided by the present invention. Detailed Implementation
[0021] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.
[0022] This invention provides a method for generating text-to-structured query language based on a self-evolving rule base. Figure 1 This is a flowchart illustrating the text-to-structured query language generation method based on a self-evolving rule base provided by the present invention, as shown below. Figure 1 As shown, the method includes steps 110, 120, 130 and 140.
[0023] Step 110: Obtain the hybrid parsing strategy; the hybrid parsing strategy includes the dynamic mapping relationship between intent phrases in natural language and structured query language.
[0024] Specifically, a hybrid parsing strategy can be obtained, which includes a dynamic mapping relationship between intent phrases in natural language and structured query language.
[0025] It should be noted that the hybrid parsing strategy refers to a comprehensive strategy that combines deterministic rule parsing and nondeterministic large language model parsing. The core of the hybrid parsing strategy is a dynamic mapping relation library, which in this embodiment of the invention can also be referred to as a self-evolving metagrammar or rule base.
[0026] Here, the content of the dynamic mapping relationship is not static, but can be added, modified, or deleted based on subsequent queries and execution feedback; and when applying the dynamic mapping relationship, the most appropriate mapping can be dynamically selected based on the context.
[0027] Intent phrases refer to words or short sentences in a user's natural language query that express a specific query intent. For example, in the natural language query for the average score of Class 2, Grade 3, in the first semester, "Class 2, Grade 3," "first semester," and "average score" can all be considered intent phrases. The granularity of intent phrases can vary. For instance, an intent phrase can be a complete query clause intent, such as a list of students who failed all courses, or it can be a reference to a single field or value, such as "Chinese" or "greater than 90 points."
[0028] Structured Query Language (SQL) is the standard language used for querying, defining, managing, and manipulating data in relational databases. Dynamic mapping aims to convert the aforementioned intent phrases into executable SQL fragments. For example, the intent phrase "average score" can be mapped to the SQL AVG(score) function fragment.
[0029] During the system initialization phase, an initial dynamic mapping relation library can be automatically or semi-automatically built through the metadata and log collection module. For example, a large language model can be used to analyze, extract, and cluster the database's metadata (schema, field annotations) and historical query logs to generate an initial semantic-SQL fragment mapping table as the starting point for a hybrid parsing strategy.
[0030] Step 120: In response to the user's natural language query, the natural language query is parsed based on the hybrid parsing strategy to obtain an initial parsing result. If the initial parsing result is incomplete, a preset language model is invoked to complete the natural language query to obtain a completed query statement.
[0031] Specifically, in response to a user's natural language query, the natural language query is parsed based on a hybrid parsing strategy to obtain an initial parsing result. If the initial parsing result is incomplete, a preset language model is invoked to complete the natural language query and obtain a completed query statement.
[0032] When the system receives a user's natural language query, such as "Please find the names of all students who enrolled in the Advanced Physics course among the new students added last week," the parser first initiates a rule-based parsing process. The parser attempts to match various intent phrases in the query's natural language statement against a dynamic mapping relational database. For example, it might successfully match "new students added last week," "student names," and "Advanced Physics course," converting them into corresponding SQL fragments, such as `creation_date>='...' AND creation_date<= '...'`, `students.name`, `courses.name = 'Advanced Physics'`, etc.
[0033] By concatenating these matched SQL fragments, the system generates an initial parsing result. This initial parsing result can be a complete SQL statement or an incomplete SQL statement. For example, the system may know to query the `students` and `courses` tables, but it doesn't know how to join these two tables (JOIN), or how to associate students with course registrations. In this case, the initial parsing result is incomplete. Incompleteness can manifest in various ways, such as missing necessary query fields (incomplete SELECT clause), missing table join conditions (missing JOIN...ON... clause), missing filtering conditions (missing or incomplete WHERE clause), or missing aggregate functions, etc. This embodiment of the invention does not specifically limit these aspects.
[0034] After determining that the initial parsing result is incomplete, a preset language model can be invoked to complete the natural language query. For example, the entire natural language query, along with the incomplete initial parsing result, can be sent as contextual hints to a preset language model, instructing the preset language model to complete or correct the SQL statement. For instance, the system might issue a command to the preset language model like: "Based on the user question '...' and the existing SQL fragment '...', please complete it into a complete SQL query." The preset language model can be a large language model, which leverages its powerful semantic understanding and code generation capabilities to generate one or more candidate completed query statements.
[0035] Step 130: Execute the completion query statement in an isolated secure verification environment to obtain the execution feedback information of the completion query statement.
[0036] Specifically, in order to prevent syntax errors, logical fallacies, or potential security risks such as unauthorized access or database deletion in the completion query statement, this embodiment of the invention will not directly execute the completion query statement in the production database.
[0037] Accordingly, the completed query statement can be executed in an isolated, secure verification environment to obtain execution feedback information. Here, the isolated, secure verification environment can be a shadow database or a sandbox environment. The isolated, secure verification environment has the same table structure as the production database, but the data is anonymized or simulated. Its core purpose is to verify the syntactic correctness, execution feasibility, and permission compliance of the completed query statement without affecting the real data.
[0038] The execution feedback information may include execution success / failure status, error messages, execution plan, and execution result summary. The execution success / failure status indicates whether the completed query statement can be accepted and executed by the database engine. Error messages may include specific error codes and descriptions, such as syntax errors, table or view not existing, invalid column names, insufficient permissions, etc. This embodiment of the invention does not impose specific limitations on these aspects.
[0039] Here, the execution plan reflects the query execution path of the completed query statement in the database. The query execution path includes, but is not limited to, data access methods, join methods, index usage, and estimated resource consumption. The execution result summary may include the number of rows returned, the number of rows affected, etc. Returning 0 rows may mean that the query conditions of the completed query statement are too strict or there is a logical error.
[0040] Step 140: If the execution feedback information indicates successful execution and the initial parsing result is incomplete, the fragment obtained by completing the natural language query is mapped to a new mapping relationship, and the new mapping relationship is added to the dynamic mapping relationship.
[0041] Specifically, if the execution feedback indicates successful execution and the initial parsing result is incomplete, the fragment obtained by completing the natural language query is mapped to a new mapping relationship, and the new mapping relationship is added to the dynamic mapping relationship.
[0042] In other words, this step requires two preconditions to be met: First, the execution feedback information indicates that the execution was successful, which proves that the completed query statement completed by the preset language model is valid and correct; Second, the initial parsing result must be determined to be incomplete, which proves that the preset language model provides a new mapping relationship that did not originally exist in the hybrid parsing strategy.
[0043] If the conditions are met, the system will analyze which part of the natural language query intent was successfully converted into which SQL fragment by the preset language model. For example, if the initial parsing cannot understand the students who have enrolled in... courses, but the LLM successfully completes the logic of INNER JOIN student_enrollments ON students.id = student_enrollments.student_id INNER JOIN courses ON student_enrollments.course_id =courses.id, the system will extract this new mapping relationship.
[0044] Through this process, the mapping relationship between the intent phrase "enrolled as a student of..." and the corresponding SQL connection query template is added to the dynamic mapping relationship. Furthermore, when similar query intents occur subsequently, the system can directly complete the parsing based on this mapping rule without calling a preset language model. This improves query efficiency, reduces computational costs, and enhances the system's response determinism and operational stability.
[0045] The method provided in this invention, on the one hand, acquires a hybrid parsing strategy that includes dynamic mapping relationships between intent phrases in natural language and structured query language, and parses user queries based on this strategy. Only when the initial parsing result is incomplete is a preset language model invoked for completion. This constructs a rule-first, model-assisted hybrid decision-making mechanism, which significantly reduces syntax errors and field mapping inaccuracies caused by direct reliance on the model through controllable dynamic mapping relationships, while ensuring the parsing capability of complex queries. On the other hand, by executing the completed query statement in an isolated, secure verification environment and obtaining feedback, potential errors or unauthorized operations are effectively intercepted, ensuring data security. Furthermore, when execution is successful and the initial parsing result is incomplete, the completed valid fragments are automatically converted into new mapping relationships and added to the dynamic mapping relationships. This self-learning mechanism eliminates the need for repeated manual intervention or model retraining when facing database structure changes or new query requirements, thus significantly improving the maintainability of text-to-structured query language generation and fundamentally overcoming the shortcomings of pure model solutions, such as high maintenance costs and poor real-time performance.
[0046] Based on the above embodiments, the attributes of each mapping relationship in the dynamic mapping relationship include confidence score and context information; Step 120, which involves parsing the natural language query based on the hybrid parsing strategy to obtain an initial parsing result, includes: Step 121: Match candidate mapping relationships corresponding to each intent phrase in the natural language query from the dynamic mapping relationship; and if there are multiple candidate mapping relationships, determine the target mapping relationship from the candidate mapping relationships based on the confidence score and context information of each candidate mapping relationship. Step 122: Based on the target mapping relationship, parse the natural language query to obtain the initial parsing result.
[0047] Specifically, the attributes of each mapping relationship in a dynamic mapping relationship include a confidence score and contextual information. The confidence score is a numerical value, typically between 0 and 1, used to characterize the reliability of each mapping relationship. The initial value of the confidence score can be set based on its source; for example, rules confirmed by a database administrator have high confidence, while rules generated once by a pre-defined language model and untested have lower confidence. In subsequent evolution, if a mapping relationship is used frequently and successfully, its confidence score will increase; conversely, if queries caused by the mapping relationship frequently fail, its confidence score will decrease.
[0048] Contextual information is used to resolve the ambiguity of intent phrases in different contexts. Contextual information may include user role, query time, and preceding and following dialogue content. User roles can be students, teachers, academic affairs personnel, etc., and this embodiment of the invention does not specifically limit this. For example, the intent phrase "my grades" should be mapped to an SQL query for querying the user's individual grades when the user is a student; when the user is a teacher, it may refer to the average grades of the classes they teach. Contextual information allows the system to dynamically adjust the priority of each candidate mapping relationship according to the current context.
[0049] Accordingly, candidate mapping relationships corresponding to each intent phrase in a natural language query can be matched from the dynamic mapping relationship, and when there are multiple candidate mapping relationships, the target mapping relationship can be determined from the candidate mapping relationships based on their respective confidence scores and context information.
[0050] In one embodiment, when the system parses a natural language query, for a given intent phrase, such as "latest grades," it may find multiple candidate mappings in a dynamic mapping database. For example: Candidate A: Mapped to ORDER BY exam_date DESC LIMIT 1 (sorted by the latest exam date), with a confidence level of 0.9.
[0051] Candidate B: Mapped to WHERE semester = 'Fall 2025' (current semester), with a confidence level of 0.8.
[0052] At this point, the system will comprehensively consider the confidence scores and contextual information of the candidate mappings to make a decision. For example, if the current time is November 2025, the system may assign a higher weight to the contextual information of "current semester," thus causing candidate B's overall score to exceed that of candidate A. Ultimately, the candidate mapping with the highest score is determined as the target mapping.
[0053] Furthermore, after obtaining the target mapping relationship, the natural language query can be parsed based on the target mapping relationship to obtain initial parsing results. It should be understood that because this decision-making process considers reliability and context adaptability, the generated initial parsing results are more accurate and closer to the user's true intent.
[0054] The method provided in this invention, by configuring confidence scores and context information attributes for dynamic mapping relationships, can comprehensively consider the reliability and applicable scenarios of each mapping relationship when determining the target mapping relationship from multiple candidate mapping relationships, thereby selecting the mapping result that best matches the current query context. This effectively improves the accuracy and context relevance of the initial parsing and avoids parsing errors caused by ambiguity in the mapping relationship.
[0055] Based on the above embodiments, the method further includes: Step 21: If the execution feedback information indicates that the execution has failed, determine the dynamic mapping relationship of the failure of the completion query statement. Step 22: Reduce the confidence score of the failed dynamic mapping relationship or mark the failed dynamic mapping relationship as pending review.
[0056] Specifically, when the execution feedback indicates failure, the system determines the dynamic mapping relationship for the failure of the completed query statement. That is, when the completed query statement executed in the secure verification environment returns a failure execution feedback, such as an SQL syntax error, a field or table not existing, the system will initiate an error attribution process.
[0057] For example, if the error message is "Unknown column 'student_name' in 'field list'", and the system used the mapping relationship of student name → student_name when generating the query, then this mapping relationship will be initially identified as a failed dynamic mapping relationship.
[0058] Furthermore, the confidence score of failed dynamic mappings can be lowered, or they can be marked as pending review. Lowering the confidence score means reducing the confidence score of the dynamic mapping by a preset value or a percentage. Therefore, when the same intent phrase is encountered again, the failed dynamic mapping is less likely to be selected as the target mapping due to its lower confidence score. It should be understood that if a mapping consistently leads to errors, its confidence score will continue to drop, eventually leading to its elimination in the competition.
[0059] Furthermore, for mappings that lead to serious errors (such as syntax errors) or whose confidence scores drop below a certain threshold, the system can directly mark them as pending review or disabled. Once marked, the failed dynamic mapping will be temporarily or permanently removed from the parser's available dynamic mappings and may be pushed to a dedicated backend interface for human reviewers to examine and correct.
[0060] The method provided in this invention automatically locates and reduces the confidence score of the corresponding failed dynamic mapping relationship or marks it as pending review when the completion query statement fails. This achieves dynamic identification and weight reduction of unreliable mapping rules, effectively preventing the system from repeatedly using the same erroneous mapping in subsequent queries, thereby improving the system's self-correction capability and overall robustness.
[0061] Based on the above embodiments, each mapping relationship in the dynamic mapping relationship includes field mapping relationships and operator mapping relationships; The field mapping relationship is used to characterize the correspondence between the components of the intent phrase and a table or field in a preset database; The operator mapping relationship is used to map the logical intent in the intent phrase to the operators in the structured query language.
[0062] Specifically, to make the mapping relationships in dynamic mapping relationships more structured and flexible, each mapping relationship can be further decomposed into finer-grained components. The mapping relationships in dynamic mapping relationships include field mapping relationships and operator mapping relationships.
[0063] Field mapping relationships are used to represent the correspondence between the components of an intent phrase and a predefined table or field in a database. Field mapping relationships are used to accurately map words in natural language that refer to data entities to the physical structure of the database.
[0064] For example, for the intent phrase "Xiaoming's Chinese language score", "Xiaoming" can be mapped to the "name" field of the "students" table, with the value "Xiaoming"; "Chinese language" can be mapped to the "name" field of the "subjects" table, with the value "Chinese language"; and "score" can be mapped to the "value" field of the "scores" table.
[0065] For example, when database table names or field names are not intuitive, such as the stu_info table and the chn_score field, field mapping relationships can map user-friendly natural language student information and Chinese language scores to these unintuitive names, thereby bridging the gap between user language and the physical implementation of the database.
[0066] Here, the operator mapping relation is used to map the logical intent in intent phrases to operators in Structured Query Language. Specifically, the operator mapping relation is used to convert words expressing logical intents such as comparison, aggregation, and sorting into comparison operators, aggregate functions, and logical connectors in SQL.
[0067] When using Structured Query Language (SCL) with comparison operators, intent phrases greater than 80 points and not lower than the passing grade can be mapped to >80 and >= 60, respectively.
[0068] In the case of aggregation functions, the average score of intent phrases, the total number of people, and the highest score can be mapped to AVG(), COUNT(), and MAX(), respectively.
[0069] In Structured Query Language (SCL), logical connectors can be used to map AND and OR.
[0070] By decomposing composite mapping relationships into independent field mapping relationships and operator mapping relationships, this invention enables the system to possess greater combination flexibility and reusability when constructing SQL queries. This not only significantly improves the coverage of diverse query intents by limited rules and reduces the redundancy of the rule base, but also makes the generation, maintenance, and interpretation of mapping rules clearer, enhancing the interpretability and maintainability of the system. For example, when the system learns to map "average score" to the aggregate function AVG() and "mathematics" to the condition subject='mathematics', it can naturally combine and understand the undefined composite intent of mathematical average score, thus demonstrating powerful generalization ability.
[0071] Based on the above embodiments, the method further includes: Configure permission filtering conditions for the completion query statement based on the user's role information; wherein, the permission filtering conditions are used to limit the range of rows or columns of data that the completion query statement can access.
[0072] Specifically, permission filtering conditions can be configured for the completion query statement based on the user's role information; the permission filtering conditions are used to limit the range of rows or columns of data that the completion query statement can access.
[0073] User role information can include student, teacher, class teacher, academic administrator, etc., and this embodiment of the invention does not specifically limit this. This role information is usually determined when the user logs into the system.
[0074] Row-level limitation is the most common access control method. For example, for a user with the role of a student, the system will automatically append the condition AND student_id = 'the ID of the currently logged-in user' to the WHERE clause of all their queries. Therefore, even if this student user queries everyone's grades, they will only actually see their own grade data. For a user with the role of a homeroom teacher, the system might append AND class_id IN ('a list of class IDs this teacher is responsible for').
[0075] For example, teachers can query students' grades and student IDs, but they are not authorized to view sensitive information such as students' home addresses and contact numbers. When a teacher's query intent may touch on sensitive fields, the system can proactively remove these sensitive fields from the SELECT clause when generating the SQL, or perform anonymization processing on the results after the query.
[0076] For example, a student user might query the class average score. The original SQL query might be `SELECT AVG(score) FROM scores WHERE class_id = '...'`. After applying permission filtering, the system will not execute this statement because it would expose information about the entire class. The system might intercept the query and indicate insufficient permissions, or rewrite it into SQL that only queries the average score of that specific student.
[0077] The method provided in this invention significantly enhances system security by forcibly embedding role-based permission filtering into the SQL generation process. It ensures that all data queries are performed within strict permission boundaries, effectively preventing data leakage and unauthorized access. This mechanism is particularly crucial for protecting data privacy and compliance in educational scenarios involving sensitive data such as student grades and personal information.
[0078] Based on the above embodiments, step 120, which involves calling a preset language model to complete the natural language query and obtain a completed query statement, includes: Step 120-1: Based on the incompleteness level of the initial parsing result, determine the target language model of the initial parsing result from the preset language model pool; Step 122: Invoke the target language model to complete the natural language query and obtain the completed query statement.
[0079] Specifically, when the initial parsing result is incomplete and a preset language model needs to be called for completion, this embodiment of the invention does not always call the same fixed language model. Instead, it first evaluates the degree of incompleteness of the initial parsing result and classifies it into incompleteness levels.
[0080] Accordingly, the target language model for the initial parsing result can be determined from a pre-defined language model pool based on the incompleteness level of the initial parsing result. The incompleteness levels include low-level incompleteness, medium-level incompleteness, and high-level incompleteness.
[0081] Here, a low-level incompleteness can simply mean the absence of a single value or field name. For example, `SELECT name FROM students WHERE age > ?`.
[0082] An incomplete intermediate level might be missing a table join condition or a simple aggregation. For example, it might require joining the student table and the grade table.
[0083] High-level incompleteness may require generating complex subqueries, window functions, or performing multi-step logical reasoning.
[0084] Meanwhile, the system pre-defines a language model pool. This pool can contain multiple language models of different sizes, capabilities, and costs. For example, Model A: a lightweight model with fast response and low operating costs, adept at simple fill-in-the-blank and code completion; Model B: a medium-sized model with balanced performance in code generation and simple logical reasoning; Model C: a very large-scale, powerful but slower-responding and more expensive model, capable of handling very complex instructions and logic.
[0085] The system selects the most suitable target language model from the language model pool based on the assessed level of incompleteness. For example, model A is used for low-level incompleteness, while model C is used for high-level incompleteness.
[0086] Then, the target language model can be invoked to complete the natural language query, resulting in a completed query statement.
[0087] The method provided in this invention achieves intelligent allocation of computing resources and fine-grained control of the incompleteness strategy by selectively choosing a target language model from a preset language model pool for completion based on the incompleteness level of the initial parsing result. For queries with low incompleteness, a lightweight and fast language model can be selected to meet efficiency requirements; for complex queries with high incompleteness, a more powerful and accurate language model is invoked to ensure parsing effectiveness. This design effectively balances query processing response speed and parsing accuracy, avoids resource waste, and improves the overall efficiency of the system.
[0088] Based on the above embodiments, step 140, which involves adding the new mapping relationship to the dynamic mapping relationship, includes: Step 140-1: Generate a natural language interpretation to explain the query intent of the completed query statement; Step 140-2: Show the natural language definition to the user, and after receiving the user's confirmation instruction, add the new mapping relationship to the dynamic mapping relationship.
[0089] Specifically, before adding new mappings to dynamic mappings, a natural language interpretation is first generated to explain the query intent of the completed query statement.
[0090] The purpose of natural language interpretation is to explain the final query logic executed by the system to the user. For example, if the final executed SQL is SELECT s.name FROM students s JOIN enrollments e ON s.id = e.student_id WHERE e.course_name = 'Physics' AND s.grade = 12, the natural language interpretation generated by the system could be: "Okay, retrieving the names of all 12th-grade students who have enrolled in the 'Physics' course." Then, the system displays the natural language definition to the user and, upon receiving confirmation from the user, adds the new mapping to the dynamic mapping. For example, the system might display the natural language definition to the user through the interface, along with the question "Save this understanding as a new dynamic mapping?" and the option "Yes / No".
[0091] If the user clicks "Yes" or issues other forms of confirmation, it means the user acknowledges that the completion logic of the preset language model matches their true intent. Only then will the system add the new mapping relationship to the dynamic mapping relationship, completing the learning process.
[0092] If the user clicks "No" or cancels the operation, the system considers that although the completion by the preset language model was successful, it did not correctly understand the user's intent. Therefore, the system will not learn this incorrect mapping relationship.
[0093] The method provided in this invention generates natural language definitions for completed query statements, obtains user confirmation, and then adds the confirmed new mapping relationships to the dynamic mapping relationships, achieving human-computer collaborative knowledge verification and closed-loop. This mechanism effectively ensures the accuracy and reliability of new knowledge acquired during self-learning, preventing erroneous or inconsistent mapping relationships from being included in the dynamic mapping relationships. Thus, while improving the system's automation level, it ensures the quality and credibility of the dynamic mapping relationships, enhancing the overall robustness of the system.
[0094] The following describes the text-to-structured query language generation apparatus based on a self-evolving rule base provided by the present invention. The text-to-structured query language generation apparatus based on a self-evolving rule base described below can be referred to in correspondence with the text-to-structured query language generation method based on a self-evolving rule base described above.
[0095] Based on any of the above embodiments, the present invention provides a text-to-structured query language generation device based on a self-evolving rule base. Figure 2 This is a schematic diagram of the structure of the text-to-structured query language generation device based on a self-evolving rule base provided by the present invention, as shown below. Figure 2As shown, the device includes: The acquisition unit 210 is used to acquire a hybrid parsing strategy; the hybrid parsing strategy includes a dynamic mapping relationship between intent phrases in natural language and structured query language; The parsing unit 220 is used to respond to the user's natural language query, parse the natural language query based on the hybrid parsing strategy, obtain an initial parsing result, and if the initial parsing result is incomplete, call a preset language model to complete the natural language query to obtain a completed query statement. The execution unit 230 is used to execute the completion query statement in an isolated security verification environment and obtain the execution feedback information of the completion query statement; The mapping unit 240 is configured to, when the execution feedback information indicates successful execution and the initial parsing result is incomplete, map the fragment obtained by completing the natural language query into a new mapping relationship, and add the new mapping relationship to the dynamic mapping relationship.
[0096] The apparatus provided in this invention, on the one hand, acquires a hybrid parsing strategy that includes dynamic mapping relationships between intent phrases in natural language and structured query language, and parses user queries based on this strategy. Only when the initial parsing result is incomplete is a preset language model invoked for completion. This constructs a rule-first, model-assisted hybrid decision-making mechanism, which significantly reduces syntax errors and field mapping inaccuracies caused by direct reliance on the model through controllable dynamic mapping relationships, while ensuring the parsing capability of complex queries. On the other hand, by executing the completed query statement in an isolated, secure verification environment and obtaining feedback, potential errors or unauthorized operations are effectively intercepted, ensuring data security. Furthermore, when execution is successful and the initial parsing result is incomplete, the completed valid fragments are automatically converted into new mapping relationships and added to the dynamic mapping relationships. This self-learning mechanism eliminates the need for repeated manual intervention or model retraining when facing database structure changes or new query requirements, thus significantly improving the maintainability of text-to-structured query language generation and fundamentally overcoming the shortcomings of high maintenance costs and poor real-time performance of pure model-based solutions.
[0097] Based on any of the above embodiments, the attributes of each mapping relationship in the dynamic mapping relationship include confidence score and context information; The parsing unit 220 is specifically used for: From the dynamic mapping relationship, candidate mapping relationships corresponding to each intent phrase in the natural language query are matched, and when there are multiple candidate mapping relationships, the target mapping relationship is determined from the candidate mapping relationships based on the confidence score of each candidate mapping relationship and the context information. Based on the target mapping relationship, the natural language query is parsed to obtain the initial parsing result.
[0098] Based on any of the above embodiments, a unit for determining the failure dynamic mapping relationship is further included, wherein the unit for determining the failure dynamic mapping relationship is specifically used for: If the execution feedback information indicates that the execution has failed, determine the dynamic mapping relationship of the failure of the completion query statement. Lower the confidence score of the failed dynamic mapping relationship or mark the failed dynamic mapping relationship as pending review.
[0099] Based on any of the above embodiments, each mapping relationship in the dynamic mapping relationship includes field mapping relationships and operator mapping relationships; The field mapping relationship is used to characterize the correspondence between the components of the intent phrase and a table or field in a preset database; The operator mapping relationship is used to map the logical intent in the intent phrase to the operators in the structured query language.
[0100] Based on any of the above embodiments, a configuration unit is further included, wherein the configuration unit is specifically used for: Configure permission filtering conditions for the completion query statement based on the user's role information; wherein, the permission filtering conditions are used to limit the range of rows or columns of data that the completion query statement can access.
[0101] Based on any of the above embodiments, the parsing unit 220 is specifically used for: Based on the incompleteness level of the initial parsing result, the target language model of the initial parsing result is determined from a preset language model pool; The target language model is invoked to complete the natural language query, resulting in a completed query statement.
[0102] Based on any of the above embodiments, the mapping unit 240 is specifically used for: Generate a natural language interpretation to explain the query intent of the completed query statement; The natural language definition is displayed to the user, and upon receiving confirmation from the user, the new mapping relationship is added to the dynamic mapping relationship.
[0103] Figure 3 This is a schematic diagram of the structure of the electronic device provided by the present invention, such as... Figure 3As shown, the electronic device may include: a processor 310, a communications interface 320, a memory 330, and a communications bus 340, wherein the processor 310, the communications interface 320, and the memory 330 communicate with each other through the communications bus 340. The processor 310 can call logical instructions in the memory 330 to execute a text-to-structured query language generation method based on a self-evolving rule base. This method includes: acquiring a hybrid parsing strategy; the hybrid parsing strategy includes a dynamic mapping relationship between intent phrases in natural language and structured query language; responding to a user's natural language query, parsing the natural language query based on the hybrid parsing strategy to obtain an initial parsing result, and if the initial parsing result is incomplete, calling a preset language model to complete the natural language query to obtain a completed query statement; executing the completed query statement in an isolated, secure verification environment to obtain execution feedback information for the completed query statement; if the execution feedback information indicates successful execution and the initial parsing result is incomplete, mapping the fragment obtained from the completed natural language query to a new mapping relationship, and adding the new mapping relationship to the dynamic mapping relationship.
[0104] Furthermore, the logical instructions in the aforementioned memory 330 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, essentially, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0105] On the other hand, the present invention also provides a computer program product, which includes a computer program that can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the text-to-structured query language generation method based on a self-evolving rule base provided by the above methods. The method includes: obtaining a hybrid parsing strategy; the hybrid parsing strategy includes a dynamic mapping relationship between intent phrases in natural language and structured query language; responding to a user's natural language query, parsing the natural language query based on the hybrid parsing strategy to obtain an initial parsing result, and if the initial parsing result is incomplete, calling a preset language model to complete the natural language query to obtain a completed query statement; executing the completed query statement in an isolated secure verification environment to obtain execution feedback information of the completed query statement; if the execution feedback information indicates successful execution and the initial parsing result is incomplete, mapping the fragment obtained by completing the natural language query to a new mapping relationship, and adding the new mapping relationship to the dynamic mapping relationship.
[0106] In another aspect, the present invention also provides a non-transitory computer-readable storage medium storing a computer program thereon. When executed by a processor, the computer program implements a text-to-structured query language generation method based on a self-evolving rule base provided by the methods described above. This method includes: acquiring a hybrid parsing strategy; the hybrid parsing strategy includes a dynamic mapping relationship between intent phrases in natural language and structured query language; responding to a user's natural language query, parsing the natural language query based on the hybrid parsing strategy to obtain an initial parsing result, and, if the initial parsing result is incomplete, calling a preset language model to complete the natural language query to obtain a completed query statement; executing the completed query statement in an isolated, secure verification environment to obtain execution feedback information for the completed query statement; if the execution feedback information indicates successful execution and the initial parsing result is incomplete, mapping the fragment obtained by completing the natural language query to a new mapping relationship, and adding the new mapping relationship to the dynamic mapping relationship.
[0107] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.
[0108] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.
[0109] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for generating text-to-structured query language based on a self-evolving rule base, characterized in that, include: Obtain a hybrid parsing strategy; the hybrid parsing strategy includes a dynamic mapping relationship between intent phrases in natural language and structured query language; In response to a user's natural language query, the natural language query is parsed based on the hybrid parsing strategy to obtain an initial parsing result. If the initial parsing result is incomplete, a preset language model is invoked to complete the natural language query to obtain a completed query statement. Execute the completion query statement in an isolated, secure verification environment to obtain the execution feedback information of the completion query statement; If the execution feedback information indicates successful execution and the initial parsing result is incomplete, the fragment obtained by completing the natural language query is mapped to a new mapping relationship, and the new mapping relationship is added to the dynamic mapping relationship.
2. The method for generating text-to-structured query language based on a self-evolving rule base according to claim 1, characterized in that, The attributes of each mapping relationship in the dynamic mapping relationship include confidence score and context information; The process of parsing the natural language query based on the hybrid parsing strategy to obtain an initial parsing result includes: From the dynamic mapping relationship, candidate mapping relationships corresponding to each intent phrase in the natural language query are matched, and when there are multiple candidate mapping relationships, the target mapping relationship is determined from the candidate mapping relationships based on the confidence score of each candidate mapping relationship and the context information. Based on the target mapping relationship, the natural language query is parsed to obtain the initial parsing result.
3. The method for generating text-to-structured query language based on a self-evolving rule base according to claim 2, characterized in that, The method further includes: If the execution feedback information indicates that the execution has failed, determine the dynamic mapping relationship of the failure of the completion query statement. Lower the confidence score of the failed dynamic mapping relationship or mark the failed dynamic mapping relationship as pending review.
4. The method for generating text-to-structured query language based on a self-evolving rule base according to any one of claims 1 to 3, characterized in that, The dynamic mapping relationships include field mapping relationships and operator mapping relationships; The field mapping relationship is used to characterize the correspondence between the components of the intent phrase and a table or field in a preset database; The operator mapping relationship is used to map the logical intent in the intent phrase to the operators in the structured query language.
5. The method for generating text-to-structured query language based on a self-evolving rule base according to any one of claims 1 to 3, characterized in that, The method further includes: Configure permission filtering conditions for the completion query statement based on the user's role information; wherein, the permission filtering conditions are used to limit the range of rows or columns of data that the completion query statement can access.
6. The method for generating text-to-structured query language based on a self-evolving rule base according to any one of claims 1 to 3, characterized in that, The step of calling a preset language model to complete the natural language query, resulting in a completed query statement, includes: Based on the incompleteness level of the initial parsing result, the target language model of the initial parsing result is determined from a preset language model pool; The target language model is invoked to complete the natural language query, resulting in a completed query statement.
7. The method for generating text-to-structured query language based on a self-evolving rule base according to any one of claims 1 to 3, characterized in that, Adding the new mapping relationship to the dynamic mapping relationship includes: Generate a natural language interpretation to explain the query intent of the completed query statement; The natural language definition is displayed to the user, and upon receiving confirmation from the user, the new mapping relationship is added to the dynamic mapping relationship.
8. A text-to-structured query language generation device based on a self-evolving rule base, characterized in that, include: An acquisition unit is used to acquire a hybrid parsing strategy; the hybrid parsing strategy includes a dynamic mapping relationship between intent phrases in natural language and structured query language; The parsing unit is used to respond to the user's natural language query, parse the natural language query based on the hybrid parsing strategy, obtain an initial parsing result, and, if the initial parsing result is incomplete, call a preset language model to complete the natural language query to obtain a completed query statement; An execution unit is used to execute the completion query statement in an isolated security verification environment and obtain the execution feedback information of the completion query statement; The mapping unit is used to map the fragment obtained by completing the natural language query into a new mapping relationship when the execution feedback information indicates that the execution was successful and the initial parsing result is incomplete, and to add the new mapping relationship to the dynamic mapping relationship.
9. 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 text-to-structured query language generation method based on a self-evolving rule base as described in any one of claims 1 to 7.
10. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the text-to-structured query language generation method based on a self-evolving rule base as described in any one of claims 1 to 7.