SQL (Structured Query Language) statement generation method and device based on knowledge base retrieval, equipment and medium
By constructing a knowledge graph and a vector database, and combining knowledge graph information with knowledge information to fuse prompt words, more accurate SQL query statements are generated, solving the problem of insufficient ability of existing tools to generate complex queries, and realizing flexible and efficient SQL queries.
Patent Information
- Application Number
- CN202511035172.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-25
- Publication Date
- 2025-10-31
AI Technical Summary
Existing SQL generation tools or services mainly rely on predefined templates or rules, which have limited ability to generate and optimize complex queries, and lack flexibility and comprehensiveness.
By constructing a knowledge graph database and a vector database, and combining knowledge graph information with knowledge information to fuse prompt words, more accurate SQL query statements are generated.
It improves the accuracy and flexibility of SQL queries, can handle complex queries, supports multi-entity relational queries, and avoids the semantic limitations and efficiency bottlenecks of traditional methods.
Smart Images

Figure CN120873004A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of intelligent decision-making technology, and in particular to a method, apparatus, device, and medium for generating SQL statements based on knowledge base retrieval. Background Technology
[0002] With the advent of the big data era, the explosive growth of data volume has made data management and analysis a major challenge for enterprises and organizations. This has led to the emergence of SQL statements as a standard language for querying and managing large amounts of data. SQL is crucial for the efficient retrieval, updating, insertion, and deletion of data, and it is also applicable to various industries. For example, in the healthcare field, SQL statements can be used to query patients' historical diagnosis records, medication information, and test results in the created electronic medical record table. In the fintech field, SQL statements can be used to query abnormal transaction patterns in the created transaction table in real time, such as logins from different locations and high-frequency small-amount transfers.
[0003] Currently, many database management systems offer a wealth of features and tools to assist in SQL writing, but these tools often focus on specific scenarios or functions and lack comprehensiveness and flexibility. In addition, existing SQL generation tools or services mainly rely on predefined templates or rules, which have limited ability to generate and optimize complex queries. Summary of the Invention
[0004] This invention provides a method, apparatus, device, and medium for generating SQL statements based on knowledge base retrieval. By processing and analyzing knowledge graph information and knowledge information, and fusing the two with prompt words, more accurate SQL query statements are generated.
[0005] Firstly, a method for generating SQL statements based on knowledge base retrieval is provided, including: Acquire multi-source knowledge data from the knowledge base, and construct a knowledge graph database based on the multi-source knowledge data; The multi-source knowledge data is vectorized, and a vector database is constructed based on the vectorization results; The pre-acquired original query language is converted into query vectors, and the similarity of the query vectors is calculated through the vector database. Based on the similarity calculation results, the similarity knowledge information with the highest similarity is selected. The original query language is retrieved using the knowledge graph database to obtain relevant graph knowledge information. The similarity knowledge information with the highest similarity and the related graph knowledge information are fused with prompt words to obtain the fusion result; The target SQL statement is generated by combining the fusion result and the original query language.
[0006] Secondly, a knowledge base-based SQL statement generation device is provided, comprising: The acquisition module is used to acquire multi-source knowledge data of the knowledge base and construct a knowledge graph database based on the multi-source knowledge data; The vectorization module is used to vectorize the multi-source knowledge data and construct a vector database based on the vectorization results. The conversion module is used to convert the pre-acquired raw query language into query vectors; The calculation and filtering module is used to calculate the similarity of the query vector through the vector database, and filter out the similarity knowledge information with the highest similarity based on the similarity calculation results; The retrieval module is used to perform relevant graph knowledge retrieval on the original query language through the knowledge graph database to obtain relevant graph knowledge information; The fusion module is used to fuse the similarity knowledge information with the highest similarity and the related graph knowledge information with prompt words to obtain the fusion result.
[0007] The generation module is used to generate a target SQL statement based on the fusion result and the original query language combination.
[0008] Thirdly, a computer device is provided, including 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 aforementioned SQL statement generation method based on knowledge base retrieval.
[0009] Fourthly, a computer-readable storage medium is provided, which stores a computer program that, when executed by a processor, implements the steps of the aforementioned SQL statement generation method based on knowledge base retrieval.
[0010] The aforementioned scheme, implemented by a knowledge base-based SQL statement generation method, apparatus, computer device, and storage medium, acquires multi-source knowledge data and constructs a knowledge graph database. The integration of multi-source data (such as text and databases) ensures broad knowledge coverage and avoids the information limitations of a single data source. Simultaneously, the graph structure provides a visual relationship chain for subsequent reasoning, enhancing the interpretability of knowledge retrieval. Vectorization transforms unstructured knowledge into dense numerical vectors, capturing semantic relationships within the text and addressing the semantic gap problem of traditional keyword matching. The vector database utilizes algorithms such as cosine similarity and Euclidean distance to achieve rapid nearest neighbor search of massive amounts of data, meeting real-time query requirements. Furthermore, it transforms the original query into a vector, enabling the system to match knowledge based on semantics rather than literal meaning, improving the accuracy of user queries. For queries involving "multi-entity associations," graph retrieval can quickly traverse along graph relationship paths, avoiding the semantic limitations of vector retrieval. The combination of semantic accuracy in vector retrieval and relational completeness in graph retrieval avoids the shortcomings of a single method. Attached Figure Description
[0011] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the description of the embodiments of the present invention will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0012] Figure 1 This is a schematic diagram of an application environment for a knowledge base retrieval-based SQL statement generation method according to an embodiment of the present invention; Figure 2 This is a flowchart illustrating a method for generating SQL statements based on knowledge base retrieval in one embodiment of the present invention. Figure 3 This is a schematic diagram of a knowledge base retrieval-based SQL statement generation device according to an embodiment of the present invention; Figure 4 This is a schematic diagram of the structure of a computer device according to one embodiment of the present invention; Figure 5 This is another structural schematic diagram of a computer device according to one embodiment of the present invention. Detailed Implementation
[0013] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0014] This invention provides a method for generating SQL statements based on knowledge base retrieval, which can be applied to applications such as... Figure 1 In this application environment, the client communicates with the server via a network. The server can acquire multi-source knowledge data from a knowledge base and construct a knowledge graph database based on the multi-source knowledge data; vectorize the multi-source knowledge data and construct a vector database based on the vectorization results; convert the pre-acquired original query language into query vectors, calculate the similarity of the query vectors through the vector database, and filter out the knowledge information with the highest similarity based on the similarity calculation results; retrieve related graph knowledge from the original query language through the knowledge graph database to obtain related graph knowledge information; fuse the knowledge information with the highest similarity and the related graph knowledge information with prompt words to obtain a fusion result; generate a target SQL statement based on the fusion result and the original query language, and feed the target SQL statement back to the client. This invention provides an SQL statement generation device based on knowledge base retrieval, which, for target SQL statement business, processes and analyzes knowledge graph information and knowledge information, and fuses the two with prompt words to generate a more accurate SQL query statement. The client can be, but is not limited to, various personal computers, laptops, smartphones, tablets, and portable wearable devices. The server can be implemented using a standalone server or a server cluster consisting of multiple servers. The invention will be described in detail below through specific embodiments.
[0015] Please see Figure 2 As shown, Figure 2 A flowchart illustrating a method for generating SQL statements based on knowledge base retrieval, provided in an embodiment of the present invention, includes the following steps: S1. Obtain multi-source knowledge data from the knowledge base and construct a knowledge graph database based on the multi-source knowledge data.
[0016] In this embodiment of the invention, "acquisition" refers to collecting raw data through ETL tools, API interfaces, web crawlers, manual input, and other means, while "construction" refers to processing multi-source raw data into a knowledge graph database.
[0017] Specifically, the process begins by collecting raw knowledge (multi-source knowledge data) from multi-dimensional data sources, such as table-related information (table creation statements, etc.), unstructured SQL knowledge (including statement performance, function keyword application strategies, optimization techniques, etc.), and knowledge from other domains. Then, through data processing, knowledge extraction, and graph modeling, the scattered information is transformed into a structured and interconnected knowledge graph database.
[0018] In specific healthcare scenarios, knowledge such as disease symptoms, test results, and treatment plans can be linked together. For example, by using the relationship chain of "pneumonia → common symptoms → cough + fever," doctors can be assisted in quickly matching case characteristics.
[0019] In fintech scenarios, we integrate asset, industry, and macroeconomic knowledge, such as "new energy stocks → policy support → raw material prices," to provide clients with personalized investment advice; we analyze the matching degree of clients' risk preferences and product attributes, such as conservative investors → bond funds → 5% annualized return, to improve the accuracy of our services.
[0020] In this embodiment of the invention, constructing a knowledge graph database based on the multi-source knowledge data includes: Extract key entities and relationships between entities from the multi-source knowledge data; The key entities are converted into nodes of the graph database according to the preset knowledge graph structure, and the relationships between the entities are converted into edges of the nodes; Add attributes to the nodes and edges respectively to obtain node attributes and edge attributes; A knowledge graph database is generated based on the nodes, node attributes, edges, and edge attributes.
[0021] In this embodiment of the invention, the extraction refers to identifying and extracting entities with semantic value and the relationships between entities from multi-source knowledge data; the transformation refers to converting the extracted key entities and relationships into a node and edge structure that can be recognized by the graph database; the attribute addition refers to adding descriptive attributes to nodes and edges to enhance the semantic expressive power of the knowledge graph; and the generation refers to integrating nodes, node attributes, edges, and edge attributes according to the graph database format to form a knowledge graph database that can be stored and queried.
[0022] Specifically, multi-source knowledge data is preprocessed, including noise removal, format standardization, and data type splitting. Then, NLP technology is used to extract entities from the text, such as identifying entities like "diabetes", "aspirin", and "blood routine examination". Relationships between entities are extracted through rule matching (such as keywords like "cause", "belong to", and "related to") or machine learning models.
[0023] Furthermore, the extracted key entities and relationships are converted into a node and edge structure recognizable by the graph database. Each independent entity corresponds to a node, and the node name is the standardized name of the entity, such as type 2 diabetes instead of the abbreviation type 2 sugar. For example, the entity "hypertension" is converted into a node, and the node label is "disease". The relationship between entities is converted into an edge between nodes. The edge needs to define the direction and type. For example, the relationship "diagnosis and treatment" between "doctor" and "patient" is converted into a directed edge, and the edge label is "diagnosis and treatment relationship". Following the data model of the graph database (such as the attribute graph model), the entity-relationship structure is mapped into a triple of "node-edge-node".
[0024] Furthermore, feature information is added to nodes, such as the attributes of the diabetes node in the medical field, pathogenesis: insulin resistance, high-risk population: middle-aged and elderly people; detailed information is added to the relationship edges, such as the attributes of the edge "diabetes → trigger → diabetic foot", association strength: high, incidence probability: 20%; finally, all node, edge and attribute data are merged, data integrity (such as whether there are nodes without attributes) and consistency are checked, a database is created using a graph database management system, the data is stored according to the graph structure, and indexes are created for node attributes to improve graph query efficiency.
[0025] In this embodiment of the invention, entities and relationships are extracted from massive amounts of data, irrelevant information is ignored, and the complexity of data processing is reduced; entities are converted into nodes and relationships into edges, which naturally supports "node-edge-node" association queries, making it more suitable for handling complex network relationships than traditional relational databases; and scattered knowledge elements are stored in a graph database format to form a standardized knowledge base that supports shared access by multiple systems.
[0026] In this embodiment of the invention, a knowledge graph database is constructed from multi-source knowledge data. Erroneous data is eliminated through entity disambiguation and relationship verification, and data integrity is improved through attribute completion, laying an accurate foundation for subsequent analysis.
[0027] S2. Vectorize the multi-source knowledge data and construct a vector database based on the vectorization results.
[0028] In this embodiment of the invention, unstructured / semi-structured knowledge data is converted into a high-dimensional numerical vector that can be computed by a computer.
[0029] Specifically, by using machine learning models to encode the semantic features of data into points in a vector space, data with similar semantics are closer together in the vector space, and complex data is compressed into vectors of fixed dimensions, thus preserving core semantic features while reducing computational complexity.
[0030] In healthcare settings, vectorizing patients' electronic medical records (symptoms, examination results, medication history) allows for rapid retrieval of similar case databases. For example, by inputting a vector for an elderly patient with chest pain and a history of diabetes, the database can match the best treatment plans and prognoses in the past, assisting doctors in developing treatment plans.
[0031] In financial scenarios, customer behavior data (transaction frequency, IP address, device fingerprint) is vectorized. For example, when the cosine similarity between a transaction vector and a historical fraud transaction vector exceeds a threshold, a risk warning is automatically triggered.
[0032] In this embodiment of the invention, the step of vectorizing the multi-source knowledge data and constructing a vector database based on the vectorization results includes: The multi-source knowledge data is cleaned to obtain cleaned data, and the cleaned data is divided into multiple data segments; Each data segment is transformed into a vector to obtain a high-dimensional dense vector corresponding to the data segment. A vector database is generated based on the data fragments and the high-dimensional dense vectors.
[0033] In this embodiment of the invention, the vector transformation refers to the process of converting data fragments from their original form into high-dimensional dense numerical vectors, and the generation refers to storing the vectors of all data fragments and their metadata in a dedicated database, and establishing an efficient index to form an underlying data structure that supports fast semantic retrieval.
[0034] Specifically, noise removal, error correction, and format standardization are performed on multi-source knowledge data to improve data quality. Large-scale data is broken down into smaller blocks suitable for vector transformation. Text data is divided into sentences, paragraphs, or fixed number of characters. Image data is segmented by region. Data with time series is divided into fixed time windows or event nodes.
[0035] Furthermore, each data segment is transformed into a vector. Text data uses pre-trained models such as BERT and RoBERTa, image data uses convolutional neural networks to extract visual features, and time-series data is processed using LSTM / Transformer. The vectors of the data segments are associated with metadata for storage. A distributed storage architecture is used to support the storage and management of massive vectors. A high-dimensional indexing algorithm is adopted to divide the vector space into multiple subspaces to accelerate similarity retrieval. Index parameters are optimized for different scenarios, and finally a vector database is constructed.
[0036] In this embodiment of the invention, data fragments are transformed into vectors to give unstructured data semantic computability, supporting similarity search, compressing data dimensions while retaining core features, and reducing storage and computing costs; the generated vector database supports millisecond-level retrieval of millions of vectors, has dynamic expansion capabilities, and supports real-time data insertion and updates.
[0037] In this embodiment of the invention, the vector database can receive new data in real time and dynamically update the vector index to ensure the timeliness of the analysis results.
[0038] S3. The pre-acquired original query language is converted into a query vector. The similarity of the query vector is calculated using the vector database. Based on the similarity calculation results, the similarity knowledge information with the highest similarity is selected.
[0039] In this embodiment of the invention, the transformation refers to mapping the user-input text query (original query language) into a high-dimensional numerical vector through natural language processing technology, and the similarity calculation refers to calculating the degree of similarity between the query vector and the knowledge vector in the vector database through mathematical metrics.
[0040] Specifically, the semantic intent of natural language is transformed into a numerical representation that can be computed by a computer. The similarity between the query vector and the knowledge vector in the vector database is calculated using mathematical metrics. Cosine similarity and Euclidean distance methods can be used to calculate the similarity between the two.
[0041] In a specific healthcare scenario, a doctor inputs a query for a 65-year-old patient with chest pain and difficulty breathing, along with possible causes. The NLP model transforms the query into a vector containing semantic features such as "elderly," "chest pain," "difficulty breathing," and "cause." The vector database retrieves historical case vectors and calculates cosine similarity.
[0042] In a financial context, an analyst inputs the debt default risk of a new energy company and queries vectors to extract semantic features of "new energy," "debt," "default," and "risk," distinguishing between different dimensions of "liquidity risk" and "credit risk." The vector database then retrieves vectors from the company's historical financial reports, industry research reports, and public opinion texts.
[0043] In this embodiment of the invention, the step of converting the pre-acquired raw query language into a query vector includes: The pre-acquired raw query language is subjected to text normalization processing to obtain a normalized text language; The semantic structure of the standard text language is parsed to obtain the parsed semantic structure; The parsed semantic structure is encoded to obtain the query vector.
[0044] In this embodiment of the invention, the text normalization process refers to standardizing, cleaning, and formatting the original query language to eliminate noise, ambiguity, or inconsistency in the text and make it conform to a unified standard. The semantic structure parsing refers to analyzing the semantic structure of the normalized text through natural language processing technology, extracting key entities, relationships, and intents, and transforming the text into a logical structure that a computer can understand. The encoding refers to transforming the parsed semantic structure into a high-dimensional numerical vector so that the text information can be processed by a computer through mathematical operations.
[0045] Specifically, text normalization of the pre-acquired original query language refers to text cleaning, standardization of format, word segmentation and normalization of the original query language, extraction of entities from the normalized text language, parsing of sentence grammatical structure, determination of the role of entities in sentences, and judgment of the user's query purpose. For example, "symptoms of diabetes" belongs to information consultation, while "buying Ping An Insurance" belongs to transaction intent.
[0046] Furthermore, individual words are mapped into low-dimensional dense vectors, and the vector distance reflects the semantic similarity of words. A deep learning model is used to encode the semantics of the entire text, generating vectors containing contextual information. The encoded vectors are then mapped to the same semantic space as the knowledge base, ensuring that the query vectors are comparable to the data vectors in the knowledge base.
[0047] In this embodiment of the invention, the step of calculating the similarity of the query vector through the vector database and filtering out the similarity knowledge information with the highest similarity based on the similarity calculation results includes: The knowledge vectors that are similar to the query vector are found using the index of the vector database; The similarity between the query vector and each vector in the knowledge vector is calculated to obtain the similarity calculation result between the query vector and each vector. The highest similarity score is selected from the similarity calculation results to obtain the highest similarity result; The similarity knowledge information of the highest similarity is determined based on the highest similarity result.
[0048] In this embodiment of the invention, the search refers to quickly locating a set of candidate vectors that may be similar to the query vector in a massive knowledge vector database through the built-in index structure of the vector database. The highest score filtering refers to calculating the similarity score between each candidate vector in the index-returned set and the query vector, sorting them from high to low scores, and filtering out the result with the highest score. The determination refers to querying the original knowledge information associated with the knowledge vector corresponding to the highest similarity score, thus completing the mapping from vector space to semantic information.
[0049] Specifically, vector databases pre-build approximate nearest neighbor indexes for knowledge vectors. Common types include: LSH (Locality Sensitive Hash): mapping similar vectors to the same hash bucket and quickly filtering candidates through hash collisions; HNSW (Hierarchical Navigable Small World Graph): constructing a graph-structured index and quickly approximating the target vector through node jumps; KD-tree: suitable for low-dimensional vectors, recursively finding nearest neighbors through spatial partitioning. In query truncation, the database inputs the query vector into the index structure, and the index returns the K closest candidate vectors in the topological structure according to distance metric rules (such as cosine similarity, Euclidean distance).
[0050] Furthermore, for each candidate knowledge vector, the similarity with the query vector is calculated using a preset distance metric formula. Cosine similarity, Euclidean distance, and inner product similarity distance are used to sort the similarity scores of all candidate vectors. The highest score is selected according to the requirements, and low-scoring vectors are filtered out. In this process, each knowledge vector in the vector database is indexed and associated with the original knowledge data (such as text content and metadata). This is usually stored in the form of "vector ID - data pointer". After finding the ID of the highest-scoring vector, the corresponding original knowledge information is located through the pointer.
[0051] In this embodiment of the invention, semantic structure parsing is performed on standard text language to go beyond surface vocabulary and capture users' real needs; unstructured text is transformed into structured data, which facilitates integration with knowledge graphs or databases; traditional keyword matching cannot handle synonyms or semantic extensions, while vector encoding can achieve deep matching through semantic spatial distance, thereby improving query accuracy.
[0052] In this embodiment of the invention, the bottlenecks of efficiency and accuracy in traditional retrieval are solved, and the evolution of fields such as medicine and finance from passive query to active knowledge recommendation is promoted. For example, when a doctor enters symptoms, the system can actively push the diagnostic path with the highest similarity; when a financial analyst studies a company, the system automatically associates the risk event vectors of similar companies to realize intelligent decision support.
[0053] S4. Retrieve relevant graph knowledge from the original query language using the knowledge graph database to obtain relevant graph knowledge information.
[0054] In this embodiment of the invention, the related graph knowledge retrieval refers to retrieving graph structure knowledge related to the query from a knowledge graph database by starting with the original query language and through entity associations and semantic logic.
[0055] Specifically, by leveraging the structured relationships of knowledge graphs, machines can understand the "connections between knowledge" just like humans do. For example, when a user queries "applications of artificial intelligence," traditional search might return documents containing "artificial intelligence," while graph search would return complete relationship paths such as "artificial intelligence - medical diagnosis - image recognition" and "artificial intelligence - finance - fraud detection."
[0056] In this embodiment of the invention, the step of retrieving relevant graph knowledge information from the original query language through the knowledge graph database to obtain relevant graph knowledge information includes: Extract the language entities and relationships between the language entities from the original query language; The language entities are mapped to nodes in the knowledge graph database, and the relationships between the language entities are mapped to edges in the knowledge graph database; Subgraph structure retrieval is performed on the mapped entity nodes according to the mapping relationship edges to obtain subgraph data; Structured knowledge extraction is performed on the subgraph data to obtain relevant graph knowledge information.
[0057] In this embodiment of the invention, the extraction refers to using a named entity recognition model to identify entities such as proper nouns and concepts from the query, and using a relation extraction algorithm to identify semantic associations between entities. The mapping refers to matching the extracted entities and relations to specific nodes and edges in the knowledge graph database to establish a correspondence between natural language and structured knowledge. The subgraph structure retrieval refers to retrieving subgraph structures containing these nodes and their associations in the knowledge graph using the mapped entity nodes and relation edges as clues. The structured knowledge extraction refers to converting the retrieved subgraph data (a set of nodes and edges) into structured knowledge information.
[0058] Specifically, named entity recognition models are used to identify proper nouns, concepts, and other entities from queries. For example, in the query "Einstein's birthplace", the entities "Einstein" and "birthplace" are extracted. When identifying semantic relationships between entities through relation extraction algorithms, for example, in the query "diabetes causes retinopathy", the relation "causes" is extracted to connect the entities "diabetes" and "retinopathy".
[0059] Furthermore, entity mapping requires entity disambiguation and entity linking to resolve ambiguity between entities with the same name (e.g., "apple" refers to a fruit or a company). By judging the context and graph entity attributes, if there is no corresponding entity in the graph, entity creation can be triggered or a missing entity can be indicated. During relation mapping, the extracted natural language relations are mapped to predefined edge types in the graph. For fuzzy relations, the specific relation type is inferred based on the graph semantic network.
[0060] Furthermore, during subgraph structure retrieval, the subgraph is first constructed, that is, the length of the retrieval relation chain is set with the mapped entity node as the center or the two ends of the relation edge as the starting point, and the retrieval is only carried out along the specified relation edge, excluding irrelevant relations; then, starting from the starting point, the adjacent nodes are expanded layer by layer, which is suitable for querying direct association relations, and the retrieval is carried out according to the predefined relation path pattern.
[0061] Furthermore, the retrieved subgraph data (a set of nodes and edges) is converted into structured knowledge information. Specifically, redundant nodes are removed, core relationship chains are retained, and the subgraph is converted into "entity-relationship-entity" triples to form relevant graph knowledge information.
[0062] In this embodiment of the invention, natural language entities are matched to unique nodes in the graph to avoid ambiguity; relations are mapped to predefined edges to unify semantic expression; and core related knowledge is quickly located and redundant data is reduced by limiting the type of relation edges and the traversal depth.
[0063] In this embodiment of the invention, relevant graph knowledge is retrieved through a knowledge graph database. The retrieval results are presented in the form of "entity-relationship-entity" triples or subgraph structures, which can be directly used for intelligent question answering, decision support, or data visualization without the need for secondary structuring processing.
[0064] S5. The similarity knowledge information with the highest similarity and the related graph knowledge information are fused with prompt words to obtain the fusion result.
[0065] In this embodiment of the invention, the prompt word fusion refers to integrating highly similar knowledge information obtained through vector retrieval with relevant graph knowledge information obtained through knowledge graph retrieval into unified prompt words according to specific rules.
[0066] Specifically, two different forms of knowledge are transformed into prompts that the AI model can understand. For example, textual information is extracted into key facts, and graph structure information is transformed into relational descriptions. After merging, they form a complete prompt. The fused prompts are usually natural language text, which may contain factual statements, relational constraints, or question guidance. They are directly used as input to the large language model to drive the model to generate responses that meet the requirements.
[0067] In this embodiment of the invention, the step of fusing the similarity knowledge information with the highest similarity and the related graph knowledge information with prompt words to obtain a fusion result includes: Extract the knowledge element set of the similarity knowledge information with the highest similarity and the knowledge element set of the related graph knowledge information respectively to obtain the similarity knowledge element set and the graph knowledge element set; The similarity knowledge element set and the graph knowledge element set are repeatedly merged, and the merged element set is structurally reorganized to obtain reorganized structured knowledge. The reorganized structured knowledge is fused with the preset query intent to obtain the fusion result.
[0068] In this embodiment of the invention, the extraction refers to identifying and extracting key knowledge units from highly similar knowledge information and graph knowledge information to form a structured element set; the duplication merging refers to merging duplicate or semantically equivalent elements in the similarity knowledge element set and the graph knowledge element set, retaining unique and valid information; the structural reorganization refers to reorganizing the merged element set according to semantic logic or a preset framework to form a hierarchical and structured knowledge representation; and the fusion refers to semantically matching and integrating the reorganized structured knowledge with the user's query intent to generate the final prompt words or answer materials.
[0069] Specifically, NLP techniques are used to extract entities, attributes, and events from text. For example, from the text "Einstein was born in Germany in 1879", the entity "Einstein" and the attributes "birth year = 1879" and "birth place = Germany" are extracted. Nodes and edges are traversed from the knowledge graph subgraph to extract triples and node attributes.
[0070] Furthermore, by comparing entities and relations in different sets, equivalence is determined through word vectors or rules. Different attribute values of the same entity are merged. For example, "birth year = 1879" and "death year = 1955" are merged into the "Einstein" node. Equivalent relation edges are merged. For example, "birthplace-Ulm" and "born in-Ulm" are merged into one relation.
[0071] Furthermore, when restructuring the merged set of elements, the structure is selected based on the query type, and entities, relations, and attributes are connected according to their dependencies. For example, "Einstein → birthplace → Ulm → country → Germany" forms a chain structure, and the merged elements are restructured as: "{entity: Einstein, relation: birthplace, target entity: Ulm, attribute: country = Germany}".
[0072] Furthermore, the intent classification model identifies the user's query target, filters and reorganizes relevant elements in the knowledge according to intent, and finally assembles them into a prompt word template.
[0073] In this embodiment of the invention, the knowledge element set is extracted and irrelevant information is filtered out, retaining only the knowledge atoms related to the query, thus reducing the amount of data. At the same time, text and graph structure knowledge are transformed into standardized elements to prepare for subsequent merging. Repeated merging avoids contradictions caused by duplicate information, reduces redundant elements, and makes subsequent processing more efficient. The reorganized chain structure can be directly used for logical deduction, improving multi-hop question answering capabilities.
[0074] In this embodiment of the invention, high similarity knowledge information and related graph knowledge information are fused with prompt words, so that the facts in the text and the entity attributes in the graph are mutually calibrated, reducing the error of a single data source; at the same time, the prompt words are structured, reducing generation bias.
[0075] S6. Generate the target SQL statement by combining the fusion result and the original query language.
[0076] In this embodiment of the invention, the combined generation refers to combining the fusion result with the semantic intent of the original query language and transforming it into an SQL statement executable by a relational database.
[0077] In this embodiment of the invention, the intent and key entities of the original query are parsed, and the query conditions are supplemented by knowledge elements in the fusion results (such as "diabetes → complications → retinopathy"). The semantic elements are mapped to SQL tables, fields and query conditions to generate executable statements.
[0078] Specifically, the system extracts entities, query types, and constraints from the original query, extracts supplementary information from the fusion results, determines the table based on the entity type, identifies the fields corresponding to entity attributes, converts the relationships and attributes in the fusion results into WHERE clauses, adds implicit conditions, sorts the data according to business rules, and generates a complete SQL statement.
[0079] As can be seen, in the above scheme, for the target SQL statement business, multi-source knowledge data of the knowledge base is obtained, and a knowledge graph database is constructed based on the multi-source knowledge data; the multi-source knowledge data is vectorized, and a vector database is constructed based on the vectorization results; the pre-obtained original query language is converted into query vectors, and the similarity of the query vectors is calculated through the vector database, and the similarity knowledge information with the highest similarity is selected based on the similarity calculation results; the original query language is retrieved for related graph knowledge through the knowledge graph database to obtain related graph knowledge information; the similarity knowledge information with the highest similarity and the related graph knowledge information are fused with prompt words to obtain a fusion result; the target SQL statement is generated by combining the fusion result and the original query language; by processing and analyzing the knowledge graph information and knowledge information, and fusing the two with prompt words, a more accurate SQL query statement is generated.
[0080] It should be understood that the sequence number of each step in the above embodiments does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.
[0081] In one embodiment, a knowledge base-based SQL statement generation apparatus is provided, which corresponds one-to-one with the knowledge base-based SQL statement generation method described in the above embodiments. For example... Figure 3 As shown, this SQL statement generation device based on knowledge base retrieval includes an acquisition and construction module 101, a vectorization module 102, a transformation module 103, a calculation and filtering module 104, a retrieval module 105, a fusion module 106, and a generation module 107. Detailed descriptions of each functional module are as follows: The acquisition module 101 is used to acquire multi-source knowledge data of the knowledge base and construct a knowledge graph database based on the multi-source knowledge data; The vectorization module 102 is used to vectorize the multi-source knowledge data and construct a vector database based on the vectorization results. The conversion module 103 is used to convert the pre-acquired raw query language into a query vector; The calculation and filtering module 104 is used to perform similarity calculation on the query vector through the vector database, and filter out the similarity knowledge information with the highest similarity based on the similarity calculation results; The retrieval module 105 is used to perform relevant graph knowledge retrieval on the original query language through the knowledge graph database to obtain relevant graph knowledge information; The fusion module 106 is used to fuse the similarity knowledge information with the highest similarity and the related graph knowledge information with prompt words to obtain a fusion result.
[0082] The generation module 107 is used to generate a target SQL statement based on the fusion result and the original query language combination.
[0083] In one embodiment, the acquisition construction module 101, when constructing a knowledge graph database based on the multi-source knowledge data, is used for: Extract key entities and relationships between entities from the multi-source knowledge data; The key entities are converted into nodes of the graph database according to the preset knowledge graph structure, and the relationships between the entities are converted into edges of the nodes; Add attributes to the nodes and edges respectively to obtain node attributes and edge attributes; A knowledge graph database is generated based on the nodes, node attributes, edges, and edge attributes.
[0084] In one embodiment, when the vectorization module 102 vectorizes the multi-source knowledge data and constructs a vector database based on the vectorization results, it is used to: The multi-source knowledge data is cleaned to obtain cleaned data, and the cleaned data is divided into multiple data segments; Each data segment is transformed into a vector to obtain a high-dimensional dense vector corresponding to the data segment. A vector database is generated based on the data fragments and the high-dimensional dense vectors.
[0085] In one embodiment, the conversion module 103, when converting the pre-acquired raw query language into a query vector, is used to: The pre-acquired raw query language is subjected to text normalization processing to obtain a normalized text language; The semantic structure of the standard text language is parsed to obtain the parsed semantic structure; The parsed semantic structure is encoded to obtain the query vector.
[0086] In one embodiment, when the calculation and filtering module 104 performs similarity calculation on the query vector through the vector database and filters out the similarity knowledge information with the highest similarity based on the similarity calculation result, it is used to: The knowledge vectors that are similar to the query vector are found using the index of the vector database; The similarity between the query vector and each vector in the knowledge vector is calculated to obtain the similarity calculation result between the query vector and each vector. The highest similarity score is selected from the similarity calculation results to obtain the highest similarity result; The similarity knowledge information of the highest similarity is determined based on the highest similarity result.
[0087] In one embodiment, when the retrieval module 105 performs a related graph knowledge retrieval on the original query language through the knowledge graph database to obtain related graph knowledge information, it is used to: Extract the language entities and relationships between the language entities from the original query language; The language entities are mapped to nodes in the knowledge graph database, and the relationships between the language entities are mapped to edges in the knowledge graph database; Subgraph structure retrieval is performed on the mapped entity nodes according to the mapping relationship edges to obtain subgraph data; Structured knowledge extraction is performed on the subgraph data to obtain relevant graph knowledge information.
[0088] In one embodiment, when the fusion module 106 fuses the similarity knowledge information with the highest similarity and the related graph knowledge information using prompt words to obtain the fusion result, it is used to: Extract the knowledge element set of the similarity knowledge information with the highest similarity and the knowledge element set of the related graph knowledge information respectively to obtain the similarity knowledge element set and the graph knowledge element set; The similarity knowledge element set and the graph knowledge element set are repeatedly merged, and the merged element set is structurally reorganized to obtain reorganized structured knowledge. The reorganized structured knowledge is fused with the preset query intent to obtain the fusion result.
[0089] This invention provides an SQL statement generation device based on knowledge base retrieval. For a target SQL statement business, it acquires multi-source knowledge data from a knowledge base and constructs a knowledge graph database based on the multi-source knowledge data. The multi-source knowledge data is vectorized, and a vector database is constructed based on the vectorization results. The pre-acquired original query language is converted into query vectors, and the similarity of the query vectors is calculated using the vector database. The knowledge information with the highest similarity is selected based on the similarity calculation results. Related graph knowledge is retrieved from the original query language using the knowledge graph database to obtain related graph knowledge information. The highest similarity knowledge information and the related graph knowledge information are fused using prompt words to obtain a fusion result. The target SQL statement is generated by combining the fusion result with the original query language. By processing and analyzing the knowledge graph information and the knowledge information, and fusing the two with prompt words, a more accurate SQL query statement is generated.
[0090] For specific limitations regarding the SQL statement generation device based on knowledge base retrieval, please refer to the limitations of the SQL statement generation method based on knowledge base retrieval mentioned above, which will not be repeated here. Each module in the aforementioned SQL statement generation device based on knowledge base retrieval can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in or independent of the processor in a computer device in hardware form, or stored in the memory of a computer device in software form, so that the processor can call and execute the operations corresponding to each module.
[0091] In one embodiment, a computer device is provided, which may be a server, and its internal structure diagram may be as follows: Figure 4 As shown, the computer device includes a processor, memory, network interface, and database connected via a system bus. The processor provides computing and control capabilities. The memory includes non-volatile and / or volatile storage media and internal memory. The non-volatile storage media stores the operating system, computer programs, and database. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The network interface is used to communicate with external clients via a network connection. When the computer program is executed by the processor, it implements the server-side functions or steps of a knowledge base-based SQL statement generation method.
[0092] In one embodiment, a computer device is provided, which may be a client, and its internal structure diagram may be as follows: Figure 5 As shown, the computer device includes a processor, memory, network interface, display screen, and input devices connected via a system bus. The processor provides computational and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The network interface is used to communicate with an external server via a network connection. When executed by the processor, the computer program implements client-side functions or steps of a knowledge base-based SQL statement generation method.
[0093] In one embodiment, a computer device is provided, including 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 perform the following steps: Acquire multi-source knowledge data from the knowledge base, and construct a knowledge graph database based on the multi-source knowledge data; The multi-source knowledge data is vectorized, and a vector database is constructed based on the vectorization results; The pre-acquired original query language is converted into query vectors, and the similarity of the query vectors is calculated through the vector database. Based on the similarity calculation results, the similarity knowledge information with the highest similarity is selected. The original query language is retrieved using the knowledge graph database to obtain relevant graph knowledge information. The similarity knowledge information with the highest similarity and the related graph knowledge information are fused with prompt words to obtain the fusion result; The target SQL statement is generated by combining the fusion result and the original query language.
[0094] In one embodiment, a computer-readable storage medium is provided having a computer program stored thereon, the computer program performing the following steps when executed by a processor: Acquire multi-source knowledge data from the knowledge base, and construct a knowledge graph database based on the multi-source knowledge data; The multi-source knowledge data is vectorized, and a vector database is constructed based on the vectorization results; The pre-acquired original query language is converted into query vectors, and the similarity of the query vectors is calculated through the vector database. Based on the similarity calculation results, the similarity knowledge information with the highest similarity is selected. The original query language is retrieved using the knowledge graph database to obtain relevant graph knowledge information. The similarity knowledge information with the highest similarity and the related graph knowledge information are fused with prompt words to obtain the fusion result; The target SQL statement is generated by combining the fusion result and the original query language.
[0095] It should be noted that the functions or steps that can be implemented by the computer-readable storage medium or computer device described above can be referred to the relevant descriptions on the server side and client side in the foregoing method embodiments. To avoid repetition, they will not be described one by one here.
[0096] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium. When executed, the computer program can include the processes of the embodiments of the above methods. Any references to memory, storage, databases, or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), dual data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), RAMbus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.
[0097] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the above-described division of functional units and modules is used as an example. In practical applications, the above functions can be assigned to different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above.
[0098] The above-described embodiments are only used to illustrate the technical solutions of the present invention, and are not intended to limit them. If any software tools or components other than those of our company appear in the embodiments, they are merely illustrative examples and do not represent actual use. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention, and should all be included within the protection scope of the present invention.
Claims
1. A method for generating SQL statements based on knowledge base retrieval, characterized in that, include: Acquire multi-source knowledge data from the knowledge base, and construct a knowledge graph database based on the multi-source knowledge data; The multi-source knowledge data is vectorized, and a vector database is constructed based on the vectorization results; The pre-acquired original query language is converted into query vectors, and the similarity of the query vectors is calculated through the vector database. Based on the similarity calculation results, the similarity knowledge information with the highest similarity is selected. The original query language is retrieved using the knowledge graph database to obtain relevant graph knowledge information. The similarity knowledge information with the highest similarity and the related graph knowledge information are fused with prompt words to obtain the fusion result; The target SQL statement is generated by combining the fusion result and the original query language.
2. The SQL statement generation method based on knowledge base retrieval as described in claim 1, characterized in that, The construction of a knowledge graph database based on the multi-source knowledge data includes: Extract key entities and relationships between entities from the multi-source knowledge data; The key entities are converted into nodes of the graph database according to the preset knowledge graph structure, and the relationships between the entities are converted into edges of the nodes; Add attributes to the nodes and edges respectively to obtain node attributes and edge attributes; A knowledge graph database is generated based on the nodes, node attributes, edges, and edge attributes.
3. The SQL statement generation method based on knowledge base retrieval as described in claim 1, characterized in that, The step of vectorizing the multi-source knowledge data and constructing a vector database based on the vectorization results includes: The multi-source knowledge data is cleaned to obtain cleaned data, and the cleaned data is divided into multiple data segments; Each data segment is transformed into a vector to obtain a high-dimensional dense vector corresponding to the data segment. A vector database is generated based on the data fragments and the high-dimensional dense vectors.
4. The SQL statement generation method based on knowledge base retrieval as described in claim 1, characterized in that, The process of converting the pre-acquired raw query language into query vectors includes: The pre-acquired raw query language is subjected to text normalization processing to obtain a normalized text language; The semantic structure of the standard text language is parsed to obtain the parsed semantic structure; The parsed semantic structure is encoded to obtain the query vector.
5. The SQL statement generation method based on knowledge base retrieval as described in claim 1, characterized in that, The step of calculating the similarity of the query vector using the vector database and filtering out the similarity knowledge information with the highest similarity based on the similarity calculation results includes: The knowledge vectors that are similar to the query vector are found using the index of the vector database; The similarity between the query vector and each vector in the knowledge vector is calculated to obtain the similarity calculation result between the query vector and each vector. The highest similarity score is selected from the similarity calculation results to obtain the highest similarity result; The similarity knowledge information of the highest similarity is determined based on the highest similarity result.
6. The SQL statement generation method based on knowledge base retrieval as described in claim 1, characterized in that, The step of retrieving relevant graph knowledge information from the original query language using the knowledge graph database to obtain relevant graph knowledge information includes: Extract the language entities and relationships between the language entities from the original query language; The language entities are mapped to nodes in the knowledge graph database, and the relationships between the language entities are mapped to edges in the knowledge graph database; Subgraph structure retrieval is performed on the mapped entity nodes according to the mapping relationship edges to obtain subgraph data; Structured knowledge extraction is performed on the subgraph data to obtain relevant graph knowledge information.
7. The SQL statement generation method based on knowledge base retrieval as described in claim 1, characterized in that, The step of fusing the similarity knowledge information with the highest similarity score and the related graph knowledge information with prompt words to obtain the fusion result includes: Extract the knowledge element set of the similarity knowledge information with the highest similarity and the knowledge element set of the related graph knowledge information respectively to obtain the similarity knowledge element set and the graph knowledge element set; The similarity knowledge element set and the graph knowledge element set are repeatedly merged, and the merged element set is structurally reorganized to obtain reorganized structured knowledge. The reorganized structured knowledge is fused with the preset query intent to obtain the fusion result.
8. A SQL statement generation device based on knowledge base retrieval, characterized in that, include: The acquisition module is used to acquire multi-source knowledge data of the knowledge base and construct a knowledge graph database based on the multi-source knowledge data; The vectorization module is used to vectorize the multi-source knowledge data and construct a vector database based on the vectorization results. The conversion module is used to convert the pre-acquired raw query language into query vectors; The calculation and filtering module is used to calculate the similarity of the query vector through the vector database, and filter out the similarity knowledge information with the highest similarity based on the similarity calculation results; The retrieval module is used to perform relevant graph knowledge retrieval on the original query language through the knowledge graph database to obtain relevant graph knowledge information; The fusion module is used to fuse the similarity knowledge information with the highest similarity and the related graph knowledge information with prompt words to obtain the fusion result. The generation module is used to generate a target SQL statement based on the fusion result and the original query language combination.
9. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the SQL statement generation method for human knowledge base retrieval as described in any one of claims 1 to 7.
10. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the SQL statement generation method based on knowledge base retrieval as described in any one of claims 1 to 7.
Citation Information
Cited By
Potential customer list mining method and device, equipment and storage medium
CN121456030A