AI Agent agent implementation method based on large language model and knowledge graph
By combining semantic feature embedding and structural embedding alignment processing in the construction of intelligent agents, the problem of the knowledge graph structure information not being explicitly organized is solved, and response generation with high accuracy and logical consistency is achieved, thereby improving the interaction stability of intelligent agents and user experience.
Patent Information
- Application Number
- CN202510957832.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-11
- Publication Date
- 2025-10-21
- Estimated Expiration
- 2045-07-11
AI Technical Summary
In existing intelligent agent construction methods, the structural information of the knowledge graph is not explicitly organized into a controllable reasoning path, resulting in a lack of structural dependence on the knowledge path when the language model generates a response, leading to semantic deviation and logical inconsistency, affecting the accuracy and credibility of the response.
By collecting natural language instructions and performing semantic encoding to generate semantic feature embedding vectors, and combining them with knowledge graphs to obtain entity nodes and relationship paths, structural embedding encoding is performed, dual-channel semantic structure alignment is performed, a path candidate set is generated, and a response draft is generated in a large language model. Path consistency verification is performed to ensure the logical consistency of the response text.
It effectively avoids semantic bias and logical inconsistency, ensures that the output text conforms to the user's intent, improves the knowledge accuracy and logical rationality of the response text, and enhances the interaction stability of the intelligent agent and the user experience.
Smart Images

Figure CN120822537A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence technology, and in particular to an AIAgent intelligent agent implementation method based on a large language model and a knowledge graph. Background Art
[0002] With the continuous development of natural language processing technology, the current mainstream methods for building intelligent agents are often based on large-scale pre-trained language models, such as GPT and T5. They mainly rely on neural language models to understand natural language input and generate corresponding responses. In practical applications, to improve the knowledge reliability and semantic responsiveness of intelligent agents, a common practice is to introduce external knowledge bases or knowledge graphs through knowledge augmentation mechanisms to assist language models in reasoning and answering. Such mechanisms typically include: extracting entities or relationships from user input using keyword matching or entity recognition techniques; then performing keyword-based query operations in the knowledge graph to retrieve relevant entity nodes or triples; finally, the search results are fed into the large language model in the form of supplementary context or embedded features for response generation.
[0003] The structural information of the knowledge graph is not explicitly organized into controllable reasoning paths, and the language model lacks structural dependence on the knowledge path when generating responses. Specifically, although the model has the ability to generate natural language using knowledge fragments, due to the lack of structural modeling and semantic alignment of the entities, relationships, and combination paths in the graph, it is difficult for the language model to accurately control whether the reasoning process unfolds along the logical chain in the graph during the response generation process. As a result, the final generated text content may have semantic deviations or logical jumps from the actual knowledge graph, which in turn affects the accuracy and credibility of the response. Summary of the Invention
[0004] In view of the above existing problems, the present invention is proposed.
[0005] Therefore, the present invention provides an AI Agent implementation method based on a large language model and a knowledge graph to solve the problem that causes deviations in the semantic accuracy and logical consistency of the response content.
[0006] In order to solve the above technical problems, the present invention provides the following technical solutions: In the first aspect, the present invention provides an AI Agent intelligent body implementation method based on a large language model and a knowledge graph, which includes collecting natural language instructions, performing semantic encoding, and generating a semantic feature embedding vector; using the semantic feature embedding vector as a query basis, obtaining entity nodes and relationship paths in the knowledge graph, and performing structural embedding encoding to generate a structural embedding vector; performing dual-channel semantic structure alignment on the semantic feature embedding vector and the structural embedding vector, aligning the entity nodes and relationship paths in the knowledge graph, and generating a path candidate set; performing structured encoding on the path candidate set to generate prompt information, and merging it with the natural language instruction and inputting it into the large language model to generate a response draft; performing path consistency check on the response draft, and if inconsistent, performing a rollback and regenerating a new response text draft, and if consistent, outputting the final response text; outputting the final response text to the user interface, and recording the semantic feature embedding vector, the structural embedding vector and the path candidate set to generate an intelligent agent behavior log.
[0007] As a preferred solution of the AI Agent implementation method based on a large language model and knowledge graph described in the present invention, wherein: the natural language instructions are collected and semantically encoded to generate semantic feature embedding vectors, the specific steps are as follows: Collect task intent descriptions, entity names, operation verbs, limiting conditions, contextual constraint information, and question expressions to obtain natural language instructions; Perform word segmentation and normalization on natural language instructions to obtain normalized word sequences, and use the pre-trained language model encoder to perform context aggregation operations to generate sentence-level semantic context representations; The sentence-level semantic context representation is input into the embedding mapping network for feature compression and semantic mapping to generate a semantic feature embedding vector.
[0008] As a preferred solution of the AI Agent implementation method based on a large language model and knowledge graph described in the present invention, wherein: the semantic feature embedding vector is used as the query basis to obtain the entity nodes and relationship paths in the knowledge graph, and structural embedding encoding is performed to generate the structural embedding vector. The specific steps are as follows: The semantic feature embedding vector is used as the retrieval vector to calculate the semantic similarity with the entity nodes in the knowledge graph to obtain a set of preliminary matching entity nodes; Starting from the initially matched entity node set, traverse the connection relationships in the knowledge graph to obtain the entity node and relationship path set; Each entity and connection relationship in the entity node and relationship path set is vectorized to generate a structural embedding coding sequence, which is then input into the embedding compression network to generate a structural embedding vector.
[0009] As a preferred solution of the AI Agent intelligent body implementation method based on a large language model and a knowledge graph described in the present invention, wherein: the semantic feature embedding vector and the structural embedding vector are subjected to dual-channel semantic structure alignment, the entity nodes and the relationship paths in the knowledge graph are aligned, and a path candidate set is generated. The specific steps are as follows: Input the semantic feature embedding vector and the structural embedding vector into the dual-channel alignment network, and calculate the matching score between each structural embedding vector and the semantic feature embedding vector; Sort the structural embedding vectors according to the matching scores, select the entity nodes and relationship paths with high scores to generate a preliminary path candidate set; The preliminary path candidate set is mapped to the knowledge graph, the path sequence information is extracted, and normalized encoding is performed to generate a normalized path candidate set.
[0010] As a preferred solution of the AI Agent implementation method based on a large language model and knowledge graph described in the present invention, the structural embedding vectors are sorted according to the matching scores, and the entity nodes and relationship paths with high scores are selected to generate a preliminary path candidate set. The specific steps are as follows: Sort the matching scores in descending order and output a sequence of structure embedding vectors arranged from high to low scores; Calculate the mean and standard deviation of the matching scores, select entity node and relationship path combinations with matching scores higher than the statistical threshold as candidates, and set selection rules; The entity node and relationship path combinations that meet the selection rules are screened from the sorted structural embedding vector sequence to generate a preliminary path candidate set.
[0011] As a preferred solution of the AI Agent implementation method based on a large language model and knowledge graph described in the present invention, wherein: the path candidate set is structuredly encoded to generate prompt information, which is then merged with the natural language instruction and input into the large language model to generate a response draft. The specific steps are as follows: Perform structured encoding on the entity nodes and relationship paths in the path candidate set to generate prompt information; Combine prompt information with natural language instructions to form joint input data; Utilize the pre-trained large language model to conduct deep semantic understanding and contextual analysis of the joint input data, trigger the semantic reasoning and response text generation process, and generate a response draft.
[0012] As a preferred solution of the AI Agent implementation method based on a large language model and knowledge graph described in the present invention, wherein: the response draft is subjected to a path consistency check, if inconsistent, a rollback is executed and a new response text draft is regenerated, if consistent, the final response text is output, and the specific steps are as follows: Perform semantic matching and logical consistency check on the response draft and the path candidate set, output the verification results, and evaluate them to generate a consistency judgment; If the consistency is judged as inconsistent, the fallback mechanism is triggered, and the path candidate set and natural language instructions are re-input into the large language model to generate a new response text draft; If the consistency is judged to be consistent, the draft response will be used as the final response text.
[0013] As a preferred solution of the AI Agent intelligent body implementation method based on the large language model and knowledge graph described in the present invention, wherein: the final response text is output to the user interface, and the semantic feature embedding vector, structural embedding vector and path candidate set are recorded to generate the intelligent body behavior log. The specific steps are as follows: The final response text is transmitted to the user interface to output the natural language result, and is aggregated and processed together with the semantic feature embedding vector, structural embedding vector and path candidate set to construct the log data content; The log data content is formatted and structured to generate a behavior log containing semantic reasoning process and path reference information, and written into the designated storage area to generate an agent behavior log.
[0014] In a second aspect, the present invention provides a computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: when the computer program is executed by the processor, it implements any step of the method for implementing an AI Agent based on a large language model and a knowledge graph as described in the first aspect of the present invention.
[0015] In a third aspect, the present invention provides a computer-readable storage medium having a computer program stored thereon, wherein: when the computer program is executed by a processor, it implements any step of the method for implementing an AI Agent based on a large language model and a knowledge graph as described in the first aspect of the present invention.
[0016] The beneficial effects of the present invention are as follows: through the fusion of the structured encoding of the path candidate set and the original instructions, the response generated by the large language model is closely integrated with the structural information of the knowledge graph, effectively avoiding semantic deviation and logical inconsistency, ensuring that the output text conforms to the user's intention and has high knowledge accuracy and logical rationality; at the same time, the path consistency verification and fallback mechanism construct a closed-loop optimization process, significantly improving the quality and credibility of the response text, and enhancing the interactive stability of the intelligent agent and the user experience. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0018] Figure 1 This is a flowchart of the AI Agent implementation method based on a large language model and knowledge graph in the present invention.
[0019] Figure 2 Flowchart for generating semantic feature embedding vectors in the present invention.
[0020] Figure 3 Flowchart for the structure embedding generation and alignment in the present invention.
[0021] Figure 4 Flowchart of the response generation and verification process in the present invention. DETAILED DESCRIPTION
[0022] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, the specific embodiments of the present invention are described in detail below with reference to the accompanying drawings.
[0023] In the following description, many specific details are set forth to facilitate a full understanding of the present invention. However, the present invention may also be implemented in other ways different from those described herein. Those skilled in the art may make similar generalizations without violating the connotation of the present invention. Therefore, the present invention is not limited to the specific embodiments disclosed below.
[0024] Secondly, the term "one embodiment" or "embodiment" herein refers to a specific feature, structure, or characteristic that may be included in at least one implementation of the present invention. The phrase "in one embodiment" appearing in various places throughout this specification does not necessarily refer to the same embodiment, nor does it refer to a separate or selective embodiment that is mutually exclusive of other embodiments.
[0025] Reference Figures 1 to 4 , is an embodiment of the present invention, which provides an AI Agent implementation method based on a large language model and a knowledge graph, comprising the following steps: S1. Collect natural language instructions, perform semantic encoding, and generate semantic feature embedding vectors.
[0026] S1.1. Collect task intent description, entity name, operation verb, limiting conditions, context constraint information and question expression to obtain natural language instructions.
[0027] Specifically, the input multi-round interaction text or single-sentence instructions are sentence-processed to identify and extract text fragments of task intent description, entity name, operation verb, restriction condition, context constraint information and question expression respectively; the extracted text fragments are subjected to part-of-speech tagging and named entity recognition to filter out fields that meet the category; the fields are merged in the order of task intent description, entity name, operation verb, restriction condition, context constraint information and question expression to form a structured natural language instruction.
[0028] S1.2. Segment and standardize the natural language instructions to obtain a normalized word sequence, and use the pre-trained language model encoder to perform context aggregation operations to generate a sentence-level semantic context representation.
[0029] Specifically, a word segmentation algorithm is used to segment natural language instructions to obtain a word sequence; the word sequence is standardized, including unifying the case, removing meaningless stop words, and normalizing synonyms to form a normalized word sequence; the normalized word sequence is input into the pre-trained language model encoder, and the pre-trained language model encoder captures the contextual dependencies between normalized word sequences through a multi-layer self-attention mechanism to perform context aggregation operations and output sentence-level semantic context representation.
[0030] It should also be explained that the pre-training process of the pre-trained language model encoder is as follows: building a large-scale corpus covering multi-domain and diverse text data; using a self-supervised learning method to train the input text based on a masked language model or an autoregressive language model, such as the GPT task; randomly masking or predicting some words in the text in sequence, and the language model learns contextual semantic representations through a multi-layer Transformer structure, optimizing the loss function to make the prediction results as consistent as possible with the real words; after multiple rounds of iterative training, the language model parameters are gradually updated to obtain a pre-trained language model encoder with rich language understanding and generation capabilities.
[0031] S1.3. Input the sentence-level semantic context representation into the embedding mapping network for feature compression and semantic mapping to generate a semantic feature embedding vector.
[0032] Specifically, each dimensional vector in the sentence-level semantic context representation is used as the input feature of the embedding mapping network, and a dimensionality compression operation is performed through a fully connected neural network. Specifically, it passes through multiple linear transformation layers in sequence, and each layer contains an activation function, such as the ReLU function, to introduce nonlinear transformation capabilities; based on the compressed features, the semantic mapping layer is used to map the compressed sentence-level semantic context representation to the semantic embedding space. The semantic mapping layer consists of a set of fixed-dimensional linear transformation parameters to generate a semantic feature embedding vector.
[0033] S2. Use the semantic feature embedding vector as the query basis to obtain the entity nodes and relationship paths in the knowledge graph, and perform structural embedding encoding to generate a structural embedding vector.
[0034] S2.1. Use the semantic feature embedding vector as the retrieval vector, calculate the semantic similarity with the entity nodes in the knowledge graph, and obtain a set of preliminary matching entity nodes.
[0035] Specifically, the semantic feature embedding vector is used as the input query vector; the constructed knowledge graph entity node semantic vector index library is called to read the entity node semantic vector corresponding to each entity node; the cosine similarity function is used to calculate the similarity score between the semantic feature embedding vector and each entity node semantic vector, where the calculation formula of the cosine similarity function is: ; in, Represents a vector and The cosine similarity score between represents the semantic feature embedding vector, Represents the entity node semantic vector, represents the vector dot product, Represents the vector modulus; All calculated similarity scores are sorted in descending order to obtain a sequence of entity nodes arranged in descending order of similarity scores; several entity nodes with high similarity scores are selected from the sorted entity node sequence, for example, the top 20, as a set of preliminary matching entity nodes.
[0036] It should also be explained that the data source of the constructed knowledge graph entity node semantic vector index library is: obtaining all entity node text information in the knowledge graph, including entity name, alias, definitional description, hyponym and hyponym class names and related annotations; performing word segmentation and stop word removal on the text information to generate a standardized word sequence; then inputting the standardized word sequence into the same pre-trained language model encoder as the semantic feature embedding vector training to obtain the contextual semantic vector representation corresponding to each entity node; finally, performing unified dimensional normalization on the semantic vector representations of all entity nodes, and using the unique identifier of the entity node as the index key to construct the knowledge graph entity node semantic vector index library.
[0037] S2.2. Starting from the set of preliminarily matched entity nodes, traverse the connection relationships in the knowledge graph to obtain the set of entity nodes and relationship paths.
[0038] Specifically, starting from the set of preliminarily matched entity nodes, the unique identifier of each entity node in the knowledge graph is read in turn, and all triples with direct connection relationships with the entity nodes are searched in the knowledge graph, and the target entity node and the corresponding relationship type are extracted to form a triple path of the starting entity, relationship type and target entity; in the extracted target entity node, the next level of connection relationship is continued to be searched in the same way, and the path depth is limited to no more than the maximum step length, for example, three steps, and the entity nodes and relationship type sequences passed by all paths are recorded, and finally summarized to form a set of entity nodes and relationship paths.
[0039] S2.3. Vectorize each entity and connection relationship in the entity node and relationship path set to generate a structural embedding coding sequence, and input it into the embedding compression network to generate a structural embedding vector.
[0040] Specifically, the entity name, upper and lower class names and definitional description text of each entity node are input into the pre-trained language model encoder, and the entity node semantic vector is output; the relationship type text and semantic annotation information of the connection relationship of each entity node semantic vector are obtained, and also input into the pre-trained language model encoder, and the relationship type semantic vector is output; each group of entity node semantic vectors and adjacent connection relationship semantic vectors are spliced in sequence according to the order in the path to form a structural embedding coding sequence; the structural embedding coding sequence is input into the embedding compression network, and dimensionality reduction mapping is performed through multi-layer linear transformation and activation function to generate a structural embedding vector.
[0041] S3. Perform dual-channel semantic structure alignment on the semantic feature embedding vector and the structural embedding vector, align the entity nodes and relationship paths in the knowledge graph, and generate a path candidate set.
[0042] S3.1. Input the semantic feature embedding vector and the structural embedding vector into the dual-channel alignment network and calculate the matching score between each structural embedding vector and the semantic feature embedding vector.
[0043] Specifically, the semantic feature embedding vector is input into the first channel for linear transformation to obtain the semantic channel representation result; the structural embedding vector is sequentially input into the second channel for linear transformation of the same dimension to obtain the structural channel representation result; the semantic channel representation result is dot-producted with each structural channel representation result to obtain the matching score between the structural embedding vector and the semantic feature embedding vector; and the matching scores corresponding to all structural embedding vectors are output.
[0044] S3.2. Sort the structural embedding vectors according to the matching scores, select the entity nodes and relationship paths with high scores, and generate a preliminary path candidate set.
[0045] S3.2.1. Sort the matching scores in descending order and output a sequence of structure embedding vectors arranged from high to low scores.
[0046] Specifically, the matching scores are bound to the corresponding structural embedding vectors in a one-to-one correspondence to form a set of matching score-structural embedding vector pairs; a sorting algorithm is used to sort the matching score-structural embedding vector pairs in descending order according to the matching scores to obtain a sequence of structural embedding vectors sorted from high to low; finally, the sorted sequence of structural embedding vectors is output.
[0047] S3.2.2. Calculate the mean and standard deviation of the matching scores, select entity node and relationship path combinations with matching scores higher than the statistical threshold as candidates, and set selection rules.
[0048] Specifically, based on the matching scores, the average matching score is calculated using the following formula: ; in, represents the average of all matching scores, Indicates the total number of matching scores, Indicates the Matching score, Indicates the serial number of the matching pair; Calculate the standard deviation of the matching score using the formula: ; in, represents the standard deviation of all matching scores; According to the calculated mean and standard deviation, the selection rule is set as follows: entity nodes and relationship paths with matching scores higher than the statistical threshold are combined as candidates.
[0049] The process of setting the statistical threshold should also be explained: collecting the matching scores corresponding to all structural embedding vectors and constructing a matching score set; performing distribution analysis on the matching score set and calculating basic statistics such as the median, quantile, and standard deviation; determining the target positioning for selecting highly relevant candidates based on specific needs, such as using the 75th percentile as a reference for the upper bound of the distribution, or setting a dynamic deviation range, such as the matching score being higher than the statistical mean plus a fixed deviation margin; finally, generating a statistical threshold value based on the selected criteria, and using the statistical threshold as the basis for subsequent path screening. The basis for setting the statistical threshold may include the task's accuracy requirements, the optimal interval distribution range in historical verification results, or the score interval corresponding to correctly labeled samples in manually labeled data.
[0050] S3.2.3. Filter entity node and relationship path combinations that meet the selection rules from the sorted structural embedding vector sequence to generate a preliminary path candidate set.
[0051] Specifically, the matching scores corresponding to each group of structural embedding vectors in the sorted structural embedding vector sequence are read in turn to determine whether the matching score is greater than the sum of the average matching score and the standard deviation. If the conditions are met, the entity node and relationship path combination corresponding to the structural embedding vector is extracted; the judgment process is repeated until the entire structural embedding vector sequence is traversed; all entity nodes and relationship path combinations that meet the conditions are aggregated to generate a preliminary path candidate set.
[0052] S3.3. Map the preliminary path candidate set to the knowledge graph, extract the path sequence information, and perform normalized encoding to generate a normalized path candidate set.
[0053] Specifically, index matching is performed on each group of entity nodes and relationship paths in the preliminary path candidate set to locate the corresponding entity nodes and connection relationships in the knowledge graph; the entity node identifiers and relationship types are read in the connection order to construct the path sequence information; a unified format conversion operation is performed on each path sequence information, including entity node naming standardization, relationship type label standardization and path structure formatting, to obtain a data format that conforms to the specifications; the normalized path sequence information is output as a normalized path candidate set.
[0054] S4. Perform structured encoding on the path candidate set to generate prompt information, merge it with the natural language instructions, and input it into the large language model to generate a response draft.
[0055] S4.1. Perform structured encoding on the entity nodes and relationship paths in the path candidate set to generate prompt information.
[0056] Specifically, character-level segmentation is performed on the entity node names and relationship path labels contained in each group of path candidate sets, and each word is mapped to the corresponding word vector representation using a fixed word table mapping method; the word vector representations are concatenated in the order of "entity name-relationship type-entity name" to form a path encoding input sequence; the path encoding input sequence is input into the position encoding network, and the position index encoding of each word vector in the path is added; average pooling is performed on the position-encoded vector sequence to obtain a path encoding representation vector; the path encoding representation vector is mapped to a fixed-length prompt information vector, which is output as the final prompt information.
[0057] S4.2. Combine and integrate the prompt information and the natural language instructions to form joint input data.
[0058] Specifically, the normalized word sequence in the natural language instruction is used as the starting part of the input, and the path encoding representation vector in the prompt information is textually reconstructed to generate a structured prompt text; then the natural language instruction text and the structured prompt text are string-level spliced in a fixed order, and a connector such as "[SEP]" is used as the boundary identifier between the two parts; the entire text after splicing is used as the joint input data, and word segmentation processing is performed on the joint input data to generate a joint input sequence.
[0059] S4.3. Use the pre-trained large language model to perform deep semantic understanding and contextual association analysis on the joint input data, trigger the semantic reasoning and response text generation process, and generate a response draft.
[0060] Specifically, the normalized word sequence of the joint input data is input into a pre-trained large language model, such as the GPT model using the Transformer architecture; in the encoding stage, the self-attention mechanism is used to calculate the weights of each word and all other words in the context to obtain a global context-related encoding representation; in the decoding stage, the generation sequence is initialized according to the encoding result, and the historical generated word vectors and the joint input data are combined, and iterative operations are performed through multi-layer causal self-attention calculations and position feedforward networks; when the word with the highest generation probability meets the termination condition, the generation process ends and the generated response draft is output.
[0061] It should also be explained that the pre-training process of the pre-trained large language model includes: building a large-scale corpus, the corpus types include encyclopedia articles, question-and-answer dialogue texts, programming codes and text data; then the text data is segmented and encoded and converted into the corresponding word vector input sequence; in the training phase, an autoregressive language modeling task is used to predict the probability distribution of the next word based on the current context, specifically by minimizing the cross-entropy loss function between the predicted word and the true word to perform parameter updates; in each round of iteration, the input is forward propagated and back-gradient propagated through the encoder and decoder of the multi-layer Transformer structure, and the parameters of the large language model are updated using an optimizer such as Adam; training is continued until the loss function of the large language model on the validation set converges, and finally a large language model that can be used for natural language generation and understanding tasks is obtained.
[0062] S5. Perform path consistency check on the response draft. If there is inconsistency, perform rollback and regenerate a new response text draft. If there is consistency, output the final response text.
[0063] S5.1. Perform semantic matching and logical consistency check on the response draft and the path candidate set, output the verification results, and evaluate them to generate a consistency judgment.
[0064] Specifically, key entity nodes and association relationships are extracted from the response draft to form a response semantic structure representation; secondly, the corresponding entity node and relationship path combination is obtained from the path candidate set to form a candidate path semantic structure representation; a vectorized semantic matching method is used to calculate the similarity score between the response semantic structure representation and the candidate path semantic structure representation, and a logical consistency judgment is performed based on the connection order and logical relationship of the entity nodes; a verification result is generated based on the similarity score and the logical consistency result; finally, the verification result is evaluated according to the set consistency threshold, and the consistency judgment result is output.
[0065] The specific steps for setting the consistency threshold should also be explained: This threshold is calculated using the mean and standard deviation based on a statistical analysis of historical semantic matching scores and logical consistency judgment results. For example, the consistency threshold can be set to the mean of the historical matching score minus 0.5 times the standard deviation, serving as the critical value for determining whether the response draft is consistent with the path candidate set. Using the consistency threshold, the real-time semantic matching score is compared with the consistency threshold. If the score is higher than the consistency threshold, the response is considered consistent; otherwise, it is considered inconsistent.
[0066] S5.2. If the consistency is judged as inconsistent, the fallback mechanism is triggered, and the path candidate set and natural language instructions are re-input into the large language model to generate a new response text draft.
[0067] Specifically, if the consistency is judged to be inconsistent, the entity node and relationship path combination corresponding to the next unused structure embedding vector with a high matching score is selected in sequence from the sorted structure embedding vector sequence to update the path candidate set; the updated path candidate set is then input into the pre-trained large language model together with the current natural language instruction, and the response text generation process based on the path candidate set is executed to obtain a new response text draft; the path consistency check is then performed again on the new response text draft. If it is still inconsistent, the operation is repeated until a response text that meets the consistency judgment is generated or the preset maximum number of fallbacks is reached.
[0068] S5.3. If the consistency is judged to be consistent, the draft response will be used as the final response text.
[0069] Specifically, if the consistency is judged to be consistent, the current response draft is extracted from the output of the large language model, and then the response draft is format checked to ensure that the text content conforms to the preset text specifications and encoding requirements; then the response draft that passes the format check is saved as the final response text and output to the user interface for display; finally, the generation time of the final response text and the corresponding path candidate set version information are recorded to complete the confirmation and release process of the final response text.
[0070] S6. Output the final response text to the user interface, record the semantic feature embedding vector, structural embedding vector and path candidate set, and generate an agent behavior log.
[0071] S6.1. The final response text is transmitted to the user interface to output the natural language result, and is aggregated and processed together with the semantic feature embedding vector, structural embedding vector, and path candidate set to construct the log data content.
[0072] Specifically, the final response text is transmitted to the user interface as a natural language output, and the semantic feature embedding vectors, structural embedding vectors, and path candidate set data are extracted. The natural language results, along with the semantic feature embedding vectors, structural embedding vectors, and path candidate sets, are then converted to a unified format and integrated to form a log data structure containing the response content, corresponding vector features, and path information. Finally, the log data structure is structured and encoded to ensure that the data fields are complete and standardized, generating the log data content.
[0073] S6.2. Format and structure the log data content to generate a behavior log containing semantic reasoning process and path reference information, and write it into the designated storage area to generate an agent behavior log.
[0074] Specifically, the log data content is formatted, including unified field naming, standardized timestamp format, and standardized data types. Then, the semantic feature embedding vectors, structural embedding vectors, path candidate sets, and associated information of the response text are structurally indexed. This clarifies the steps of the semantic reasoning process and the corresponding path reference information, forming structured behavior log data. The structured behavior log data is then converted to a storage format and written to a designated storage area to generate the agent behavior log.
[0075] This embodiment also provides a computer device suitable for the implementation method of an AI Agent based on a large language model and a knowledge graph, including: a memory and a processor; the memory is used to store computer-executable instructions, and the processor is used to execute computer-executable instructions to implement the AI Agent implementation method based on a large language model and a knowledge graph as proposed in the above embodiment.
[0076] The computer device may be a terminal, comprising a processor, memory, a communication interface, a display, and an input device connected via a system bus. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores an operating system and computer programs. The internal memory provides an environment for the operating system and computer programs stored in the non-volatile storage media. The communication interface of the computer device is used to communicate with external terminals via wired or wireless communication. Wireless communication may be achieved via Wi-Fi, a carrier network, NFC (near-field communication), or other technologies. The display of the computer device may be a liquid crystal display or an electronic ink display. The input device may be a touchscreen overlay on the display, buttons, a trackball, or a touchpad on the computer device housing, or an external keyboard, touchpad, or mouse.
[0077] This embodiment also provides a storage medium having a computer program stored thereon, which, when executed by a processor, implements the method for implementing an AI Agent based on a large language model and a knowledge graph as proposed in the above embodiment; the storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk.
[0078] In summary, the present invention achieves this by: integrating the structured encoding of the path candidate set with the original instructions, thereby ensuring that the responses generated by the large language model are closely integrated with the structural information of the knowledge graph, effectively avoiding semantic deviations and logical inconsistencies, and ensuring that the output text conforms to the user's intentions and has high knowledge accuracy and logical rationality; at the same time, the path consistency check and fallback mechanism construct a closed-loop optimization process, significantly improving the quality and credibility of the response text, and enhancing the interactive stability of the intelligent agent and the user experience.
[0079] It should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit the present invention. Although the present invention has been described in detail with reference to the preferred embodiments, those skilled in the art should understand that the technical solutions of the present invention may be modified or replaced by equivalents without departing from the spirit and scope of the technical solutions of the present invention, which should all be included in the scope of the claims of the present invention.
Claims
1. A method for implementing an AI agent based on a large language model and a knowledge graph, characterized by: include, Collect natural language instructions, perform semantic encoding, and generate semantic feature embedding vectors; Using the semantic feature embedding vector as the query basis, we obtain the entity nodes and relationship paths in the knowledge graph, perform structural embedding encoding, and generate a structural embedding vector. Perform dual-channel semantic structure alignment on the semantic feature embedding vector and the structural embedding vector, align the entity nodes and relationship paths in the knowledge graph, and generate a path candidate set; Perform structured encoding on the path candidate set to generate prompt information, which is then combined with the natural language instructions and input into the large language model to generate a response draft. Perform path consistency check on the response draft. If there is inconsistency, perform rollback and regenerate a new response text draft. If there is consistency, output the final response text. The final response text is output to the user interface, and the semantic feature embedding vector, structural embedding vector and path candidate set are recorded to generate an agent behavior log.
2. The AI Agent implementation method based on a large language model and a knowledge graph as claimed in claim 1, characterized in that: The natural language instructions are collected and semantically encoded to generate semantic feature embedding vectors. The specific steps are as follows: Collect task intent descriptions, entity names, operation verbs, limiting conditions, contextual constraint information, and question expressions to obtain natural language instructions; Perform word segmentation and normalization on natural language instructions to obtain normalized word sequences, and use the pre-trained language model encoder to perform context aggregation operations to generate sentence-level semantic context representations; The sentence-level semantic context representation is input into the embedding mapping network for feature compression and semantic mapping to generate a semantic feature embedding vector.
3. The AI Agent implementation method based on a large language model and a knowledge graph as claimed in claim 2 is characterized by: The semantic feature embedding vector is used as the query basis to obtain the entity nodes and relationship paths in the knowledge graph, and perform structural embedding encoding to generate the structural embedding vector. The specific steps are as follows: The semantic feature embedding vector is used as the retrieval vector to calculate the semantic similarity with the entity nodes in the knowledge graph to obtain a set of preliminary matching entity nodes; Starting from the initially matched entity node set, traverse the connection relationships in the knowledge graph to obtain the entity node and relationship path set; Each entity and connection relationship in the entity node and relationship path set is vectorized to generate a structural embedding coding sequence, which is then input into the embedding compression network to generate a structural embedding vector.
4. The AI Agent implementation method based on a large language model and a knowledge graph as claimed in claim 3 is characterized by: The dual-channel semantic structure alignment of the semantic feature embedding vector and the structural embedding vector is performed, and the entity nodes and relationship paths in the knowledge graph are aligned to generate a path candidate set. The specific steps are as follows: Input the semantic feature embedding vector and the structural embedding vector into the dual-channel alignment network, and calculate the matching score between each structural embedding vector and the semantic feature embedding vector; Sort the structural embedding vectors according to the matching scores, select the entity nodes and relationship paths with high scores to generate a preliminary path candidate set; The preliminary path candidate set is mapped to the knowledge graph, the path sequence information is extracted, and normalized encoding is performed to generate a normalized path candidate set.
5. The AI Agent implementation method based on a large language model and a knowledge graph as claimed in claim 4 is characterized by: Sort the structural embedding vectors according to the matching scores, select the entity nodes and relationship paths with high scores, and generate a preliminary path candidate set. The specific steps are as follows: Sort the matching scores in descending order and output a sequence of structure embedding vectors arranged from high to low scores; Calculate the mean and standard deviation of the matching scores, select entity node and relationship path combinations with matching scores higher than the statistical threshold as candidates, and set selection rules; The entity node and relationship path combinations that meet the selection rules are screened from the sorted structural embedding vector sequence to generate a preliminary path candidate set.
6. The AI Agent implementation method based on a large language model and a knowledge graph as claimed in claim 4, characterized in that: The path candidate set is structuredly encoded to generate prompt information, which is then merged with the natural language instructions and input into the large language model to generate a response draft. The specific steps are as follows: Perform structured encoding on the entity nodes and relationship paths in the path candidate set to generate prompt information; Combine prompt information with natural language instructions to form joint input data; Utilize the pre-trained large language model to conduct deep semantic understanding and contextual analysis of the joint input data, trigger the semantic reasoning and response text generation process, and generate a response draft.
7. The AI Agent implementation method based on a large language model and a knowledge graph according to claim 6 is characterized by: The response draft is checked for path consistency. If there is inconsistency, a rollback is performed and a new response text draft is regenerated. If there is consistency, the final response text is output. The specific steps are as follows: Perform semantic matching and logical consistency check on the response draft and the path candidate set, output the verification results, and evaluate them to generate a consistency judgment; If the consistency is judged as inconsistent, the fallback mechanism is triggered, and the path candidate set and natural language instructions are re-input into the large language model to generate a new response text draft; If the consistency is judged to be consistent, the draft response will be used as the final response text.
8. The AI Agent implementation method based on a large language model and a knowledge graph as claimed in claim 7, characterized in that: The final response text is output to the user interface, and the semantic feature embedding vector, structural embedding vector and path candidate set are recorded to generate the agent behavior log. The specific steps are as follows: The final response text is transmitted to the user interface to output the natural language result, and is aggregated and processed together with the semantic feature embedding vector, structural embedding vector and path candidate set to construct the log data content; The log data content is formatted and structured to generate a behavior log containing semantic reasoning process and path reference information, and written into the designated storage area to generate an agent behavior log.
9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the AI Agent implementation method based on a large language model and a knowledge graph are implemented as described in any one of claims 1 to 8.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method for implementing an AI Agent based on a large language model and a knowledge graph as described in any one of claims 1 to 8 are implemented.
Citation Information
Patent Citations
Knowledge base construction method and question and answer dialogue method and system based on generative large language model
CN117056471A
Method, medium and system for realizing chat memory by using large language model
CN117891925A
Code abstract generation method oriented to semantic structure feature joint enhancement
CN119537579A
Output detection method for large language model, apparatus, electronic device and storage medium
WO2025139382A1
Cited By
Graph-driven self-attention compressible memory management method
CN120994822A
A graph-driven self-attention compressible memory management method
CN120994822B
Model distillation method, device and equipment for knowledge graph unknown entity reasoning and medium
CN121413708A
Intelligent code generation method and system for railway main data and electronic equipment
CN121457442A
A method, system, and electronic equipment for intelligent coding generation of railway master data
CN121457442B
