NL2SQL and NL2VIS method based on large model collaborative intelligent agent
By employing a semantic-structural dual-path collaborative mode connection based on a large model and a collaborative intelligent agent, along with a step-by-step SQL generation strategy, the reliability and interpretability issues of existing NL2SQL and NL2VIS methods in complex queries and multi-table joins are resolved, achieving efficient and accurate database querying and visualization generation.
Patent Information
- Application Number
- CN202511899284.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-16
- Publication Date
- 2026-03-06
AI Technical Summary
Existing NL2SQL and NL2VIS methods based on large language models suffer from problems such as insufficient understanding of database schema, poor generation reliability, lack of interpretability, and excessive coupling between data query and visualization when dealing with complex queries, multi-table joins, and nested structures.
By adopting a large-model collaborative intelligent agent approach, and through semantic-structural dual-path collaborative mode connection, step-by-step SQL generation strategy, structured intermediate representation and SQL-to-VQL conversion, a completely decoupled and visualized generation path is achieved, enabling accurate identification of database tables and fields and interpretability of the generation process.
It improves the integrity and reliability of schema joins in multi-table joins and complex nested queries, reduces the generation error rate, enhances the accuracy and controllability of SQL generation, and ensures the independence and efficiency of data query and visualization generation.
Smart Images

Figure CN121614490A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of natural language processing technology, specifically to an NL2SQL and NL2VIS method based on a large model collaborative intelligent agent. Background Technology
[0002] Natural Language to SQL (NL2SQL) and Natural Language to Visualization (NL2VIS) are both rapidly developing intelligent data analysis methods in recent years, and their basic principles are well known to those skilled in the art. Taking NL2SQL as an example, its core objective is to automatically convert user-input natural language questions into Structured Query Language (SQL) statements. This process typically includes the following steps: first, semantic parsing of the natural language question to identify the query target, constraints, and key entities it contains; then, combining database schema information to determine the relevant tables and fields; and finally, generating grammatically correct SQL statements. The technical implementation methods of NL2SQL range from early template matching and neural network-based generative models to the more recently widely used methods based on large language models.
[0003] Similarly, NL2VIS technology aims to translate users' natural language needs into corresponding data visualizations. Its working principle generally includes the following steps: First, semantic understanding of the input statement is performed, extracting semantic elements such as numerical indicators, grouping conditions, and time intervals; then, according to visualization design specifications, these elements are mapped to visualization parameters such as chart type, axes, colors, or sizes; next, an executable visualization script or standardized language description (such as Vega-Lite or Plotly) is generated; finally, a rendering tool is invoked to generate and display the chart results. This technology significantly reduces the operational threshold for users in the data analysis process and has been adopted by various intelligent analysis systems.
[0004] Initially, NL2SQL (Natural Language Generation) and NL2VIS (Natural Language Generation Visualization) relied heavily on manually written rules or templates to map natural language queries to predefined database operations or chart types. These methods offered good interpretability and controllability in specific, closed scenarios, but heavily depended on expert knowledge, had poor generalization ability, and struggled to adapt to varied user expressions and complex multi-table query requirements. With the development of deep learning technology, especially methods based on sequence-to-sequence (Seq2Seq) models and the Transformer architecture, end-to-end generation from natural language to SQL (NL2SQL) or visualization (NL2VIS) has gradually been achieved. These methods reduced the reliance on manual rules, but still suffer from inaccurate semantic understanding and high error rates in generating logic when handling complex queries, multi-table joins, and nested structures. Furthermore, they heavily rely on large amounts of high-quality labeled data for training, resulting in high domain transfer costs.
[0005] In recent years, Large Language Models (LLMs) have made significant progress in this field due to their powerful natural language understanding and context learning capabilities. In NL2SQL, various methods have emerged based on cue engineering (such as zero-shot, few-shot, and thought chain reasoning), model fine-tuning, and agent-cooperative proxies, enabling more flexible parsing of user intent and generation of structurally correct query statements. In NL2VIS, the aforementioned methods can also be used to guide Large Language Models (LLMs) to directly convert natural language requirements into visualization code (such as Vega-Lite, Plotly, etc.) or intermediate representations (such as Visual Query Language VQL) to drive chart generation, thereby enhancing the flexibility, accuracy, and diversity of the visualization generation process.
[0006] However, existing methods based on large language models still have shortcomings such as insufficient understanding of database schemas, poor reliability of complex query generation, lack of interpretability in the generation process, and excessive coupling between data query and visualization.
[0007] Insufficient Database Schema Understanding: Existing NL2SQL methods generally suffer from insufficient parsing accuracy when dealing with database schemas containing multiple tables, complex relationships, and heterogeneous fields. This problem mainly manifests in two aspects: structural relationship identification and semantic correspondence establishment. At the structural level, indirect relationships or implicit connections may exist between multiple tables, making it difficult for the model to fully cover the necessary relationship information when extracting relevant schemas. This can easily lead to structural biases such as table join errors or omissions of intermediate tables. At the semantic level, natural language descriptions and database field names are often inconsistent, and the model's understanding of field semantics is uncertain, resulting in field matching biases. Because existing methods mostly adopt a single-path inference process, there is a lack of coordination between structural analysis and semantic matching, making it difficult for them to correct each other. Therefore, in complex databases or complex query scenarios, insufficient pattern recognition is more likely to occur, ultimately affecting the accuracy of SQL generation.
[0008] Structural flaws in complex query generation: Existing methods generally employ a "single-step generation" strategy within their SQL generation modules. This requires the model to output the complete SQL statement at once, making it difficult to coordinate structural planning with detailed data filling. When handling complex queries such as multi-table joins and deep nesting, macro-level logical errors are prone to occur, such as disordered JOIN order, incorrect subquery hierarchy, or missing conditions, severely limiting the reliability of the generated statements.
[0009] Lack of intermediate states and interpretability: Existing methods, due to the single-step black-box operation of the generation process, only output the final code, making it difficult to accurately locate the root cause of errors. The model struggles to distinguish whether the problem originates from semantic understanding, structural construction, detail filling, or visual code, resulting in inefficient repair strategies that can only rely on regeneration or local tuning, hindering system iteration and optimization.
[0010] The coupling between data acquisition and visualization generation is too high: Existing NL2VIS methods typically combine data querying and chart rendering, ignoring the fundamental differences between the two in terms of objectives and constraints. This coupled design makes it difficult for the model to balance data accuracy and visual plausibility in complex scenarios, resulting in outputs with both data bias and expression defects, affecting the overall visualization effect and user cognitive efficiency. Summary of the Invention
[0011] This invention discloses an NL2SQL and NL2VIS method based on a large model collaborative intelligent agent, which can more accurately identify the database tables, fields and their relationships involved in the user's query intent, fundamentally reducing connection errors, field misselection and information omission caused by schema misunderstanding.
[0012] To achieve the above objectives, the technical solution of the present invention includes the following:
[0013] A method for NL2SQL based on a large-model collaborative intelligent agent, the method comprising: In database schema Retrieving candidate database patterns related to natural language queries And based on the candidate database schema The number of tables included determines the task complexity of the natural language query. Based on the aforementioned task complexity, the natural language query is converted into a preliminary SQL query statement; The process involves verifying, repairing, and providing feedback on the initial SQL query statement, until the final SQL query statement is generated.
[0014] Furthermore, in database schema Retrieving candidate database patterns related to natural language queries ,include: Building a database schema that includes natural language problems Triads of external knowledge; The triples are fed into a large language model, enabling the model to analyze natural language queries and database patterns. Semantic relationships between data tables and data columns generate database schema candidates. ; Triples and few-shot examples are provided to the large language model so that the large language model can extract database pattern candidates corresponding to natural language queries based on the few-shot examples. ; Candidates based on database schema and database schema candidates Information completion and pattern fusion are performed to obtain candidate database patterns related to natural language queries. .
[0015] Furthermore, based on the pattern candidate set and database schema Information completion and pattern fusion are performed to obtain candidate database patterns related to natural language queries. ,include: Calculate database schema candidates and database schema candidates The intersection of the database schemas is used to add the database schemas in the intersection to the candidate database schemas. ; For the current candidate database schema Database schema candidates If the remaining database schema candidates The database schema candidate exists. If the necessary join tables are not involved, then the database schema candidate will be... Add to candidate database pattern ; For the remaining database schema candidates If the database schema is a candidate The table to which it belongs is not in the current candidate database schema. Database schema candidates A complete representation will then include database schema candidates. Add to candidate database pattern .
[0016] For the remaining database schema candidates If the remaining pattern candidates The field (i.e., column) is not in the current candidate database schema. Database schema candidates If the data is fully represented, then it is determined whether the table to which the field belongs is already in the candidate database schema. In the candidate database schema: The table to which this field belongs is already in the candidate database schema. In the case of [the above], add this field to the candidate database schema. The table to which this field belongs is not in the candidate database schema. In the case of [the specified field], add the field and the table to which the field belongs to the candidate database schema. .
[0017] Furthermore, based on this candidate database pattern The number of tables included determines the task complexity of the natural language query, including: In this candidate database mode When the number of tables included is 1, the task complexity corresponding to this natural language query is simple; In this candidate database mode When the number of tables included is greater than 1, the task complexity corresponding to this natural language query is complex.
[0018] Furthermore, when the task complexity corresponding to the natural language query is simple, the step of converting the natural language query into a preliminary SQL query statement includes: A first basic skeleton is generated based on the question intent of the natural language query. The first basic skeleton includes: SELECT field, FROM field and WHERE field. The field names, aggregate functions, and filter conditions are populated sequentially according to the first basic skeleton. During the population process, reasoning based on thought processes is used to ensure that the fields and conditions match the query intent. If the basic skeleton contains aggregation or sorting operations, the syntax and logical constraints are also checked during the population process.
[0019] Furthermore, when the task complexity corresponding to the natural language query is complex, the step of converting the natural language query into a preliminary SQL query statement includes: By analyzing table information and complexity information, a second basic skeleton is generated, which includes: JOIN field, GROUP BY field, ORDER BY field and nested subqueries. Determine the table's fill order and JOIN conditions, and then gradually fill in fields, aggregate functions, filter conditions, and nested subqueries based on this fill order and JOIN conditions; during the fill process, placeholders are adjusted to accommodate complex constraints.
[0020] Furthermore, based on the initial SQL query statement, the SQL query statement is validated, repaired, and feedback is provided until the final SQL query statement is generated, including: The SQL query statement is executed in the target database to test whether there are any problems with the SQL query statement. The problems include: syntax errors, missing fields, non-existent tables, and aggregation conflicts. If a problem is detected in the SQL query statement, the source of the problem is located based on the error type, prompt message, and context, and the SQL query statement is repaired based on the source of the problem. The SQL query statement is re-executed in the target database based on the repaired SQL query statement until there are no problems with the SQL query statement, and then the final SQL query statement is obtained.
[0021] An NL2VIS method based on large-model collaborative intelligent agents, the method comprising: In database schema Retrieving candidate database patterns related to natural language queries And based on the candidate database schema The number of tables included determines the task complexity of the natural language query. Based on the aforementioned task complexity, the natural language query is converted into a preliminary SQL query statement; The process involves verifying, repairing, and providing feedback on the initial SQL query statement, until the final SQL query statement is generated. The final SQL query statement is transformed into a visual query language.
[0022] Furthermore, the final SQL query statement is transformed into a visual query language, including: Map the selection fields, aggregation conditions, and grouping logic in the final SQL query statement to the corresponding structure in the visual query language; Based on the mapping results, graph preferences from natural language queries are introduced to obtain a preliminary visual query language; Based on the initial visual query language, the process involves verifying, fixing, and providing feedback on the visual query language until the final visual query language is generated.
[0023] Furthermore, the process involves validating, fixing, and providing feedback on the visual query language until the final visual query language is generated, including: Generate visualization rendering code based on the chart type, field mapping, and aggregation operations specified in the visualization query language; The visualization rendering code is executed in a sandbox environment, and the generated chart rendering results are checked for problems, including: missing fields, aggregation conflicts, and improper configuration. In cases where there are issues with the chart rendering, the final SQL query can be repaired by capturing the error type and location. Based on the repaired final SQL query statement, the process of mapping the selection fields, aggregation conditions, and grouping logic in the final SQL query statement to the corresponding structure of the visual query language is repeated until the chart rendering result is no longer problematic, thus obtaining the final visual query language.
[0024] Compared with the prior art, the present invention has at least the following beneficial effects.
[0025] Key Point 1: An enhanced pattern connection method based on "semantic-structural" dual-path collaboration.
[0026] Beneficial Technical Effects: The "semantic-structural" dual-path collaborative pattern connection method proposed in this invention achieves parallel modeling and cross-constraint of field semantics and inter-table relationships. Compared with traditional methods that rely on single-path inference, the dual-path collaborative mechanism of this invention can use structural candidates to verify semantic candidates, or use semantic information to complete structural relationships, thereby effectively reducing pattern recognition biases such as misselection of fields, omission of connection relationships, and missing intermediate tables. In environments with multi-table joins, complex nesting, and heterogeneous fields, this mechanism can more accurately determine the correspondence between fields and tables, improve the integrity and reliability of pattern connections, and provide a stable and accurate structural foundation for subsequent SQL generation and visualization statement generation.
[0027] Key Point 2: Step-by-step SQL generation strategy (skeleton first, content later).
[0028] Beneficial technical effects: This invention decomposes the SQL generation process into two stages: structural skeleton generation and content filling. It realizes step-by-step planning of macro logic (such as table join order, nesting level, aggregation framework) and micro details (field selection, filtering conditions, aggregation functions), reduces the LLM generation load, effectively avoids structural errors caused by single-step generation, significantly reduces the error rate of multi-table and multi-nested queries, improves the executability and semantic accuracy of SQL, and enhances the generation stability in complex query scenarios, providing a reliable foundation for the generation of complex query SQL.
[0029] Key Point 3: A phased generation method for structured intermediate representations.
[0030] Beneficial technical effects: By introducing multiple structured intermediate states (such as SQL skeletons, VQL, etc.) as verifiable generation steps, the model generation process becomes more interpretable and controllable. Each intermediate state can be independently checked and verified, thereby quickly locating the source of generation errors (such as the skeleton stage, detail filling stage, or VQL transformation stage), and making adjustments and iterations at each stage to improve generation accuracy and stability. This mechanism significantly enhances system robustness, debugging efficiency, and consistency of generation results, while reducing the uncontrollable risks of traditional single-step black-box generation in complex query or multi-table join scenarios.
[0031] Key Point 4: A completely decoupled visual generation path based on the "SQL-to-VQL" transformation.
[0032] Beneficial technical effects: By first obtaining accurate data through a high-performance NL2SQL module, then converting the SQL structured data into VQL, and combining this with visualization preferences in natural language for mapping and attribute completion, a clear decoupling between data querying and visualization generation is achieved. This method not only improves the accuracy of data extraction but also enhances the rationality and controllability of chart generation, making the NL2VIS process more robust and efficient in complex scenarios. Attached Figure Description
[0033] Figure 1 Flowchart of the NL2VIS method based on large-model collaborative intelligent agents.
[0034] Figure 2 Pattern candidate set Example of extracted prompt words.
[0035] Figure 3 Example of prompts for the SQL generator agent.
[0036] Figure 4 This is an example of how the SQL generation agent transforms a query question Q into preliminary SQL.
[0037] Figure 5Example of prompts from the SQL Repair Feedback Agent.
[0038] Figure 6 Example diagram of VQL.
[0039] Figure 7 Example of prompts for a visual conversion agent.
[0040] Figure 8 Example prompts for chart generation and feedback agents. Detailed Implementation
[0041] The present invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be noted that the embodiments described below are intended to facilitate the understanding of the present invention and do not limit it in any way.
[0042] The NL2SQL method based on a large-model collaborative intelligent agent of this invention firstly involves a semantic connection agent receiving natural language questions and database schema inputs. The agent filters and simplifies the schemas, identifies tables and fields closely related to the semantics of the question, and performs validation and fusion based on results extracted directly from Pre-SQL and LLM. Simultaneously, it determines the query complexity and outputs a subset of schemas and complexity labels. Subsequently, an SQL generation agent selects an appropriate generation strategy based on the aforementioned complexity information, first constructing the SQL framework using a "skeleton-content" approach, then gradually completing table names, field names, and filtering conditions, and generating candidate SQL statements using a few-sample approach and thought chain hints. Next, an SQL feedback and repair agent executes and verifies the generated results, detecting syntax, field, or logical errors, and iteratively repairs them based on the error information until an executable and semantically sound final SQL is obtained. Building upon this foundation, the present invention also discloses an NL2VIS method based on a large-model collaborative intelligent agent. Its visualization transformation agent converts the final SQL into VQL, maintaining data logic consistency while supplementing the visualization requirements in the user's original instructions (such as chart type, dimensions, metrics, and sorting methods), constructing an intermediate representation that satisfies both data logic and visual expression. Finally, the chart generation and feedback agent generates executable visualization code based on VQL, runs it in a sandbox environment to obtain chart results, and captures and feeds back errors to the VQL layer for repair. Through an iterative mechanism, the method is continuously optimized until a valid and user-intended visualization chart is obtained. Figure 1 This is a flowchart of the NL2VIS method based on large-model collaborative intelligent agents.
[0043] In this invention, the NL2SQL task is given a triple (Q, S, K), where Q represents the natural language problem, S represents the database schema, and K represents external knowledge (optional). The database schema S is defined as follows: ,in Representing multiple data tables , Represents data columns The goal of the NL2SQL task is to generate the correct SQL statement corresponding to the input triples using an LLM. ,Right now = .in, This represents LLM prediction generation. Similarly, the goal of the NL2VIS task is to generate a corresponding visualization chart based on the input triples. (This is an intermediate representation for visualization), that is = .
[0044] Step 1: Semantic Linking Agent The goal of a semantic joiner is to effectively and structurally connect a user's natural language query Q with a database schema S, thereby bridging the gap between semantics and database structure. Its main process includes two stages: schema joining and complexity classification.
[0045] 1) Schema Join Definition: Given a query Q and a database schema S={T,C}, LLM parses the natural language query Q to obtain a set of candidate tables and columns related to the query: ={ , },in, ⊆ and ⊆ These represent the table and field subsets after semantic filtering, respectively.
[0046] 2) Pattern Connection: To improve the accuracy of pattern connection, a dual-path extraction mechanism is introduced. The two paths provide semantic-side candidates and structural-side candidates respectively, and achieve synergistic enhancement through lightweight complementary rules.
[0047] Path 1: Pattern candidate set directly extracted based on LLM .
[0048] The triplet is provided to the LLM, which then obtains the filtered pattern. .
[0049] This approach excels at capturing semantic relationships such as field meaning and user intent. Hint words include... Figure 2 As shown.
[0050] Path 2: Candidate set of patterns extracted from Pre-SQL .
[0051] The triples are provided to the LLM, and preliminary SQL is generated using carefully designed thought chain prompts and few-sample examples. For ease of representation, this is named Pre-SQL. Subsequently, based on this Pre-SQL, the database schema corresponding to question Q is extracted. ,Right now = ,in It extracts and parses the database schema from the SQL.
[0052] This path excels at identifying structural information such as relationships between tables and cross-table connection links.
[0053] 3) Path collaboration: A fusion mechanism based on verification and completion.
[0054] To avoid omissions caused by a single path, this embodiment uses simple and controllable rules to make the two types of candidate information complementary, thereby obtaining a more complete pattern set. The collaborative process includes: 1. Intersection confirms high-confidence pattern: Calculation The tables and fields in this data are considered as high-confidence components and will be retained preferentially in subsequent merging. 2. Structural completion semantic information: If the structural path Includes semantic path Necessary join tables not involved (such as intermediate tables in a Pre-SQL JOIN chain) are added to the final schema to avoid structural chain breaks. 3. Semantic completion structural information: If semantic path If a field appears in the schema but is not fully represented in the Pre-SQL of the structure path, then the field is retained (if the table has been retained) or the field and its table (if the table has not been retained), so that the final schema includes important semantic feature fields. 4. Schema Fusion: Through the above verification and completion steps, the fused candidate database schema is obtained. =Merge( , The merged candidate database schemas are defined as follows: Merge(·) represents schema fusion based on validation and completion rules. The final connection result is passed to the downstream intelligent agent agent.
[0055] 4) Complexity Classification: LLM classifies task complexity based on the number of tables involved in the query. Single-table queries are classified as simple, while multi-table (two or more tables) queries are classified as complex, so that downstream modules can select the appropriate generation strategy.
[0056] The second step is the SQL Generation Agent.
[0057] The SQL generation agent is responsible for transforming the pattern subset and complexity information output by the semantic join agent into high-quality SQL query statements. Its generation process mainly includes three stages: strategy selection, structure construction, and content filling. It combines few-shot hints and thought chain reasoning mechanisms to improve logical stability, semantic accuracy, and generation efficiency in complex query scenarios. Examples of hint words are shown below. Figure 3 As shown.
[0058] 1) Strategy selection: Select different generation strategies based on the complexity of upstream queries to match task complexity and improve inference efficiency and accuracy.
[0059] Single-table strategy: Suitable for queries involving only a single table or those containing only basic aggregation operations. It features a simple skeletal structure, short generation path, and localized inference chain. The strategy prioritizes clear and direct aggregation and filtering logic, avoiding unnecessary complex steps. Failure to differentiate strategies may lead to overly complex simple queries, increasing generation costs and the probability of errors.
[0060] Multi-table strategy: Suitable for multi-table joins, nested queries, or complex aggregations, with complex skeletal structures and inference chains spanning tables and levels. This strategy emphasizes the planning of join relationships and nested logic, ensuring effective control over both macro-level logic and micro-level details, reducing structural errors and semantic biases, and improving generation accuracy and controllability.
[0061] 2) Structure Construction (Skeleton Generation): The SQL generation agent first constructs an SQL framework, which retains placeholders for fields, table names, and filter conditions to ensure the integrity of the query logic and provide a structured foundation for subsequent content filling.
[0062] Single-table strategy: The agent generates a basic skeleton containing SELECT, FROM, and WHERE based on the problem intent. The skeleton structure is relatively simple, and the focus is on ensuring the integrity of aggregation operations and filtering logic.
[0063] Multi-table strategy: Analyze table relationships and complexity information to generate a skeleton containing JOIN, GROUP BY, ORDER BY and nested subqueries, clarify the connection logic and aggregation hierarchy between tables, and reduce the possibility of logical conflicts in complex scenarios.
[0064] 3) Content population: After the SQL framework is built, placeholders (such as specific table names, field names and filter conditions) are gradually replaced with executable content to ensure semantic accuracy, thereby generating complete and executable SQL query statements.
[0065] Single-table strategy: First, populate the field names according to the skeleton, then add aggregate functions and filter conditions in sequence. The agent uses logical reasoning at each step to ensure that the fields and conditions match the query intent; if the skeleton contains aggregation or sorting operations, syntax and logical constraints are validated during population to avoid conflicts or semantic errors. The entire process is short, direct, and quickly generates executable SQL.
[0066] Multi-table strategy: First, determine the population order and JOIN conditions for each table, then gradually populate fields, aggregate functions, filter conditions, and nested subqueries. The system can dynamically adjust placeholders during the population process to adapt to complex constraints. For complex scenarios such as multidimensional grouping or time series analysis, the agent will progressively verify the logical correctness of each field and condition in stages, ensuring that the final generated SQL is highly executable and semantically accurate.
[0067] Process example as follows Figure 4 As shown. Ultimately, the SQL generation agent transforms query question Q into preliminary SQL, providing a reliable foundation for subsequent validation and optimization.
[0068] Step 3: SQL Feedback Agent.
[0069] The SQL Feedback and Repair Agent is responsible for verifying and repairing the query statements output by the SQL Generation Agent, ensuring the executability of the results. It mainly includes three steps: execution verification, feedback and repair, and iterative closed-loop processing. Through this "execution-feedback-repair" closed-loop mechanism, the agent can significantly improve the accuracy and robustness of SQL, especially in complex query scenarios, ensuring the system has self-correction and adaptive capabilities. Specific command prompts are as follows: Figure 5 As shown: 1) Execution verification: The generated SQL is executed and tested in the target database (e.g., sqlite3, psycopg2, etc.) to automatically detect problems such as syntax errors, missing fields, non-existent tables, or aggregation conflicts.
[0070] 2) Feedback and Repair: Once an execution failure is detected, the specific error type and message (such as syntax error) are captured, and the source of the problem is located in the context, such as incorrect table name, field mismatch, or logical conflict. Based on the diagnostic results, the error information is fed back to the proxy model to repair the SQL.
[0071] 3) Iterative closed loop: The repaired SQL will be executed and verified again. If there are still problems, it will continue to the next round of feedback and repair until an executable and semantically reasonable final result is generated.
[0072] Finally, we obtain the final SQL corresponding to the query Q. ).
[0073] At this point, the NL2SQL function is complete. The next step will be to implement the NL2VIS function based on requirements.
[0074] Step 4: Visualize the conversion agent (VQL Conversion Agent).
[0075] The Visualization Transformation Agent is responsible for converting SQL query results into Visual Query Language (VQL), thus establishing a structured bridge between data logic and chart generation. A concrete example of VQL is shown below. Figure 6 As shown: The agent's main tasks include two steps: structural mapping and visual attribute completion. This ensures that the generated visualizations are controllable in terms of logical consistency and expressive rationality. The prompt word design, for example... Figure 7 As shown: 1) Structure mapping: By leveraging the structural similarity between SQL and VQL, the selection fields, aggregation conditions and grouping logic of SQL are directly mapped to the corresponding structures of VQL, ensuring the consistency of data semantics.
[0076] 2) Visual Attribute Completion: Building upon the mapping, the system incorporates chart preferences from the user's original natural language commands, such as chart type (bar chart, line chart, pie chart, etc.), sorting, aggregation, or filtering conditions. Through the attribute completion mechanism, the generated VQL simultaneously satisfies the requirements for data logic correctness and visual representation.
[0077] Finally, the intermediate representation VQL (preliminary) required for visualization is output.
[0078] Step 5: Chart Generation and Feedback Agent.
[0079] The chart generation and feedback agent is responsible for converting VQL queries into executable visualization code or scripts (such as Python or Vega-Lite) and ensuring the usability and accuracy of the final charts through a feedback mechanism. The main process includes code generation, execution feedback and fixing, and iterative optimization (specifically for VQL). [Hint words] Figure 8 as follows: 1) Code generation: Based on the chart type, field mapping, aggregation operations, and other information specified in VQL, generate syntactically correct and semantically clear visualization rendering code (in this case, Python) to ensure that the chart has good readability and expressive effect.
[0080] 2) Execution Feedback and Repair: Execute the generated visualization code in a sandbox environment to obtain chart rendering results and detect errors such as missing fields, aggregation conflicts, or improper configuration. Capture error types and locations; pass feedback information to the visualization transformation agent to trigger automatic repair and reconstruction of VQL queries; prioritize correcting VQL rather than directly modifying the visualization code to leverage the structural controllability of the intermediate layer.
[0081] 3) Iterative optimization: The repaired VQL generates the chart code again and executes it. This process is repeated until a valid, usable chart result that meets expectations is generated.
[0082] Finally, output the visualization charts required for the query.
[0083] This completes the NL2VIS functionality.
[0084] In summary, to ensure the structural correctness and logical integrity of complex SQL queries: Addressing the structural defects of existing "single-step generation" strategies when dealing with complex queries such as multi-table joins and deep nesting, this invention designs a new SQL generation mechanism to solve problems such as macro-structural planning errors (e.g., disordered JOIN order, nesting errors) and omissions of key logical components, ensuring the executability and accuracy of the generated statements.
[0085] Achieving controllability and error traceability in the generation process: To address the problems of difficulty in error location and low repair efficiency caused by the lack of intermediate states in existing methods, this invention introduces a generation framework with structured intermediate output and iterative feedback capabilities, enabling the system to accurately locate the root cause of errors and effectively repair them, thereby improving the transparency of the generation process.
[0086] Achieving efficient decoupling and collaboration between data querying and visualization generation: To address the problem of excessive coupling between data acquisition and chart rendering in existing NL2VIS methods, this invention constructs a new technical path that clearly separates the data querying stage from the visualization generation stage, ensuring that reasonable visual mapping is performed based on accurate data, thereby simultaneously improving the quality and reliability of data extraction and chart generation.
[0087] The above embodiments are provided merely for the purpose of describing the present invention and are not intended to limit the scope of the invention. The scope of the invention is defined by the appended claims. Various equivalent substitutions and modifications made without departing from the spirit and principles of the invention should be covered within the scope of the invention.
Claims
1. An NL2SQL method based on a large model collaborative intelligent agent, characterized in that, The method comprises: In the database schema acquire a candidate database schema related to the natural language query , and according to the candidate database schema , the number of tables contained in the candidate database schema, obtain the task complexity corresponding to the natural language query; Converting the natural language query into a preliminary SQL query statement in combination with the task complexity; Performing verification, repair and feedback of the SQL query statement based on the preliminary SQL query statement until a final SQL query statement is generated.
2. The method of claim 1, wherein, In a database schema retrieving candidate database schemas related to a natural language query , comprising: constructing triples comprising natural language questions, database schema and external knowledge; sending the triples into a large language model to cause the large language model to generate database schema candidates by analyzing semantic associations between data tables, data columns in the natural language query and the database schema ; The triple and the few-shot example are provided to the large language model, so that the large language model extracts a database schema candidate corresponding to the natural language query based on the few-shot example ; Database schema candidates based on database schema patterns And database schema candidates Perform information completion and schema fusion to obtain a candidate database schema related to the natural language query .
3. The method of claim 2, wherein, Based on the pattern candidate set and database patterns Perform information completion and pattern fusion to obtain candidate database patterns related to the natural language query , comprising: Computing intersection of database schema candidates and database schema candidates and adding database schemas in the intersection to the candidate database schemas ; For the current candidate database schema Database schema candidates If the remaining database schema candidates The database schema candidate exists. If the necessary join tables are not involved, then the database schema candidate will be... Add to candidate database pattern ; For the remaining database schema candidates , if the tables belonging to the database schema candidate are not fully embodied in the database schema candidates in the current candidate database schema , the database schema candidate is added to the candidate database schema ; For the remaining database schema candidates , if the fields of the remaining schema candidate are not fully embodied in the database schema candidates in the current candidate database schema , it is determined whether the table to which the fields belong has been in the candidate database schema : in the case that the table to which the fields belong has been in the candidate database schema , the fields are added to the candidate database schema ; in the case that the table to which the fields belong has not been in the candidate database schema , the fields and the table to which the fields belong are added to the candidate database schema .
4. The method of claim 1, wherein, According to the candidate database schema The number of tables contained, obtaining the task complexity corresponding to the natural language query, comprising: In the candidate database schema When the number of included tables is 1, the task complexity corresponding to the natural language query is simple. In the candidate database schema When the number of included tables is greater than 1, the task complexity corresponding to the natural language query is complex.
5. The method of claim 4, wherein, In a case where the task complexity corresponding to the natural language query is simple, the converting the natural language query into a preliminary SQL query statement comprises: Generating a first basic skeleton according to the question intention of the natural language query, the first basic skeleton comprising a SELECT field, a FROM field and a WHERE field; Filling in the field name, aggregation function and filtering condition in turn according to the first basic skeleton; wherein the filling is combined with the chain-of-thought reasoning to ensure that the field and condition meet the query intention, and if the basic skeleton contains an aggregation operation or a sorting operation, the filling also checks the syntax and logical constraints.
6. The method of claim 5, wherein, In a case where the task complexity corresponding to the natural language query is complex, the converting the natural language query into a preliminary SQL query statement comprises: Generating a second basic skeleton by analyzing the table information and complexity information, the second basic skeleton comprising a JOIN field, a GROUP BY field, an ORDER BY field and a nested subquery; Determining the filling order and JOIN condition of the table, and gradually filling in the field, aggregation function, filtering condition and nested subquery based on the filling order and the JOIN condition; wherein the placeholders are adjusted during the filling process to adapt to the complex constraints.
7. The method of claim 1, wherein, Performing verification, repair and feedback of the SQL query statement based on the preliminary SQL query statement until a final SQL query statement is generated, comprising: Performing execution test of the SQL query statement in the target database to detect whether the SQL query statement has problems, the problems comprising syntax errors, missing fields, non-existent tables and aggregation conflicts; If the SQL query statement is detected to have problems, locating the problem source based on the error type, prompt information and context, and repairing the SQL query statement based on the problem source; Re-executing the execution test of the SQL query statement in the target database based on the repaired SQL query statement until the SQL query statement has no problems, and obtaining the final SQL query statement.
8. An NL2VIS method based on a large model cooperative intelligent agent, characterized in that, The method comprises: In database schema Retrieving candidate database patterns related to natural language queries And based on the candidate database schema The number of tables included determines the task complexity of the natural language query. Converting the natural language query into a preliminary SQL query statement in combination with the task complexity; Performing verification, repair and feedback of the SQL query statement based on the preliminary SQL query statement until a final SQL query statement is generated; Converting the final SQL query statement into a visual query language.
9. The method of claim 8, wherein, The converting the final SQL query statement into a visual query language comprises: Mapping the selection field, aggregation condition and grouping logic in the final SQL query statement to the corresponding structure of the visual query language; Introducing the chart preference in the natural language query based on the mapping result to obtain a preliminary visual query language; Performing verification, repair and feedback of the visual query language based on the preliminary visual query language until a final visual query language is generated.
10. The method of claim 9, wherein, Performing validation, fixing and feedback of the visual query language until generating a final visual query language, including: Generating a visual rendering code according to a chart type, a field mapping and an aggregation operation specified in the visual query language; Executing the visual rendering code in a sandbox environment and detecting whether a generated chart rendering result has a problem, the problem including: a missing field, an aggregation conflict and an improper configuration; In a case where the chart rendering result has the problem, fixing a final SQL query statement by capturing an error type and a location; Re-executing the mapping of the selected fields, the aggregation condition and the grouping logic in the final SQL query statement to a corresponding structure of the visual query language until the chart rendering result does not have the problem based on the fixed final SQL query statement, and obtaining the final visual query language.
Citation Information
Cited By
Zero sample Text2SQL (Structured Query Language) generation method based on structure perception and adaptive routing
CN121958318A