A text2SQL generation method based on multi-source knowledge fusion and adaptive progression
By using a multi-source knowledge fusion and adaptive progressive Text2SQL generation method, the problems of semantic gap and difficulty in complex query processing in existing technologies are solved, achieving efficient and accurate SQL generation and reducing system costs and response latency.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- DALIAN NEUSOFT UNIV OF INFORMATION
- Filing Date
- 2026-03-30
- Publication Date
- 2026-07-10
AI Technical Summary
Existing Text2SQL technology suffers from problems such as semantic gap, single knowledge source, difficulty in processing complex queries, long response latency, and security risks in enterprise data-driven decision-making, making it difficult to achieve fast, plug-and-play functionality without training.
A multi-source knowledge fusion architecture is constructed, including an enhanced schema semantic library, an experience vector database, and a business knowledge graph. SQL queries are generated by combining a pre-trained LLM large language model with multi-granular semantic annotation and an adaptive progressive decision-making strategy.
It achieves intelligent disambiguation of cross-table dependencies and complex business relationships, reduces token consumption and LLM call count, improves query efficiency and system economy, and ensures the accuracy and response speed of generated SQL.
Smart Images

Figure CN122364261A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of natural language processing and database interaction technology, and in particular to a Text2SQL generation method based on multi-source knowledge fusion and adaptive progressive processing. Background Technology
[0002] In the context of data-driven decision-making in enterprises, the technical barriers to database queries have become a major obstacle for business personnel to gain data insights. Text2SQL technology allows users to interact directly with databases using natural language, and is a key technology for enhancing the inclusive value of data, widely used in business intelligence, data analysis, intelligent customer service, and other fields.
[0003] Currently, Text2SQL implementations based on large language models mainly fall into two categories. The first is a method based on supervised fine-tuning of the large model. This method trains the model using domain-specific datasets to adapt to the target database schema and query syntax. This approach is effective but requires a large amount of labeled data and has high computational costs; further fine-tuning is needed after schema changes. The second category is based on prompt word engineering. This method injects database schemas, business rules, and examples into the context of the large model through carefully designed prompt words, enabling it to directly generate SQL. This is the mainstream solution currently used by enterprises due to its rapid deployment and low cost. In addition, Retrieval Augmentation (RAG) technology has also been introduced, using vector databases to retrieve historical similar questions and SQL queries as references to improve generation accuracy. Another type of Text2SQL system is based on large model "Skills" (such as Open Claw). This relies on AI to dynamically obtain database schemas and execute queries through multiple calls to predefined skills, completing the query task through multiple interactions.
[0004] In the context of data-driven decision-making in enterprises, lowering the technical barrier to database queries is crucial. Text2SQL technology allows users to interact directly with databases using natural language, and is a key technology for enhancing the inclusive value of data, widely used in business intelligence, data analysis, intelligent customer service, and other fields. Despite some progress in existing technologies, numerous challenges remain in practical applications: a semantic gap exists between natural language representation and database schemas; models lack domain-specific business knowledge and common-sense understanding, leading to inaccurate generated SQL; to improve accuracy, prompt words are often injected into the entire database schema, resulting in contextual redundancy, huge token consumption, and large models may experience "illusions" due to information overload, generating irrelevant or incorrect SQL; existing solutions often rely on single knowledge sources, lacking the ability to dynamically integrate and collaboratively reason with diverse knowledge such as structured business rules and semantic relationships, making it difficult to handle complex cross-table joins and fuzzy queries; in terms of deployment, many solutions require extensive fine-tuning or complex engineering adaptation for specific databases or business scenarios, making it difficult to achieve fast, plug-and-play functionality without training, and often requiring re-adaptation when the database schema changes; furthermore, Skill-based Text2SQL systems rely on AI repeatedly calling predefined Skills to dynamically obtain schemas and execute queries, resulting in long response delays, high API call costs, and security risks such as data leakage and unauthorized access due to AI directly accessing the database. Summary of the Invention
[0005] This invention provides a Text2SQL generation method based on multi-source knowledge fusion and adaptive progressive processing to overcome the above-mentioned technical problems.
[0006] To achieve the above objectives, the technical solution of the present invention is as follows: A Text2SQL generation method based on multi-source knowledge fusion and adaptive progressive processing includes the following steps: S1: Retrieve schema metadata for all tables in the target database; Furthermore, the schema metadata includes, but is not limited to, table names, table comments, field names, field data types, field constraints, primary key information, and foreign key relationships; A static schema semantic library is constructed based on schema metadata and synchronized with the target database in real time. S2: Perform multi-granular semantic annotation on the data in the static schema semantic library to obtain an enhanced schema semantic library that provides basic metadata for SQL generation; the multi-granular semantic annotation includes core semantic annotation, extended semantic annotation, ambiguous semantic annotation, and negative semantic annotation; S3: Obtain a distributed vector database for storing historical successful interaction cases, and obtain historical triple data from the distributed vector database based on the defined experience data storage structure; the historical triple data is represented as "user natural language query, historical SQL example, execution context data"; wherein the execution context data includes the table range corresponding to the query, business rules, execution time, and preset result feedback labels; at the same time, a pre-trained word embedding model is used to vectorize only the user natural language query in the historical triple data, obtain fixed-dimensional semantic vectors, and set corresponding vector indexes to obtain a dynamic experience vector knowledge base; S4: Acquire known domain business data, and simultaneously use natural language processing (NLP) technology to extract business node data and edge relationship data from the domain business data. Configure dynamically adjusted confidence attributes with weights for the extracted business node data and edge relationship data, and store the extracted business node data and relationship data in a structured manner through a graph database to obtain a business domain knowledge graph. The business domain knowledge graph, dynamic experience vector knowledge base, and enhanced schema semantic base together constitute a multi-source knowledge storage repository. S5: Construct an adaptive progressive decision-making strategy based on a multi-source knowledge repository to obtain the query processing path from the natural language query text input by the user; based on the query processing path, call the prompt word assembler and combine it with the preset LLM large language model to generate SQL candidate query statements; S6: Perform SQL validation on the SQL candidate query statements. The SQL validation operation includes, but is not limited to, checking the correctness of the SQL syntax and the consistency of field / table names. If the SQL validation operation passes, the corresponding SQL candidate query statement is used as the final Text2SQL, and the final Text2SQL is used as a successful interaction case to dynamically update the dynamic experience vector knowledge base. At the same time, the confidence attribute is adjusted based on the experience value to update the business knowledge graph. If the SQL validation operation fails, an error message is sent and the natural language query text is re-entered.
[0007] Furthermore, the core semantic annotations mentioned in S2 refer to the pre-configured standard business names and business definitions of tables / fields in the static schema semantic library; the extended semantic annotations refer to the business aliases, synonyms, and industry-standard terms of tables / fields in the static schema semantic library; the ambiguous semantic annotations refer to the pre-configured context disambiguation rules of same-name / ambiguous fields in the static schema semantic library; and the negation semantic annotations refer to the pre-configured exclusionary rules corresponding to negation words in tables / fields in the static schema semantic library.
[0008] Furthermore, the method for obtaining a business domain knowledge graph in S4 specifically includes the following steps: S41: Obtain known domain business data; S42: Use natural language processing technology to extract business node data and edge relationship data from domain business data, and define a graph structure network based on the business node data and edge relationship data; The graph structure network includes a cross-table semantic relationship network, a structured business rule network, and a terminology semantic relationship network; The cross-table semantic relationship network is a structural network that constructs a mapping relationship between business concepts and database schema, with business concepts as core nodes and business logic associations as edges. The structured business rule network is a network with business rules as the core nodes, and the core nodes are decomposed into conditional sub-nodes and logical relationship edges related to the business rules. The terminology semantic relationship network is a structural network of synonym relationships, hierarchical relationships, and negation exclusion relationships of storage business terms; S43: Use the graph structure network as a graph database for structured data storage, and configure a confidence attribute with dynamically adjusted weights for the corresponding business node data and edge relationship data in the graph database, thereby obtaining a business domain knowledge graph.
[0009] Furthermore, S5 specifically includes the following steps: S51: Obtain the natural language query text input by the user, and perform preprocessing operations on the natural language query text to obtain the preprocessed query text Q; and the preprocessing operations include, but are not limited to, text denoising, word segmentation, sensitive word filtering, and business term recognition. S52: Using the same pre-trained word embedding model as in S3, the pre-processed query text is transformed into a text semantic vector; S53: Based on the dynamic experience vector knowledge base, perform full-database semantic similarity retrieval on the text semantic vector to obtain the semantic retrieval result corresponding to the highest similarity score. The semantic retrieval result is the historical SQL sample and the corresponding execution context data. Simultaneously, the complexity of the target database schema corresponding to the natural language query text is obtained based on the enhanced schema semantic library; the quantitative dimensions corresponding to the complexity include the total number of tables in the database, the average number of fields in a single table, the hierarchy of primary and foreign key associations between tables, and the complexity of cross-table associations. S54: Construct an adaptive progressive decision strategy based on the semantic retrieval results and complexity corresponding to the highest similarity score to obtain the query processing path for the natural language query text input by the user; based on the query processing path, call the prompt word assembler and combine it with the preset LLM large language model to generate SQL candidate query statements.
[0010] Further, in S54, an adaptive progressive decision-making strategy is constructed to obtain a query processing path for the received natural language query text of the user input. The method of calling the prompt assembler based on the query processing path and combining the preset large language model (LLM) to generate a SQL candidate query statement includes the following steps: S541: Define a preset complexity threshold and obtain the complexity Comp(Schema); If Comp(Schema) < the preset complexity threshold, then continue to execute S542; otherwise, execute S543; S542: Obtain the highest similarity score Sim(Q), and judge the numerical relationship between the highest similarity score Sim(Q) and the preset high threshold T_high and the preset low threshold T_low; If Sim(Q) ≥ T_high, then use the historical SQL example corresponding to the historical triple data matched based on the highest similarity score Sim(Q) as the current query processing path; and use the historical SQL example and its corresponding execution context data at this time as the SQL candidate query statement; If T_low ≤ Sim(Q) < T_high, then retrieve the refined Schema information related to the query semantics that matches the preprocessed query text Q from the enhanced Schema semantic library; the refined Schema information includes table names, field names, field types, primary and foreign key relationships, and corresponding semantic annotation information; Retrieve the business association information related to the preprocessed query text Q from the business knowledge graph; the business association information includes business rules, cross-table semantic relationships, and term semantic relationships; Retrieve and obtain the top N historical SQL statement examples that are semantically similar to the current query corresponding to the preprocessed query text Q from the dynamic experience vector knowledge base; Input the refined Schema information, business association information, and historical SQL examples into the preset prompt assembler, and perform prompt assembly based on the preset prompt assembly format to obtain the structured prompt, and generate a SQL candidate query statement based on the structured prompt to guide the preset large language model (LLM); If Sim(Q) < T_low, then continue to execute S543; S543: Construct a two-stage generation strategy; the two-stage generation strategy includes a table scope identification and query parsing stage and a SQL generation stage; The table range identification and query parsing stage: Based on the enhanced schema semantic library, the first pre-built large language model is called to retrieve and obtain the list of table names (tables) and corresponding core semantic annotations of the data tables related to the preprocessed query text Q; the pre-built parser performs semantic parsing on the preprocessed query text Q based on the core semantic annotations to obtain the parsed query text (parsed_query); The SQL generation stage involves: obtaining data information related to the table name list (tables) based on the enhanced schema semantic library; the data information includes full field information, semantic annotations, and primary and foreign key relationships; and retrieving historical SQL cases similar to the query parsed_query from the experience vector database based on the query parsed_query, while retrieving business association information related to the query parsed_query from the business knowledge graph. A second pre-set large language model is set up, and data information, historical SQL cases, and business-related information are input into the pre-set prompt word assembler. The prompt words are assembled based on the pre-set prompt word assembly format to obtain structured prompt words. The structured prompt words guide the second pre-set large language model to generate SQL candidate query statements.
[0011] This invention provides a Text2SQL generation method based on multi-source knowledge fusion and adaptive progressive generation. The beneficial effects are as follows: (1) It constructs a "three-in-one" hybrid knowledge fusion architecture, which integrates an enhanced schema semantic library, an experience vector database, and a business knowledge graph. At the same time, it can deeply understand professional terms and implicit business logic through multi-granular semantic annotation and dynamic weight adjustment. This invention achieves intelligent disambiguation of cross-table dependencies and connection paths by using graph reasoning and case matching, which fundamentally solves the fundamental defects of traditional methods such as single knowledge source, difficulty in handling fuzzy semantics, and complex business associations. (2) In order to achieve the optimal balance between query efficiency and resource cost, this invention has built-in a complexity-aware adaptive progressive decision strategy, which dynamically evaluates query similarity and semantic complexity based on a lightweight decision model and intelligently selects cache reuse, single generation, or two progressive generation query paths. Through this knowledge supply mode of "on-demand retrieval and targeted injection", it greatly reduces redundant token consumption and the number of calls to the LLM large language model while ensuring high accuracy, and significantly improves the system's economy and response speed. Attached Figure Description
[0012] To more clearly illustrate the technical solutions in the embodiments of the present 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 the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0013] Figure 1 The flowchart shows the Text2SQL generation method based on multi-source knowledge fusion and adaptive progressive processing of this invention. Figure 2 This is the core block diagram of the Text2SQL generation method based on multi-source knowledge fusion and adaptive progressive in this embodiment. Detailed Implementation
[0014] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0015] This embodiment provides a Text2SQL generation method based on multi-source knowledge fusion and adaptive progressive processing, such as... Figures 1 to 2 As shown, the specific steps include: S1: Obtain the schema metadata of all data tables in the target database; and the schema metadata includes, but is not limited to, table names, table comments, field names, field data types, field constraints, primary key information, and foreign key relationships; A static schema semantic library is built based on schema metadata and synchronized with the target database in real time.
[0016] Specifically, in this embodiment, based on the connection configuration and metadata access permissions of the target database, a standardized database driver connects to the target database, automatically executes the DDL information collection script, and fully extracts and persistently stores the schema metadata of all data tables in the target database. The collected schema metadata is then structured according to a pre-defined hierarchical structure of "database table field relationships" and stored in a locally deployed relational database to form a schema semantic library. A schema metadata synchronization mechanism is configured to support both timed polling synchronization and triggered real-time synchronization modes. When the target database undergoes schema changes (including adding / deleting tables, adding / modifying / deleting fields, and adjusting primary / foreign key relationships), the system can automatically capture the change information and update the local schema semantic library without manual intervention or system redeployment. This embodiment achieves physical isolation between the large model and the original business database through a locally independent schema semantic library, avoiding the security risks of the large model directly accessing the original database from an architectural perspective. Simultaneously, it provides a precise and lightweight metadata source for subsequent prompt word assembly, avoiding response latency and token loss caused by the large model dynamically acquiring metadata.
[0017] S2: Perform multi-granular semantic annotation on the data in the static schema semantic library to obtain an enhanced schema semantic library that provides basic metadata for SQL generation; the multi-granular semantic annotation includes core semantic annotation, extended semantic annotation, ambiguous semantic annotation, and negative semantic annotation; Specifically, this embodiment defines a semantic annotation management module to provide a visual annotation interface and a batch import interface. To bridge the semantic gap between natural language and database schemas and improve the semantic alignment accuracy of prompt word assembly, the system supports multi-dimensional and multi-granular semantic annotation of tables and fields in the schema semantic library. The main form of annotation is adding textual descriptions to relevant tables and fields through this system. The annotation process is completely independent of the original database; all annotation content is bound to schema metadata and stored in a local relational database, requiring only read-only permissions from the original database. Specific annotation types include core semantic annotation, extended semantic annotation, ambiguous semantic annotation, and negative semantic annotation. Core semantic annotations are pre-defined standard business names and definitions for tables / fields in a static schema semantic library, clarifying their business meaning. For example, the core semantic annotation for the orders.order_id field in an e-commerce database is "unique order number".
[0018] Extended semantic annotations configure business aliases, synonyms, and industry-standard terms for tables / fields in the static schema semantic library, and support many-to-many mappings. For example, the extended semantic annotation for the products.product_name field can be "product name, goods name, SKU name, item title".
[0019] Ambiguity semantic annotation involves configuring pre-defined contextual disambiguation rules for fields with the same name or ambiguous fields in the static schema semantic library to clarify the matching priority under different query contexts. For example, for the status field shared by multiple tables, the rules can be configured as follows: when the query text contains keywords such as "place order", "ship", and "logistics", the orders.status field will be matched first; when it contains keywords such as "payment", "refund", and "reconciliation", the payments.status field will be matched first.
[0020] The negation semantic annotation refers to the pre-defined exclusion rules corresponding to the negation words configured in the table / field of the static schema semantic library. For example, the exclusion rule for annotating the order table with "returned order" and "cancelled order" is orders.order_status NOT IN ('returned', 'canceled').
[0021] This embodiment utilizes standardized, multi-granular semantic annotation to provide semantic enhancement information for prompt word assembly, addressing the issue of large models' misunderstanding of business terms and fundamentally reducing the SQL generation error rate caused by the semantic gap. Furthermore, the annotation process is not a mandatory initialization step, allowing for addition, modification, and deletion at any time during system operation based on business feedback and user interaction data, achieving incremental optimization. All annotation results are bound to the basic schema metadata and stored in the schema semantic library.
[0022] S3: Obtain a distributed vector database for storing historical successful interaction cases, and retrieve historical triple data from the distributed vector database based on the defined experience data storage structure; the historical triple data is represented as "user natural language query, historical SQL example, execution context data"; wherein the execution context data includes the table range corresponding to the query, business rules, execution time, and preset result feedback labels; simultaneously, a pre-trained word embedding model is used to vectorize only the user natural language query in the historical triple data, obtain fixed-dimensional semantic vectors, and set corresponding vector indexes to obtain a dynamic experience vector knowledge base. The dynamically growing experience memory base constructed in this embodiment provides core data support for subsequent adaptive strategy decision-making and similar case injection in prompt word assembly, while achieving extremely fast response for high similarity queries.
[0023] S4: Acquire known domain business data, and simultaneously use natural language processing (NLP) technology to extract business node data and edge relationship data from the domain business data. Configure dynamically adjusted confidence attributes with weights for the extracted business node data and edge relationship data, and store the extracted business node data and relationship data in a structured manner through a graph database to obtain a business domain knowledge graph. The business domain knowledge graph, dynamic experience vector knowledge base, and enhanced schema semantic base together constitute a multi-source knowledge storage repository. In this embodiment, a structured business knowledge graph is constructed based on a graph database. This graph serves as a source of reasoning knowledge, supporting semantic disambiguation for complex queries, cross-table join reasoning, and business rule injection. The graph stores three types of core knowledge, all using a standardized graph structure of "nodes, relationships, and attributes." The method for obtaining the business domain knowledge graph in this embodiment specifically includes the following steps: S41: Obtain known domain business data; S42: Use natural language processing technology to extract business node data and edge relationship data from domain business data, and define a graph structure network based on the business node data and edge relationship data; The graph structure network includes a cross-table semantic relationship network, a structured business rule network, and a terminology semantic relationship network; The cross-table semantic relationship network is a structural network that constructs a mapping relationship between business concepts and database schema, with business concepts as core nodes and business logic associations as edges. For example, a business relationship edge of "customer purchases goods" is constructed, and at the same time, through the "mapping to" relationship edge, the "customer" node is bound to the users table and the "goods" node is bound to the products table, providing a reasonable association path for complex cross-table queries. The structured business rule network is a network with business rules as the core nodes, which are then broken down into conditional sub-nodes and logical relational edges related to the business rules. This network can be directly used for business constraints generated by SQL. For example, the "high-value customer" rule can be broken down into users.total_spent>10000 and users. The two condition nodes, _level = 'diamond', are connected by an AND logical edge, supporting dynamic combination and injection of rules; The terminology semantic relationship network is a structural network that stores the synonym relationships, hierarchical relationships, and negation exclusion relationships of business terms; for example, the synonym relationship edge between "mobile phone" and "mobile phone", the hierarchical relationship edge between "digital product" and "mobile phone", and the negation exclusion relationship edge between "refund order" and "valid order". S43: Use the graph structure network as a graph database for structured data storage, and configure a confidence attribute with dynamically adjusted weights for the corresponding business node data and edge relationship data in the graph database, thereby obtaining a business domain knowledge graph.
[0024] In this embodiment, a dynamically adjustable confidence attribute is configured for each relation edge and each rule node in the business domain knowledge graph. The initial confidence level can be manually configured, with a default value of 0.7. The confidence level is dynamically adjusted based on the usage effect of the rule: the confidence level increases after successful verification and confirmation by user feedback; the confidence level decreases after misuse or user feedback negation; rules with a confidence level higher than the preset effective threshold (default 0.8) are marked as high-confidence rules and prioritized for invocation, while rules with a confidence level lower than the preset elimination threshold (default 0.3) are automatically paused or removed from the business domain knowledge graph. In this embodiment, through a structured knowledge graph, unstructured business rules are transformed into reasoning information with standardized injection prompts, solving the problem of insufficient understanding of vertical domain business logic by large models and improving the generation accuracy of complex cross-table queries.
[0025] This embodiment is based on a constructed multi-source knowledge repository. It achieves intelligent selection of query processing paths through an adaptive progressive decision-making strategy, precise injection of multi-source knowledge through standardized prompt word assembly, ensures quality control through SQL validation and simple self-correction, and enables continuous evolution of the knowledge system through feedback learning, forming a complete technological closed loop. Specifically, it includes the following steps: S5: Construct an adaptive progressive decision-making strategy based on a multi-source knowledge repository to obtain the query processing path from the received natural language query text input by the user; based on the query processing path, call the prompt word assembler and combine it with a preset LLM large language model to generate SQL candidate query statements, including the following steps: S51: Obtain the natural language query text input by the user, and perform preprocessing operations on the natural language query text to obtain the preprocessed query text Q; and the preprocessing operations include, but are not limited to, text denoising, word segmentation, sensitive word filtering, and business term recognition. S52: Using the same pre-trained word embedding model as in S3, the pre-processed query text is transformed into a text semantic vector; S53: Based on the dynamic experience vector knowledge base, perform full-database semantic similarity retrieval on the text semantic vector to obtain the semantic retrieval result corresponding to the highest similarity score. The semantic retrieval result is the historical SQL sample and the corresponding execution context data. Meanwhile, obtain the complexity of the target database schema corresponding to the natural language query text based on the enhanced Schema semantic library; the quantization dimensions corresponding to the complexity include the total number of database tables, the average number of fields per table, the primary-foreign key association level between tables, and the cross-table association complexity; S54: Construct an adaptive progressive decision strategy based on the semantic retrieval result with the highest similarity score and the complexity to obtain a query processing path for the received natural language query text input by the user; call a prompt assembler based on the query processing path and combine it with a preset large language model (LLM) to generate a SQL candidate query statement, and the adaptive progressive decision strategy includes Strategy 0, Strategy 1, and Strategy 2, specifically including the following steps: S541: Define a preset complexity threshold and obtain the complexity Comp(Schema); If Comp(Schema) < the preset complexity threshold, then continue to execute S542; otherwise, execute S543; S542: Obtain the highest similarity score Sim(Q), and judge the numerical relationship between the highest similarity score Sim(Q) and the preset high threshold T_high (with a default value of 0.98) and the preset low threshold T_low (with a default T_low = 0.7); Strategy 0 (0 progressive steps, cache reuse path): If Sim(Q) ≥ T_high, then use the historical SQL example corresponding to the historical triple data matched based on the highest similarity score Sim(Q) as the current query processing path; and use the historical SQL example and its corresponding execution context data at this time as the SQL candidate query statement; at this time, it is determined as a high-similarity repeated query process, which can directly trigger the cache reuse path without calling the large language model; Strategy 1 (1 progressive step, single-generation path): If T_low ≤ Sim(Q) < T_high, then retrieve the refined Schema information related to the query semantics matched by the query text Q after retrieval and preprocessing based on the enhanced Schema semantic library; the refined Schema information includes table names, field names, field types, primary-foreign key relationships, and corresponding semantic annotation information; Retrieve and obtain business association information related to the query text Q after preprocessing based on the business knowledge graph; the business association information includes business rules, cross-table semantic relationships, and term semantic relationships; Retrieve and obtain the Top N historical SQL statement examples that are similar to the current query semantics corresponding to the query text Q after preprocessing based on the dynamic experience vector knowledge base; Input the refined Schema information, business association information, and historical SQL examples into a pre-set prompt assembler, and assemble the prompts based on the pre-set prompt assembly format to obtain structured prompts. Then, use the structured prompts to guide a pre-set large language model (LLM) to generate SQL candidate query statements; at this time, it is determined as a normal query process with medium similarity, and the full-scale enhanced prompt assembly and single large language model call can be completed一次性完成全量增强提示词组装与单次大语言模型调用; by triggering a single generation path. In this embodiment, after the prompt assembly is completed, the complete structured prompt can be sent to a specified large language model for a single call to obtain the result returned by the large model. It also includes format verification of the result returned by the large language model, and only extracts the SQL code content therein, removing redundant text, as the initially generated SQL candidate query statement. It is ensured that the SQL candidate query statement meets the following requirements: 1. Only output SQL code that conforms to the syntax specification of {database type} and can be directly executed; 2. Do not output any content other than SQL code, including but not limited to explanations, comments, code block markers, and special symbols other than line breaks; 3. The SQL code must ensure that the logic completely matches the user's question and conforms to the injected business rules and Schema information. If Sim(Q) < T_low, then continue to execute S543; Strategy 2 (two-stage progressive, phased generation path), at this time it is determined as a complex query process with low similarity, triggering a phased generation path, and gradually narrowing the query scope through two large language model calls to accurately generate SQL. The specific process is as follows: S543: Construct a two-stage generation strategy; the two-stage generation strategy includes a table scope identification and query parsing stage and a SQL generation stage; in this embodiment, for complex queries, the query scope can be gradually focused through two large model calls to avoid information overload; The table scope identification and query parsing stage: Based on the enhanced Schema semantic library, call the first pre-set large language model to retrieve and obtain a list of table names tables of the data tables related to the pre-processed query text Q and the corresponding core semantic annotations; use the pre-set parser to perform semantic parsing on the pre-processed query text Q based on the core semantic annotations to obtain the query parsing text parsed_query; and take the list of table names tables (an array storing the involved table names) and the query parsing text parsed_query (an object containing the semantic parsing information of the query) as two fields and output them in a strict JSON format; For example: Output a strict JSON object in the following format: { "tables": ["Table Name 1", "Table Name 2",...], "parsed_query": { "entities": [{"entity": "entity text", "type": "entity type", "mapped_term": "standard terminology (if any)"}], "conditions": [{"time_window": "time description", "negation": "negation description", "comparison": "comparison condition"}], "aggregation": "aggregation operation", "group_by": "grouping dimension", "intent": "Intent description" } } Note: Table names must be selected from the following list and cannot be fabricated; please use predefined types for entity types (such as product, user, order, location, etc.); if certain fields cannot be recognized, they can be left blank or omitted; List of database table names users: User information table orders: order table products: Product list order_items: Order details table Payments: Payment Schedule ... [User Questions] Find the age distribution of users who purchased iPhones in 2023 and whose delivery address is in Beijing. Output the JSON result. In this embodiment, the assembled prompt words are sent to a large language model to obtain the returned results. The returned results are then validated in JSON format, and the table name list (tables) and the parsed_query object are extracted. An example output is provided (addressing the user question Q above): { "tables": ["users", "orders", "order_items", "products"], "parsed_query": { "entities": [ {"entity": "iPhone", "type": "product", "mapped_term": "iPhone"}, {"entity": "Beijing", "type": "location", "mapped_term": "Beijing"} ], "conditions": [ {"time_window": "2023"}, {"negation": "", "comparison": "Shipping address is in Beijing"} ], "aggregation": "count", "group_by": "age group", "intent": "Statistics on the age distribution of users who purchased a specific product and received it at a specific delivery address in 2023" } }; The SQL generation stage involves: acquiring data information related to the table name list (tables) based on the enhanced schema semantic library; the data information includes full field information, semantic annotations, and primary and foreign key relationships; and retrieving historical SQL cases similar to the query parsed_query from the experience vector database (similarity retrieval) based on the query parsed_query, while retrieving business association information related to the query parsed_query (such as association rules between products and orders, address filtering rules, etc.) from the business knowledge graph. A second pre-set large language model is set up, and data information, historical SQL cases, and business-related information are input into the pre-set prompt word assembler. The prompt words are assembled based on the pre-set prompt word assembly format to obtain structured prompt words. The structured prompt words guide the second pre-set large language model to generate SQL candidate query statements.
[0026] This embodiment sends the assembled prompt words to a large language model, obtains the returned SQL candidate statements, and performs format verification and SQL code extraction. Through a standardized and modular prompt word assembly process that corresponds one-to-one with the adaptive progressive decision-making strategy, it achieves on-demand and accurate injection of multi-source knowledge. This ensures the integrity of the prompt word information, avoids contextual redundancy and information overload, and standardizes the return format of the large model through mandatory output requirements, significantly reducing the cost of subsequent parsing and verification, and improving the accuracy and stability of SQL generation.
[0027] S6: Perform SQL validation on the SQL candidate query statements. The SQL validation operation includes, but is not limited to, checking the correctness of the SQL syntax and the consistency of field / table names. If the SQL validation operation passes, the corresponding SQL candidate query statement is used as the final Text2SQL, and the final Text2SQL is used as a successful interaction case to dynamically update the dynamic experience vector knowledge base. At the same time, the confidence attribute is adjusted based on the experience value to update the business knowledge graph. If the SQL validation operation fails, an error message is sent and the natural language query text is re-entered.
[0028] Specifically, in this embodiment, the generated SQL candidate statements are directly submitted to the preset target database for execution, and the execution results or error information are captured. If the execution is successful, the successful results are visualized. If the execution fails, an error message is sent and the natural language query text is re-entered to re-execute the SQL query statement generation process, or a one-time self-correction process is initiated to attempt correction and then execute again. The specific process corresponding to initiating a one-time self-correction process to attempt correction and then execute again is as follows: S61: First execution: Through the pre-configured isolated secure execution engine, the SQL candidate query statement is executed in read-only mode in the target database, and protection measures such as execution timeout and row count limit are configured; if the execution is successful, the result set corresponding to the successful execution is obtained, and the process proceeds directly to step S7; S62: Error Capture and Correction Attempt: If the first execution fails, this embodiment will capture the error information returned by the database (including error codes, error messages, etc.); subsequently, the following information (AE) will be assembled into a correction prompt word and sent to the preset large language model: A. System Instructions: The model is required to act as an SQL error correction expert, generating corrected SQL based on the provided original problem, erroneous SQL, and error information, and outputting only the SQL code; B. Original User Question: The received user's natural language query text Q; C. Error SQL: SQL statements that failed to execute on the first attempt; D. Error Message: Details of structured error messages returned by the database; E. Output Requirements: Output only the corrected, complete SQL code and do not include any explanations or other content; S63: Second Execution: The corrected SQL returned by the large language model is executed again through the secure execution engine; if the execution is successful, the result set is obtained and the process proceeds to step S7; if it still fails, the process is terminated, and a prompt containing error information is returned to the user (e.g., "Unable to generate executable SQL, please adjust the query and try again"). In this embodiment, real database errors are captured by direct execution, and a targeted correction is performed using the large language model, improving the SQL success rate while minimizing additional overhead; at the same time, the secure execution engine ensures stable database operation.
[0029] S7: Secure SQL Execution and Result Visualization: Through a pre-configured isolated database connection pool, validated SQL queries are executed in the target business database to obtain the execution result set. The entire execution process takes place within a secure sandbox, with only read-only permissions granted. Execution timeouts, row count limits, and resource usage limits are configured to prevent SQL execution from impacting the business database's performance. The execution results are returned to the user in a structured table format. The data visualization engine automatically matches the optimal visualization scheme based on the data characteristics of the result set. These data characteristics include the row and column structure of the result set, field data types (numerical, categorical, and time), data statistical distribution, and the implicit analytical intent of the query (trend analysis, comparative analysis, and composition analysis). For example, bar charts are recommended for categorical + numerical result sets, line charts for time series result sets, and pie charts for percentage-based result sets. Users can manually switch chart types.
[0030] S8: Feedback Learning and Knowledge System Self-Evolution: This embodiment incrementally updates the multi-source knowledge system based on the final SQL query statement of the interaction and user feedback, achieving the optimization of "the more it is used, the more accurate it becomes". It includes: (1) Dynamic experience knowledge base update: The triple "user natural language query - final verified SQL sample - execution context data" is vectorized and stored in the vector knowledge base to provide case reference for subsequent similar queries; (2) Confidence update of business knowledge graph: The business rules and semantic relationships called in this query are statistically analyzed; if the user feedback is positive (such as the adoption result), the confidence of the corresponding rule / relationship is increased according to the preset algorithm; if the user feedback is negative (such as manually modifying the SQL), the corresponding confidence is reduced, and the rule whose confidence is lower than the elimination threshold is automatically suspended from being called. In this embodiment, the system performance is continuously optimized through the simplest feedback loop - successful case accumulation and dynamic adjustment of rule weights, without the need for manual annotation or model fine-tuning, and long-term benefits are obtained at the lowest cost.
[0031] This embodiment also includes a Text2SQL generation system based on multi-source knowledge fusion and adaptive progressive processing, including a multi-source knowledge storage module, a knowledge management engine module, an intelligent decision-making module, a semantic retrieval and fusion module, an LLM interaction and SQL generation module, an SQL verification and execution engine module, and a visualization and feedback processing module. (1) The multi-source knowledge storage module, as the system's "memory," is responsible for storing the structured and empirical knowledge generated during the construction phase, and includes three sub-units: Static knowledge storage unit (local relational database): Stores database schema information, including table structure, field types, primary and foreign key constraints, and semantic annotations (core semantics, extended semantics, ambiguous semantics, etc.), providing basic metadata for SQL generation. Dynamic knowledge storage unit (vector knowledge base): Stores historical query cases, successful SQL examples, high-frequency pattern fragments, and other experiential information, supporting fast retrieval based on semantic similarity. Reasoning knowledge storage unit (business knowledge graph): Stores domain business rules, concept mappings, cross-table semantic relationships, and reasoning constraints, used for logical disambiguation and join path optimization in complex queries.
[0032] (2) The knowledge management engine module, acting as the system's "knowledge administrator," is responsible for the full lifecycle management of multi-source knowledge: Initialization Phase: Extract metadata from the target database, perform semantic annotation, and build the initial knowledge base and graph. Synchronous Updates: Real-time awareness of database schema changes and automatic updates to the local schema; maintenance of vector indexes and graph relationships via offline or near real-time methods. Knowledge Evolution: Based on feedback from the online phase, refine successful cases, adjust rule confidence levels, and drive continuous optimization of the knowledge system.
[0033] (3) The intelligent decision-making and routing module, as the "scheduling center" of the system, dynamically decides the processing path based on multi-factor analysis (such as query similarity, semantic complexity, and historical hit rate). This module implements an adaptive progressive strategy and outputs three routing instructions: Strategy 0 (Cache Reuse): For highly similar queries, directly route to the historical result reuse process; Strategy 1 (Single Generation): For queries of general complexity, instruct a single generation process; Strategy 2 (Two-Step Progressive Generation): For complex queries, initiate progressive generation (first identify the table / field range, then refine and generate the final SQL).
[0034] (4) The semantic retrieval and fusion module, as the system's "information collection and assembly workshop," retrieves and fuses information from the multi-source knowledge storage module as needed according to decision instructions: Extract a simplified schema from the static knowledge base (only involving querying relevant tables / fields); Retrieve similar cases from a vector knowledge base; Obtain business rules and connection templates from the knowledge graph; Multi-source information is integrated into enhanced prompts for use by the LLM interaction and SQL generation modules; (5) The LLM interaction and SQL generation module, as the "core productivity" of the system, is responsible for interacting with the large language model. This module receives the fused prompts to construct the context, calls the LLM large language model to generate preliminary SQL candidates, and calls the model in stages to gradually refine the query for the two progressive strategies.
[0035] (6) The SQL validation and execution engine module, as the system's "quality inspection station," performs multi-level validation and final execution on the generated SQL: Syntax / Logic Validation: Checks the correctness of SQL syntax, consistency of field / table names, and validity of connection paths; Self-Correction: Automatically categorizes common errors (such as missing fields and type mismatches) and attempts to repair them, triggering upstream regeneration when necessary; Secure Execution: Executes SQL in an isolated environment through a heterogeneous database connection pool (supporting MySQL, Oracle, PostgreSQL, etc.) to ensure data security.
[0036] (7) The visualization and feedback processing module, as the system's "user interface and optimization sensor", is responsible for: displaying execution results (in the form of tables, charts, etc.); collecting user feedback (such as correctness annotations and modification suggestions); and sending successful cases and correction information to the knowledge management engine to trigger knowledge evolution.
[0037] The system described in this embodiment first sends the user's natural language query text to the intelligent decision-making and routing module. This module selects a processing path and issues instructions based on the query characteristics. Subsequently, the semantic retrieval and fusion module retrieves and integrates information from the multi-source knowledge storage module according to the instructions, and passes the enhanced prompts to the LLM interaction and SQL generation module to generate a preliminary SQL query statement. The generated SQL query statement enters the SQL verification and execution engine module for verification and execution. If the verification fails, self-correction is triggered or the system returns to the upstream for correction. The final result is presented to the user through the visualization and feedback processing module. At the same time, the success signal and feedback data are captured by the knowledge management engine module and used to update the multi-source knowledge base, completing a full adaptive cycle.
[0038] This embodiment also includes the following application examples: The method or system described in this embodiment is applied to the clinical research database of a large medical research institution. This database is an extension based on the publicly available MIMICIV structure and includes a patient information table (`patients`) and a clinical test table (`... `), Prescriptions Table, Genomics Data Table There are more than 20 tables, including `_variants`, which are connected by `subject_id`, `` The relationships between keys like `_id` are complex. Researchers proposed a cross-disciplinary, highly complex query requirement: "Find individuals who have taken the drug 'osimertinib' within the past two years..." The number of patients whose specific gene locus, exon 19 of EGFR, was deleted but whose key clinical indicator, carcinoembryonic antigen (CEA), did not worsen (i.e., the value did not increase in two consecutive tests) was statistically analyzed and distributed by age group in ten-year periods. In this embodiment, query reception and adaptive decision-making are as follows: the user inputs query text, the system vectorizes it and searches in the vector knowledge base. No high similarity match is found (similarity <85%), and the pattern complexity is high. Therefore, the system automatically starts the two progressive strategies, namely strategy 2.
[0039] First incremental approach (table focus): The system only sends user questions and a list of database table names to the LLM. The LLM initially determines that the issue involves `prescriptions`, `` _variants`、` The system retrieves information from the `patients` table and requests specific field information.
[0040] Knowledge Retrieval and Fusion: Based on feedback from the large language model, detailed schemas and semantic annotations of the four tables mentioned above are extracted from the local relational database (e.g., the code 'J8501' corresponds to "osimertinib" under the `drug` field); predefined rules are retrieved from the knowledge graph: "EGFR exon 19 deletion mutation" is mapped to ` _variants.gene='EGFR' AND _variants.variant_type='Exon 19 The algorithm retrieves the related knowledge that "osimertinib is a first-line targeted drug for EGFR mutations" for logical verification; it also retrieves approximate historical SQL fragments of "finding patients taking specific drugs" and "changes in statistical indicators by time window" from the vector knowledge base as examples. The second progressive (precise generation) process sends the fused enhanced context to the LLM large language model to generate complex SQL query statements that include multi-table joins, nested subqueries, and grouped statistics.
[0041] Verification and Execution: The SQL validator performs syntax checks and executes the logic in the test environment to confirm its correctness before the secure execution engine executes it successfully on the production database.
[0042] Feedback Learning: This successful case was stored in the vector knowledge base; the system discovered that "osimertinib" and "EGFR mutation" appeared together in multiple queries, and after confidence evaluation, it was updated to the knowledge graph as a new association rule. Furthermore, assuming the medical institution added a "follow-up record table" in subsequent studies and discarded some old fields, the method described in this embodiment can automatically detect database schema changes and update the schema semantic library in the local relational database through periodic schema metadata collection; subsequently, when a user submits a new query involving follow-up records, the system can generate an SQL query statement based on the latest schema information without any manual intervention, demonstrating its ability to adapt to database changes in real time. Compared with existing technologies, the method described in this embodiment, by directly using a large model (inputting the full DDL), suffers from information overload and low accuracy when facing a massive schema of more than 20 tables; the traditional RAG framework cannot understand technical terms such as "EGFR exon 19 deletion mutation," making retrieval error-prone; and the Skill-based system requires multiple dynamic schema acquisitions, resulting in long response delays, high costs, and security risks due to AI directly accessing the database. The method described in this embodiment has high overall accuracy, low average response time, significantly reduced API call costs, and becomes faster with continued use. Furthermore, the AI is completely isolated from the original database, ensuring data security.
[0043] The beneficial effects of the method described in this embodiment are as follows: (1) A "three-in-one" hybrid knowledge fusion architecture is constructed, namely, an enhanced schema semantic library, an experience vector database and a business knowledge graph are integrated. At the same time, through multi-granularity semantic annotation and dynamic weight adjustment, it can deeply understand professional terms and implicit business logic. This invention achieves intelligent disambiguation of cross-table dependencies and connection paths by using graph reasoning and case matching, which fundamentally solves the fundamental defects of traditional methods such as single knowledge source, difficulty in handling fuzzy semantics and complex business associations. (2) In order to achieve the optimal balance between query efficiency and resource cost, this invention has built-in a complexity-aware adaptive progressive decision strategy, namely, based on a lightweight decision model, it dynamically evaluates query similarity and semantic complexity, and intelligently selects cache reuse, single generation or two progressive generation query paths. Through this "on-demand retrieval and targeted injection" knowledge supply mode, while ensuring high accuracy, it greatly reduces redundant token consumption and the number of calls to the LLM large language model, and significantly improves the system's economy and response speed. (3) The system has excellent advantages in industrial deployment. It adopts the "knowledge augmentation at inference time" paradigm, which completely eliminates the need for fine-tuning of large models. It can complete the deep adaptation of new databases within hours through an automated toolchain, achieving "out-of-the-box use". Through the pre-built closed-loop feedback learning mechanism, successful interactions are precipitated as knowledge and the knowledge base is continuously optimized, driving the system to become "more accurate with use". In terms of security, the local security execution agent realizes the physical isolation between AI and the original database, and supports multiple mainstream databases through a heterogeneous database connection pool; at the same time, the automated metadata synchronization mechanism perceives changes in database mode in real time, ensuring that SQL generation is always based on the latest structure, greatly reducing enterprise maintenance costs and perfectly adapting to agile development environments.
[0044] 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 or all of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.
Claims
1. A Text2SQL generation method based on multi-source knowledge fusion and adaptive progressive processing, characterized in that, The specific steps include: S1: Retrieve schema metadata for all tables in the target database; Furthermore, the schema metadata includes, but is not limited to, table names, table comments, field names, field data types, field constraints, primary key information, and foreign key relationships; A static schema semantic library is constructed based on schema metadata and synchronized with the target database in real time. S2: Perform multi-granular semantic annotation on the data in the static schema semantic library to obtain an enhanced schema semantic library that provides basic metadata for SQL generation; the multi-granular semantic annotation includes core semantic annotation, extended semantic annotation, ambiguous semantic annotation, and negative semantic annotation; S3: Obtain a distributed vector database for storing historical successful interaction cases, and obtain historical triple data from the distributed vector database based on the defined experience data storage structure; the historical triple data is represented as "user natural language query, historical SQL example, execution context data"; wherein the execution context data includes the table range corresponding to the query, business rules, execution time, and preset result feedback labels; at the same time, a pre-trained word embedding model is used to vectorize only the user natural language query in the historical triple data, obtain fixed-dimensional semantic vectors, and set corresponding vector indexes to obtain a dynamic experience vector knowledge base; S4: Acquire known domain business data, and simultaneously use natural language processing (NLP) technology to extract business node data and edge relationship data from the domain business data. Configure dynamically adjusted confidence attributes with weights for the extracted business node data and edge relationship data, and store the extracted business node data and relationship data in a structured manner through a graph database to obtain a business domain knowledge graph. The business domain knowledge graph, dynamic experience vector knowledge base, and enhanced schema semantic base together constitute a multi-source knowledge storage repository. S5: Construct an adaptive progressive decision-making strategy based on a multi-source knowledge repository to obtain the query processing path from the natural language query text of the received user input; Based on the query processing path, the prompt word assembler is invoked and combined with the preset LLM large language model to generate SQL candidate query statements; S6: Perform SQL validation on the SQL candidate query statements. The SQL validation operation includes, but is not limited to, checking the correctness of the SQL syntax and the consistency of field / table names. If the SQL validation operation passes, the corresponding SQL candidate query statement is used as the final Text2SQL, and the final Text2SQL is used as a successful interaction case to update the dynamic experience vector knowledge base. At the same time, the confidence attribute is adjusted based on the experience value to update the business knowledge graph. If the SQL validation operation fails, an error message is sent and the natural language query text is re-entered.
2. The Text2SQL generation method based on multi-source knowledge fusion and adaptive progressive processing according to claim 1, characterized in that, The core semantic annotations mentioned in S2 are the standard business names and business definitions pre-configured in the table / field configuration of the static schema semantic library; the extended semantic annotations are the business aliases, synonyms, and industry-standard terms configured in the table / field configuration of the static schema semantic library; the ambiguous semantic annotations are the pre-configured context disambiguation rules configured in the same-name field / ambiguous field configuration of the static schema semantic library; and the negation semantic annotations are the pre-configured exclusionary rules corresponding to the negation words configured in the table / field configuration of the static schema semantic library.
3. The Text2SQL generation method based on multi-source knowledge fusion and adaptive progressive processing according to claim 2, characterized in that, The method for obtaining a business domain knowledge graph in S4 includes the following steps: S41: Obtain known domain business data; S42: Use natural language processing technology to extract business node data and edge relationship data from domain business data, and define a graph structure network based on the business node data and edge relationship data; The graph structure network includes a cross-table semantic relationship network, a structured business rule network, and a terminology semantic relationship network; The cross-table semantic relationship network is a structural network that constructs a mapping relationship between business concepts and database schema, with business concepts as core nodes and business logic associations as edges. The structured business rule network is a structured network with business rules as the core nodes, and the core nodes are decomposed into conditional sub-nodes and logical relationship edges related to the business rules. The terminology semantic relationship network is a structural network of synonym relationships, hierarchical relationships, and negation exclusion relationships of storage business terms; S43: Use the graph structure network as a graph database for structured data storage, and configure a confidence attribute with dynamically adjusted weights for the corresponding business node data and edge relationship data in the graph database, thereby obtaining a business domain knowledge graph.
4. The Text2SQL generation method based on multi-source knowledge fusion and adaptive progressive processing according to claim 3, characterized in that, S5 specifically includes the following steps: S51: Obtain the natural language query text input by the user, and perform preprocessing operations on the natural language query text to obtain the preprocessed query text Q; and the preprocessing operations include, but are not limited to, text denoising, word segmentation, sensitive word filtering, and business term recognition. S52: Using the same pre-trained word embedding model as in S3, the pre-processed query text is transformed into a text semantic vector; S53: Based on the dynamic experience vector knowledge base, perform full-database semantic similarity retrieval on the text semantic vector to obtain the semantic retrieval result corresponding to the highest similarity score. The semantic retrieval result is the historical SQL sample and the corresponding execution context data. Simultaneously, the complexity of the target database schema corresponding to the natural language query text is obtained based on the enhanced schema semantic library; the quantitative dimensions corresponding to the complexity include the total number of tables in the database, the average number of fields in a single table, the hierarchy of primary and foreign key associations between tables, and the complexity of cross-table associations. S54: Construct an adaptive progressive decision strategy based on the semantic retrieval results and complexity corresponding to the highest similarity score to obtain the query processing path for the natural language query text input by the user. Based on the query processing path, the prompt word assembler is invoked and combined with the preset LLM large language model to generate SQL candidate query statements.
5. The Text2SQL generation method based on multi-source knowledge fusion and adaptive progressive processing according to claim 4, characterized in that, Build an adaptive progressive decision-making strategy in S54 to obtain a query processing path for the received natural language query text of the user input, and call a prompt assembler based on the query processing path to fuse and generate a SQL candidate query statement in combination with a preset large language model (LLM). The specific steps include: S541: Define a preset complexity threshold and obtain the complexity Comp(Schema); If Comp(Schema) < the preset complexity threshold, continue to execute S542; otherwise, execute S543; S542: Obtain the highest similarity score Sim(Q), and judge the numerical relationship between the highest similarity score Sim(Q) and the preset high threshold T_high and the preset low threshold T_low; If Sim(Q) ≥ T_high, use the historical SQL example corresponding to the historical triple data matched based on the highest similarity score Sim(Q) as the current query processing path; and use the historical SQL example and its corresponding execution context data at this time as the SQL candidate query statement; If T_low ≤ Sim(Q) < T_high, retrieve and preprocess the query text Q based on the enhanced Schema semantic library to obtain concise Schema information related to the query semantics; the concise Schema information includes table names, field names, field types, primary and foreign key relationships, and corresponding semantic annotation information; Retrieve business association information related to the preprocessed query text Q based on the business knowledge graph; the business association information includes business rules, cross-table semantic relationships, and term semantic relationships; Retrieve and obtain the top N historical SQL statement examples that are similar to the current query semantics corresponding to the preprocessed query text Q based on the dynamic experience vector knowledge base; Input the concise Schema information, business association information, and historical SQL examples into a preset prompt assembler, and perform prompt assembly based on the preset prompt assembly format to obtain structured prompts, and generate SQL candidate query statements based on the structured prompts to guide the preset large language model (LLM); If Sim(Q) < T_low, continue to execute S543; S543: Build a two-stage generation strategy; the two-stage generation strategy includes a table scope identification and query parsing stage and a SQL generation stage; The table scope identification and query parsing stage: Based on the enhanced Schema semantic library, call the first preset large language model to retrieve and obtain the list of table names tables of the data tables related to the preprocessed query text Q and the corresponding core semantic annotations; use a preset parser to perform semantic parsing on the preprocessed query text Q based on the core semantic annotations to obtain the query parsing text parsed_query; The SQL generation stage involves: obtaining data information related to the table name list (tables) based on the enhanced schema semantic library; the data information includes full field information, semantic annotations, and primary and foreign key relationships; and retrieving historical SQL cases similar to the query parsed_query from the experience vector database based on the query parsed_query, while retrieving business association information related to the query parsed_query from the business knowledge graph. A second pre-set large language model is set up, and data information, historical SQL cases, and business-related information are input into the pre-set prompt word assembler. The prompt words are assembled based on the pre-set prompt word assembly format to obtain structured prompt words. The structured prompt words guide the second pre-set large language model to generate SQL candidate query statements.