An agent long-term memory modeling method based on a memory network
By employing a long-term memory modeling method for intelligent agents based on memory networks, combined with a dual-storage architecture and a community detection algorithm that enhances emotional temporal sequence, the problems of memory gaps and emotional jumps in intelligent companionship technology are solved. This enables the continuity and natural transition of emotional interaction between intelligent agents, thereby improving the user experience.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-16
- Publication Date
- 2026-03-03
AI Technical Summary
Existing intelligent companion technologies have shortcomings in long-term memory management and the continuity of emotional interaction, which makes it impossible for intelligent agents to fully understand the user's emotional development trajectory, resulting in memory gaps and emotional jumps, thus affecting the user experience.
We employ a long-term memory modeling method for intelligent agents based on memory networks, manage interactive data through a dual storage architecture, and introduce a community detection algorithm that enhances emotional temporal sequence. We construct a multi-dimensional association graph for memory filtering to ensure the relevance and coherence of retrieval results.
It significantly improves the long-term memory capacity and emotional interaction coherence of intelligent agents, providing a more natural and smooth emotional interaction experience, and is suitable for application scenarios that require long-term emotional interaction, such as intelligent companionship.
Smart Images

Figure CN120688545B_ABST
Abstract
Description
Technical Field
[0001] This 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 Technology
[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 LLMs, AI agents can interact with users in real time through text, voice, and other forms, providing emotional interaction and psychological support, thus realizing "intelligent companionship" scenarios. These agents can not only understand users' semantic needs but also perceive their emotional states, providing a more natural and humanized interactive experience. Currently, intelligent companionship is widely used in various scenarios such as mental health support, elderly care, and educational guidance, 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, increasing user stickiness and satisfaction.
[0003] However, existing intelligent companionship technologies still have significant shortcomings in long-term memory management and the continuity of emotional interaction. Specifically, during the dialogue between the AI agent and the user, the large language model loads the most recent dialogue record between the agent and the user in a "context window" as prompts for the model's response output, which is then input into the large model to generate the next round of responses. Due to the technical limitations of the large language model, the length of its "context window" has a fixed upper limit. When the dialogue record between the AI agent and the user exceeds the length of the large model's "context window," the system truncates the old content, resulting in the loss of historical dialogue content.
[0004] This "short-term memory" mechanism exhibits significant shortcomings in long-term emotional interaction scenarios: as the interaction deepens, a large amount of important historical information is discarded, making it impossible for the agent to gain a comprehensive and coherent understanding of the user; emotional interaction has obvious temporal and gradual characteristics, and when historical emotional state information is lost, the agent struggles 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 inconsistent interactions during long-term emotional interactions, severely impacting user experience and effectiveness. Therefore, there is an urgent need for an intelligent agent memory modeling method that can effectively manage long-term memory and maintain the continuity of emotional interactions. Summary of the Invention
[0006] To address the insufficient coherence of emotional interactions in long-term memory of intelligent agents, this application provides a method for modeling long-term memory of intelligent agents based on memory networks. By managing the dual storage architecture and introducing a community detection algorithm with enhanced emotional timing to optimize memory filtering, the method improves the coherence of emotional interactions in intelligent companionship scenarios.
[0007] This application provides a method for modeling long-term memory of an intelligent agent based on a memory network, comprising: S1, collecting interaction data between the agent and the 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 the structured metadata in a relational database; S3, receiving the 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 dataset; S4, filtering the candidate dataset; S5, using the result filtered in step S4 as memory information, inputting the memory information into a large language model through prompt word engineering to generate response 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, ByteDance's Doubao, Zhipu AI's ChatGLM series, etc. S6. Combine the user's input request and the generated response content to form a new interaction record. Extract structured metadata from the interaction record and store it in a relational database. Convert the interaction record into vector data and store it in a vector database. S7. Process the new interaction record and the historical interaction records stored in the vector database and the relational database using a large language model to generate a memory summary. S8. Compare the new interaction record and 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] Further, in step S3, a candidate dataset is formed, 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 cosine similarity greater than a threshold, and obtaining corresponding semantically similar segments; 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 summaries generated and stored in step S7, including: obtaining the N1 most recent 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; and combining the obtained semantically similar segments, structured metadata, and memory summaries to form a candidate dataset.
[0009] Further, S4, filtering the candidate dataset includes: calculating the timeliness score and relevance score for each data item based on the candidate dataset; 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 relevance score, where P(i) = w t ×T(i)+w r ×R(i), where, t represents the timeliness score of data item i. current t represents the current timestamp. i R represents the timestamp of data item i; R(i) represents the relevance score of data item i; w t and w r The weight coefficients for timeliness and relevance are respectively represented; a data association graph is constructed, where nodes in the association graph are data items and edges represent the association relationships between data; based on the data association graph, starting from the data with the highest priority, the M1 data with the highest relevance are obtained through a community detection algorithm as the filtering result.
[0010] In particular, traditional memory retrieval methods typically select the K highest-priority memories, ignoring the connections and emotional coherence between memories. However, emotional companionship scenarios require associative memories to construct a complete emotional narrative, rather than scattered high-scoring memory fragments. Simply relying on priority ranking leads to fragmented retrieval results, failing to form a coherent emotional interaction narrative.
[0011] Therefore, this application innovatively proposes a memory filtering method based on association graphs and community detection. First, by weighting the timeliness score and relevance score, it ensures that memory items that are recent in time and semantically related are given priority. Second, by constructing an association graph between data, the semantic, temporal, and sentiment relationships between memory items are explicitly expressed. Finally, starting from the highest priority data, a community detection algorithm is applied to obtain the M1 data items with the highest relevance, ensuring that the retrieval results are not only highly relevant but also have internal coherence.
[0012] Furthermore, a data association graph is constructed, including: calculating the semantic association, temporal association, and sentiment association between any two data points in the candidate dataset; and calculating the comprehensive association W between each data point based on the semantic association, temporal association, and sentiment association. orig (i,j); with data items as nodes and comprehensive correlation W orig For each (i,j) relationship greater than a preset threshold, an edge is formed, and an undirected weighted association graph is constructed between data items.
[0013] Furthermore, the semantic relevance is calculated using the following formula: Where Sem(i,j) represents the semantic association between data item i and data item j; V i V represents the vector representation of data item i; j The vector representation of data item j is used; the temporal correlation degree is calculated using the following formula: Where Temp(i,j) represents the temporal correlation between data item i and data item j; T i T represents the timestamp of data item i; j Represents the timestamp of data item j; τ represents the time decay factor, used to control the degree of influence of time differences on the correlation; 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 the condition is met, and β otherwise. t (where 0 < β) t <1).
[0014] Furthermore, the emotional relevance 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 sentiment correlation between data item i and data item j; α e The balancing factor is defined as [0, 1]; ES(i,j) represents the sentiment marker similarity, calculated using the following formula: Among them, E i E represents the sentiment vector of data item i, containing intensity values for different sentiment categories; j Let represent the sentiment vector of data item j; ET(i,j) represents the naturalness of the sentiment transition, calculated using the following formula: Where M represents the emotional transition probability matrix; a i This represents the dominant sentiment category index for data item i; a j T represents the dominant sentiment category index for data item j; s This represents the standard time parameter for emotional transition.
[0016] In particular, traditional methods for calculating memory association typically focus only on semantic or temporal dimensions, neglecting the emotional dimension. In emotional companionship scenarios, the coherent transition of emotional states is crucial, but current technologies lack a quantitative assessment mechanism for the naturalness of emotional shifts. Simple emotional label matching cannot reflect the complex transition patterns between emotional states, resulting in mechanical and disjointed emotional responses from the agent.
[0017] Therefore, this application decomposes emotional association into two key dimensions: static similarity and dynamic transition. It breaks through the limitations of traditional single emotional labels by adopting a vector E... i This represents the multidimensional characteristics of emotions, with each dimension corresponding to the intensity value of different emotion categories; drawing. An emotion transition probability matrix M is introduced, quantifying the rationality of transitions between different emotion states based on psychological research findings, thus solving the problem that simple matching cannot assess the naturalness of emotion transitions.
[0018] The emotional transition probability matrix M can be calculated using Russell's Circumplex Model, which calculates the transition probability based on the distance between emotional states in the two-dimensional space of "pleasure-arousal," or using Plutchik's Wheel of Emotions, which defines the naturalness of transition based on the adjacent, opposing, and combined relationships in the wheel of emotions. Specifically, the matrix dimension is n×n, where n is the number of basic emotional categories defined by the system (usually 6-8); the matrix element M[i][j] represents the naturalness of transition from emotion i to emotion j, with a value range of [0, 1].
[0019] Furthermore, based on the association graph, starting from the highest priority data, the community detection algorithm is used to obtain the M1 data points with the highest relevance as the filtering results. This includes: selecting the highest priority data item as the seed node based on the priority P(i); obtaining a time-enhanced graph by setting sentiment time-series weights for the edges based on the constructed association graph; and setting the time window length t based on the time-enhanced graph. w And according to step size δ t The system slides through windows to capture sentiment change patterns within each time window, thereby identifying data communities with close relationships and coherent sentiment development. Based on the identified data communities, an importance score Imp(C) is calculated for each community. Based on the importance score Imp(C), data is selected from the community with the highest score in chronological order of data items until M1 data items are obtained, which serve as the final filtering result. If the first community has fewer than M1 data items, the selection continues from the next community based on the score until M1 data items are obtained.
[0020] In particular, traditional community detection algorithms such as 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 have obvious temporal continuity and progressiveness. The communities detected by static community detection algorithms may contain data items with large time spans, causing breaks in the emotional development trajectory and failing to support a coherent emotional interaction experience.
[0021] Therefore, this application proposes a community detection algorithm based on temporal enhancement: Seed nodes are selected according to the priority P(i) of data items 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; sentiment temporal weights are set for the edges of the association graph, significantly improving the connection strength of temporally similar and naturally transitioning emotions, making the graph structure reflect the natural laws of emotional development; and the algorithm further enhances the community detection algorithm by setting the time window length t. w And according to step size δ t By sliding the algorithm across different time periods to capture emotional change patterns, it achieves accurate identification of dynamic emotional development trajectories. Based on the coherence and priority within communities, it calculates an importance score (Imp(C)) to provide an objective basis for subsequent selection, solving the problem of difficulty in quantifying community value in traditional methods. Starting with the most important community, it selects according to the chronological order of data items, ensuring that the retrieval results maintain the natural temporal sequence of emotional development, rather than a simple priority ranking. Compared with 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 thread, thus supporting a more natural and fluid emotional interaction experience. It is particularly suitable for application scenarios requiring long-term emotional interaction, such as intelligent companionship.
[0022] Furthermore, the emotional time sequence weights are set using the following formula:
[0023] W(i,j)=W orig (i,j)×(1+λ×SEQ(i,j))
[0024] Among them, W orig (i,j) represents the overall correlation; SEQ(i,j) represents the sentiment temporal coherence index.
[0025]
[0026] Among them, T i and T j These are the timestamps of data items i and j, respectively; θ t ET(i,j) is the temporal proximity threshold; ET(i,j) is the emotional transition naturalness; λ is the emotional temporal enhancement coefficient.
[0027] Specifically, through an edge weight enhancement mechanism, the dynamic characteristics of temporal development and emotional changes are explicitly encoded into the graph structure, enabling static graph theory algorithms to handle dynamic emotional development trajectories. The enhanced edge weights allow temporally continuous data items with natural emotional transitions to form tighter connections, increasing the probability that they are simultaneously identified as a community. Based on multi-dimensional community importance assessment, the selected memory fragments are ensured to be both relevant and coherent, avoiding the fragmentation problem caused by traditional methods that rely solely on a single dimension for selection.
[0028] Furthermore, the importance score Imp(C) for 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 within community C; Coh(C) represents the cohesion of community C, calculated as the ratio of the weight of edges within the community to the total weight of edges in the community; w p and w c These represent the weighting coefficients for average priority and cohesion, respectively.
[0031] In particular, by prioritizing calculations and assessing community importance, the system can prioritize processing the most relevant and important memory information with limited computing resources, avoiding indiscriminate processing of all historical data, improving system operating efficiency and reducing resource consumption.
[0032] Further, in step S5, the filtered result from step S4 is used as memory information. This memory information is then input into the large language model through prompt word engineering to generate response content. This includes: setting different identifiers for semantically similar segments, structured metadata, and memory summaries in the memory information; constructing a prompt word template, which includes an instruction part and a context part; defining the agent's role positioning, dialogue style, and response strategy based on emotional relevance in the instruction part; setting the user input request and the filling positions for the memory information in the context part; filling the constructed prompt word template with the user's input request and the memory information after setting the identifiers to generate complete prompt words; and inputting the complete prompt words into the large language model to generate response 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 templates, enabling the large language model to generate more coherent and emotionally rich response content, thereby enhancing the consistency of the user's interactive experience.
[0034] Furthermore, in S7, the new interaction record and historical interaction records stored in the vector database and relational database are processed by a large language model to generate a memory summary. This includes: extracting key information, including topic and sentiment, from the new interaction record; 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 based on the key information; ranking the retrieved historical interaction records and structured metadata based on timeliness and relevance scores, selecting the N3 most relevant historical interaction records and their corresponding structured metadata; constructing summary generation prompts containing summary generation instructions based on the selected historical interaction records and their corresponding structured metadata, combined with the new interaction record; and inputting the summary generation prompts into the large language model to generate the memory summary.
[0035] Furthermore, in step S8, the new interaction record and historical interaction records are compared to identify and process memory fragments with overlapping information or logical contradictions, and the structured metadata in the relational database is updated. This includes: converting the new interaction record formed in step S6 into a vector representation; retrieving historical interaction records from the vector database whose semantic similarity to the new interaction record is higher than a preset threshold; 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 records and structured metadata using a large language model to identify content with overlapping information or logical contradictions; for the identified overlapping information, retaining the most recent information based on the calculated timeliness score and marking duplicate information; for the identified logical contradictions, constructing contradiction resolution prompts, inputting the contradictory content into the large language model, and generating conflict resolution strategies; and updating the corresponding structured metadata in the relational database based on the processing results, and adding confidence markers and update timestamps to the updated data.
[0036] Compared to existing technologies, the advantages of this application are:
[0037] (1) By combining a vector database and a relational database in a dual storage architecture, the comprehensive preservation and efficient management of interactive data are realized, overcoming the memory fragmentation problem caused by the traditional context window length limitation, enabling the agent to maintain the interactive memory with the user for a long time, and significantly improving the agent's long-term memory ability.
[0038] (2) An emotional temporal enhancement community detection algorithm is introduced. The naturalness of the emotional transition between data items is evaluated by the SEQ(i,j) emotional temporal coherence index. The temporal enhancement graph is used to replace the traditional static graph, which effectively captures the dynamic change pattern in emotional interaction, enabling the agent to maintain the coherence and natural transition of emotional response and reducing the sense of emotional jump.
[0039] (3) By comprehensively considering semantic relevance, temporal relevance and emotional relevance, a multidimensional relevance graph is constructed, and community discovery is carried out based on the graph, which realizes 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. Attached Figure Description
[0040] This application will be further described by way of exemplary embodiments, which will be described in detail with reference to the accompanying drawings. These embodiments are not limiting; in these embodiments, the same reference numerals denote the same structures, wherein:
[0041] Figure 1 This is an exemplary flowchart of a method for modeling long-term memory of an intelligent agent based on a memory network, according to some embodiments of this application.
[0042] Figure 2 This is a component architecture diagram shown according to some embodiments of this application;
[0043] Figure 3 This is a technical roadmap shown according to some embodiments of this application. Detailed Implementation
[0044] The methods and systems provided in the embodiments of this application will now be described in detail with reference to the accompanying drawings.
[0045] like Figure 1 As shown, the system collects interaction data between the agent and the user, converts the interaction data into vector data, and extracts structured metadata from the interaction data. The vector data is stored in a vector database, and the structured metadata is stored in a relational database. The system receives user input requests, performs vector similarity retrieval in the vector database to obtain semantically similar fragments, performs structured data queries in the relational database to obtain structured metadata, and retrieves and stores generated and stored memory summaries. The semantically similar fragments, structured metadata, and memory summaries are merged to form a candidate dataset. The candidate dataset is filtered. The filtered results are used as memory information, and the memory information is input into a large language model through prompt word engineering to generate response content. The user's input request and the generated response content are combined to form a new interaction record. Structured metadata is extracted from the interaction record and stored in the relational database, and the interaction record is converted into vector data and stored in the vector database. The large language model processes the new interaction record, as well as historical interaction records stored in the vector and relational databases, to generate a memory summary. The new interaction record and historical interaction records are compared to identify and process memory fragments with overlapping information or logical contradictions, and the structured metadata in the relational database is updated.
[0046] like Figure 2As shown, the intelligent agent long-term memory modeling system based on memory networks 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. The model service layer, as the underlying capability support of the architecture, includes two core modules: a large language model service, which provides natural language understanding and generation capabilities, supporting high-level functions such as dialogue logic and memory extraction. This embodiment uses a pre-trained language model with 100B parameters and optimizes emotional interaction capabilities through RLHF technology, realizing prompt word engineering and response generation. The vectorization service converts text memory (user historical dialogues, knowledge fragments, etc.) into high-dimensional vectors, providing a foundation for subsequent semantic retrieval. This embodiment uses a vector representation with a dimension of 1536 and uses the BERT model for text embedding, supporting the implementation of step S1.
[0047] The core memory processing layer achieves memory retrieval and dynamic management through multi-level processing modules. Its components include: memory information filtering and sorting: implementing a filtering mechanism to denoise the original memory based on multiple dimensions such as timeliness and relevance. Specifically, the timeliness weight w... t Set to 0.4, relevance weight w r Setting it to 0.6 is suitable for the long-term interactive characteristics of emotional companionship scenarios.
[0048] Memory retrieval and extraction: Combining similarity search and structured query from a vector database, rapid retrieval of multi-source memories is achieved. In this embodiment, the cosine similarity threshold is set to 0.75, and semantically similar fragments are retrieved from the vector database; simultaneously, structured metadata from the most recent 30 days is obtained from a relational database.
[0049] The emotion association modeling module enables multi-dimensional association calculation. Emotion vectors are represented in 8 dimensions (joy, sadness, anger, fear, surprise, disgust, trust, and expectation), 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 dialogue data points.
[0050] Temporal augmentation 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 For a period of 1 day, the emotional temporal enhancement coefficient λ is 0.8, and the average priority weight w in the community importance score is... p The cohesion weight is 0.6, w c It is 0.4.
[0051] Memory Extraction and Summarization: Raw memories are compressed and abstracted using a large language model to generate a high-level memory summary, achieving step S7. The memory summary is generated once every 100 interactions or every 24 hours to ensure its timeliness.
[0052] Memory update mechanism: Supports CRUD operations on memories, dynamically maintains the timeliness and consistency of memory relationship chains, and implements step S8. The contradiction detection threshold is set to 0.85. When the contradiction between new and old memories exceeds the threshold, the new memory is retained first and the old memory is marked as outdated.
[0053] Cue word engineering: Dynamically inject memory retrieval results into the large language model's cue words to generate responses. The cue word template includes three parts: role definition, memory guidance, and emotional response strategy, and sets special identifiers for different types of memories: [SEM] semantic fragment, [STR] structured metadata, and [SUM] memory summary.
[0054] The memory persistence layer implements long-term memory storage and includes: a MySQL database storing structured memory metadata, including memory IDs, timestamps, topic tags, sentiment tags, etc. The table structure design includes a main table `memory_items` and a relational table `memory_relations`, implemented in step S2.
[0055] Vector database: Implemented using Milvus, it stores the vectorized representation of memories, supports cosine similarity retrieval, uses HNSW index type, and achieves efficient approximate nearest neighbor search.
[0056] like Figure 3 As shown, taking a user's query "You said that restaurant was so delicious yesterday!" as an example, the system processes it according to the following procedure:
[0057] The user inputs "You said that restaurant was so delicious yesterday!" which is passed to the agent processing module via 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 magnitude is 1, which is convenient for subsequent cosine similarity calculation. The NLP preprocessing module extracts the time indicator word "yesterday" (converted to the specific date 2025-05-31) and the topic word "restaurant" (categorized as "food and beverage") from the input. The sentiment analysis engine detects the positive emotion in the user input (detection value "expectation / positive", intensity 0.72) and uses this as the sentiment filter condition for retrieval.
[0058] Vector database retrieval: An ANN query was performed in the Milvus vector database using the HNSW indexing algorithm; the nprobe=128 parameter was set to optimize the recall rate, and the retrieval distance was calculated using the inner product (equivalent to cosine similarity); 5 semantically similar fragment records with similarity > 0.75 were obtained, including: "LaVita Italian restaurant's truffle pasta is really worth recommending, especially their handmade pasta..." (similarity 0.92); "If you like Asian flavors, the Tom Yum soup at Sawadee Thai restaurant that I mentioned yesterday..." (similarity 0.87); [3 other related records].
[0059] Execute the optimized SQL query: SELECT * FROM memory_items WHERE DATE(timestamp) = DATE_SUB(CURDATE(), INTERVAL1DAY) AND topic_tags LIKE '%restaurant%' AND emotion_tags LIKE '%positive%' ORDER BY relevance_score DESCLIMIT 10; Add a sentiment filter condition to prioritize returning records that match the current user's sentiment; Use a composite index of topic_tags and timestamp to improve query performance; Retrieve 8 structured metadata entries, including memory ID, timestamp, topic tags, sentiment tags, etc.
[0060] Memory summary retrieval: Retrieves the three most recently generated memory summaries from the summary cache (updated every 24 hours); calculates the two historical memory summaries with the highest relevance to the current input using vector similarity; summary example: "The user expresses a preference for both Italian and Thai cuisine, particularly enjoys trying newly opened restaurants, and has a habit of dining out on weekends...". A duplicate content merging algorithm is applied to remove duplicate information and combine them into 15 candidate memory data items; each data item is assigned a unique ID for easy subsequent graph construction.
[0061] To calculate semantic relevance, use 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] To calculate the temporal correlation degree, use the formula... Where τ = 86400 (the number of seconds in a day), controlling the time decay rate; I(T) i ,T j Timing indicator function implementation: When T i Before T jAnd if the time interval is less than 3 hours, take 1; otherwise, take 0.4. For example, two memories separated by 2 hours:
[0064] The emotional relevance is calculated using the formula Emo(i,j)=α. e ×ES(i,j)+(1-α e Emo(i,j) = 0.3 × ES(i,j) + 0.7 × ET(i,j); the sentiment vector is represented in 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]; sentiment marker similarity: ES(i,j) = cos(E i E j =0.89; Naturalness of emotional transition:
[0066] The emotional transition probability matrix M[0][4]=0.95 indicates that the natural transition probability from "joy" to "surprise" is relatively high; the comprehensive emotional correlation degree: Emo(i,j)=0.3*0.89+0.7*0.83=0.85.
[0067] Calculate the overall correlation degree.
[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 edges with weight ≥0.6, and construct a weighted undirected graph; finally, a relational graph containing 15 nodes and 12 edges is obtained.
[0070] Construct a time-series augmented community and set the emotional time-series weights W(i,j) = W orig (i,j)×(1+0.8×SEQ(i,j)), calculate the sentiment temporal coherence index. Time Adjacency Threshold θ t Set to 4 hours (14400 seconds). Enhanced edge weight: W(3,7)=0.88×(1+0.8×0.415)=1.17.
[0071] Window length t w Set to 1 day, step size δ tThe timeframe 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 between users in the [yesterday 12:00, yesterday 18:00] window are captured, forming the main emotional context.
[0072] The improved Louvain algorithm was implemented. In the initial stage, each node was treated as an independent community. In the first stage, the modularity gain ΔQ of moving node i to community C was calculated iteratively, while considering temporal consistency. In the second stage, the discovered communities were regarded as "super nodes", a new graph was constructed, and the process was repeated 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 the community importance score.
[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: Cohesion of community C1: Coh(C1) = Sum of internal edge weights / Sum of total edge weights = 4.85 / 5.20 = 0.933; Importance score of community C1: Imp(C1) = 0.6 × 0.815 + 0.4 × 0.933 = 0.862; Importance scores of each community: C1 (0.862) > C2 (0.756) > C3 (0.645).
[0075] Memory selections are made chronologically from the most important community C1, which contains the memory items {1, 3, 5, 9, 11, 14}. These are then sorted by timestamp, resulting in four memories: {3, 5, 9, 14} (in ascending order). Setting M1 = 6, two more memories are needed. From the second most important community C2, two memories are selected that are most emotionally similar to those in C1 and closest in time to the selected memories: {7, 12}. Combining the selections from C1 and C2, these memories are arranged chronologically as: {3, 5, 7, 9, 12, 14}. A deduplication algorithm is applied to ensure no content repetition. Emotional coherence is verified to ensure a natural emotional transition > 0.7. Finally, six memories are selected as the filtered results, forming a complete emotional framework for restaurant recommendations.
[0076] To generate a response, first add a memory identifier. Add an identifier for semantically similar fragments: [SEM] Yesterday I recommended LaVita Italian restaurant's truffle pasta to you; they use imported black truffles and homemade handmade noodles. Add an identifier for structured metadata: [STR] 2025-05-31 14:23 | Topic: Restaurant Recommendation | Sentiment: Positive | User Response: Interested. Add an identifier for the memory summary: [SUM] The user showed continued interest in Italian cuisine, especially handmade pasta, and mentioned wanting to try a new restaurant this weekend.
[0077] Construct a prompt template: [Instruction] You are an emotional companion AI assistant. Please provide natural and coherent responses based on the user's emotional state and past communication content. When discussing past conversations, maintain emotional coherence and avoid jarring repetitions of memories. Replies should maintain a warm and friendly tone, appropriately expressing empathy and support based on the user's positive emotions. [Context] User input: {user_input}. Related memories: {memory_items}. Please generate natural and fluent responses based on the above information, avoiding direct repetition of memorized content, but rather integrating the information into the conversation.
[0078] Prompt word filling and assembly: Enter "You said that restaurant was so delicious yesterday!" in the user input section, enter 6 processed memory information in the memory section, and add the system instruction: "Maintain a 4:6 ratio of recall to new information, and control the response length to 80-120 words."
[0079] The system utilizes a large model to generate responses, using a temperature setting of 0.7 to increase response diversity while maintaining consistency. The sampling range is controlled by a top_p parameter of 0.92. A sentiment consistency filter is applied to ensure the output sentiment matches the user input. The final generated response is: "Yes, we talked about LaVita Italian restaurant yesterday! Their truffle pasta is truly outstanding, made with imported black truffles and freshly handmade pasta, with an excellent texture. You were very interested in trying it this weekend. If you decide to go, I suggest making a reservation in advance, as they are often full on weekends. Their tiramisu is also a signature dessert and well worth trying!"
[0080] To update the memory, a new interaction record is first generated by combining user input and system response. The record ID is automatically generated as MEM-20250601-08742. The record contains complete contextual information, emotional state, and timestamp.
[0081] Extract and store structured metadata, automatically extracting topic tags: [restaurant recommendations, Italian food, weekend plans]; sentiment tags: [positive, expectant]; 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-0110: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 of "restaurant preference" is detected, memory extraction is triggered; an asynchronous task ID: TASK-20250601-4312 is started, with a priority of medium; interactions about restaurant preferences over the past 30 days are summarized, and key information is extracted using an improved TextRank algorithm; a new memory summary is generated: "The user prefers Italian and Thai food, pays special attention to handmade ingredients and unique flavors, plans to try LaVita restaurant on the weekend, and is also interested in desserts."
[0084] A semantic contradiction detection algorithm is executed to compare old and new information. It is detected that the user previously mentioned "disliking desserts" on May 20th, but now shows interest in tiramisu. The contradiction score is calculated to be 0.87, exceeding the threshold of 0.85. An update operation is performed, marking the old memory as "updated" in the database. A related record is added: `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')`. Thus, this embodiment utilizes an emotional temporal enhancement community detection algorithm to extract a coherent emotional development trajectory from fragmented memories, providing users with personalized responses that are both relevant and emotionally coherent. Simultaneously, it effectively handles information updates and contradictions, maintaining the consistency and timeliness of long-term memories.
[0085] The foregoing illustrative description of the present application and its embodiments is not restrictive and can be implemented in other specific forms without departing from the spirit or essential characteristics of the present application. The accompanying drawings are only one embodiment of the present application, and the actual structure is not limited thereto. Therefore, if those skilled in the art are inspired by this description and design similar structures and embodiments without departing from the spirit of the present application, such designs should fall within the scope of protection of this application. Furthermore, the word "comprising" does not exclude other elements or steps, and the word "a" preceding an element does not exclude the inclusion of "a plurality" of that element. Terms such as "first," "second," etc., 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 memory networks, characterized in that, include: S1 collects 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 structured metadata in a relational database. S3 receives user input requests, performs vector similarity retrieval in the vector database to obtain semantically similar fragments, and performs structured data query in the relational database to obtain structured metadata; The memory summaries generated and stored in step S7 are retrieved; semantically similar fragments, structured metadata, and memory summaries are merged to form a candidate dataset; S4, filtering the candidate dataset; S5. The filtered result from step S4 is used as memory information. The memory information is input into the large language model through prompt word engineering to generate response content. S6 combines the user's input request and the generated response content to form a new interaction record, extracts structured metadata from the interaction record and stores it in a relational database, and converts the interaction record into vector data and stores it in a vector database; S7 processes new interaction records, as well as historical interaction records stored in vector and relational databases, using a large language model to generate memory summaries; S8 compares new interaction records with historical interaction records, identifies and processes memory fragments with overlapping 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 forms a candidate dataset, including: Convert user input requests into vector data; Based on the vector data, calculate the cosine similarity in the vector database, retrieve the vector data with a cosine similarity greater than the threshold, and 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; The memory summaries generated and stored in step S7 include: obtaining the memory summaries generated most recently in time order, and obtaining the N2 historical memory summaries that are most relevant to the user's input request based on vector similarity calculation; The acquired semantically similar fragments, 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 filters 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 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. Calculate the priority of each data item based on its timeliness and relevance scores. , ,in, This represents the timeliness score of data item i; This represents the relevance score of data item i; and These represent the weighting coefficients for timeliness and relevance, respectively. Construct a data association graph, where nodes in the graph represent data items and edges represent the relationships between data items; Based on the data association graph, starting from the highest priority data, the community detection 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, characterized in that: Constructing a graph of relationships between data, including: Calculate the semantic correlation, temporal correlation, and sentiment correlation between any two data points in the candidate dataset; The overall correlation between the data is calculated based on semantic correlation, temporal correlation, and sentiment correlation. ; Using data items as nodes, and based on comprehensive correlation... Relationships exceeding a preset threshold are used as edges to construct an undirected weighted association graph between data items.
5. The method for modeling long-term memory of an intelligent agent based on a memory network according to claim 4, characterized in that: The semantic relevance is calculated using the following formula: in, This indicates the semantic correlation between data item i and data item j; A vector representation of data item i; The vector representation of data item j; The temporal correlation degree is calculated using the following formula: in, This represents the temporal correlation between data item i and data item j; Represents the timestamp of data item i; This represents the timestamp of data item j; This represents the time decay factor, used to control the degree of influence of time differences on the correlation. Indicates a timing indicator function, when Prior to And the time interval is less than the preset threshold. The value is 1 if the condition is met, otherwise it is 1. , in, .
6. The method for modeling long-term memory of an intelligent agent based on a memory network according to claim 4, characterized in that: Emotional affinity is calculated using the following formula: in, This represents the sentiment correlation between data item i and data item j; This represents the balance factor, with a value range of [0, 1]. Sentiment marker similarity is calculated using the following formula: in, This represents the sentiment vector of data item i, containing intensity values for different sentiment categories; Represents the sentiment vector of data item j; The naturalness of emotional transition is indicated by the following formula: Where M represents the emotional transition probability matrix; Indicates the dominant sentiment category index for data item i; Indicates the dominant sentiment category index for data item j; This represents the standard time parameter for emotional transition.
7. The method for modeling long-term memory of an intelligent agent based on a memory network according to claim 3, characterized in that: Based on the association graph, starting with the highest priority data, the M1 data points with the highest association are obtained through a community detection algorithm and used as the filtering results, including: Based on the priority of data items Select the data item with the highest priority as the seed node; Based on the constructed association graph, a temporal-enhanced graph is obtained by setting sentiment temporal weights for the edges; Based on the time-series enhancement map, the time window length is set. And according to step size By sliding windows and capturing patterns of sentiment change within each time window, data communities with close relationships and coherent sentiment development can be identified. Based on the identified data communities, calculate the importance score Imp(C) for each community. Based on the importance score Imp(C), starting from the community with the highest score, data is selected according to the time order of the data items until M1 data points are obtained, which are then used as the final filtering results. If the number of data items in the first community is less than M1, then the selection continues from the next community based on 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 sequence weights are set using the following formula: in, To assess overall relevance; As an indicator of the temporal coherence of emotions; in, and These are the timestamps of data items i and j, respectively. It is a time proximity threshold; λ represents the naturalness of the 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: The importance score Imp(C) for each community is calculated using the following formula: Where Imp(C) represents the importance score of community C; This represents the average priority score of all nodes within community C; The cohesion of community C is represented by the ratio of the weight of the internal edges to the total weight of the community edges. and These represent the weighting coefficients for 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 from step S4 is used as memory information. This memory information is then input into the large language model through prompt word engineering to generate response content, including: Different identifiers are set for semantically similar segments, structured metadata, and memory summaries in the memory information; Construct a prompt word template, which includes an instruction part and a context part; define the agent's role positioning, dialogue style, and response strategy based on emotional relevance in the instruction part; set the filling positions for user input requests and memory information in the context part; The user's input request and the memory information after setting the identifier are filled into the constructed prompt word template to generate a complete prompt word; Enter the complete prompt into the large language template to generate the response.
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