A knowledge graph-based large model context semantic retrieval method
By extracting entities and intents, using vectorized knowledge retrieval and KG subgraph queries, and combining multiple source databases, the semantic alignment problem between knowledge graphs and large language models was solved, achieving efficient and accurate knowledge question answering.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- XIAN KAIBO HAICHENG TECHNOLOGY CO LTD
- Filing Date
- 2026-04-07
- Publication Date
- 2026-07-03
Smart Images

Figure CN122332418A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of large language model technology, specifically to a large model context semantic retrieval method based on knowledge graphs. Background Technology
[0002] Existing technologies propose combining knowledge graphs with large language models, relying on the explicit structured knowledge of knowledge graphs to fill the knowledge gaps of large language models. This combination method is mainly based on the Retrieval Enhanced Generation (RAG) framework, that is, providing query-related context fragments to large language models through external knowledge bases to help the models generate accurate answers.
[0003] However, existing fusion schemes of knowledge graphs and large language models still have significant drawbacks: A semantic gap exists, as there is a lack of direct semantic alignment between the continuous vector representations of large language models and the discrete structural representations of knowledge graphs, leading to comprehension discrepancies when used together. Simultaneously, there is a disconnect between retrieval targets; the objective functions of existing retrieval modules are inconsistent with the core KG substructure target required by large language models for precise matching of user query semantics, resulting in insufficient relevance of retrieval results. Furthermore, there is an imbalance between efficiency and accuracy; while knowledge graph queries can obtain precise structured knowledge, their high computational complexity makes them unsuitable for real-time retrieval, and while vector retrieval is fast, it cannot guarantee the factual accuracy of the results. The retrieval results of existing RAG frameworks mostly originate from knowledge graphs and cannot be integrated with real-time updated business databases, resulting in an silo effect between static knowledge and dynamic business data. This leads to answers generated by large language models lacking timeliness and domain detail, failing to meet industrial-grade knowledge-based question-answering requirements.
[0004] In view of this, we propose a large-scale model contextual semantic retrieval method based on knowledge graphs. Summary of the Invention
[0005] The purpose of this invention is to provide a large-scale model contextual semantic retrieval method based on knowledge graphs to solve the problems mentioned in the background art.
[0006] To achieve the above objectives, the present invention provides the following technical solution: a large-scale model contextual semantic retrieval method based on knowledge graphs, comprising the following steps: Step S1: Entity and Intent Extraction. The natural language query input by the user is input into the pre-trained large language model. The deep semantic analysis module of the pre-trained large language model performs semantic parsing on the natural language query, extracts the key entity information and the user's core query intent in the natural language query, and obtains the structured semantic representation result. Step S2, Vectorized Knowledge Retrieval: The key entity information and core user query intent obtained in Step S1 are transformed into high-dimensional continuous vectors through a pre-trained graph embedding model. These high-dimensional continuous vectors are input into a vector database and subjected to cosine similarity matching with the pre-stored knowledge graph embedding vectors in the vector database. Knowledge graph fragments highly relevant to the user's query semantics are selected in descending order of similarity as preliminary retrieval results. The knowledge graph embedding vectors stored in the vector database are obtained by pre-training the graph embedding model with the entities, relations, and attributes of the knowledge graph. Step S3: KG subgraph query construction. Based on the preliminary search results obtained in step S2, and combined with the context information of the user's original natural language query, the preliminary search results are filtered, integrated and reconstructed through a subgraph reconstruction algorithm to generate KG subgraph query statements that can accurately express the knowledge context and entity relationships required for knowledge question answering of large language models. The KG subgraph query statements are structured query statements adapted to knowledge graph retrieval. Step S4: Multi-source database SQL conversion and context acquisition. A bidirectional mapping relationship is pre-established between knowledge graph instances and multi-source heterogeneous business databases. This mapping relationship includes correspondences between knowledge graph entities and database tables, knowledge graph relationships and database fields, and knowledge graph attributes and database field values. Based on this mapping relationship, the KG subgraph query statement generated in step S3 is converted into a structured query language (SQL) adapted to the multi-source heterogeneous business databases using an SQL compilation module. The SQL is then input into the corresponding business databases to execute the query and obtain real-time business context data related to the user query. Step S5, Final Prompt Construction and Knowledge Question Answering: The user's original natural language query, the preliminary search results of Step S2, and the real-time business context data of Step S4 are fused and spliced according to a preset Prompt template to construct a fused Prompt containing all knowledge constraints and contextual information. The fused Prompt is input into a pre-trained large language model, and the answer generation module of the pre-trained large language model performs knowledge question answering reasoning to generate an accurate answer to the user's natural language query.
[0007] Optionally, in step S1, the deep semantic analysis module includes an entity recognition layer, an intent classification layer, and a semantic fusion layer; the entity recognition layer uses a named entity recognition algorithm to annotate natural language queries and extract key entities; the intent classification layer uses a pre-trained text classification model to classify the intent of natural language queries and determine the user's core query intent; the semantic fusion layer fuses key entities and core query intent to generate a structured semantic representation result containing entity intent relationships.
[0008] Optionally, in step S2, the graph embedding model is one or more combinations of TransE, TransR, Node2Vec, or GraphSAGE, and the training samples of the graph embedding model are triple data of the knowledge graph, wherein the triple data includes head entity, relation, and tail entity.
[0009] Optionally, in step S2, the threshold for cosine similarity matching is 0.7 to 0.9, and knowledge graph nodes and associated edges with similarity greater than or equal to the threshold are selected to form the knowledge graph fragment.
[0010] Optionally, in step S3, the subgraph reconstruction algorithm includes the following steps: S31. Assign weights to the knowledge graph nodes in the preliminary search results, where the weights are the semantic relevance of the nodes to the user's core query intent; S32. Remove redundant nodes and associated edges whose weights are lower than the preset threshold; S33. Based on the topology and entity relationships of the knowledge graph, complete the paths for the remaining nodes and edges to form a connected KG subgraph; S34. Convert the KG subgraph into a structured KG subgraph query statement adapted for knowledge graph retrieval.
[0011] Optionally, in step S4, the multi-source heterogeneous business database includes a relational database, a non-relational database, and a data warehouse. The relational database is at least one of MySQL and Oracle, the non-relational database is at least one of MongoDB and Redis, and the data warehouse is at least one of Hive and ClickHouse.
[0012] Optionally, the bidirectional mapping relationship mentioned in step S4 is stored in a mapping database, which supports dynamic updates. When the structure of the knowledge graph or business database changes, the bidirectional mapping relationship is updated synchronously.
[0013] Optionally, in step S5, the preset Prompt template includes a user query area, a knowledge graph retrieval area, and a business data area. The user query area is used to fill in the user's original natural language query, the knowledge graph retrieval area is used to fill in the preliminary retrieval results of step S2, and the business data area is used to fill in the real-time business context data of step S4.
[0014] Optionally, the method further includes an update step for the knowledge graph and the vector database: when the triple data of the knowledge graph is added, modified or deleted, the updated knowledge graph embedding vector is regenerated through the graph embedding model, and the updated graph embedding vector is synchronized to the vector database to realize real-time linkage update of the knowledge graph and the vector database.
[0015] Compared with existing technologies, this invention provides a large-scale model contextual semantic retrieval method based on knowledge graphs, which has the following beneficial effects: 1. This knowledge graph-based large-scale model contextual semantic retrieval method constructs a deep alignment representation between structured and natural language. Through a graph embedding model, it transforms the discrete structure representation of the knowledge graph into a continuous vector representation, unifying it with the vector representation system of the large language model. Simultaneously, it vectorizes the entities and intents of the user query and performs similarity matching, achieving semantic soft alignment between KG triples and natural language queries. This enables the large language model to perceive the topological structure and semantic constraints of the knowledge graph, reducing reliance on statistical associations and significantly reducing factual errors.
[0016] 2. This knowledge graph-based large-scale model contextual semantic retrieval method uses the user's core query intent as the control variable. It optimizes the initial retrieval results through a subgraph reconstruction algorithm, removes redundant information, completes entity relationship paths, and generates accurate KG subgraph query statements. This enables the retrieval results to directly match the core KG substructure required for knowledge question answering in large language models, significantly improving the relevance of retrieval results and the purity of model input, and solving the problem of the disconnect between existing retrieval targets and the needs of large models.
[0017] 2. This knowledge graph-based large-scale model contextual semantic retrieval method constructs a three-level retrieval architecture: vector matching, graph subgraph, and multi-source SQL. First, vector retrieval filters more than 95% of irrelevant data within milliseconds, significantly compressing the search space. Then, KG subgraph query achieves accurate positioning of structured knowledge, avoiding the inefficiency of traditional graph traversal. Finally, SQL is used to connect to a high-performance business database, balancing retrieval speed and factual accuracy, controlling retrieval latency to the millisecond level, making it suitable for high real-time scenarios such as medical and legal fields. Attached Figure Description
[0018] Figure 1 This is a schematic diagram of the process of the present invention. Detailed Implementation
[0019] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0020] like Figure 1 As shown, the present invention provides a technical solution: a large-scale model contextual semantic retrieval method based on knowledge graphs, comprising the following steps: Step S1: Entity and Intent Extraction. The natural language query input by the user is fed into the pre-trained large language model. The deep semantic analysis module of the pre-trained large language model performs semantic parsing on the natural language query, extracts key entity information and the user's core query intent from the natural language query, and obtains a structured semantic representation result. The deep semantic analysis module includes an entity recognition layer, an intent classification layer, and a semantic fusion layer. The entity recognition layer uses a named entity recognition algorithm to annotate natural language queries and extract key entities. The intent classification layer uses a pre-trained text classification model to classify the intent of natural language queries and determine the user's core query intent. The semantic fusion layer merges key entities and core query intent to generate a structured semantic representation that includes the relationship between entity intents.
[0021] Step S2, Vectorized Knowledge Retrieval: The key entity information and core user query intent obtained in Step S1 are transformed into high-dimensional continuous vectors through a pre-trained graph embedding model. These high-dimensional continuous vectors are input into a vector database and matched with the pre-stored knowledge graph embedding vectors in the vector database using cosine similarity. Knowledge graph fragments highly relevant to the user's query semantics are selected in descending order of similarity as preliminary retrieval results. The knowledge graph embedding vectors stored in the vector database are obtained by pre-training the graph embedding model with the entities, relations, and attributes of the knowledge graph. The graph embedding model is one or more combinations of TransE, TransR, Node2Vec, or GraphSAGE. The training samples of the graph embedding model are triple data of the knowledge graph, and the triple data includes head entity, relation, and tail entity.
[0022] The threshold for cosine similarity matching is 0.7~0.9. Knowledge graph nodes and associated edges with similarity greater than or equal to the threshold are selected to form knowledge graph fragments.
[0023] Step S3: KG subgraph query construction. Based on the preliminary search results obtained in step S2, and combined with the context information of the user's original natural language query, the preliminary search results are filtered, integrated and reconstructed through the subgraph reconstruction algorithm to generate KG subgraph query statements that can accurately express the knowledge context and entity relationships required for knowledge question answering of large language models. The KG subgraph query statements are structured query statements adapted to knowledge graph retrieval. S31. Assign weights to the knowledge graph nodes in the preliminary search results. The weights are the semantic relevance of the nodes to the user's core query intent. S32. Remove redundant nodes and associated edges whose weights are lower than the preset threshold; S33. Based on the topology and entity relationships of the knowledge graph, complete the paths for the remaining nodes and edges to form a connected KG subgraph; S34. Transform the KG subgraph into a structured KG subgraph query statement adapted for knowledge graph retrieval.
[0024] Step S4: Multi-source database SQL transformation and context acquisition. A bidirectional mapping relationship is pre-established between knowledge graph instances and multi-source heterogeneous business databases. This mapping relationship includes the correspondence between knowledge graph entities and database tables, knowledge graph relationships and database fields, and knowledge graph attributes and database field values. Based on this mapping relationship, the KG subgraph query statement generated in step S3 is transformed into a structured query language (SQL) adapted to the multi-source heterogeneous business databases through the SQL compilation module. The SQL is then input into the corresponding business databases to execute the query and obtain real-time business context data related to the user query. Multi-source heterogeneous business databases include relational databases, non-relational databases, and data warehouses. The relational database is at least one of MySQL and Oracle, the non-relational database is at least one of MongoDB and Redis, and the data warehouse is at least one of Hive and ClickHouse.
[0025] The bidirectional mapping relationships are stored in the mapping database, which supports dynamic updates. When the structure of the knowledge graph or business database changes, the bidirectional mapping relationships are updated synchronously.
[0026] Step S5, Final Prompt Construction and Knowledge Question Answering: The user's original natural language query, the preliminary search results of Step S2, and the real-time business context data of Step S4 are fused and spliced according to the preset Prompt template to construct a fused Prompt containing all knowledge constraints and contextual information. The fused Prompt is input into the pre-trained large language model, and the answer generation module of the pre-trained large language model performs knowledge question answering reasoning to generate an accurate answer to the user's natural language query.
[0027] The preset Prompt template includes a user query area, a knowledge graph retrieval area, and a business data area. The user query area is used to populate the user's original natural language query, the knowledge graph retrieval area is used to populate the preliminary retrieval results of step S2, and the business data area is used to populate the real-time business context data of step S4.
[0028] The present invention also includes an update step for the knowledge graph and the vector database: when the triple data of the knowledge graph is added, modified or deleted, the updated knowledge graph embedding vector is regenerated through the graph embedding model, and the updated graph embedding vector is synchronized to the vector database to realize real-time linkage update of the knowledge graph and the vector database.
[0029] As one application of this embodiment: This embodiment constructs a deep alignment representation between structured and natural language. By using a graph embedding model, the discrete structure representation of the knowledge graph is transformed into a continuous vector representation, which is unified with the vector representation system of the large language model. At the same time, the entities and intents of the user query are vectorized and similarity matching is performed, realizing the semantic soft alignment between KG triples and natural language queries. This enables the large language model to perceive the topological structure and semantic constraints of the knowledge graph, reduces the dependence on statistical associations, and significantly reduces factual errors.
[0030] The present invention has been described in detail above. However, modifications or improvements can be made to it, which will be obvious to those skilled in the art. Therefore, any modifications or improvements that do not depart from the spirit of the present invention are within the scope of protection of the present invention.
Claims
1. A large-scale model contextual semantic retrieval method based on knowledge graphs, characterized in that, Includes the following steps: Step S1: Entity and Intent Extraction. The natural language query input by the user is input into the pre-trained large language model. The deep semantic analysis module of the pre-trained large language model performs semantic parsing on the natural language query, extracts the key entity information and the user's core query intent in the natural language query, and obtains the structured semantic representation result. Step S2, Vectorized Knowledge Retrieval: The key entity information and core user query intent obtained in Step S1 are transformed into high-dimensional continuous vectors through a pre-trained graph embedding model. These high-dimensional continuous vectors are input into a vector database and subjected to cosine similarity matching with the pre-stored knowledge graph embedding vectors in the vector database. Knowledge graph fragments highly relevant to the user's query semantics are selected in descending order of similarity as preliminary retrieval results. The knowledge graph embedding vectors stored in the vector database are obtained by pre-training the graph embedding model with the entities, relations, and attributes of the knowledge graph. Step S3: KG subgraph query construction. Based on the preliminary search results obtained in step S2, and combined with the context information of the user's original natural language query, the preliminary search results are filtered, integrated and reconstructed through a subgraph reconstruction algorithm to generate KG subgraph query statements that can accurately express the knowledge context and entity relationships required for knowledge question answering of large language models. The KG subgraph query statements are structured query statements adapted to knowledge graph retrieval. Step S4: Multi-source database SQL conversion and context acquisition. A bidirectional mapping relationship is pre-established between knowledge graph instances and multi-source heterogeneous business databases. This mapping relationship includes correspondences between knowledge graph entities and database tables, knowledge graph relationships and database fields, and knowledge graph attributes and database field values. Based on this mapping relationship, the KG subgraph query statement generated in step S3 is converted into a structured query language (SQL) adapted to the multi-source heterogeneous business databases using an SQL compilation module. The SQL is then input into the corresponding business databases to execute the query and obtain real-time business context data related to the user query. Step S5, Final Prompt Construction and Knowledge Question Answering: The user's original natural language query, the preliminary search results of Step S2, and the real-time business context data of Step S4 are fused and spliced according to a preset Prompt template to construct a fused Prompt containing all knowledge constraints and contextual information. The fused Prompt is input into a pre-trained large language model, and the answer generation module of the pre-trained large language model performs knowledge question answering reasoning to generate an accurate answer to the user's natural language query.
2. The knowledge graph-based large-scale model contextual semantic retrieval method according to claim 1, characterized in that, In step S1, the deep semantic analysis module includes an entity recognition layer, an intent classification layer, and a semantic fusion layer; the entity recognition layer uses a named entity recognition algorithm to annotate natural language queries and extract key entities; the intent classification layer uses a pre-trained text classification model to classify the intent of natural language queries and determine the user's core query intent. The semantic fusion layer merges key entities and core query intents to generate a structured semantic representation that includes the relationships between entity intents.
3. The knowledge graph-based large-scale model contextual semantic retrieval method according to claim 1, characterized in that, In step S2, the graph embedding model is one or more combinations of TransE, TransR, Node2Vec, or GraphSAGE, and the training samples of the graph embedding model are triple data of the knowledge graph, wherein the triple data includes head entity, relation, and tail entity.
4. The knowledge graph-based large-scale model contextual semantic retrieval method according to claim 1, characterized in that, In step S2, the threshold for cosine similarity matching is 0.7~0.
9. Knowledge graph nodes and associated edges with similarity greater than or equal to the threshold are selected to form the knowledge graph fragment.
5. The knowledge graph-based large-scale model contextual semantic retrieval method according to claim 1, characterized in that, In step S3, the subgraph reconstruction algorithm includes the following steps: S31. Assign weights to the knowledge graph nodes in the preliminary search results, where the weights are the semantic relevance of the nodes to the user's core query intent; S32. Remove redundant nodes and associated edges whose weights are lower than the preset threshold; S33. Based on the topology and entity relationships of the knowledge graph, complete the paths for the remaining nodes and edges to form a connected KG subgraph; S34. Convert the KG subgraph into a structured KG subgraph query statement adapted for knowledge graph retrieval.
6. The knowledge graph-based large-scale model context semantic retrieval method according to claim 1, characterized in that, In step S4, the multi-source heterogeneous business database includes a relational database, a non-relational database, and a data warehouse. The relational database is at least one of MySQL and Oracle, the non-relational database is at least one of MongoDB and Redis, and the data warehouse is at least one of Hive and ClickHouse.
7. The knowledge graph-based large-scale model contextual semantic retrieval method according to claim 1, characterized in that, The bidirectional mapping relationship mentioned in step S4 is stored in the mapping database, which supports dynamic updates. When the structure of the knowledge graph or business database changes, the bidirectional mapping relationship is updated synchronously.
8. The knowledge graph-based large-scale model contextual semantic retrieval method according to claim 1, characterized in that, In step S5, the preset Prompt template includes a user query area, a knowledge graph retrieval area, and a business data area. The user query area is used to fill in the user's original natural language query, the knowledge graph retrieval area is used to fill in the preliminary retrieval results of step S2, and the business data area is used to fill in the real-time business context data of step S4.
9. A knowledge graph-based large-scale model contextual semantic retrieval method according to any one of claims 1-8, characterized in that, The method also includes an update step for the knowledge graph and vector database: when the triple data of the knowledge graph is added, modified or deleted, the updated knowledge graph embedding vector is regenerated through the graph embedding model, and the updated graph embedding vector is synchronized to the vector database to realize real-time linkage update of the knowledge graph and the vector database.