Intelligent agent long-term memory modeling method based on memory network
Through the intelligent agent long-term memory modeling method based on memory network, combined with the dual storage architecture and emotion time series enhanced community detection algorithm, the problems of memory gaps and emotional incoherence in the intelligent companion system are solved, the consistency and naturalness of emotional interaction are achieved, and the user experience is improved.
Patent Information
- Application Number
- CN202510796741.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-16
- Publication Date
- 2025-09-23
- Estimated Expiration
- 2045-06-16
AI Technical Summary
Existing intelligent companionship technology has shortcomings in long-term memory management and emotional interaction consistency, which results in the inability of the intelligent agent to fully understand the user's emotional development trajectory, incoherent emotional interaction, and affecting the user experience.
A memory network-based agent long-term memory modeling method is adopted. Through dual storage architecture management and community detection algorithm enhanced by emotion temporal sequence, semantic similarity and structured query are combined to optimize memory filtering and retrieval to generate coherent emotional interaction responses.
It improves the emotional interaction coherence of the intelligent body, reduces the emotional jump, enhances the consistency and naturalness of the user experience, and is suitable for long-term emotional interaction scenarios.
Smart Images

Figure CN120688545A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of intelligent agents, and in particular to a method for modeling long-term memory of intelligent agents based on memory networks. Background Art
[0002] With the rapid development of artificial intelligence technology, the emergence of large language models (LLMs) has enabled the field of intelligent companionship to achieve a qualitative leap from "chatbots" to "emotional partners." Based on large language models, AI agents can interact with users in real time through various forms such as text and voice, providing emotional interaction and psychological support, thus realizing "intelligent companionship" scenarios. This type of agent can not only understand the user's semantic needs, but also perceive the user's emotional state, providing a more natural and humane interactive experience. Currently, the field of intelligent companionship has been widely used in multiple scenarios such as mental health support, elderly care, and educational counseling, significantly improving users' emotional experience and quality of life. Related research shows that AI agents with emotional interaction capabilities can more effectively establish trust relationships with users and improve user stickiness and satisfaction.
[0003] However, existing intelligent companionship technologies still have significant deficiencies in long-term memory management and emotional interaction consistency. Specifically, during a conversation between an AI agent and a user, the large language model will load the most recent conversation record between the agent and the user into a "context window," using this as a prompt for the model's reply output, which is then input into the large model to generate the next round of replies. Due to the technical limitations of the large language model, the length of its "context window" has a fixed upper limit. When the conversation record between the AI agent and the user exceeds the length of the large model's "context window," the system will truncate the old content, resulting in the loss of historical conversation content.
[0004] This "short-term memory" mechanism exhibits obvious defects in long-term emotional interaction scenarios: as the interaction deepens, a large amount of important historical information is discarded, resulting in the inability of the intelligent agent to fully and coherently understand the user; emotional interaction has obvious temporal and gradual characteristics. When historical emotional state information is lost, it is difficult for the intelligent agent to grasp the complete trajectory of the user's emotional development; most existing technologies use simple similarity calculations or keyword matching for memory retrieval, failing to fully consider emotional coherence and temporal relationships, resulting in retrieval results that do not match the current emotional context.
[0005] In summary, existing intelligent companion systems often exhibit memory gaps, emotional jumps, and incoherent interactions during long-term emotional interactions, severely impacting user experience and effectiveness. Therefore, there is an urgent need for an agent memory modeling method that can effectively manage long-term memory and maintain the coherence of emotional interactions. Summary of the Invention
[0006] In response to the lack of coherence in the long-term memory and emotional interaction of intelligent agents, this application provides a long-term memory modeling method for intelligent agents based on a memory network. It improves the coherence of emotional interaction in intelligent companionship scenarios through dual storage architecture management and the introduction of a community detection algorithm with enhanced emotional timing to optimize memory filtering.
[0007] The present application provides a long-term memory modeling method for an intelligent agent based on a memory network, including: S1, collecting interaction data between an intelligent agent and a user, converting the interaction data into vector data, and extracting structured metadata from the interaction data; S2, storing the vector data in a vector database, and storing the structured metadata in a relational database; S3, receiving a user's input request, performing vector similarity retrieval in the vector database to obtain semantically similar fragments; performing structured data query in the relational database to obtain structured metadata; and retrieving the memory summary generated and stored in step S7; merging the semantically similar fragments, structured metadata, and memory summary to form a candidate data set; S4, filtering the candidate data set; S5, using the result after filtering in step S4 as memory information, inputting the memory information into a large language model through a prompt word project, and generating reply content; wherein the large language model can adopt OpenAI's GPT series models (such as GPT-4, GPT-4Turbo, GPT-3.5-turbo), Baidu's Wenxin Yiyan, Alibaba's Tongyi Qianwen, Byte's Doubao, Zhipu AI's ChatGLM series, etc. S6: Combine the user's input request and the generated reply content to form a new interaction record, extract structured metadata from the interaction record and store it in a relational database, and convert the interaction record into vector data and store it in the vector database. S7: Use a large language model to process the new interaction record and historical interaction records stored in the vector database and relational database to generate a memory summary. S8: Compare the new interaction record with the historical interaction record, identify and process memory fragments with overlapping information or logical contradictions, and update the structured metadata in the relational database.
[0008] Furthermore, S3 forms a candidate data set, including: converting the user's input request into vector data; calculating cosine similarity in a vector database based on the vector data, retrieving vector data with a cosine similarity greater than a threshold, and obtaining corresponding semantically similar fragments; extracting timestamps and keywords from the user's input request, constructing a structured query statement, executing the query in a relational database, and obtaining structured metadata; retrieving the memory summary generated and stored in step S7, including: obtaining the memory summary generated N1 times most recently in chronological order, and obtaining the N2 historical memory summaries with the highest relevance to the user's input request based on vector similarity calculation; combining the obtained semantically similar fragments, structured metadata, and memory summary to form a candidate data set.
[0009] Furthermore, S4 filters the candidate data sets, including: calculating the timeliness score and relevance score of each data item based on the candidate data sets; wherein the timeliness score is inversely proportional to the time difference between the timestamp of the data item and the current timestamp; the relevance score is calculated based on the cosine similarity between the vector of the data item and the vector of the user's input request; and calculating the priority P(i) of each data item based on the timeliness score and the relevance score, P(i) = w t ×T(i)+w r ×R(i), where represents the timeliness score of data item i, t current Indicates the current timestamp, t i represents the timestamp of data item i; R(i) represents the relevance score of data item i; w t and w r Represent the weight coefficients of timeliness and relevance respectively; construct a data association map, where the nodes of the association map are data items and the edges represent the association relationship between data; based on the data association map, starting from the data with the highest priority, obtain the M1 data with the highest correlation through the community discovery algorithm as the filtering result.
[0010] In particular, traditional memory retrieval methods typically simply select the K highest-priority memory items, ignoring the interrelationships and emotional coherence between memories. However, emotional companionship scenarios require connecting memories to form a complete emotional context, rather than scattered, high-scoring memory fragments. Relying solely on priority sorting results in fragmented retrieval results, failing to form a coherent emotional interaction context.
[0011] Therefore, this application innovatively proposes a memory filtering method based on association graphs and community discovery. First, by weighted calculation of timeliness scores and relevance scores, priority is given to memory items that are close in time and semantically related. Second, by constructing an association graph between data, the semantic, temporal, and emotional associations between memory items are explicitly expressed. Finally, starting from the highest-priority data, a community discovery algorithm is applied to obtain the M1 data with the highest degree of association, ensuring that the retrieval results are not only highly relevant but also internally coherent.
[0012] Furthermore, the data association graph is constructed, including: calculating the semantic association, temporal association and emotional association between any two data in the candidate data set respectively; calculating the comprehensive association W between each data according to the semantic association, temporal association and emotional association orig (i, j); take data items as nodes and comprehensive correlation degree W orig The association relationship (i, j) greater than the preset threshold is regarded as an edge, and an undirected weighted association graph between data items is constructed.
[0013] Furthermore, the semantic relevance is calculated using the following formula: Among them, Sem(i,j) represents the semantic association between data item i and data item j; V i represents the vector representation of data item i; V j Represents the vector representation of data item j; calculate the time series correlation using the following formula: Among them, Temp(i,j) represents the temporal correlation between data item i and data item j; T i Represents the timestamp of data item i; T j represents the timestamp of data item j; τ represents the time decay factor, which is used to control the influence of time difference on the correlation degree; I(T i ,T j ) represents the timing indicator function, when T i Before T j And the time interval is less than the preset threshold θ t The value is 1 when , otherwise it is β t (where 0<β t <1).
[0014] Furthermore, the sentiment correlation is calculated using the following formula:
[0015] Emo(i,j)=α e ×ES(i,j)+(1-α e )×ET(i,j); where Emo(i,j) represents the emotional correlation between data item i and data item j; α e represents the balance factor, with a value range of [0, 1]; ES(i, j) represents the sentiment tag similarity, which is calculated by the following formula: Among them, E i The sentiment vector of data item i contains the intensity values of different sentiment categories; E j represents the sentiment vector of data item j; ET(i,j) represents the naturalness of sentiment transition, which is calculated by the following formula: Where M represents the emotion transition probability matrix; a i represents the dominant sentiment category index of data item i; a j represents the dominant sentiment category index of data item j; T s Indicates the standard time parameter for emotion conversion.
[0016] In particular, traditional methods for calculating memory relevance typically focus solely on semantic or temporal dimensions, neglecting the emotional dimension. In emotional companionship scenarios, a coherent transition of emotional states is crucial, but existing technologies lack a quantitative assessment mechanism for the naturalness of emotional transitions. Simple emotion label matching fails to capture the complex transition patterns between emotional states, resulting in mechanical and jumpy emotional responses from intelligent agents.
[0017] Therefore, this application decomposes the emotional association into two key dimensions: static similarity and dynamic transition. Breaking through the limitations of traditional single emotional label, the vector E i The multidimensional nature of emotion is represented by a graph, where each dimension corresponds to the intensity value of a different emotion category. The emotional transition probability matrix M is introduced to quantify the rationality of transitions between different emotional states based on psychological research results, solving the problem that simple matching cannot assess the naturalness of emotional transitions.
[0018] The emotional transition probability matrix M can be constructed using Russell's Circumplex Model, which calculates transition probabilities based on the distance between emotional states in the two-dimensional space of "pleasure-arousal," or Plutchik's Wheel of Emotions, which defines transition naturalness based on the adjacent, opposite, and combined relationships in the wheel. Specifically, the matrix dimension is n×n, where n is the number of basic emotion categories defined by the system (usually 6-8). The matrix element M[i][j] represents the transition naturalness from emotion i to emotion j, with a value range of [0, 1].
[0019] Furthermore, according to the association graph, starting from the data with the highest priority, the community discovery algorithm is used to obtain M1 data with the highest correlation as the filtering result, including: according to the priority P(i) of the data item, the data item with the highest priority is selected as the seed node; according to the constructed association graph, the time series enhancement graph is obtained by setting the emotional time series weight for the edge; according to the time series enhancement graph, the time window length t is set. w And follow the step size δ t Perform window sliding to capture sentiment change patterns within each time window, thereby identifying data communities with close correlations and coherent sentiment development; calculate the importance score Imp(C) of each community based on the identified data communities; based on the importance score Imp(C), start from the community with the highest score and select data in chronological order of the data items until M1 data items are obtained as the final filtering result; if the number of data items in the first community is less than M1, continue selecting from the next community based on the score until M1 data items are reached.
[0020] In particular, traditional community detection algorithms like Louvain are designed for static networks, treating all node relationships equally and ignoring the temporal dependencies between data items. In emotional interaction scenarios, emotional states exhibit distinct temporal continuity and gradual progression. Communities detected by static community detection algorithms may contain data items spanning significant time periods, resulting in fragmented emotional development trajectories and an inability to support a coherent emotional interaction experience.
[0021] Therefore, this application proposes a community discovery algorithm based on time series enhancement: select seed nodes according to the priority P(i) of the data item to ensure that the retrieval starting point is most relevant to the current interaction, avoiding the problem of indiscriminate starting points in traditional community detection; set emotional time series weights for the edges of the associated graph, significantly improve the connection strength of those that are close in time and have natural emotional transitions, so that the graph structure reflects the natural law of emotional development; by setting the time window length t w And by step size δ t Sliding captures emotional change patterns over different time periods, enabling accurate identification of dynamic emotional development trajectories. An importance score, Imp(C), is calculated based on the internal coherence and priority of communities, providing an objective basis for subsequent selection and addressing the difficulty in quantifying community value in traditional methods. Starting with the most important communities, selection is made according to the chronological order of data items, ensuring that retrieval results maintain a natural temporal sequence of emotional development rather than simply prioritizing them. Compared to traditional community detection methods, this temporally enhanced community discovery algorithm significantly improves the agent's ability to capture emotional development trajectories, enabling memory retrieval results to form a coherent emotional context, thereby supporting a more natural and fluid emotional interaction experience. This is particularly suitable for applications requiring long-term emotional interaction, such as intelligent companionship.
[0022] Furthermore, the sentiment time series weight is set using the following formula:
[0023] W(i,j)=W orig (i,j)×(1+λ×SEQ(i,j))
[0024] Among them, W orig (i, j) is the comprehensive correlation; SEQ(i, j) is the emotional temporal coherence index;
[0025]
[0026] Among them, T i and T j are the timestamps of data items i and j respectively; θ t is the temporal adjacent threshold; ET(i,j) is the naturalness of emotional transition; λ is the emotional temporal enhancement coefficient;
[0027] Specifically, through an edge weight enhancement mechanism, the dynamic characteristics of temporal development and emotional change are explicitly encoded into the graph structure, enabling static graph-theoretic algorithms to handle dynamic emotional development trajectories. The enhanced edge weights create closer connections between temporally continuous data items with natural emotional transitions, increasing the probability that they will be simultaneously identified as a community. Multi-dimensional community importance assessment ensures that the selected memory fragments are both relevant and coherent, avoiding the fragmentation problem caused by traditional methods that select only on a single dimension.
[0028] Furthermore, the importance score Imp(C) of each community is calculated using the following formula:
[0029] Imp(C)=w p ×AvgP(C)+w c ×Coh(C); where Imp(C) represents the importance score of community C;
[0030] AvgP(C) represents the average priority score of all nodes in community C; Coh(C) represents the cohesion of community C, which is calculated by the ratio of the edge weight within the community to the total edge weight of the community; w p and w c Represent the weight coefficients of average priority and cohesion respectively;
[0031] In particular, through priority calculation and community importance assessment, the system can prioritize the most relevant and important memory information within limited computing resources, avoiding indiscriminate processing of all historical data, improving system operation efficiency, and reducing resource consumption.
[0032] Furthermore, S5 uses the filtered result of step S4 as memory information, inputs the memory information into the large language model through the prompt word project, and generates reply content, including: setting different identifiers for semantically similar fragments, structured metadata, and memory summaries in the memory information; constructing a prompt word template, the prompt word template including an instruction part and a context part; defining the role positioning, dialogue style, and reply strategy based on emotional relevance of the intelligent agent in the instruction part; setting the filling position of the user input request and memory information in the context part; filling the user's input request and the memory information after setting the identifier into the constructed prompt word template to generate a complete prompt word; and inputting the complete prompt word into the large language template to generate reply content.
[0033] In particular, through the optimized design of the prompt word engineering, special identifiers are set for different types of memory information, and emotional association strategies are defined in the prompt word template, so that the large language model can generate more coherent and emotionally rich response content, enhancing the consistency of the user's interactive experience.
[0034] Furthermore, S7 processes the new interaction record and the historical interaction records stored in the vector database and the relational database through the large language model to generate a memory summary, including: extracting key information including the topic and emotional tendency from the new interaction record; based on the key information, retrieving historical interaction records semantically related to the new interaction record from the vector database; based on the key information, retrieving structured metadata related to the new interaction record from the relational database; sorting the retrieved historical interaction records and structured metadata according to the timeliness score and the relevance score, and selecting N3 historical interaction records that are most relevant to the new interaction record, and the corresponding structured metadata; based on the selected historical interaction records and the corresponding structured metadata, combined with the new interaction record, constructing a summary generation prompt word containing a summary generation instruction; inputting the summary generation prompt word into the large language model to generate a memory summary.
[0035] Furthermore, S8 compares the new interaction record with the historical interaction record, identifies and processes memory fragments with overlapping or logically contradictory information, and updates the structured metadata in the relational database, including: converting the new interaction record formed in S6 into a vector representation; retrieving historical interaction records with a semantic similarity with the new interaction record higher than a preset threshold from the vector database; retrieving structured metadata related to the topic in the new interaction record from the relational database; comparing the new interaction record with the retrieved historical interaction record and structured metadata through the large language model to identify content with overlapping or logically contradictory information; for the identified overlapping information content, retain the latest information based on the calculated timeliness score and mark the duplicate information; for the identified logically contradictory content, construct a contradiction resolution prompt word, input the contradictory content into the large language model, and generate a conflict resolution strategy; based on the processing results, update the corresponding structured metadata in the relational database, and add a confidence mark and an update timestamp to the updated data.
[0036] Compared with the existing technology, the advantages of this application are:
[0037] (1) Through the dual storage architecture combining vector database and relational database, the comprehensive preservation and efficient management of interactive data are achieved, overcoming the memory fragmentation problem caused by the traditional context window length limitation, enabling the intelligent agent to maintain the interactive memory with the user for a long time, and significantly improving the long-term memory capacity of the intelligent agent.
[0038] (2) An emotional temporal enhancement community detection algorithm is introduced. The naturalness of emotional transitions between data items is evaluated through the SEQ(i, j) emotional temporal coherence index. A temporal enhancement graph is used to replace the traditional static graph to effectively capture the dynamic change patterns in emotional interactions, enabling the intelligent agent to maintain the coherence and natural transition of emotional responses, thereby reducing the sense of emotional jumps.
[0039] (3) By comprehensively considering semantic relevance, temporal relevance, and emotional relevance, a multidimensional correlation graph is constructed, and community discovery is performed based on the graph, thereby achieving accurate screening of memory data. Compared with simple keyword matching or similarity calculation, the retrieval results are more in line with the current emotional context. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] The present application will be further described in the form of exemplary embodiments, which will be described in detail with reference to the accompanying drawings. These embodiments are not limiting, and in these embodiments, the same numbers represent the same structures, wherein:
[0041] Figure 1 is an exemplary flow chart of a method for modeling long-term memory of an intelligent agent based on a memory network according to some embodiments of the present application;
[0042] Figure 2 is a component architecture diagram according to some embodiments of the present application;
[0043] Figure 3 It is a technical roadmap shown in some embodiments of this application. DETAILED DESCRIPTION
[0044] The method and system provided in the embodiments of the present application are described in detail below with reference to the accompanying drawings.
[0045] like Figure 1 As shown, the interaction data between the intelligent agent and the user is collected, the interaction data is converted into vector data, and structured metadata is extracted from the interaction data; the vector data is stored in a vector database, and the structured metadata is stored in a relational database; the user's input request is received, and vector similarity retrieval is performed in the vector database to obtain semantically similar fragments; structured data query is performed in the relational database to obtain structured metadata; and the generated and stored memory summary is retrieved; the semantically similar fragments, structured metadata and memory summary are merged to form a candidate data set; the candidate data set is filtered; the filtered results are used as memory information, and the memory information is input into a large language model through a prompt word project to generate reply content; the user's input request and the generated reply content are combined to form a new interaction record, structured metadata is extracted from the interaction record and stored in a relational database, and the interaction record is converted into vector data and stored in the vector database; the new interaction record and the historical interaction records stored in the vector database and the relational database are processed by the large language model to generate a memory summary; the new interaction record and the historical interaction record are compared with the historical interaction record, memory fragments with overlapping information or logical contradictions are identified and processed, and the structured metadata in the relational database is updated.
[0046] like Figure 2As shown, the intelligent long-term memory modeling system based on the memory network proposed in this embodiment adopts a three-layer architecture design, including a model service layer, a memory core processing layer and a memory persistence layer. As the underlying capability support of the architecture, the model service layer includes two core modules: Large language model service: provides natural language understanding and generation capabilities, and supports high-level functions such as dialogue logic and memory extraction. This embodiment adopts a pre-trained language model with a parameter volume of 100B, and optimizes the emotional interaction capability through RLHF technology to realize prompt word engineering and reply generation. Vectorization service: converts text memory (user historical conversations, knowledge fragments, etc.) into a high-dimensional vector to provide a basis for subsequent semantic retrieval. This embodiment adopts a vector representation with a dimension of 1536, uses the BERT model for text embedding, and supports the implementation of step S1.
[0047] The memory core processing layer realizes memory retrieval and dynamic management through multi-level processing modules. Its components include: Memory information filtering and sorting: Implementing filtering mechanism to remove noise from original memory based on multiple dimensions such as timeliness and relevance. In the specific implementation, the timeliness weight w t Set to 0.4, the correlation weight w r It is set to 0.6, which is suitable for the long-term interactive characteristics of emotional companionship scenarios.
[0048] Memory Retrieval and Extraction: Combining similarity search and structured query in a vector database enables rapid recall of multi-source memories. In this example, a cosine similarity threshold of 0.75 is set to retrieve semantically similar segments from the vector database; simultaneously, structured metadata from the last 30 days is obtained from the relational database.
[0049] Emotional Correlation Modeling Module: This module implements multi-dimensional correlation calculations. Emotion vectors are represented using an 8-dimensional representation (joy, sadness, anger, fear, surprise, disgust, trust, and expectation) and extracted using a pre-trained emotion classifier. The emotion transition probability matrix M is constructed based on the Plutchik emotion wheel theory and optimized and calibrated using 100,000 annotated conversation data points.
[0050] Temporal Enhanced Community Discovery: Implement the algorithms in claims 7.3, 8.7, and 9.7. Time window length t w Set to 7 days, step size δ t is 1 day, the sentiment temporal enhancement coefficient λ is 0.8, and the average priority weight w in the community importance score p is 0.6, and the cohesion weight w c is 0.4.
[0051] Memory extraction and summarization: The large language model compresses and abstracts the original memory to generate a high-level memory summary, implementing step S7. Memory summaries are generated every 100 interactions or every 24 hours to ensure the timeliness of memory summaries.
[0052] Memory update mechanism: This mechanism supports memory addition, deletion, modification, and query operations, dynamically maintaining the timeliness and consistency of the memory relationship chain, and implementing step S8. The conflict detection threshold is set to 0.85. When the conflict between the new and old memories exceeds the threshold, the new memory is retained first and the old memory is marked as obsolete.
[0053] Prompt word engineering: Dynamically inject memory search results into the large language model prompt word to generate responses. The prompt word template includes three parts: role definition, memory guidance, and emotional response strategy. It also provides dedicated identifiers for different memory types: [SEM] semantic fragment, [STR] structured metadata, and [SUM] memory summary.
[0054] The memory persistence layer implements long-term memory storage and includes: MySQL database: stores structured memory metadata, including memory IDs, timestamps, topic tags, sentiment tags, etc. The table structure design includes the main table memory_items and the relationship table memory_relations, which implements step S2.
[0055] Vector database: implemented using Milvus, which stores vectorized representations of memories, supports cosine similarity retrieval, and uses the HNSW index type to achieve efficient approximate nearest neighbor search.
[0056] like Figure 3 As shown in the figure, taking the user query "You said yesterday that restaurant was delicious!" as an example, the system processes it according to the following process:
[0057] The user input "You said yesterday that restaurant was delicious!" is transmitted to the intelligent agent processing module through the front-end HTTP / WebSocket interface; the vectorization service calls the improved BERT-Large model to convert the input into a 1536-dimensional vector, and uses L2 regularization to ensure that the vector modulus is 1 to facilitate subsequent cosine similarity calculation; the NLP preprocessing module extracts the time indicator "yesterday" (converted to the specific date 2025-05-31) and the keyword "restaurant" (classified as "catering / food") from the input; the sentiment analysis engine detects the positive emotion in the user input (the detection value is "expectation / positive", with an intensity of 0.72) and uses this as the sentiment filter condition for the retrieval.
[0058] Vector database search: Execute ANN queries in the Milvus vector database using the HNSW indexing algorithm; set the nprobe parameter to 128 to optimize recall, and use the inner product (equivalent to cosine similarity) to calculate the search distance. Obtain five semantically similar fragment records with a similarity greater than 0.75, including: "The truffle pasta at LaVita, an Italian restaurant, is definitely worth recommending, especially their handmade pasta..." (similarity 0.92); "If you like Asian flavors, the Tom Yum Goong at Sawadee, a Thai restaurant I mentioned yesterday..." (similarity 0.87); and [three other related records].
[0059] Execute the optimized SQL query: SELECT * FROM memory_items WHERE DATE(timestamp) = DATE_SUB(CURDATE(), INTERVAL1DAY) AND topic_tagsLIKE '%restaurant%' AND emotion_tagsLIKE '%positive%' ORDER BY relevance_score DESCLIMIT10; add sentiment filtering conditions to prioritize returning records that match the current user's sentiment; use a composite index of topic_tags and timestamp to improve query performance; obtain 8 structured metadata, including memory ID, timestamp, topic tag, emotion tag, etc.
[0060] Memory Summary Retrieval: Retrieve the three most recently generated memory summaries from the summary cache (updated every 24 hours). Use vector similarity calculation to retrieve the two most relevant historical memory summaries for the current input. Example summary: "The user expressed a preference for both Italian and Thai cuisine, particularly enjoys trying new restaurants, and has a habit of dining out on weekends..." A duplicate content merging algorithm is applied to remove duplicate information and combine them to form 15 candidate memory data items. Each data item is assigned a unique ID to facilitate subsequent graph construction.
[0061] Calculate the semantic relevance using the formula Calculate the semantic similarity between any two memories; for example, the semantic association between data item 1 and data item 2:
[0062] Sem(1,2)=cos([0.12,0.35],[0.1,0.33])=0.87.
[0063] Calculate the timing correlation using the formula Where τ = 86400 (number of seconds in a day), controls the time decay rate; I(T i ,T j ) Timing indicator function implementation: When T i Before T jIf the time interval is less than 3 hours, the value is 1; otherwise, the value is 0.4. For example, two memories separated by 2 hours:
[0064] Calculate the emotional relevance using the formula Emo(i,j)=α e ×ES(i,j)+(1-α e )×ET(i,j), Emo(i,j)=0.3×ES(i,j)+0.7×ET(i,j); the emotion vector is represented by 8 dimensions:
[0065] E i = [0.8, 0.1, 0, 0, 0.6, 0, 0.5, 0.7], corresponding to [joy, sadness, anger, fear, surprise, disgust, trust, expectation]; emotional label similarity: ES(i, j) = cos(E i ,E j )=0.89; Naturalness of emotional transition:
[0066] Among them, the emotional transition probability matrix M[0][4]=0.95 indicates that the probability of natural transition from "joy" to "surprise" is high; the comprehensive emotional correlation: Emo(i, j)=0.3*0.89+0.7*0.83=0.85.
[0067] Calculate comprehensive correlation, comprehensive correlation
[0068] W orig (i,j)=0.4×Sem(i,j)+0.3×Temp(i,j)+0.3×Emo(i,j);
[0069] W orig (i, j) = 0.4 × 0.87 + 0.3 × 0.92 + 0.3 × 0.85 = 0.88; set the threshold to 0.6, retain the edges with weight ≥ 0.6, and construct a weighted undirected graph; finally, we obtain an association graph containing 15 nodes and 12 edges.
[0070] Build a time-order enhanced community and set the sentiment time-order weight W(i,j)=W orig (i,j)×(1+0.8×SEQ(i,j)), calculate the emotional temporal coherence index Time adjacent threshold θ t Set to 4 hours (14400 seconds), The enhanced edge weight is: W(3,7)=0.88×(1+0.8×0.415)=1.17.
[0071] Window length t w Set to 1 day, step size δ tThe time interval is 6 hours. Subgraphs are constructed and community detection is performed in four windows: [yesterday 00:00, yesterday 06:00], [yesterday 06:00, yesterday 12:00], [yesterday 12:00, yesterday 18:00], and [yesterday 18:00, today 00:00]. Continuous conversations about restaurants among users in the [yesterday 12:00, yesterday 18:00] window are captured, forming the main emotional context.
[0072] The improved Louvain algorithm was implemented. Initially, each node was treated as an independent community. In the first stage, the modularity gain ΔQ of node i moving to community C was iteratively calculated, while considering temporal consistency. In the second stage, the discovered communities were regarded as "supernodes", and a new graph was constructed, repeating the process until the modularity no longer increased. After the algorithm was enhanced with temporal weights, it tended to group memories that were temporally continuous and emotionally similar into the same community. Finally, three communities were identified: C1 (6 nodes), C2 (5 nodes), and C3 (4 nodes).
[0073] Calculate community importance scores,
[0074] Imp(C)=w p ×AvgP(C)+w c × Coh(C) = 0.6 × AvgP(C) + 0.4 × Coh(C), the average priority of community C1: The cohesion of community C1: Coh(C1) = internal edge weight sum / total edge weight sum = 4.85 / 5.20 = 0.933; the importance score of community C1: Imp(C1) = 0.6×0.815+0.4×0.933 = 0.862; the importance scores of each community: C1 (0.862) > C2 (0.756) > C3 (0.645).
[0075] Memories are selected from the most important community, C1, in chronological order. C1 contains the following memory items: {1, 3, 5, 9, 11, 14}. Sorted by timestamp, four memories are selected: {3, 5, 9, 14} (in chronological order from earliest to latest). Setting M1 to 6 requires two additional memories. From the second most important community, C2, two memories are selected that are most similar in sentiment to C1 and closest in time to the selected memories: {7, 12}. Combining the selections from C1 and C2, the results are arranged in chronological order: {3, 5, 7, 9, 12, 14}. A deduplication algorithm is applied to ensure that content is not repeated. Emotional coherence is verified to ensure that the naturalness of emotional transitions is greater than 0.7. Finally, six memories are selected as the filtered results, forming a complete emotional context for restaurant recommendations.
[0076] To generate a response, first add a memory identifier. Add identifiers for semantically similar segments: [SEM] Yesterday I recommended the truffle pasta at LaVita Italian Restaurant. They use imported black truffles and homemade handmade noodles. Add identifiers for structured metadata: [STR] 2025-05-31 14:23 | Subject: Restaurant Recommendation | Sentiment: Positive | User Response: Interested. Add identifiers for the memory summary: [SUM] The user shows continued interest in Italian cuisine, particularly handmade pasta, and mentioned wanting to try a new restaurant this weekend.
[0077] Construct a prompt template. [Command]: You are an emotional companion AI assistant. Please provide a natural and coherent response based on the user's emotional state and past communication content. When discussing past conversations, maintain emotional continuity and avoid stilted memory playback. Replies should maintain a warm and friendly tone, appropriately expressing empathy and support based on the positive emotions expressed by the user. [Context]: User input: {user_input}. Related memories: {memory_items}. Please generate a natural and fluent response based on the above information. Avoid directly repeating memory content and instead integrate information into the conversation.
[0078] Fill in and assemble the prompt words: Fill in the user input part with: "You said that restaurant was delicious yesterday!", fill in the memory part with the 6 processed memory information, and add system instructions: "Maintain a 4:6 ratio of recall to new information, and control the response length to 80-120 words."
[0079] The large model generates responses with a temperature setting of 0.7 to increase response diversity while maintaining consistency. The top_p parameter of 0.92 is used to control the sampling range. A sentiment consistency filter is applied to ensure that the output sentiment matches the user input. Finally, the generated response is: "Yes, we talked about that LaVita Italian restaurant yesterday! Their truffle pasta is really outstanding, using imported black truffles and freshly made handmade pasta, and it tastes excellent. You were very interested in trying it that weekend. If you decide to go, it is recommended to make a reservation in advance, as they are often fully booked on weekends. Their tiramisu is also a signature dessert and well worth a try!"
[0080] To update the memory, first generate a new interaction record, combining user input and system response to form a new interaction record; the record ID is automatically generated as MEM-20250601-08742; the record contains complete context information, emotional state and timestamp.
[0081] Extract and store structured metadata, automatically extract topic tags: [restaurant recommendation, Italian food, weekend plan]; sentiment tags: [positive, anticipation]; intent recognition: [information confirmation, seeking advice]; execute SQL: INSERT INTO memory_items(id, timestamp, content, topic_tags, emotion_tags, intent_tags) VALUES('MEM-20250601-08742', '2025-06-01 10:23:45', '...').
[0082] Store vector data, vectorize the complete interaction record (user input + system response), and execute in Milvus: collection.insert([{'id': 'MEM-20250601-08742', 'vector': [0.13, 0.25, ...], 'text': '...'}]).
[0083] When the important topic "restaurant preference" is detected, memory extraction is triggered; asynchronous task ID: TASK-20250601-4312, priority: medium is started; interactions about restaurant preferences in the past 30 days are summarized and key information is extracted using the improved TextRank algorithm; a new memory summary is generated: "The user prefers Italian and Thai cuisine, pays special attention to handmade ingredients and unique flavors, plans to try LaVita restaurant on the weekend, and is also interested in desserts."
[0084] Execute the semantic contradiction detection algorithm to compare the new and old information; detect that the user previously mentioned (on May 20) that they "don't like desserts," but now express an interest in tiramisu; calculate the contradiction degree to be 0.87, which exceeds the threshold of 0.85; perform an update operation, and mark the old memory as "updated" in the database. Add an associated record: INSERT INTO memory_updates(old_id, new_id, update_type, timestamp) VALUES('MEM-20250520-04231', 'MEM-20250601-08742', 'preference_change', '2025-06-01 10:25:12'). At this point, this embodiment uses the community discovery algorithm enhanced by emotional temporal sequence to extract a coherent emotional development trajectory from fragmented memories, providing users with personalized responses that are both relevant and emotionally coherent. At the same time, it effectively handles information updates and contradictions, maintaining the consistency and timeliness of long-term memory.
[0085] The invention of the present application and its implementation methods are described schematically above. This description is not restrictive. Without departing from the spirit or basic features of the present application, the present application can be implemented in other specific forms. What is shown in the accompanying drawings is only one of the implementation methods of the invention of the present application, and the actual structure is not limited to this. Therefore, if a person of ordinary skill in the art is inspired by it, without departing from the purpose of the invention, a structural method and embodiment similar to the technical solution are designed without creativity, which should all fall within the scope of protection of the present application. In addition, the word "including" does not exclude other elements or steps, and the word "one" before an element does not exclude the inclusion of "multiple" elements. Words such as first and second are used to indicate names and do not indicate any specific order.
Claims
1. A method for modeling long-term memory of an intelligent agent based on a memory network, characterized in that: include: S1, collects the interaction data between the agent and the user, converts the interaction data into vector data, and extracts structured metadata from the interaction data; S2, stores vector data in a vector database and stores structured metadata in a relational database; S3 receives the user's input request, performs vector similarity search in the vector database to obtain semantically similar segments; and performs structured data query in the relational database to obtain structured metadata; and retrieve the memory summary generated and stored in step S7; merge the semantically similar segments, structured metadata, and memory summary to form a candidate data set; S4, filtering candidate data sets; S5, taking the result of the filtering in step S4 as memory information, inputting the memory information into the large language model through the prompt word engineering to generate the reply content; S6, combining the user's input request and the generated reply content to form a new interaction record, extracting structured metadata from the interaction record and storing it in a relational database, and converting the interaction record into vector data and storing it in the vector database; S7, uses a large language model to process new interaction records and historical interaction records stored in vector databases and relational databases to generate memory summaries; S8, compares new interaction records with historical interaction records, identifies and processes memory fragments with overlapping information or logical contradictions, and updates the structured metadata in the relational database.
2. The method for modeling long-term memory of an intelligent agent based on a memory network according to claim 1, characterized in that: S3, forming a candidate dataset, including: Convert the user's input request into vector data; Based on the vector data, the cosine similarity is calculated in the vector database, and the vector data with a cosine similarity greater than a threshold is retrieved to obtain the corresponding semantically similar segments; Extract timestamps and keywords from user input requests, construct structured query statements, execute queries in relational databases, and obtain structured metadata; Retrieving the memory summary generated and stored in step S7 includes: obtaining the most recent N1 memory summaries generated in chronological order, and obtaining the N2 historical memory summaries with the highest relevance to the user's input request based on vector similarity calculation; The acquired semantically similar segments, structured metadata, and memory summaries are combined to form a candidate dataset.
3. The method for modeling long-term memory of an intelligent agent based on a memory network according to claim 2, characterized in that: S4, filter the candidate datasets, including: Based on the candidate dataset, calculate the timeliness score and relevance score for each data item. The timeliness score is inversely proportional to the time difference between the data item's timestamp and the current timestamp. The relevance score is calculated based on the cosine similarity between the data item's vector and the user's input request vector. According to the timeliness score and relevance score, calculate the priority P(i) of each data item, P(i) = w t ×T(i)+w r ×R(i), where T(i) represents the timeliness score of data item i; R(i) represents the relevance score of data item i; w t and w r Represent the weight coefficients of timeliness and relevance respectively; Construct a data association graph, where the nodes of the association graph are data items and the edges represent the association relationships between data; According to the association graph between data, starting from the data with the highest priority, the community discovery algorithm is used to obtain the M1 data with the highest correlation as the filtering result.
4. The method for modeling long-term memory of an intelligent agent based on a memory network according to claim 3, wherein: Construct a data association graph, including: Calculate the semantic relevance, temporal relevance and sentiment relevance between any two data in the candidate dataset respectively; According to the semantic relevance, temporal relevance and emotional relevance, the comprehensive relevance W between each data is calculated. orig (i,j); Take data items as nodes and comprehensive correlation degree W orig The association relationship (i, j) greater than the preset threshold is regarded as an edge, and an undirected weighted association graph between data items is constructed.
5. The method for modeling long-term memory of an intelligent agent based on a memory network according to claim 4, characterized in that: Calculate the semantic relevance using the following formula: Among them, Sem(i,j) represents the semantic association between data item i and data item j; V i represents the vector representation of data item i; V j represents the vector representation of data item j; Calculate the timing correlation using the following formula: Among them, Temp(i,j) represents the temporal correlation between data item i and data item j; T i Represents the timestamp of data item i; T j represents the timestamp of data item j; τ represents the time decay factor, which is used to control the influence of time difference on the correlation degree; I(T i ,T j ) represents the timing indicator function, when T i Before T j And the time interval is less than the preset threshold θ t The value is 1 when , otherwise it is β t (where 0<β t <1).
6. The method for modeling long-term memory of an intelligent agent based on a memory network according to claim 4, characterized in that: Calculate the emotional relevance using the following formula: Emo(i,j)=a e ×ES(i,j)+(1-a e )×ET(i,j) Among them, Emo(i,j) represents the emotional correlation between data item i and data item j; α e represents the balance factor, with a value range of [0, 1]; ES(i, j) represents the sentiment tag similarity, which is calculated by the following formula: Among them, E i The sentiment vector of data item i contains the intensity values of different sentiment categories; E j represents the sentiment vector of data item j; ET(i,j) represents the naturalness of emotional transition and is calculated using the following formula: Where M represents the emotion transition probability matrix; a i represents the dominant sentiment category index of data item i; a j represents the dominant sentiment category index of data item j; T s Indicates the standard time parameter for emotion conversion.
7. The method for modeling long-term memory of an intelligent agent based on a memory network according to claim 3, wherein: Based on the association graph, starting from the data with the highest priority, the community discovery algorithm is used to obtain the M1 data with the highest correlation as the filtering result, including: According to the priority P(i) of the data item, the data item with the highest priority is selected as the seed node; According to the constructed association graph, the time series enhanced graph is obtained by setting the sentiment time series weight for the edge; According to the time series enhancement graph, by setting the time window length t w And follow the step size δ t Perform window sliding to capture the emotion change pattern within each time window, thereby identifying data communities with close correlations and coherent emotion development; Based on the identified data communities, calculate the importance score Imp(C) of each community; According to the importance score Imp(C), starting from the community with the highest score, data are selected in the chronological order of the data items until M1 data are obtained as the final filtering result; If the number of data items in the first community is less than M1, the selection continues from the next community according to the score until M1 data items are reached.
8. The method for modeling long-term memory of an intelligent agent based on a memory network according to claim 7, characterized in that: The emotional time series weight is set using the following formula: W(i,j)=W orig (i,j)×(1+λ×SEQ(i,j)) Among them, W orig (i, j) is the comprehensive correlation; SEQ(i, j) is the emotional temporal coherence index; Among them, T i and T j are the timestamps of data items i and j respectively; θ t is the temporal adjacent threshold; ET(i,j) is the naturalness of emotional transition; λ is the emotional temporal enhancement coefficient.
9. The method for modeling long-term memory of an intelligent agent based on a memory network according to claim 7, characterized in that: Calculate the importance score Imp(C) of each community using the following formula: Imp(C)=w p ×AvgP(C)+w c ×Coh(C) Among them, Imp(C) represents the importance score of community C; AvgP(C) represents the average priority score of all nodes in community C; Coh(C) represents the cohesion of community C, which is calculated by the ratio of the edge weight within the community to the total edge weight of the community; w p and w c Represent the weight coefficients of average priority and cohesion respectively.
10. The method for modeling long-term memory of an intelligent agent based on a memory network according to claim 6, characterized in that: S5: The filtered result of step S4 is used as memory information, and the memory information is input into the large language model through the prompt word engineering to generate the reply content, including: Different identifiers are set for semantically similar fragments, structured metadata, and memory summaries in memory information; Constructing a prompt word template, which includes an instruction part and a context part; defining the role positioning of the agent, the dialogue style, and the response strategy based on the emotional relevance in the instruction part; setting the filling position of the user input request and memory information in the context part; Fill the user's input request and the memory information after setting the identifier into the constructed prompt word template to generate a complete prompt word; Enter the complete prompt words into the large language template to generate the reply content.
Citation Information
Patent Citations
Information display method, device and equipment, and readable storage medium
CN110728983A
Associative long-term memory method for simulating human memory function
CN118364860A
Automatic optimization method and system based on intelligent data memory bank
CN118503390A
Intelligent dialogue method and system
CN119075300A
Interaction method, device and equipment based on memory system, storage medium and vehicle
CN119293191A
Cited By
Intelligent agent memory management method and device based on asynchronous processing and storage medium
CN121598989A
Intelligent agent memory management method and device based on asynchronous processing and storage medium
CN121598989B
Hybrid memory management method and system for large model agent
CN121599075A
Intelligent agent memory management method based on multi-layer architecture, electronic equipment and computer program product
CN121681775A
Hierarchical memory and context awareness retrieval method of role large model and related products
CN121743515A