Natural language database query method and system based on large-model two-channel context enhancement
By employing a dual-channel context-enhanced NL2SQL method, which utilizes knowledge graphs and schema graphs to parse user intent in parallel, the problem of insufficient semantic understanding and poor domain adaptability in existing technologies is solved, achieving efficient query generation and cross-domain adaptation.
Patent Information
- Application Number
- CN202511759655.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-27
- Publication Date
- 2026-01-16
AI Technical Summary
Existing NL2SQL technology suffers from insufficient semantic understanding, inaccurate database structure mapping, and poor domain adaptability in industrial applications, resulting in low query accuracy, long cross-domain deployment cycles, and low resource utilization.
We adopt a dual-channel context enhancement method based on a large model, which parses user intent in parallel through business links and pattern links. We use knowledge graphs and schema graphs to process query statements at the business and technical levels respectively, and generate SQL statements by combining vectorized fusion and complexity calculation.
It improved query accuracy, shortened the domain adaptation cycle, optimized resource utilization, and enhanced the accuracy and response speed of query generation.
Smart Images

Figure CN121350101A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of artificial intelligence natural language processing and database query technology, and particularly relates to a natural language database query method and system based on large-model dual-channel context enhancement. Background Technology
[0002] NL2SQL technology primarily focused on basic syntax parsing and simple query transformation during the 1960s-1980s. Due to the immaturity of natural language processing technology at the time, coupled with the difficulty in accurately mapping the complex structures of relational databases, technological development remained stagnant for a long period. It wasn't until 2017 that Google's Analyza system pioneered end-to-end semantic parsing based on deep learning, and the subsequent nQuery tool also proposed a hybrid architecture that combined template matching and neural networks.
[0003] However, in practical industrial applications, the existing NL2SQL technology still falls short of the query accuracy requirements of the production environment, mainly in the following three aspects:
[0004] (1) Semantic ambiguity: The same word in natural language has multiple meanings in different business scenarios, and there are phenomena such as omission and reference, which makes it difficult for the system to accurately capture the user's true intention.
[0005] (2) Database complexity: Multi-table JOIN operations require accurate identification of related fields, nested queries require understanding the logical relationship between subqueries and main queries, and also require handling complex SQL syntax such as aggregate functions and grouping conditions, which increases the difficulty of query generation.
[0006] (3) Insufficient domain adaptability: Different industries (such as energy, finance, etc.) have significant differences in table structure characteristics, business term mapping and query logic. Existing NL2SQL technology is difficult to adapt to multi-domain schema design quickly, resulting in long cross-domain deployment cycle and decreased accuracy.
[0007] In addition, issues such as ambiguous expressions in natural language, implicit conditional inference of query intent, and unified understanding of cross-domain terminology have exacerbated the difficulty of implementing NL2SQL technology in industrial scenarios. Summary of the Invention
[0008] Purpose of the invention: This invention provides a natural language database query method and system based on large-model dual-channel context enhancement, aiming to overcome the problems of insufficient semantic understanding, inaccurate database structure mapping, and poor domain adaptability of existing NL2SQL technology in industrial applications.
[0009] Technical solution: This invention provides a natural language database query method based on large-model dual-channel context enhancement, including:
[0010] Retrieve the database and the user's natural language query statement;
[0011] The database table structure is converted into a graph structure to obtain a schema diagram;
[0012] The query statement is processed in parallel with dual-channel connections, including business connections and pattern connections. The business connection inputs the query statement into the knowledge graph, performs business entity matching and relationship retrieval, and outputs business context information. The pattern connection inputs the query statement into the large model, performs preliminary screening and constraint verification based on the schema graph, and obtains the pattern connection information after constraint verification.
[0013] The business context information and the pattern link information are vectorized and fused to obtain combined context information;
[0014] A query intent vector is generated based on combined contextual information. A complexity calculation model is built based on the database. The query intent vector is used as the input to the complexity calculation model, and the complexity label of the query statement is output.
[0015] Based on the complexity label differentiation, a prompt template is generated. The prompt template is input into the large model to generate an SQL statement, which is then executed to return the query results.
[0016] Furthermore, the process of converting the database table structure into a graph structure to obtain a schema diagram includes:
[0017] Read the metadata in the database and extract the physical layer table structure, including table elements, column elements, and foreign keys; construct the physical layer graph structure based on the physical layer table structure, including nodes and connection relationships, converting table elements and column elements into nodes and foreign keys into connection relationships;
[0018] The large model is used to annotate the nodes in two dimensions, constructing description layer annotation and structural feature layer annotation to form a logical layer graph structure.
[0019] The GraphSAGE algorithm is used to embed the physical layer graph structure and the logical layer graph structure into the same vector space to obtain the schema graph.
[0020] Furthermore, the business links include:
[0021] Natural language processing technology is used to semantically analyze the query statement, decompose it into multiple business keywords, and form a business keyword group.
[0022] The obtained business keyword groups are input into the graph database of the knowledge graph. The business keyword groups and the node names in the graph database are vectorized. The text edit distance and cosine similarity between the vectorized business keywords and the graph node names are calculated respectively. The text edit distance and cosine similarity are weighted and summed to obtain a mixture value. The graph database node corresponding to the highest mixture value is taken as the business entity matched by the business keyword. The business entities matched by each business keyword in the business keyword group are combined into a business entity group.
[0023] Starting from each business entity in a business entity group, the shortest path between business entity groups is retrieved from the graph database to obtain the association relationship between business entity groups; business entity paths are then constructed based on the association relationship between business entity groups.
[0024] Based on the business entity path, business context information is generated, and a path connectivity score is calculated. The business context information includes a core entity list, a core relationship list, and a subgraph path description. The core entity list includes entity data types, and the core relationship list includes relationship data types. The path connectivity score is calculated by weighting path matching degree, path relationship confidence, and path completeness. Specifically, the calculation is as follows:
[0025] Let the set of business entities in the business entity path be... ,in These are the business entities in the path, and the relationships between them are as follows: k is the length of the business entity path, the number of associations is k-1, and the corresponding business entity in the database. The table elements or column elements are Using sets Storage; corresponding relationships in the database foreign key , ,..., Using sets storage;
[0026] For each business entity The path matching degree is calculated using the following formula:
[0027] ;
[0028] in, For business entities Metadata in the corresponding database The normalized value of the text edit distance, which takes a value of 0-1, indicates that the texts are more similar; For business entities Metadata in the corresponding database The normalized value of the cosine similarity, taking a value between 0 and 1. Weights for text edit distance;
[0029] The path matching degree is the average of the path matching degrees of each business entity in the business entity path, and the formula is:
[0030] ;
[0031] For each business entity The relationship between them is Based on the retrieval frequency and semantic consistency of relationships in the database, the relationship confidence is calculated using the following formula:
[0032] ;
[0033] in, For association The normalized value of the retrieval frequency in the database, with a value between 0 and 1; The correlations calculated for the large model The corresponding foreign key in the database The normalized value of semantic similarity, with a value between 0 and 1. Weights based on retrieval frequency;
[0034] The path relationship confidence score is the average of the relationship confidence scores of each business entity, and the formula is:
[0035] ;
[0036] Path integrity The calculation formula is:
[0037] ;
[0038] in, The ratio of the number of core relationships. This represents the number of relationships in the business entity path that conform to database foreign key constraints. This represents the total number of associations along the business entity path, i.e., k-1. The number of redundant business entities in the business entity path; The weight is the ratio of the number of core relationships; if there are no foreign key constraints, then... It needs to be multiplied by a penalty factor of 0.5 before proceeding to the next calculation;
[0039] The original path connectivity score is calculated based on path matching degree, path relationship confidence, and path integrity, using the following formula:
[0040] ;
[0041] in, , , Weights for path matching degree, path relationship confidence, and path integrity;
[0042] The path connectivity score is normalized using the Sigmoid function, with the following formula:
[0043] .
[0044] Furthermore, the pattern link includes:
[0045] An inverted index is built for the nodes, attributes, and connections in the schema diagram. The query statement is segmented using the large model to obtain entities and conditions. The initial matching nodes for each segment are quickly located based on the inverted index, and the matching score for each initial matching node is calculated using the following formula:
[0046] ;
[0047] in, For initial matching nodes Match score, For query statement With nodes The normalized value of the direct similarity. Indicates the initial matching node A set of nodes that are connected. Indicates the initial matching node The sum of matching scores for all nodes that have connections; The weighting coefficients for direct similarity;
[0048] Sort the preliminary matching nodes and select the top K preliminary matching nodes to form candidate nodes;
[0049] The candidate nodes and query statements are input into a preset Prompt template. The large model is used to parse and verify the predicted nodes and their connectivity paths during the result generation process by performing foreign key constraint verification, data type matching degree verification, and schema integrity constraint verification. The predicted node with the highest verification score and its connectivity path are output, and these are corresponding to entity-node mapping information, condition-node mapping information, and node connection paths to obtain schema link information. The entity-node mapping information includes entity data types, and the condition-node mapping information includes condition data types.
[0050] The foreign key constraint, data type matching constraint, and schema integrity constraint are calculated in the following manner:
[0051] Let the set of prediction nodes of the large model be . d represents the number of nodes, and the connection path is... o represents the number of connection relationships in the connection path;
[0052] For each node in the node set If a node has a connection relationship, the foreign key constraint of the node is equal to 1; otherwise, it is equal to 0. The foreign key constraints of all nodes are averaged to obtain the foreign key constraints of the large model prediction nodes.
[0053] Based on the data types of the predicted nodes output by the large model for the given conditions and the actual data types of the corresponding columns in the database, the data type matching constraints of each condition in the query statement are calculated using the following formula:
[0054] ;
[0055] Where n is the total number of conditions in the query statement; Let i be the weight of the i-th condition. Let i be the expected data type for the i-th condition. Given the actual data type of the corresponding column in the database, map(x,y) is the type matching mapping function; average the data type matching degree constraints of all conditions to obtain the data type matching degree constraints of the prediction nodes of the large model for the condition output.
[0056] For the predicted nodes and their connection paths in the output of a large model, the mode integrity constraints are calculated using the following formula:
[0057] schema_score ;
[0058] Among them, the number of matched core tables is the number of word segments in which entity-node mapping information can be obtained, the number of matched core columns is the number of word segments in which condition-node mapping information can be obtained, and the number of valid foreign key relationships is the number of connection relationships in the connection path; the number of core tables to be matched is the number of entities in the word segment, the number of core columns to be matched is the number of conditions in the word segment, and the total number of foreign key relationships to be matched is the number of word segments. The weights for the number of core columns that have been matched and the number of core columns that need to be matched. The weights are the number of effective foreign key relationships and the total number of foreign key relationships that need to be matched;
[0059] If the foreign key constraints of the nodes and their connected paths predicted by the large model meet the preset foreign key constraint verification range, and the data type matching degree constraint meets the data type matching degree constraint verification range, and the schema integrity constraint meets the schema integrity constraint verification range, then the nodes and their connected paths predicted by the large model pass the verification.
[0060] Furthermore, the step of vectorizing and fusing the business context information with the pattern linking information to obtain combined context information includes:
[0061] A bidirectional mapping table is established between the connection paths of nodes in the pattern link information and the business entity paths, and conflict detection is performed. If there is an association between entities in the entity business path, but the nodes in the corresponding node's connection path are missing connections, the number of missing connections is counted, and the missing connections are filled using a graph diffusion algorithm. The path connectivity score of the business entity path is then updated. If the nodes in the node's connection path or the entities in the entity business path cannot achieve field alignment, the business context information and the pattern link information are uniformly mapped to homonyms, heteronyms, unit differences, and enumeration encoding differences based on a preset thesaurus, unit conversion rule library, and enumeration value mapping table.
[0062] Based on the multi-dimensional dynamic calculation of the weights of business context information and pattern linking information, the business context information and pattern linking information are vectorized. The combined context information is then obtained by weighting the vectorized business context information and pattern linking information, as shown in the formula:
[0063] ;
[0064] in, Weights for business context information, Weights for pattern link information; The path connectivity score is calculated using the following formula:
[0065] ;
[0066] Where k is the slope coefficient. Score the path connectivity of the entity business path corresponding to the business context information;
[0067] The formula for calculating foreign key constraints, data type matching degree, and schema integrity using weighted averages is:
[0068] ;
[0069] in, Indicates a foreign key constraint. This indicates a data type matching constraint. Representation of schema integrity constraints, The numerical value ranges from 0 to 1.
[0070] Furthermore, the step of generating a query intent vector based on combined contextual information, building a complexity calculation model based on the database, using the query intent vector as input to the complexity calculation model, and outputting the complexity label of the query statement includes:
[0071] Structural features, semantic features, conflict features, and graph features are extracted from the combined context information. Structural features include the path length of the business entity path corresponding to the business context information and the path length of the connected paths of nodes in the pattern link information. Semantic features include the data type entropy calculated based on the data type obtained from the business context information, and the data type entropy calculated based on the data type obtained from the entity-node mapping information and condition-node mapping information in the pattern link information. Conflict features include the foreign key missing rate, which is obtained by calculating the proportion of the number of missing connections to the path length of the node's connected paths. Graph features include the average number of connections of nodes that can be corresponding to the business entity path in the pattern link information.
[0072] Structural features, semantic features, conflict features, and graph features are Z-score standardized to construct a query intent vector;
[0073] Based on the database, historical query statements are obtained, and complexity labels are assigned to the historical query statements. The complexity labels include any one of complex query, medium query, and simple query.
[0074] The complexity calculation model is trained based on the labeled historical query statements. The hyperparameters of the complexity calculation model are iteratively optimized with the goal of minimizing the cross-entropy loss function to obtain the trained complexity calculation model.
[0075] Input the query intent vector into the trained complexity calculation model, and output the predicted complexity probability of the query statement;
[0076] If the predicted complexity probability is greater than the first probability value, the query statement is labeled as a complex statement; if the predicted complexity probability is less than the second probability value, the query statement is labeled as a simple statement; if the predicted complexity probability is between the second probability value and the first probability value, the query statement is labeled as a medium query, and the first probability value is greater than the second probability value.
[0077] Furthermore, the step of generating a prompt template based on the complexity label differentiation, and inputting the prompt template into the large model to generate SQL statements, includes:
[0078] If the complexity label is simple statement, the combined context information and the preset simple sample will be assembled to form a prompt template, and the input of the large model will generate an SQL statement;
[0079] If the complexity label is a complex statement, a query intermediate representation is generated based on the combined context information. Using the combined context and the query intermediate representation as the retrieval key, a hybrid similarity calculation is performed from the pre-built augmented case library through near nearest neighbor retrieval. The cases with the highest hybrid similarity are then formed into an augmented case set. The hybrid similarity calculation is performed by weighted summation of cosine similarity and Jaccard distance, with the weight of cosine similarity being higher than that of Jaccard distance. The combined context information, the query intermediate representation, and the augmented case set are assembled into a prompt template, which is then input into the large model to generate an SQL statement.
[0080] If the complexity label is medium, several medium-complexity case studies are retrieved from the enhanced case library. The combined context information, standardized business context information, standardized pattern link information, and several medium-complexity case studies are assembled into a prompt template, which is then input into the large model to generate SQL statements.
[0081] This invention also provides a natural language database query system based on large-model dual-channel context enhancement, comprising:
[0082] The data acquisition module is used to acquire the database and the natural language query statements input by the user;
[0083] The conversion module is used to convert the table structure of the database into a graph structure, resulting in a schema diagram;
[0084] The linking module is used to perform parallel dual-channel connections on the query statement, including business links and pattern links. The business links input the query statement into the knowledge graph, perform business entity matching and relationship retrieval, and output business context information. The pattern links input the query statement into the large model, perform preliminary screening and constraint verification based on the schema graph, and obtain the pattern link information after constraint verification.
[0085] The fusion module is used to perform vectorized fusion of business context information and the pattern link information to obtain combined context information.
[0086] The complexity determination module is used to generate a query intent vector based on combined context information, build a complexity calculation model based on the database, take the query intent vector as the input of the complexity calculation model, and output the complexity label of the query statement.
[0087] The SQL generation module is used to generate prompt templates based on complexity labels, input the prompt templates into the large model to generate SQL statements, execute the SQL statements and return the query results.
[0088] Beneficial effects: Compared with existing technologies, this invention has advantages in innovative architectural design, breakthrough progress in technical implementation, and improved application performance, specifically:
[0089] The innovative advantages of the architectural design include:
[0090] (1) By using a dual-channel parallel parsing mechanism, the limitations of traditional NL2SQL single-schema linking are overcome. Through the collaborative work of business linking (knowledge graph driven) and schema linking (schema graph driven), a three-dimensional parsing of user intent is achieved. The business linking channel adopts a vectorized entity matching algorithm to solve the problem of mapping industry terminology aliases, which helps to improve the accuracy of path retrieval; the schema linking channel uses a graph-structured schema representation, which helps to improve the efficiency of table association recognition.
[0091] (2) By using a dynamic complexity grading system, unlike the fixed threshold judgment method, the dynamic weight calculation of the fusion vector v is used. ∈[0.7,1], (∈[0,0.3]), to achieve quantitative classification of query intent.
[0092] Technological breakthroughs include:
[0093] (1) This invention pioneered the use of Query Intermediate Representation (QIR) as a semantic buffer layer. By decomposing query elements in a structured manner (such as SELECT / WHERE / GROUP BY), it bridges the semantic gap between natural language and SQL statements. Experiments show that it can improve the accuracy of LLM generation by 25%.
[0094] (2) By using the enhanced sample retrieval mechanism, the pre-computed {Question, Schema, QIR, SQL} quadruple samples are retrieved using ANN. Compared with the traditional {Question, SQL} binary tuples, the semantic understanding depth, context alignment accuracy and complex query accuracy are improved.
[0095] Application performance improvements include:
[0096] (1) In conflict detection, this invention achieves automatic alignment between business logic and database constraints, with a conflict resolution rate of 92%. It ensures system security through logical association deduction (non-physical schema modification) and has conflict resolution capabilities.
[0097] (2) The method and system of the present invention have domain adaptability, and the business link channel supports hot updates of the domain knowledge graph, which shortens the adaptation cycle of the system in different industries by 80%.
[0098] (3) This invention can also optimize resource utilization. By preprocessing the schema diagram structure and pre-enhancing the case library, the average response time of a single query in the system is controlled within 800ms, which reduces the computational overhead by 62% compared with the traditional end-to-end LLM solution. Attached Figure Description
[0099] Figure 1 This is a flowchart of the method of the present invention. Detailed Implementation
[0100] like Figure 1 As shown, the natural language database query method based on large-model dual-channel context enhancement described in this invention includes:
[0101] (1) Obtain the database and the natural language query statement input by the user;
[0102] (2) Convert the table structure of the database into a graph structure to obtain a schema diagram;
[0103] (3) The query statement is executed in parallel with dual-channel connections, including business links and pattern links; the business links input the query statement into the knowledge graph, perform business entity matching and relationship retrieval, and output business context information; the pattern links input the query statement into the large model, perform preliminary screening and constraint verification based on the schema graph, and obtain the pattern link information after verification of constraints.
[0104] (4) The business context information and the pattern link information are vectorized and fused to obtain combined context information;
[0105] (5) Generate query intent vector based on combined context information, build a complexity calculation model based on the database, take the query intent vector as the input of the complexity calculation model, and output the complexity label of the query statement;
[0106] (6) Generate a prompt template based on the complexity label differentiation, input the prompt template into the large model to generate an SQL statement, execute the SQL statement and return the query result.
[0107] In this embodiment,
[0108] Step (1) Obtain the database and the natural language query statement input by the user;
[0109] Step (2) The database includes tables (or table elements), fields (or column elements), and foreign keys. Before converting the table structure of the database into a graph structure, the table structure of the database is annotated in two dimensions, including semantic description layer annotation and structural feature layer annotation, in order to improve the accuracy of subsequent graph modeling and semantic understanding.
[0110] The semantic description layer adds business semantic information to tables and fields in the database to help understand their true meaning; for example, table-level business definitions explain the role of each table in the business process; column-level natural language explanations provide easy-to-understand explanations of fields; and security classification tags identify sensitive fields (such as mobile phone numbers and ID card numbers) for access control and de-identification processing. The structural feature layer annotates tables from the perspective of data structure and query performance; for example, core table tags identify core tables that are frequently used or on critical paths in the business; and index utility scoring evaluates whether fields have indexes and the query efficiency of those indexes, helping to determine the importance of indexes in queries.
[0111] This invention constructs a schema diagram, which is the graph structure of the database, including nodes, attributes, and relationships, targeting tables, fields, foreign key relationships, and optimizing the annotation of tables and fields in the database.
[0112] Converting a database table structure into a graph structure includes:
[0113] (2-1) Read the metadata in the database and extract the physical layer table structure of the database, including table elements, column elements and foreign keys; construct the physical layer graph structure based on the physical layer table structure, including nodes and connection relationships, convert table elements and column elements into nodes, and foreign keys into connection relationships;
[0114] (2-2) Based on semantic description layer annotation and structural feature layer annotation, use a large model to perform dual-dimensional annotation on nodes, construct description layer annotation and structural feature layer annotation, and form a logical layer graph structure; for example, product.id → "product identifier | weight 0.8".
[0115] (2-3) The GraphSAGE algorithm is used to embed the physical layer graph structure and the logical layer graph structure into the same vector space to obtain the schema graph.
[0116] The schema diagram is encoded using a dual-channel diagram structure, with the following architecture:
[0117] graph LR
[0118] A[Physical Schema] -->|Table Structure| B[GraphSAGE Encoding]
[0119] C[Logic Schema] -->|Semantic Tag| B
[0120] B --> D [Unified Embedded Space]
[0121] D --> E [Pattern Linking Engine]
[0122] Where A is the physical schema graph structure, which refers to the physical table structure of the database. It contains metadata information such as the database tables, fields and foreign key relationships. By reading this metadata, table nodes, column nodes and foreign key edges can be generated to form a table-column topology network. The table nodes, column nodes and foreign key edges are then mapped to the nodes, attributes and relationships in the database graph structure.
[0123] B stands for GraphSAGE encoding, a graph neural network algorithm used to embed physical schema graph structures and logical schema graph structures into the same vector space;
[0124] C represents the logical schema graph structure, which is obtained by annotating and optimizing the database table structure. It generates semantic descriptions and feature annotations for nodes and attributes in the schema graph, and provides semantic information for GraphSAGE encoding and embedding.
[0125] D is the joint embedding space, in which the semantic description and feature annotation of the table-column topology network of the physical schema graph structure and the logical schema graph structure are correspondingly integrated into the same graph structure;
[0126] E stands for Schema Linking Engine, which is responsible for handling the association and matching between natural language query statements and schema graphs, and is the core component for implementing schema linking.
[0127] Step (3) uses two parallel channels for context linking, including business linking and pattern linking, to understand the natural language query statement entered by the user from both business and technical perspectives.
[0128] (3-1) Business Links: By parsing the business entities and their relationships in the query statement using a domain knowledge graph, structured business context information and path connectivity scores are obtained, including:
[0129] (3-1-1) Use natural language processing technology to perform semantic parsing on the query statement, decompose it into multiple business keywords, and form a business keyword group;
[0130] Specifically, NLP is used to perform semantic parsing on the query statement, and business keyword groups are obtained through word segmentation. These include multiple business keywords. The word segmenter in NLP is loaded with a domain-specific dictionary containing all business entities, metrics, and process nodes in the knowledge graph, and supports incremental updates to ensure that proprietary business terms are correctly identified during word segmentation and to avoid incorrect segmentation.
[0131] (3-1-2) Input the obtained business keyword group into the graph database of the knowledge graph, and vectorize the business keyword group and the node name in the graph database; calculate the text edit distance and cosine similarity between the vectorized business keyword and the graph node name respectively, and calculate the weighted sum of the text edit distance and cosine similarity to obtain the mixture value. Take the graph database node corresponding to the highest mixture value as the business entity matched by the business keyword, and form a business entity group by matching the business entities of each business keyword in the business keyword group.
[0132] Starting with each business entity in a business entity group, the shortest path (or related subgraph) between business entity groups is retrieved from the graph database to obtain the association relationship between business entity groups; business entity paths are then formed based on the association relationship between business entity groups.
[0133] Specifically, the all-MiniLM-L6-v2 model (v2.2.0) of sentence-transformers is selected to perform entity vectorization processing on business keywords and node names of business entities in the graph database, supporting dual vector generation at the core entity level (such as standard names and aliases) and phrase context level;
[0134] Next, the text edit distance and cosine similarity between the vectorized business keywords and the node names of business entities in the graph database are calculated separately. The text edit distance and cosine similarity are weighted at a ratio of 0.3:0.7 to obtain a mixture value. The graph database node with the highest mixture value calculated with the business keyword is taken as the business entity matched with the business keyword. The business keyword is linked to the business entity matched with the business keyword. The business entities matched by each business keyword in the business keyword group are grouped into business entity groups.
[0135] Finally, starting with multiple business entities in the business entity group, the association relationships of business entities stored in the knowledge graph are used to query and retrieve the shortest path or most relevant subgraph connecting these business entities in the same graph database, obtain the association relationships of the business entity group, and form business entity paths based on the association relationships between business entity groups, thereby understanding the complete business chain behind the user's question, such as from "customer placing an order" to "payment" and then to "delivery".
[0136] (3-1-3) Based on the business entity path, generate business context information and calculate the path connectivity score; the business context information includes a core entity list, a core relationship list, and a subgraph path description; the core entity list includes entity data types, the core relationship list includes relationship data types, and the path connectivity score is calculated by weighting path matching degree, path relationship confidence and path integrity.
[0137] Specifically, the business context information describes the business entities and relationships involved in the problem, including a list of core entities, a list of core relationships, and a description of sub-graph paths, in the following format:
[0138] {Core Entity List: [{Entity ID, Entity Data Type, Name}],
[0139] Core relationship list: [{relationship data type, starting entity ID, ending entity ID}],
[0140] Subgraph path description: (structured text or small graph representation)}
[0141] Based on the entity type information in the business context, calculate the distribution uniformity of the entity data type, i.e., the entity data type entropy. The formula is:
[0142] ;
[0143] Where n is the number of entity data types, The percentage of each entity data type;
[0144] Path connectivity score It is used to quantify the effectiveness and completeness of business entity paths. It is obtained by weighted summation of three indicators: node matching degree, relationship confidence degree, and path completeness, and then normalized to [0,1] by Sigmoid. The higher the score, the better the connectivity.
[0145] Let the set of business entities in the business entity path be... ,in For business entities (such as users and orders) in the path, the relationships between business entities are as follows: (e.g., user-order placement, order placement-order), k is the length of the business entity path, and the path contains k-1 relationships; the metadata set of the corresponding business entity in the database. , Metadata (including tables, fields, foreign key relationships, etc.) of the corresponding business entities in the database; corresponding relationships in the database. foreign key , ,..., Using sets storage;
[0146] For each business entity The path matching degree is calculated using the following formula:
[0147] ;
[0148] in, For business entities Metadata in the corresponding database The normalized value of the text edit distance, which takes a value of 0-1, indicates that the texts are more similar; For business entities Metadata in the corresponding database The normalized value of the cosine similarity, taking a value between 0 and 1. The weight for text editing distance, the default value is 0.3, which can be dynamically adjusted;
[0149] The path matching degree is the average of the path matching degrees of each business entity in the business entity path, and the formula is:
[0150] ;
[0151] For each business entity The relationship between them is Based on the retrieval frequency and semantic consistency of relationships in the database, the relationship confidence is calculated using the following formula:
[0152] ;
[0153] in, For association The normalized value of the retrieval frequency in the database, with a value between 0 and 1; The correlations calculated for the large model The corresponding foreign key in the database The normalized value of semantic similarity, with a value between 0 and 1. The weight for retrieval frequency is 0.5 by default and can be dynamically adjusted.
[0154] The path relationship confidence score is the average of the relationship confidence scores of each business entity, and the formula is:
[0155] ;
[0156] Calculate path integrity , The formula used to determine whether a path conforms to database constraints (such as foreign key relationships) and to avoid breaks or redundancy is:
[0157] ;
[0158] in, The ratio of the number of core relationships. This represents the number of relationships in the business entity path that conform to database foreign key constraints. This represents the total number of associations along the business entity path, i.e., k-1. The number of redundant business entities in the business entity path; The weight of the ratio of the number of core relationships, with a default value of 0.6, prioritizes foreign key constraint matching;
[0159] The original path connectivity score is calculated using the following formula:
[0160] ;
[0161] in, , , Weights can be dynamically adjusted; default is... =0.3、 =0.4、 =0.3;
[0162] If the path length k is 1, meaning a single business entity with no associated relationships, then =1, the default confidence level is the highest when there is no relationship, only calculation is performed. and ;
[0163] If the path is broken, and there is no corresponding foreign key constraint, then Multiply by a penalty factor of 0.5;
[0164] The final path connectivity score is normalized using the Sigmoid function to ensure the output is within the 0-1 range. The formula is as follows:
[0165] ;
[0166] (3-2) Schema linking is based on the schema diagram. Candidate retrieval and constraint verification are performed on the query statement. A large model is used to identify and extract all database table nodes, column nodes, and foreign key edges directly related to the query statement, outputting a technical schema linking information, including:
[0167] (3-2-1) Create inverted indexes for nodes, attributes, and connections in the schema diagram, such as attribute -> node, relationship -> node; use the large model to segment the query statement to obtain entities and conditions, and quickly locate the preliminary matching nodes for each segmented word based on the inverted index, and calculate the matching score for each preliminary matching node, using the following formula:
[0168] ;
[0169] in, The matching score for nodes (table nodes or column nodes). For query statement With nodes The direct similarity, Represents a node The set of neighboring nodes, in the schema diagram, represents the node that was initially matched. A set of nodes that are connected. Represents a node All neighboring nodes Matching score The sum; This is a weighting coefficient for direct similarity, with a value between 0 and 1. It is used to balance the direct relevance of a node to the query statement with the impact of neighbor node score propagation on the node. The contribution of the matching score;
[0170] Sort the preliminary matching nodes and select the top K preliminary matching nodes to form candidate nodes;
[0171] (3-2-2) Input the candidate nodes and query statements into the preset Prompt template, use the large model to parse and perform foreign key constraint verification, data type matching degree verification, and pattern integrity constraint verification on the predicted nodes and node connectivity paths output during the result generation process. Output the predicted node and node connectivity paths that pass the verification and have the highest matching score, and correspond them to entity-node mapping information, condition-node mapping information, and node connection paths to obtain pattern link information; the entity-node mapping information includes entity data types, and the condition-node mapping information includes condition data types.
[0172] The foreign key constraint, data type matching constraint, and schema integrity constraint are calculated in the following manner:
[0173] Let the set of prediction nodes of the large model be . d represents the number of nodes, and the connection path is... o represents the number of connection relationships in the connection path;
[0174] For each node in the node set If a node has a connection relationship, the foreign key constraint of the node is equal to 1; otherwise, it is equal to 0. The foreign key constraints of all nodes are averaged to obtain the foreign key constraints of the large model prediction nodes.
[0175] Based on the data types of the predicted nodes output by the large model for the given conditions and the actual data types of the corresponding columns in the database, the data type matching constraints of each condition in the query statement are calculated using the following formula:
[0176] ;
[0177] Where n is the total number of conditions in the query statement; Let i be the weight of the i-th condition. Let i be the expected data type for the i-th condition. Given the actual data type of the corresponding column in the database, map(x,y) is a type matching mapping function. If x and y are completely identical, such as integer and int, it returns 1; if they are compatible types, such as floating-point number and decimal, it returns 0.7; if they are convertible types, such as date string and DATE, it returns 0.4; if they are completely incompatible, such as text and int, it returns 0. The average of the data type matching constraints of all conditions is taken to obtain the data type matching constraints of the prediction nodes of the large model for the condition output.
[0178] For the predicted nodes and their connection paths in the output of a large model, the mode integrity constraints are calculated using the following formula:
[0179] schema_score ;
[0180] Among them, the number of matched core tables is the number of word segments in which entity-node mapping information can be obtained, the number of matched core columns is the number of word segments in which condition-node mapping information can be obtained, and the number of valid foreign key relationships is the number of connection relationships in the connection path; the number of core tables to be matched is the number of entities in the word segment, the number of core columns to be matched is the number of conditions in the word segment, and the total number of foreign key relationships to be matched is the number of word segments. The weights for the number of core columns that have been matched and the number of core columns that need to be matched. The weights are the number of effective foreign key relationships and the total number of foreign key relationships that need to be matched;
[0181] If the foreign key constraints of the nodes and their connected paths predicted by the large model meet the preset foreign key constraint verification range, and the data type matching degree constraint meets the data type matching degree constraint verification range, and the schema integrity constraint meets the schema integrity constraint verification range, then the nodes and their connected paths predicted by the large model pass the verification.
[0182] The Prompt template is as follows:
[0183] / * Prompt structure for pattern linking * /
[0184] You need to map natural language queries to the database schema. Known:
[0185] {SCHEMA_GRAPH}
[0186] Special note:
[0187] - Tables must be joined via {foreign key path}
[0188] - Column {col} must conform to {business rules}
[0189] Current query: "{USER_QUERY}"
[0190] Please output the JSON result following the steps, including:
[0191] 1. Entity → Table Mapping
[0192] 2. Condition → Column Mapping
[0193] 3. Connection path verification
[0194] The pseudocode for the pattern linking is as follows:
[0195] / / Data structure definition
[0196] Struct SchemaNode {
[0197] string id / / Unique identifier for the node
[0198] int type / / 0 = table node, 1 = column node
[0199] float
[256] embedding / / Embedding vector generated by GraphSAGE
[0200] map<string,float> tags / / Semantic tags and weights
[0201] DataType dtype / / Physical data type
[0202] }
[0203] Struct SchemaEdge {
[0204] string source / / Source node ID
[0205] string target / / Target node ID
[0206] float weight / / Relationship strength [0,1]
[0207] int edge_type / / 1 = foreign key, 2 = semantic association
[0208] }
[0209] / / Main processing flow
[0210] Procedure BuildEnhancedSchema(db_conn):
[0211] / / Phase 1: Physical Schema Extraction
[0212] physical_nodes = []
[0213] For table in db_conn.get_tables():
[0214] / / Table node
[0215] table_node = NewNode(
[0216] id: "table_"+table.name,
[0217] type: 0,
[0218] tags: {} )
[0220] physical_nodes.append(table_node)
[0221] / / Column nodes
[0222] For column in table.columns:
[0223] col_node = NewNode(
[0224] id: "col_"+table.name+"."+column.name,
[0225] type: 1,
[0226] dtype: column.type )
[0228] physical_nodes.append(col_node)
[0229] physical_edges.append(NewEdge(table_node, col_node, 1.0,1))
[0230] / / Phase 2: Logical Semantic Enhancement
[0231] For node in physical_nodes:
[0232] prompt = f"Explain the business meaning of the database entity [{node.id}] and output in JSON format"
[0233] llm_response = LLM_Query(prompt)
[0234] node.tags = ParseTagWeights(llm_response) / / e.g., {"product identifier":0.8}
[0235] node.embedding = GraphSAGE_Encode(node)
[0236] / / Phase 3: Joint Graph Construction
[0237] enhanced_graph = {
[0238] nodes: physical_nodes,
[0239] edges: physical_edges + GenerateSemanticEdges(physical_nodes)
[0240] }
[0241] Return enhanced_graph
[0242] End Procedure
[0243] / / Pattern Linking Engine
[0244] Procedure SchemaLinking(query, schema_graph):
[0245] / / Candidate Search
[0246] candidates = []
[0247] For node in schema_graph.nodes:
[0248] sim = CosineSim(EncodeQuery(query), node.embedding)
[0249] If sim > 0.5:
[0250] candidates.append({node, sim})
[0251] / / Graph diffusion sorting
[0252] ranked_nodes = []
[0253] For candidates:
[0254] score = candidate.sim
[0255] For neighbor in GetNeighbors(candidate.node):
[0256] score += 0.3 * neighbor.sim / / Weight propagation
[0257] ranked_nodes.append({candidate.node, score})
[0258] / / LLM Validation and Correction
[0259] prompt = BuildPrompt(query, schema_graph, ranked_nodes)
[0260] llm_result = LLM_Query(prompt)
[0261] If ValidateWithSchema(llm_result, schema_graph):
[0262] Return llm_result
[0263] Else:
[0264] Return GraphBasedRepair(llm_result, schema_graph)
[0265] End Procedure
[0266] Step (4) Perform vector fusion of the business context information output by the business link channel and the pattern link information output by the pattern link channel to form combined context information.
[0267] (4-1) Establish a bidirectional mapping table between the connection paths of nodes in the pattern link information and the business entity paths, and perform conflict detection. If there is an association between entities in the entity business path, but the nodes in the corresponding node connection path are missing connection relationships, count the number of missing connection relationships, use the graph diffusion algorithm to fill in the missing connection relationships, and update the path connectivity score of the business entity path. If the nodes in the node connection path or the entities in the entity business path cannot achieve field alignment, then according to the preset thesaurus, unit conversion rule library and enumeration value mapping table, perform unified mapping of homonyms, homonyms, unit differences and enumeration encoding differences between the business context information and the pattern link information.
[0268] Specifically, a subgraph matching algorithm is used to map business entity paths (e.g., "customer → order → logistics") to schema diagrams (e.g., customer.order_id = order.id), establishing a bidirectional mapping table. Conflict detection and handling are performed on each mapping pair to achieve collaborative optimization. A pre-defined thesaurus and regular expression matching rules automatically identify the correspondence between different naming conventions (e.g., "customer ID" and "CUST_ID" have different names but the same meaning). A unit conversion rule library resolves unit inconsistencies (e.g., the business uses "ten thousand yuan" while the database stores "yuan"). For enumerated data, an enumeration value mapping table handles encoding differences (e.g., correctly mapping the business expression "gender = male" to the database's "GENDER = M" field), thus ensuring the accuracy of numerical comparisons.
[0269] The collaborative optimization employs a three-layer collaborative mechanism, including a conflict detection and mapping layer, a collaborative execution engine layer, and a feedback calibration layer. The conflict detection and mapping layer establishes a bidirectional mapping relationship between business entity relationship paths and the database schema structure, and identifies structural conflicts during the mapping process. The collaborative execution engine layer transforms graph structure path queries into relational algebra operations based on the mapping results and selects the optimal execution strategy; specifically, it compiles high-confidence business paths (such as the shortest logistics path from "warehouse to customer") into SQL JOIN operations (such as `warehouse JOIN route ON distance`), and selects an execution plan (such as prioritizing HashJoin) based on a cost model (such as index selectivity, join cost, etc.). When the path connectivity score S_{\text{score}} > 0.9, the system determines that the business path is a high-confidence chain and prioritizes its corresponding JOIN strategy to ensure SQL generation. The feedback calibration layer is used to complete missing logical associations and provide feedback on potential structural problems, offering a repair mechanism when paths are broken or mapping fails. If a path is broken due to a missing foreign key (e.g., customer.phone and order.contact have no physical foreign key), the system uses a graph diffusion algorithm (e.g., random walk) to infer potential related nodes in the schema graph, complete the path, and record the inferred logical association in the composite context or QIR (e.g., customer.phone ≈ order.contact). During the SQL generation phase, the system attempts to construct a JOIN condition using this logical association; if it still cannot generate a valid SQL, it provides feedback to the user regarding insufficient association or schema design flaws, suggesting potential optimization directions. To avoid the performance overhead of graph traversal, the system limits the graph diffusion depth (e.g., a maximum of 3 hops) with query latency as a threshold, achieving a balance between completion capability and response efficiency. Through the above three-layer collaborative mechanism, this invention achieves high-precision alignment between business paths and database structures, resolves structural conflicts and semantic discrepancies without modifying the physical schema, and achieves a conflict resolution rate of 92%.
[0270] The pseudocode for the collaborative optimization mechanism is as follows:
[0271] # Conflict Detection Mapping Layer
[0272] def detect_conflict(business_path, schema_graph):
[0273] mapping_table = {}
[0274] conflict_vector = [0, 0]
[0275] for edge in business_path.edges:
[0276] matched = schema_graph.match(edge) # Subgraph matching algorithm
[0277] mapping_table[edge] = matched
[0278] conflict_vector += 1 if matched else 0
[0279] conflict_vector^1^ += check_foreign_key(edge) # Foreign key check
[0280] return normalize(conflict_vector), mapping_table
[0281] # Cooperative Execution Engine Layer
[0282] def query_rewrite(graph_query, threshold=0.9):
[0283] if graph_query.score > threshold:
[0284] sql_ast = compile_to_join(graph_query) # Graph query is compiled into a JOIN
[0285] return optimize_plan(sql_ast) # Optimize based on cost model
[0286] return fallback_to_original()
[0287] # Feedback Calibration Layer
[0288] def path_completion(broken_path, max_hops=3):
[0289] completed_nodes = random_walk(
[0290] start=broken_path[-1],
[0291] stop_condition=lambda n: n in schema_graph,
[0292] max_hops=max_hops )
[0294] return broken_path + completed_nodes
[0295] (4-2) Based on the multi-dimensional dynamic calculation of the weights of business context information and pattern linking information, the business context information and pattern linking information are vectorized. The combined context information is obtained by weighting the vectorized business context information and pattern linking information, as shown in the formula:
[0296] ;
[0297] in, Weights for business context information, The weights are assigned to the pattern linking information; vectorization is performed using L2 normalization to ensure that the business context information and the pattern linking information are in the same vector space, thus avoiding weight failure due to differences in the scale of the original features.
[0298] To ensure the balance of the fusion, it is usually necessary to satisfy... For example: If ( = 0.6、 = 0.4), then the combined vector is more biased towards business semantics; if = 0.3、 If the value is 0.7, then the focus is more on technical constraints.
[0299] Obtained based on path connectivity score, Based on foreign key constraints, data type matching degree, and schema integrity, when the business path is reliable (i.e., ... When the pattern constraint is high, the combined contextual information will retain more business semantic features; when the pattern constraint is strict (i.e., When the complexity is high, the combined context information will further enhance the technical mapping features; by dynamically adjusting the weights, the generated combined context information contains both business intent and conforms to database technical specifications, providing a unified feature basis for subsequent complexity determination and SQL generation.
[0300] The path connectivity score is calculated using the following formula:
[0301] ;
[0302] Where k is the slope coefficient, which is 3 in this embodiment. The path connectivity score is assigned to the entity business path corresponding to the business context information. When the path connectivity score of the business entity path is greater than 0.9, the business entity path is considered highly reliable, and the weight of the path is enhanced by a logarithmic curve growth method.
[0303] The formula for calculating foreign key constraints, data type matching degree, and schema integrity using weighted averages is:
[0304] ;
[0305] in, Indicates a foreign key constraint. This indicates a data type matching constraint. Representation of schema integrity constraints, The numerical range is between 0 and 1. When a foreign key cascading operation is detected, the weight of the pattern constraint is increased, but an upper limit (not exceeding 0.6) is used to avoid excessive bias towards technical factors.
[0306] The pseudocode for the dynamic feature fusion is as follows:
[0307] Business path weight calculation function
[0308] function calculateBusinessWeight(pathScore, confidence,conflictCount):
[0309] baseWeight = 2 / (1 + exp(-pathScore / 3)) - 1 / / Improved Sigmoid function
[0310] conflictFactor = 1 - 0.04 * conflictCount / / Conflict penalty coefficient
[0311] return clamp(baseWeight * confidence * conflictFactor, 0.2, 0.9)
[0312] / / Schema constraint weight calculation function
[0313] function calculateSchemaWeight(fkExists, typeMatch, ruleLevel):
[0314] hardWeight = 0.6 * fkExists * (1 + 0.2 * ruleLevel)
[0315] softWeight = 0.4 * pow(typeMatch, 1.5) / / Non-linear enhancement
[0316] return min(0.8, hardWeight + softWeight)
[0317] / / Dynamic fusion main process
[0318] function dynamicFusion(businessVec, schemaVec):
[0319] / / Get real-time evaluation metrics
[0320] pathScore = getPathScore(businessVec)
[0321] fkStatus = checkForeignKeyConstraints()
[0322] / / Calculate dynamic weights
[0323] alpha_b = calculateBusinessWeight(pathScore, ...)
[0324] alpha_s = calculateSchemaWeight(fkStatus, ...)
[0325] / / High-confidence path enhancement
[0326] if pathScore > 0.9:
[0327] alpha_b *= 1 + log(1 + 10*(pathScore - 0.9))
[0328] / / Foreign key cascade compensation
[0329] if hasCascadeConstraint():
[0330] alpha_s = min(0.6, alpha_s * 1.2)
[0331] / / Generate intent vector
[0332] intentVec = normalize(alpha_b*businessVec + alpha_s*schemaVec)
[0333] return intentVec
[0334] Step (5) Generate a query intent vector based on the combined context information, build a complexity calculation model based on the database, use the query intent vector as the input of the complexity calculation model, and output the complexity label of the query statement, including:
[0335] (5-1) Extract structural features, semantic features, conflict features, and graph features from the combined context information; structural features include the path length of the business entity path corresponding to the business context information and the path length of the connected path of the node in the pattern link information; semantic features include the data type entropy calculated based on the data type obtained from the business context information, and the data type entropy calculated based on the data type obtained from the entity-node mapping information and condition-node mapping information in the pattern link information; conflict features include the foreign key missing rate, which is obtained by calculating the proportion of the number of missing connections to the path length of the node's connected path; graph features include the average number of connections of the nodes that can be corresponding to the business entity path in the pattern link information.
[0336] Structural features, semantic features, conflict features, and graph features are Z-score standardized to construct a query intent vector;
[0337] (5-2) Based on the database, obtain historical query statements and label the historical query statements with complexity tags, the complexity tags including any one of complex query, medium query, and simple query;
[0338] The complexity calculation model is trained based on the labeled historical query statements. The hyperparameters of the complexity calculation model are iteratively optimized with the goal of minimizing the cross-entropy loss function to obtain the trained complexity calculation model.
[0339] Input the query intent vector into the trained complexity calculation model, and output the predicted complexity probability of the query statement;
[0340] Specifically, the complexity calculation model (in this example, a dynamically weighted logistic regression model) is built to meet the needs of the scenario. Its design and training process must be adapted to the complexity determination scenario of natural language database queries. The specific details are as follows:
[0341] By collecting natural language query sample data from actual business scenarios of enterprises, the number of data entries should be between 10,000 and 50,000. Annotation rules are established to label the sample data. These annotation rules are based on the number of tables involved in the query statement, the complexity of the conditions, and the length of the business link. Condition complexity is determined by counting the number of condition combinations or nested logic, and the length of the business link is determined by counting the number of connections between business entities involved in the query statement. Query statements with 1 table and 1 condition combination or nested logic are labeled as simple queries. Query statements with 3 or more connections between business entities and 3 or more condition combinations or nested logic are labeled as complex queries. Query statements between simple and complex queries are labeled as medium queries. For example, querying the total number of operation tickets in 2023 is a simple query; querying a list of users in Beijing whose electricity consumption in the past 30 days exceeded 1000 kWh is a medium query; and statistically analyzing the top 3 types of equipment with the highest failure rates in each province over the past six months and their supplier response times is a complex query. The consistency of the annotations is verified by the Kappa coefficient, which must be ≥0.85.
[0342] To avoid bias in the training of the complexity calculation model towards any one category, the collected sample data is resampled to ensure a balanced ratio, such as 1:1:1. Four-dimensional features are extracted from each sample data, and the sample data after feature extraction is stratified and sampled in a ratio of 7:2:1 to divide the data into training set, validation set, and test set, ensuring that the distribution of the three types of complexity is consistent in each set.
[0343] The complexity calculation model adopts a multi-class logistic regression model (such as Softmax regression). The input is a four-dimensional feature, and the output is a probability distribution of three types of complexity, namely P (simple query), P (medium query), and P (complex query). The dynamic weighting is reflected in the adaptive weighting of different features through training optimization.
[0344] The complexity calculation model is trained on the training set by iteratively optimizing its hyperparameters by minimizing the cross-entropy loss function. The formula for the cross-entropy loss function is:
[0345] ;
[0346] in, The true label for the i-th sample is (e.g., 0 for simple query, 1 for medium query, and 2 for complex query). The complexity calculation model predicts the probability of class c, where N is the number of samples. Training is iterated for 50-200 epochs using the Adam optimizer with an initial learning rate of 0.001. After each epoch, the hyperparameters (such as the learning rate) of the complexity calculation model are dynamically adjusted based on the accuracy of the validation set. L2 regularization is added during training to prevent overfitting; the coefficients of L2 regularization are determined through grid search. If the accuracy of the validation set does not improve for 5 consecutive epochs, an early stopping strategy is triggered, the parameters of the optimal model are saved, and the trained complexity calculation model is obtained.
[0347] The trained complexity calculation model is used to calculate the accuracy and macro-average F1 score on the validation set, and auxiliary calculation metrics are obtained: confusion matrix and false positive rate. The false positive rate of complex queries and simple queries should be ≤5% to avoid errors in subsequent SQL generation strategies due to false positives.
[0348] For medium-sized queries, if the accuracy on the test set is ≥85% and the Macro-F1 score is ≥80%, the trained complexity calculation model is considered to be up to standard. For complex queries, if the accuracy on the test set is ≥85%, the Macro-F1 score is ≥80%, and the recall of complex queries calculated based on the confusion matrix is ≥85%, the trained complexity calculation model is considered to be up to standard. For simple queries, if the accuracy on the test set is ≥85%, the Macro-F1 score is ≥80%, and the precision of simple queries calculated based on the confusion matrix is ≥90%, the trained complexity calculation model is considered to be up to standard.
[0349] (5-3) If the predicted complexity probability is greater than the first probability value, the complexity label of the query statement is determined to be a complex statement; if the predicted complexity probability is less than the second probability value, the complexity label of the query statement is determined to be a simple statement; if the predicted complexity probability is between the second probability value and the first probability value, the complexity label of the query statement is determined to be a medium query, and the first probability value is greater than the second probability value.
[0350] Specifically, a dual threshold of 70% / 40% is set to achieve a three-level judgment of "complex-medium-simple". The complexity calculation model obtains the prediction probability of complex queries. When the prediction probability of complex queries is greater than 70%, the query is judged as complex; when the prediction probability of complex queries is between 40% and 70%, the query is judged as medium; when the prediction probability of complex queries is less than 40%, the query is judged as simple.
[0351] Obtain the prediction probability of the largest category and the prediction probability of the second largest category, calculate the difference as the confidence level, and trigger manual review when the confidence level of the complexity calculation model is less than a preset threshold (such as 15%), and use the review result as the final query complexity label.
[0352] The pseudocode for determining the complexity is as follows:
[0353] # Input: The fused query intent vector v
[0354] # Output: Complexity ranking (complex / medium / easy)
[0355] def classify_complexity(v):
[0356] # Feature Extraction Module
[0357] features = {
[0358] 'path_length': len(v['query_path']),
[0359] 'join_count': count_join_operations(v['sql_ast']),
[0360] 'entity_entropy': calculate_entropy(v['entity_types']),
[0361] 'fk_missing_rate': v['missing_fk'] / max(1, v['total_relations']),
[0362] 'avg_degree': sum(v['node_degrees']) / len(v['node_degrees'])
[0363] }
[0364] # Feature standardization (assuming predefined min / max values)
[0365] normalized = {
[0366] k: (v-feature_ranges[k]['min']) / (feature_ranges[k]['max']-feature_ranges[k]['min'])
[0367] for k,v in features.items()
[0368] }
[0369] # Model calculation (pre-trained weights)
[0370] model_weights = {'path_length':0.3, 'join_count':0.25, ...}
[0371] raw_score = sum(model_weights[k]*normalized[k] for k in features)
[0372] prob = 1 / (1+exp(-raw_score))
[0373] # Hierarchical decision making
[0374] if prob > 0.7:
[0375] return 'complex'
[0376] elif prob > 0.4:
[0377] if check_keywords(v['query_text']): # Degrade to keyword review
[0378] return 'complex'
[0379] return 'medium'
[0380] else:
[0381] return 'easy'
[0382] In step (6), a hint template is generated based on the complexity label differentiation. The hint template is then input into the large model to generate SQL statements, including:
[0383] (6-1) If the complexity label is a simple statement, the combined context information and the preset simple sample will be assembled to form a prompt template, and the input of the large model will generate an SQL statement;
[0384] (6-2) If the complexity label is a complex statement, a query intermediate representation is generated based on the combined context information. The combined context and the query intermediate representation are used as the retrieval keys. The hybrid similarity calculation is performed by retrieving from the pre-built enhanced case library through near nearest neighbor retrieval. The cases with the highest hybrid similarity are obtained and formed into an enhanced sample set. The hybrid similarity calculation is performed by weighted summation of cosine similarity and Jaccard distance. The weight of cosine similarity is higher than that of Jaccard distance. The combined context information, the query intermediate representation and the enhanced sample set are assembled into a prompt template and input into the large model to generate an SQL statement.
[0385] Specifically, the pre-built augmented case library is vectorized using the text-embedding-ada-002 model to construct augmented case units {Question_i, SQL_i, S_i, I_i, Vector_i}, where Question_i is the i-th query statement in the augmented case library, SQL_i is the SQL statement corresponding to the i-th query statement, S_i is the connection relationship between the i-th query statement and the schema graph, I_i is the intermediate query representation obtained by parsing the i-th query statement and its context through the QIR module, and Vector_i is the vectorized representation of the i-th query statement.
[0386] The text-embedding-ada-002 model is used to generate an intermediate query representation based on combined context information. Using the combined context and the intermediate query representation as the retrieval key, a hierarchical and navigable HNSW index is constructed for the Vector_i of all cases using the FAISS engine. Nearest neighbor retrieval is used to retrieve cases from a pre-built enhanced case library and perform hybrid similarity calculations to obtain the cases with the highest hybrid similarity, forming an enhanced sample set. The hybrid similarity calculation primarily uses cosine similarity (weight 0.7) and secondarily uses Jaccard distance (weight 0.3). The combined context information, the intermediate query representation, and the enhanced sample set are assembled into a prompt template, which is then input into the large model to generate an SQL statement.
[0387] When a new question arrives, a query vector is generated using the same text-embedding-ada-002 model. Top-K (K=5) similar cases are retrieved from the FAISS index, and pre-built augmented case units are directly returned, avoiding real-time computation overhead. It supports near-nearest neighbor search with millisecond-level response time, a dynamic incremental update mechanism (automatic index reconstruction triggered by new cases), and direct reuse of pre-computed augmented results during online retrieval, reducing latency by more than 60%. Versioning management ensures data consistency.
[0388] The pseudocode for the enhanced case library ANN retrieval is as follows:
[0389] / / Pre-built Enhanced Case Library
[0390] Procedure BuildEnhancedCaseBase(caseDB):
[0391] enhancedCases ← []
[0392] For each case in caseDB:
[0393] / / Text vectorization
[0394] vector ← text-embedding-ada-002(case.question):ml-citation{ref="3,4" data="citationList"}
[0395] / / Generate pattern links
[0396] schemaLinks ← PromptEngine("Generate SchemaLinks", case.question,case.DB_schema):ml-citation{ref="3" data="citationList"}
[0397] / / Generate QIR representation
[0398] qir ← PromptEngine("Generate QIR", case.question, schemaLinks):ml-citation{ref="3" data="citationList"}
[0399] / / Storage Enhancement Case
[0400] enhancedCase ← {question:case.question, sql:case.sql,
[0401] schema:schemaLinks, qir:qir, vector:vector}
[0402] enhancedCases.append(enhancedCase)
[0403] EndFor
[0404] / / Build FAISS index
[0405] index ← FAISS.BuildHNSWIndex(enhancedCases.vectors):ml-citation{ref="4" data="citationList"}
[0406] Return (enhancedCases, index)
[0407] EndProcedure
[0408] / / Online search process
[0409] Function RetrieveSimilarCases(query, enhancedCases, index, K=5):
[0410] / / Query vectorization
[0411] queryVec ← text-embedding-ada-002(query):ml-citation{ref="4"data="citationList"}
[0412] / / ANN search
[0413] caseIDs ← index.Search(queryVec, K):ml-citation{ref="4" data="citationList"}
[0414] / / Return to Enhanced Case
[0415] result ← []
[0416] For each id in caseIDs:
[0417] result.append(enhancedCases[id])
[0418] EndFor
[0419] Return result
[0420] EndFunction
[0421] The prompt assembly template is as follows:
[0422] # Database Schema Summary (from schema linking results)
[0423] [List the relevant table names, column names and their descriptions (from the annotations in step 1), and foreign key relationships]
[0424] # User query intent parsing (from combined context / QIR)
[0425] [Structured presentation of business logic (such as core business processes / entity relationships), technical mapping (table relationships), complexity assessment criteria, potential conflicts / points to note]
[0426] # Similar Reference Examples (K=5)
[0427] [Example 1: Problem Description (Q1), Schema Summary (S1), Intent Resolution (I1), SQL (SQL1)] ...
[0429] [Example 5: ...]
[0430] # Current query to generate task
[0431] Current user issue: [Q_new]
[0432] Current database schema summary: [S_new]
[0433] Current query intent resolution: [I_new] (or integrated into the intent resolution section above)
[0434] Based on the information above, please generate an accurate SQL statement to query the current database.
[0435] (6-3) If the complexity label is medium, then retrieve several medium statement cases from the enhanced case library, combine the combined context information with the standardized business context information and standardized pattern link information, and assemble the several medium statement cases into a prompt template, input the large model to generate SQL statements;
[0436] Specifically, with the combined context information as the core, it is supplemented with standardized business context information (core entities, relationship paths) and standardized schema link information (table-column mapping, foreign key connections).
[0437] Then, 2-3 similar cases of medium complexity are retrieved from the enhanced case library (instead of 5 cases for complex queries). Case selection focuses on scenarios with "2-3 JOIN operands" and "medium combination of conditions" to avoid introducing too much redundant information. The medium complexity characteristics of the query are clearly marked (such as "involving 2 table joins, including 1 range condition and 1 enumeration condition") to guide the large model to match the generation logic of corresponding difficulty.
[0438] Prompt Template Design
[0439] # Database Schema Summary (Related Tables, Columns, and Foreign Keys)
[0440] [A concise list of core tables and related columns to avoid redundancy]
[0441] # User query intent (medium complexity)
[0442] [Core business logic: such as "querying the distribution of product types with an amount greater than 500 yuan in orders placed in a certain region over the past 3 months"]
[0443] [Technical Constraint: 2-table JOIN, including date range + monetary conditions]
[0444] # Reference Cases (2)
[0445] [Case 1: Problem, Simplified Schema, Corresponding SQL (including 2-table JOIN and multiple conditions)]
[0446] [Case 2: Problem, Simplified Schema, Corresponding SQL (including enumeration condition handling)]
[0447] # Task
[0448] Generate SQL that conforms to the above schema and intent, including necessary JOINs and conditional filtering, and avoid overly complex subqueries.
[0449] Compared to simple queries, medium queries do not rely on fixed examples. Instead, they introduce a small number of similar cases to assist in handling multi-table joins and medium-level condition combinations, thus avoiding template rigidity.
[0450] Compared to complex queries that do not enable full enhanced case library retrieval (such as without QIR), the number of cases is reduced and the structure is simplified, reducing suggestion length and computational overhead. At the same time, it does not force large models to strictly replicate case logic, preserving adaptability to business semantics.
[0451] This strategy of generating prompt templates based on complexity labels and inputting these prompt templates into a large model to generate SQL statements strikes a balance between efficiency and accuracy through appropriate case enhancements and controllable flexibility. It adapts to the business logic depth of queries with varying complexity while avoiding redundant interference introduced by complex cases, ensuring the targeted and efficient generation of SQL.
[0452] Regarding the above technical solution in this embodiment, the experimental process of applying the technical solution to a specific experiment and the technical effect of the technical solution will be described in detail below.
[0453] This invention selects databases from three industries, including e-commerce (15 tables, 386 fields), finance (12 tables, 298 fields), and healthcare (10 tables, 254 fields). All of these databases contain multi-table joins, heterogeneous data types, and business-specific fields, such as SKUs in e-commerce and overdue days in finance.
[0454] We collected 5,000 natural language queries from each industry as sample data. The data was stratified according to complexity: simple queries accounted for 30%, medium queries accounted for 40%, and complex queries accounted for 30%. The sample data included ambiguous expressions (such as average order value, corresponding order amount / number of users), cross-table logic (such as linking user profiles and consumption records), and conflict scenarios (such as business terms not matching table field names).
[0455] This invention performs two-dimensional annotation on databases from three industries. The semantic layer supplements descriptions such as "overdue days: the number of natural days the loan is overdue", and the structural layer marks core tables such as "transaction table". A joint embedding space is generated through GraphSAGE to construct a schema graph.
[0456] Business links load knowledge graphs from various industries (e-commerce includes the "user-order-payment" link, and finance includes the "loan-repayment-overdue" link); pattern links enable graph diffusion sorting (α=0.6) and constraint verification.
[0457] A complexity calculation model was built and trained using historical data from three industries. A dual threshold of 70% / 40% and a 15% confidence level verification threshold were set.
[0458] Import 10,000 historical cases from each industry, build an HNSW index using FAISS, and support complex query ANN retrieval;
[0459] Finally, the natural language query and the constructed schema graph are input into the large language model DeepSeek 3.1.0 to generate SQL statements;
[0460] For 15,000 query statements, SQL statements were generated using this solution and the baseline solution (DeepSeek 3.1.0 only, an end-to-end LLM). After execution, the results were compared with the manually annotated standard answers, and the accuracy rate, i.e. the proportion of correct SQL statements, was calculated.
[0461] Filter out 3000 query statements containing conflicts, such as "gender=male" corresponding to "GENDER=M" in the database and "amount of ten thousand yuan" corresponding to "yuan". Record the number of conflicts that this solution resolves through the thesaurus, unit conversion, and enumeration mapping.
[0462] Record the time taken to adapt this solution to a new industry (such as healthcare) compared to the baseline solution: This solution requires configuring an industry knowledge graph and annotation schema (assisted by automated tools); the baseline solution requires fine-tuning the LLM (full data training), and the differences in the time cycle are compared;
[0463] Record the average response time (including context linking, merging, SQL generation, and execution) for a single query from input to return result.
[0464] The token consumption and computing resource usage (GPU memory / CPU utilization) during the SQL generation process were statistically analyzed, and the overhead differences between this solution and the baseline solution were compared.
[0465] The experimental results are shown in Table 1.
[0466] Table 1 Comparison of experimental results
[0467] index This plan Baseline approach (end-to-end LLM) Improve / Optimize performance LLM generation accuracy 89.50% 71.60% An increase of 25% (89.5% - 71.6% = 17.9%, but due to differences in industry data, the core scenario saw an increase of 25%). Conflict resolution rate 92.30% 41.50% The conflict resolution rate reached 92% (achieved through standardized mapping and collaborative optimization). Domain adaptation cycle 1.5 days (New industry configuration) 7.8 days (LLM fine-tuning) Shortened by 80% ((7.8-1.5) / 7.8≈80%) Single query response time 680ms (average) 1,790ms Controlled within 800ms, a 62% reduction compared to the baseline ((1790-680) / 1790≈62%) Computational overhead Baseline value 1.0 2.63 A decrease of 62% ((2.63-1.0) / 2.63≈62%)
[0468] As can be seen from the table, dual-channel fusion solves the problem of discontinuity in the semantic-schema diagram of the baseline solution. Business links supplement domain knowledge (such as the business definition of "average order value"), and schema links ensure accurate table-column mapping (such as binding "loan_overdue.days" to "overdue days"). The accuracy of complex queries is particularly improved (from 52% in the baseline to 85%).
[0469] By using intelligent mapping mechanisms (matching "customer" and "cust" using a thesaurus and converting units from "ten thousand yuan to yuan") and collaborative optimization (completing logical associations when foreign keys are missing), problems such as naming differences, unit conflicts, and structural mismatches are effectively resolved, with a conflict resolution rate of 92%, which is 50.8 percentage points higher than the baseline (relying on LLM fuzzy inference).
[0470] No need to fine-tune large models; new domains can be adapted simply by configuring industry knowledge graphs and schema annotations (automated tools complete 80% of the annotation work). Baseline solutions require full data fine-tuning (average 7.8 days), shortening the domain adaptation cycle by 80% and significantly improving adaptation efficiency.
[0471] The differentiated strategy reduces unnecessary computation (simple queries use a fixed template, taking 210ms; complex queries reuse pre-computed cases to avoid real-time parsing), combined with FAISS index acceleration (retrieval time < 50ms), the final response time is controlled within 800ms, and the computational overhead is reduced by 62% (due to the reduction of the number of input tokens and inference steps for large models).
[0472] This invention also provides a natural language database query system based on large-model dual-channel context enhancement, comprising:
[0473] The data acquisition module is used to acquire the database and the natural language query statements input by the user;
[0474] The conversion module is used to convert the table structure of the database into a graph structure, resulting in a schema diagram;
[0475] The linking module is used to perform parallel dual-channel connections on the query statement, including business links and pattern links. The business links input the query statement into the knowledge graph, perform business entity matching and relationship retrieval, and output business context information. The pattern links input the query statement into the large model, perform preliminary screening and constraint verification based on the schema graph, and obtain the pattern link information after constraint verification.
[0476] The fusion module is used to perform vectorized fusion of business context information and the pattern link information to obtain combined context information.
[0477] The complexity determination module is used to generate a query intent vector based on combined context information, build a complexity calculation model based on the database, take the query intent vector as the input of the complexity calculation model, and output the complexity label of the query statement.
[0478] The SQL generation module is used to generate prompt templates based on complexity labels, input the prompt templates into the large model to generate SQL statements, execute the SQL statements and return the query results.
[0479] The computer device of the present invention includes a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the steps of the above method.
[0480] The computer-readable storage medium of the present invention stores a computer program thereon, which, when executed by a processor, implements the steps of the above-described method.
Claims
1. A natural language database query method based on large model double-channel context enhancement, characterized in that, The method comprises the following steps: obtaining a database and a natural language query sentence input by a user; converting a table structure of the database into a graph structure to obtain a Schema graph; performing parallel connection of the query sentence through a double-channel connection, including a business link and a schema link; the business link inputs the query sentence into a knowledge graph to perform business entity matching and associated relationship retrieval, and outputs business context information; the schema link inputs the query sentence into a large model to perform preliminary screening and constraint verification based on the Schema graph, and obtains mode link information after verification constraint; vectorizing and fusing the business context information and the schema link information to obtain combined context information; generating a query intention vector based on the combined context information, building a complexity calculation model based on the database, taking the query intention vector as the input of the complexity calculation model, and outputting a complexity label of the query sentence; differentially generating a prompt template according to the complexity label difference, inputting the prompt template into a large model to generate an SQL statement, executing the SQL statement and returning a query result.
2. The natural language database query method based on large model double-channel context enhancement according to claim 1, characterized in that, The conversion of the table structure of the database into the graph structure to obtain the Schema graph comprises the following steps: reading metadata in the database, extracting a physical layer table structure of the database, including table elements, column elements and foreign keys; constructing a physical layer graph structure based on the physical layer table structure, including nodes and connection relationships, converting the table elements and column elements into nodes, and converting the foreign keys into connection relationships; using a large model to perform double-dimension labeling on the nodes to construct description layer labeling and structure feature layer labeling, and forming a logical layer graph structure; embedding the physical layer graph structure and the logical layer graph structure into the same vector space using a GraphSAGE algorithm to obtain the Schema graph.
3. The natural language database query method based on large model double-channel context enhancement according to claim 1, characterized in that, The business link comprises the following steps: using natural language processing technology to perform semantic analysis on the query sentence, decomposing the query sentence into a plurality of business keywords to form a business keyword group; inputting the obtained business keyword group into a graph database of a knowledge graph to perform vectorization processing on the business keyword group and node names in the graph database; calculating a text edit distance and a cosine similarity between the vectorized business keyword and the graph node name, respectively, and weighting and summing the text edit distance and the cosine similarity to obtain a hybrid value; taking the graph database node corresponding to the highest hybrid value as the business entity matched by the business keyword; and grouping the business entities matched by each business keyword in the business keyword group to form a business entity group; taking each business entity in the business entity group as a starting point, retrieving a shortest path between the business entity groups in the graph database to obtain an associated relationship of the business entity group; and grouping the business entity group and the associated relationship of the business entity group to form a business entity path; generating business context information according to the business entity path and calculating a path connectivity score; the business context information comprises a core entity list, a core relationship list and a subgraph path description; the core entity list comprises an entity data type, and the core relationship list comprises a relationship data type; and the path connectivity score is calculated by weighting a path matching degree, a path relationship confidence and a path integrity, and the calculation is specifically as follows: Let the set of business entities in a business entity path be , where is a business entity in the path, and the association relationship between business entities is , k is the length of the business entity path, the number of association relationships is k-1, and the table element or column element corresponding to the business entity in the database is , which is stored using the set ; the foreign key corresponding to the association relationship , ,..., in the database is stored using the set ; For each business entity , the path matching degree is calculated according to the following formula: ; wherein, is the business entity the normalized value of cosine similarity of metadata in the corresponding database, taking values from 0 to 1, the larger the value, the more similar the text is; is the normalized value of text edit distance of metadata in the corresponding database, taking values from 0 to 1, the larger the value, the more similar the text is; is the business entity the normalized value of cosine similarity of metadata in the corresponding database, taking values from 0 to 1, the normalized value of cosine similarity of metadata in the corresponding database, taking values from 0 to 1, is the weight of text edit distance; the path matching degree is an average value of the path matching degrees of the business entities in the business entity path, and the formula is as follows: ; For each business entity The association relationship is Based on the retrieval frequency and semantic consistency in the database, the relationship confidence is calculated, and the formula is: ; wherein, is the association relationship the normalized value of the search frequency in the database, the value being between 0 and 1; is the association relationship calculated by the large model the corresponding foreign key in the database the normalized value of the semantic similarity, the value being between 0 and 1, is the weight of the search frequency; The path relationship confidence is an average value of the relationship confidence of each business entity, and a formula is as follows: ; Path integrity The calculation formula is: ; wherein, is the number of core association relations, is the number of association relations in the business entity path that meet the database foreign key constraint, is the total number of association relations in the business entity path, i.e., k-1; is the number of redundant business entities in the business entity path; is the weight of the number of core association relations; if there is no foreign key constraint, it needs to be multiplied by a penalty coefficient 0.5 before entering the subsequent calculation; The original path connectivity score is calculated according to the path matching degree, the path relationship confidence and the path integrity, and a formula is as follows: ; wherein, , , is a weight of path matching degree, path relationship confidence, path integrity. The path connectivity score is normalized by using a Sigmoid function on the original path connectivity score, and a formula is as follows: 。 4. The natural language database query method based on large model double-channel context enhancement according to claim 1, characterized in that, The mode link includes: An inverted index is established for the nodes, attributes and connection relationships in the Schema graph, a large model is used to perform word segmentation on the query statement to obtain entities and conditions, and the inverted index is used to quickly locate the preliminary matching nodes of each word segmentation, and a matching score of each preliminary matching node is calculated, and a formula is as follows: ; wherein, is a matching score of the preliminary matching node , is a query statement , is a normalized value of the direct similarity of the node , represents a set of nodes having a connection relationship with the preliminary matching node , represents a sum of matching scores of all nodes having a connection relationship with the preliminary matching node is a weight coefficient of the direct similarity. The preliminary matching nodes are sorted, and the first K preliminary matching nodes are selected to form candidate nodes; The candidate nodes and the query statement are input into a preset Prompt template, a large model is used to analyze and perform foreign key constraint verification, data type matching degree verification and mode integrity constraint verification on the predicted nodes and the connected paths of the nodes output in the result generation process, and the predicted nodes and the connected paths of the nodes that pass the verification and have the highest matching score are output, and corresponding entity-node mapping information, condition-node mapping information and node connection paths are obtained to obtain mode link information; the entity-node mapping information includes an entity data type, and the condition-node mapping information includes a condition data type; The foreign key constraint, the data type matching degree constraint and the mode integrity constraint are calculated in the following manner: Let the set of large model prediction nodes be , d is the number of nodes, the connection path is , and o is the number of connection relationships of the connection path. For each node in the node set If the node has a connection relationship, the foreign key constraint of the node is equal to 1, otherwise it is equal to 0. The foreign key constraint of all nodes is averaged to obtain the foreign key constraint of the large model prediction node. According to the data type of the predicted node output by the large model and the actual data type of the corresponding column in the database, the data type matching degree constraint of each condition in the query statement is calculated, and a formula is as follows: ; wherein n is the total number of conditions in the query statement; is the weight of the i-th condition, is the expected data type of the i-th condition, is the actual data type of the corresponding column in the database, and map(x, y) is a type matching mapping function; the data type matching degrees of constraints of all conditions are averaged to obtain the data type matching degree constraint of the prediction node output by the large model for the condition. The mode integrity constraint of the predicted node and the connected path of the node output by the large model is calculated in the following formula: schema_score ; Wherein, the number of matched core tables is the number of the split words that can obtain the mapping information of entity-node, the number of matched core columns is the number of the split words that can obtain the mapping information of condition-node, and the number of effective foreign key relations is the number of connection relations of the connection path; the number of core tables to be matched is the number of entities in the split words, the number of core columns to be matched is the number of conditions in the split words, and the total number of foreign key relations to be matched is the number of split words; the weight of the number of matched core columns and the number of core columns to be matched, the weight of the number of effective foreign key relations and the total number of foreign key relations to be matched; If the foreign key constraint of the node and the connected path of the node predicted by the large model meets the preset foreign key constraint verification range, the data type matching degree constraint meets the data type matching degree constraint verification range, and the mode integrity constraint meets the mode integrity constraint verification range, the node and the connected path of the node predicted by the large model pass the verification.
5. The natural language database query method based on large model double-channel context enhancement according to claim 1, characterized in that, The vector fusion of the business context information and the mode link information to obtain combined context information includes: A bidirectional mapping table between the connection paths of the nodes in the mode link information and the business entity paths is established, and conflict detection is performed, if an associated relationship exists between the entities in the entity business path but a connection relationship between the nodes in the corresponding connection path of the nodes is missing, the number of missing connection relationships is counted, a graph diffusion algorithm is used to complete the missing connection relationships, and the path connectivity score of the business entity path is updated, if the nodes in the connection path of the nodes or the entities in the entity business path cannot be aligned, the same-name different meanings, different-name same meanings, unit differences and enumeration coding differences in the business context information and the mode link information are uniformly mapped according to a preset synonym library, a unit conversion rule library and an enumeration value mapping table. The business context information and the mode link information are vectorized based on weights of multi-dimensional dynamic calculation of the business context information and weights of mode link information, and combined context information is obtained by weighting the vectorized business context information and mode link information, and the formula is: ; wherein, is the weight for the service context information, is the weight for the pattern link information; The path connectivity score is calculated by the formula: ; wherein k is a slope coefficient, a path connectivity score of an entity service path corresponding to the service context information; The foreign key constraint, data type matching degree, and schema integrity are calculated by weighting, and the formula is: ; wherein, represents a foreign key constraint, represents a data type match constraint, represents a schema integrity constraint, the numerical range is between 0-1.
6. The natural language database query method based on large model double-channel context enhancement according to claim 1, characterized in that, The query intention vector is generated based on the combined context information, and a complexity calculation model is built based on the database, the query intention vector is used as an input of the complexity calculation model, and a complexity label of the query statement is output, including: The structural features, semantic features, conflict features and graph features are extracted from the combined context information; the structural features include path lengths of business entity paths corresponding to the business context information and path lengths of connected paths of nodes in the mode link information; the semantic features include data type entropy calculated according to data types obtained from the business context information, and data type entropy calculated according to entity-node mapping information and condition-node mapping information in the mode link information; the conflict features include a foreign key missing rate, which is obtained by calculating a proportion of a number of missing connection relationships to path lengths of connected paths of nodes; and the graph features include an average number of connection relationships of nodes that can be corresponded in the business entity path in the mode link information; The structural features, semantic features, conflict features and graph features are Z-score standardized to construct the query intention vector; Based on the database, historical query statements are obtained, and complexity labels are labeled for the historical query statements, the complexity labels including any one of complex query, medium query and simple query; Based on the labeled historical query statements, the complexity calculation model is trained, and hyperparameters of the complexity calculation model are iteratively optimized to minimize a cross-entropy loss function, to obtain a trained complexity calculation model; The query intention vector is input into the trained complexity calculation model, and a predicted complexity probability of the query statement is output; If the predicted complexity probability is greater than a first probability value, it is determined that the complexity label of the query statement is a complex statement; if the predicted complexity probability is less than a second probability value, it is determined that the complexity label of the query statement is a simple statement; and if the predicted complexity probability is between the second probability value and the first probability value, it is determined that the complexity label of the query statement is a medium query, the first probability value being greater than the second probability value.
7. The natural language database query method based on large model double-channel context enhancement according to claim 1, characterized in that, The prompt template is generated based on the complexity label difference, and the SQL statement is generated by inputting the prompt template into the large model, including: If the complexity label is a simple statement, the combined context information and a preset simple example are assembled to form a prompt template, and the SQL statement is generated by inputting the prompt template into the large model. If the complexity label is a complex sentence, a query intermediate representation is generated based on the combined context information, and the combined context and the query intermediate representation are used as retrieval keys to retrieve a mixed similarity calculation from a pre-built enhanced case library through approximate nearest neighbor retrieval, to obtain a number of cases with the highest mixed similarity, which constitute an enhanced example set; the mixed similarity calculation is obtained by weighted summation of cosine similarity and Jaccard distance, and the weight of the cosine similarity is higher than that of the Jaccard distance; the combined context information, the query intermediate representation and the enhanced example set are assembled into a prompt template, and a large model is input to generate an SQL statement; If the complexity label is a medium sentence, a number of medium sentence cases are retrieved from the enhanced case library, and the combined context information, the standardized business context information and the standardized schema link information, and the number of medium sentence cases are assembled into a prompt template, and a large model is input to generate an SQL statement.
8. A natural language database query system based on large model double-channel context enhancement, characterized in that, Comprise: A data acquisition module for acquiring a database and a natural language query sentence input by a user; A conversion module for converting the table structure of the database into a graph structure to obtain a Schema graph; A linking module for performing double-channel connection on the query sentence in parallel, including business linking and schema linking; the business linking inputs the query sentence into a knowledge graph to perform business entity matching and association relationship retrieval, and outputs business context information; the schema linking inputs the query sentence into a large model to perform preliminary screening and constraint verification based on the Schema graph, and obtains schema link information after verification constraint; A fusion module for vectorizing and fusing the business context information and the schema link information to obtain combined context information; A complexity judgment module for generating a query intention vector based on the combined context information, and building a complexity calculation model based on the database, taking the query intention vector as the input of the complexity calculation model, and outputting a complexity label of the query sentence; A SQL generation module for differentiating the generation of a prompt template according to the complexity label, inputting the prompt template into a large model to generate an SQL statement, executing the SQL statement and returning a query result.
9. A computer device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, The processor executes the computer program to realize the steps of the method of any one of claims 1 to 7.
10. A computer-readable storage medium having stored thereon a computer program, characterized in that, The computer program is executed by the processor to realize the steps of the method of any one of claims 1 to 7.
Citation Information
Patent Citations
Database query method and device, electronic equipment and nonvolatile storage medium
CN119226315A
Multi-table SQL (Structured Query Language) generation method and device based on large model and metadata knowledge graph
CN119719145A
RAG enhanced Text-to-SQL query method and system for large-scale database environment
CN120492494A
Intelligent SQL (Structured Query Language) generation system based on multistage intention recognition and generation method thereof
CN120910240A
Zinc Finger Proteins For Use In Treatment Of Charcot-Marie-Tooth Disease And Method Of Expressing The Same
KR1020250126990A
Cited By
Database sub-mode division-based txt-to-sql generation method
CN121636549A
Method for generating database query by natural language
CN121880369A
Database exception query processing method, system and equipment
CN122112044A
A database exception query processing method, system and device
CN122112044B
