Long dialogue context dynamic management method and system based on semantic chunk aggregation
By employing semantic block aggregation and deep semantic understanding methods, the system dynamically manages the context of long dialogues, solving the problems of information redundancy and response latency, and achieving efficient and accurate dialogue system response and self-optimization capabilities.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- BEIJING ZHONGLUXUNKE TECHNOLOGY CO LTD
- Filing Date
- 2025-12-10
- Publication Date
- 2026-04-14
AI Technical Summary
Existing technologies suffer from problems such as information redundancy, excessive noise, high computational load, response latency, and insufficient generation accuracy when managing long dialogue contexts. Furthermore, knowledge bases and semantic understanding models are difficult to learn and optimize on their own.
We employ a semantic block aggregation method, which dynamically manages dialogue data into structured semantic blocks through clustering and semantic classification models. We combine this with a key-value cache for storage and retrieval, utilize deep semantic understanding for context management, and optimize the model through offline learning.
It achieves high-density, semantically coherent context generation, reduces computational overhead and response latency, improves the accuracy of generated responses and the system's self-optimization capabilities, and adapts to dynamic environments.
Smart Images

Figure CN121858692A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of artificial intelligence and natural language processing technology, specifically relating to a method and system for dynamic management of long dialogue context based on semantic block aggregation. Background Technology
[0002] With the widespread application of large language models in dialogue systems, intelligent customer service and other fields, how to effectively manage and utilize the contextual information of long dialogues has become a key challenge.
[0003] Existing technologies suffer from several drawbacks: First, many existing tools or methods for automatically optimizing context (hints) exhibit a clear "simplicity bias" in their design. Their optimization goals tend to generate the shortest and most general instructions or hints possible, sacrificing specific, domain-specific key details and heuristic knowledge. This excessive pursuit of simplicity essentially erases many important operational instructions, causing models to struggle to perform complex tasks due to a lack of sufficient contextual detail, ultimately affecting the accuracy and reliability of their output. Second, in some iterative learning or dynamically updated context schemes, the system or model needs to rewrite or summarize the ever-growing complete context at each step. When the learned strategies, historical interactions, and knowledge accumulate to a certain extent, and the context becomes very long, the model, during the rewriting process, may abandon understanding and instead over-compress it into an extremely short summary. This aggressive compression leads to the loss of a large amount of valuable, effective information gained in previous iterations, causing a significant decline in model performance, potentially even falling below the baseline level without any optimization, resulting in "context collapse" of the model's stored context.
[0004] Therefore, a method and system for dynamic management of long dialogue context based on semantic block aggregation has emerged. Summary of the Invention
[0005] This invention aims to solve at least one of the technical problems existing in the prior art; to this end, this invention proposes a method and system for dynamic management of long dialogue context based on semantic block aggregation, to solve the following technical problem: Existing systems generally manage context using fixed-window-length truncation or simple vector similarity retrieval. The former loses crucial early dialogue information due to window limitations, disrupting the logical coherence of the dialogue; the latter lacks the dynamic perception of the natural evolution and switching of topics in the dialogue flow, failing to intelligently aggregate scattered but related multi-turn dialogues. This results in a large amount of redundancy and noise in the context input to the large language model, which not only reduces the density of effective information but also significantly increases the computational load of the large language model due to the excessively long input sequence, leading to high response latency and a degraded user experience. Traditional methods rely on keyword matching or shallow semantic similarity calculation based on general models, making it difficult to accurately capture the deep semantic connections between dialogue segments. They cannot accurately understand these deep semantic relevances, resulting in the retrieved context segments potentially deviating from the current query intent, directly affecting the accuracy and professionalism of the responses generated by the large language model. After deployment, the knowledge base and semantic understanding models of most existing dialogue systems tend to be static, unable to automatically utilize the large amount of daily interaction data for self-learning and optimization.
[0006] To address the aforementioned problems, a first aspect of the present invention provides a method for dynamic management of long dialogue context based on semantic block aggregation, comprising the following steps: S1: Obtain training corpus. The training corpus is a Chinese dataset generated by distillation technology based on the Qwen3-235B-Thinking-2507 model. The training corpus is unsupervised clustered using a clustering algorithm to form multiple semantic categories. Each semantic category is manually semantically annotated. The clustering results and semantic annotations are used as supervision signals to train the m3e-small model as a semantic classification model for semantic classification of input text. S2: Acquire real-time dialogue data stream, including input queries and their corresponding responses, generate sentence vectors for each dialogue data using a word embedding model, calculate the cosine similarity of adjacent sentence vectors, determine whether to merge adjacent sentences into the same semantic block based on a preset threshold, and perform semantic compression processing on the merged semantic block. S3: Use the semantic classification model to classify semantic blocks, generate an embedding vector and a corresponding Key-Value Cache for each semantic block; construct a context database, associate and store the semantic blocks, embedding vectors and their Key-Value Caches according to their semantic categories, and establish an index based on semantic embedding vectors and time sequence; S4: Receive a new input query from the user, classify it using the semantic classification model, determine its target semantic category, perform a hybrid retrieval from the context database, combine the retrieved semantic blocks with the dialogue record to form the context of the current query, if the length of the selected context exceeds the input window limit of the large language model, dynamically compress it by deleting the semantic block with the lowest semantic similarity, and generate a response through the large language model by combining the retrieved context and its Key-Value Cache. S5: Add the newly generated dialogue data to the context database in real time, and repeat steps S2-S3 to perform semantic block aggregation and database updates.
[0007] Preferably, in step S2, determining whether to merge adjacent statements into the same semantic block based on a preset threshold includes the following steps: Calculate the cosine similarity of adjacent statement vectors. If the cosine similarity is greater than a preset threshold, then merge the adjacent statements into the same semantic block. If the cosine similarity is equal to or less than a preset threshold, a new semantic block is created for the current statement.
[0008] Preferably, in step S2, the semantic compression process of the merged semantic block includes the following steps: Framework regularization is performed on the text in semantic blocks through architectural structured representation; Identify and resolve conflicting texts in the semantic block, filter stop words in the text, and apply a sentence reduction algorithm to remove redundant modifiers while retaining the core subject-verb-object or subject-verb structure. The output is a compressed semantic block containing timestamps and core semantic information.
[0009] Preferably, in step S3, generating an embedding vector and a corresponding Key-Value Cache for each semantic block includes the following steps: Using a preset embedding vector generation model, the core semantic information of the compressed semantic block is input, and the embedding vector corresponding to the semantic block is calculated and output through the model. The embedding vector is a vector representation used to characterize the overall semantic features of the semantic block. Based on the self-attention mechanism of a pre-trained large language model, the compressed semantic block is used as input. Through the forward computation process of the model, the corresponding key matrix and value matrix are extracted and saved. The key matrix and value matrix together constitute the key-value cache of the semantic block. The generated embedding vector, Key-Value Cache, and corresponding semantic block time information are associated to form a semantic block data structure.
[0010] Preferably, in step S3, establishing an index based on semantic embedding vectors and time sequence involves creating an inverted index for each semantic category partition, where the semantic embedding vector serves as the content index item and the timestamp of the semantic block serves as the time sequence index item.
[0011] Preferably, in step S4, performing a hybrid retrieval from the context database includes the following steps: Based on the target semantic category of the new input query, locate the corresponding category partition in the context database; Within the category partition, the similarity between the semantic embedding vector of the query and the semantic block embedding vector in the context database is calculated, and the N2 semantic blocks with the highest similarity are selected. At the same time, across all category partitions, the N1 most recent dialogue records are selected directly based on the time-series index item.
[0012] Preferably, in step S4, dynamic compression is performed by deleting the semantic blocks with the lowest semantic similarity, which includes the following steps: The N2 semantic blocks and N1 dialogue records are merged into a candidate context set; Calculate the semantic similarity between each element in the candidate context set and the new input query; The elements in the context set are sorted from low to high according to the semantic similarity. Remove elements that are sorted to the end in turn until the text length of the remaining content meets the input window limit of the large language model.
[0013] A second aspect of the present invention provides a long dialogue context dynamic management system based on semantic block aggregation, comprising the following modules: Offline model preparation module: Acquire training corpus, which is a Chinese dataset generated by distillation technology based on the Qwen3-235B-Thinking-2507 model. Unsupervised clustering is performed on the training corpus to form multiple semantic categories, and each semantic category is manually semantically annotated. The clustering results and semantic annotations are used as supervision signals to train the m3e-small model as a semantic classification model for semantic classification of input text. Data processing and semantic block aggregation module: acquires real-time dialogue data stream, including input queries and their corresponding responses, generates sentence vectors for each dialogue data using a word embedding model, calculates the cosine similarity of adjacent sentence vectors, determines whether to merge adjacent sentences into the same semantic block based on a preset threshold, and performs semantic compression processing on the merged semantic block; Context database construction module: Classifies semantic blocks using the semantic classification model, generates an embedding vector and a corresponding Key-Value Cache for each semantic block; constructs a context database, associates and stores the semantic blocks, embedding vectors and their Key-Value Caches according to their semantic categories, and establishes an index based on semantic embedding vectors and time sequence; Online query response module: Receives new input queries from users, classifies them using the semantic classification model, determines their target semantic category, performs a hybrid retrieval from the context database, combines the retrieved semantic blocks with the dialogue record to form the context of the current query, and if the length of the selected context exceeds the input window limit of the large language model, dynamically compresses it by deleting the semantic blocks with the lowest semantic similarity, and generates a response by combining the retrieved context and its Key-Value Cache through the large language model; Dynamic update module: Adds newly generated dialogue data to the context database in real time, and repeats the data processing and semantic block aggregation module to the context database construction module for semantic block aggregation and database update.
[0014] The beneficial effects of this invention are: This invention dynamically aggregates lengthy dialogue streams into structured semantic blocks by calculating the semantic similarity of adjacent statements online in real time, and performs deep semantic compression on them, fundamentally solving the problem of information redundancy and generating a context with high information density and semantic coherence. Combined with the technology of pre-computing and storing key-value cache for each semantic block, large models do not need to repeatedly encode historical information during inference, achieving retrieval acceleration and significantly reducing computational overhead and response latency. At the same time, due to the high-quality input of the context, the generated response is more accurate and coherent. This invention introduces an offline pre-trained m3e-small deep semantic model as the core of classification and representation. By learning on massive amounts of high-quality dialogue data, it can accurately understand the deep semantic relationship between user queries and historical semantic blocks, rather than just matching words on the surface. Based on this deep semantic understanding, the classification and vectorization enable the system to accurately recall the most relevant and valuable contextual information in subsequent hybrid retrieval (combining semantics and temporal sequence), which greatly improves the quality of the context on which the large language model is based. This invention designs a complete technical closed loop from online interaction to offline learning and then back to the online system. It can automatically collect newly generated dialogue data and continuously mine new knowledge patterns and optimize semantic understanding models through offline pipelines such as clustering, annotation and model fine-tuning. This achieves fully automated knowledge accumulation and performance iteration, greatly reduces long-term operation and maintenance costs, and ensures the long-term applicability and advanced nature of the system in dynamically changing environments. Attached Figure Description
[0015] Figure 1 This is a schematic diagram of the method flow of the present invention; Figure 2 This is a schematic diagram of the module flow of the present invention. Detailed Implementation
[0016] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0017] Please see Figure 1 As shown, this invention is a method for dynamic management of long dialogue context based on semantic block aggregation, comprising the following steps: S1: Obtain training corpus. The training corpus is a Chinese dataset generated by distillation technology based on the Qwen3-235B-Thinking-2507 model. The training corpus is unsupervised clustered using a clustering algorithm to form multiple semantic categories. Each semantic category is manually semantically annotated. The clustering results and semantic annotations are used as supervision signals to train the m3e-small model as a semantic classification model for semantic classification of input text. S2: Acquire real-time dialogue data stream, including input queries and their corresponding responses, generate sentence vectors for each dialogue data using a word embedding model, calculate the cosine similarity of adjacent sentence vectors, determine whether to merge adjacent sentences into the same semantic block based on a preset threshold, and perform semantic compression processing on the merged semantic block. S3: Use the semantic classification model to classify semantic blocks, generate an embedding vector and a corresponding Key-Value Cache for each semantic block; construct a context database, associate and store the semantic blocks, embedding vectors and their Key-Value Caches according to their semantic categories, and establish an index based on semantic embedding vectors and time sequence; S4: Receive a new input query from the user, classify it using the semantic classification model, determine its target semantic category, perform a hybrid retrieval from the context database, combine the retrieved semantic blocks with the dialogue record to form the context of the current query, if the length of the selected context exceeds the input window limit of the large language model, dynamically compress it by deleting the semantic block with the lowest semantic similarity, and generate a response through the large language model by combining the retrieved context and its Key-Value Cache. S5: Add the newly generated dialogue data to the context database in real time, and repeat steps S2-S3 to perform semantic block aggregation and database updates.
[0018] Specifically, this embodiment demonstrates a complete system operation cycle, which is as follows: The system loads the Chinese dataset "Chinese-Qwen3-235B-Thinking-2507-Distill-100k" as training corpus. Subsequently, the following steps are performed: The HDBSCAN clustering algorithm is used to aggregate all dialogue data into 100 categories; Professionals assign easily understandable semantic labels to these 100 categories, such as "email processing", "confirmation information", "hardware configuration", "project management", and "movies"; Using the above clustering results and manually labeled labels as supervision signals, a pre-trained m3e-small model is fine-tuned to finally obtain a classification model that can accurately semantically classify any input text; After the system goes online, it begins to receive real-time dialogue data streams. Assume that the following four data are received consecutively: 1. {"time": "2025 / 05 / 24 09:12", "input": "Is our current project A currently at risk?", "output": "System response content..."}; 2. {"time": "2025 / 05 / 24 09:15, "input": "What are the core risk points?", "output": "System response content..."}; 3. {"time": "2025 / 05 / 24 10:42", "input": "Has the UI design solution received customer feedback?", "output": "System response content..."}; 4. {"time": "2025 / 05 / 24 10:45", "input": "Has the meeting time for the cybersecurity topic been confirmed?", "output": "System response content..."}; The system processes these data sequentially, specifically by: using a word embedding model (e.g., pre-trained BERT) to generate corresponding sentence vectors X1, X2, X3, X4 for the input field of each data; the system calculates the cosine similarity of adjacent sentence vectors. If cos(X1, X2) > the preset threshold, data 1 and data 2 belong to the same topic; if cos(X2 ... If cos(X3) > the preset threshold, data 3 belongs to the same topic as the previous two; therefore, data 1, 2, and 3 are merged into the same semantic block; if cos(X3, X4) < the preset threshold, it indicates that the topic has switched, and the system creates a new semantic block for data 4; the merged semantic block (e.g., the block containing data 1, 2, and 3) is compressed; the text is regularized through structured representation, possible conflicting descriptions are resolved, stop words are filtered, and sentence reduction algorithms are applied to simplify the content, finally forming a compressed semantic block containing core semantic information and a timestamp; all semantic blocks generated above are processed as follows: each semantic block is classified using an offline-trained m3e-small classification model, for example, the semantic block containing data 1, 2, and 3 is classified into the "Project Management" category (Category C).An embedding vector representing the overall semantic meaning of each semantic block is generated; the compressed semantic block text is input into a pre-trained large language model (such as ChatGLM, LLaMA, etc.), and the key matrix and value matrix corresponding to the block are extracted and saved through its forward computation process, i.e., the key-value cache; the semantic block, its embedding vector, and KV cache are stored in the context database according to the classification results. Taking the "project management" category as an example, its storage structure is shown below: Category C: Project Management |-- Semantic Block 1: {"time": "2025 / 05 / 24 09:12", "text_content": "compressed core text...", "embedding_vector": [...], "KV cache": {...}} |-- Semantic Block 2: {"time": "...", "text_content": "...", "embedding_vector": [...], "KV cache": { ...}} -- ... At the same time, the database builds an inverted index for each category partition, with the embedding vector as the content index item and the timestamp as the time-series index item; When a user initiates a new query: Qnew:{"time": "2025 / 06 / 25 12:15", "input": "How to solve the problem that customers think the UI interface is not intelligent enough?"}, the system processes it as follows: First, it uses an offline-trained m3e-small classifier to classify Qnew's input, determining it belongs to the "Project Management" category. Within the "Project Management" category, it calculates the similarity between Qnew's vector and all semantic block vectors in the library, retrieving the N2 semantic blocks with the highest similarity (e.g., blocks containing data 1, 2, 3 are retrieved due to their high relevance). Then, across all categories, it directly retrieves the latest N1 independent dialogue records based on the timestamp index. Finally, it merges the N2 semantic blocks and N1 records into a candidate context set. If the total text length of this set exceeds the input window limit of the large language model, it calculates the semantic similarity between each element in the set and Qnew, removing elements sequentially starting with the lowest similarity until the length requirement is met. The refined final context set and its corresponding pre-calculated key-value pair are then processed. The cache is fed into the large language model; due to the existence of the Key-Value Cache, the model does not need to recalculate the encoding of this historical information, but directly performs attention calculation and generates responses, significantly improving inference speed; finally, the response to Qnew is output; the new data points generated by the above interaction: {"time": "2025 / 06 / 25 12:15", "input": "How to solve the problem that customers think the UI interface is not intelligent enough?", "output": "System-generated response..."} will be added to the dialogue data stream in real time. The system will automatically return to step 2, and perform a new round of dynamic semantic block aggregation, classification, compression, and updating of the context database together with the subsequent data; thus, the system realizes a complete closed loop of continuous self-optimization and knowledge accumulation.
[0019] In one embodiment of the present invention, step S2, determining whether to merge adjacent statements into the same semantic block based on a preset threshold, includes the following steps: Calculate the cosine similarity of adjacent statement vectors. If the cosine similarity is greater than a preset threshold, then merge the adjacent statements into the same semantic block. If the cosine similarity is equal to or less than a preset threshold, a new semantic block is created for the current statement.
[0020] Specifically, the preset threshold is determined by randomly sampling a representative dialogue stream sample from the system's historical dialogue logs, hiring professionals to manually annotate each pair of adjacent dialogues in the sample, and determining whether they should belong to the same semantic block; the annotation results are usually binary labels, such as "should be merged" and "should not be merged"; the model is used to calculate the cosine similarity of the statement vectors of each pair of adjacent dialogues in the annotated dataset, and the distribution map of the calculated cosine similarity is plotted, and similarity distribution curves for "should be merged" dialogue pairs and "should not be merged" dialogue pairs are plotted according to the manual annotation results; an optimal threshold is found that can best distinguish between these two types of dialogue pairs. Usually, this optimal threshold point is located near the intersection of the two distribution curves, or it may be biased towards one side according to business needs; the manual annotation results are used as the gold standard, and the similarity calculation is regarded as a binary classifier. By traversing a series of possible thresholds, the precision and recall of the classification results corresponding to each threshold are calculated, and their F1 scores are calculated; finally, the threshold that maximizes the F1 score is selected as the preset threshold.
[0021] In one embodiment of the present invention, step S2 involves semantic compression processing of the merged semantic block, including the following steps: Framework regularization is performed on the text in semantic blocks through architectural structured representation; Identify and resolve conflicting texts in the semantic block, filter stop words in the text, and apply a sentence reduction algorithm to remove redundant modifiers while retaining the core subject-verb-object or subject-verb structure. The output is a compressed semantic block containing timestamps and core semantic information.
[0022] Specifically, the system first analyzes the original text within the semantic block to identify its internal logical structure. For example, for a semantic block centered around "project risks", the system will regularize its content into a predefined framework, such as [Project Name: Project A], [Risk Status: Existent], [List of Risk Points: [Description of Risk Point 1], [Description of Risk Point 2],..]; this step transforms unstructured natural language into a semi-structured data form that is more easily processed by machines, laying the foundation for subsequent deep compression. Within the regularized structural framework, the system will detect whether there are logically contradictory or information-inconsistent statements. For example, the previous sentence might say "The UI solution has been confirmed", while the next sentence says "The UI solution is pending review". Among them, the solution strategies include: Solution based on timeliness: The system defaults to adopting the statement with the latest timestamp within the semantic block as the correct information, as it represents the latest progress of the conversation; Solution based on confidence: If the text source is accompanied by a confidence score, the information with a higher confidence is adopted; Context disambiguation: For ambiguous expressions, the specific reference is determined by analyzing the context. Finally, the system will automatically select or integrate the information to output an internally consistent and conflict-free semantic version. The system loads a stop word list containing common function words and modal particles such as "de", "le", "zai", and "then", and filters out these words that contribute little to the core semantics from the text. On the basis of filtering the stop words, the system further applies a sentence contraction algorithm based on syntactic analysis. This algorithm extracts the core subject-predicate-object structure or subject-predicate structure of the sentence by identifying and removing long modifying components (such as adjectives, adverbial phrases, and supplementary inserted clauses); for example: Original sentence: "The project meeting we just held urgently this morning has very clearly determined the final UI design solution." After being processed by this step, it is compressed to: "The project meeting determined the UI design solution." Finally, the system integrates the processing results of all the above steps to generate a compressed semantic block data structure; this structure contains at least two key fields, specifically: Timestamp: Usually follows the timestamp of the earliest or most core conversation within the semantic block to identify the start of the topic; Core semantic information: The refined text content obtained after regularization, disambiguation, filtering, and sentence contraction, which highly summarizes the essential information of the original semantic block.
[0023] In one embodiment of the present invention, in step S3, generating an embedding vector and a corresponding Key-Value Cache for each semantic block includes the following steps: Using a preset embedding vector generation model, inputting the core semantic information of the compressed semantic block, and calculating and outputting the embedding vector corresponding to the semantic block through the model. The embedding vector is a vector representation used to characterize the overall semantic features of the semantic block; Based on the self-attention mechanism of a pre-trained large language model, the compressed semantic block is used as input. Through the forward computation process of the model, the corresponding key matrix and value matrix are extracted and saved. The key matrix and value matrix together constitute the key-value cache of the semantic block. The generated embedding vector, Key-Value Cache, and corresponding semantic block time information are associated to form a semantic block data structure.
[0024] Specifically, a pre-defined, high-performance embedding vector generation model is used, such as the m3e-small model trained on a large-scale corpus. This model is specifically designed to map text sentences or paragraphs into a dense vector space, ensuring that semantically similar texts are also located close to each other in the vector space. The core semantic information (i.e., the refined text content) of the compressed semantic block output in step S2 is used as input and fed into the embedding model. The model outputs a fixed-dimensional, high-dimensional floating-point vector through its forward computation. This output vector is the embedding vector of the semantic block, serving as a powerful semantic fingerprint that uniquely represents the overall semantic features of the semantic block. This vector will be mainly used for efficient similarity calculation and semantic retrieval in the context database. Based on a pre-trained large language model (as an inference engine, such as LLaMA, ChatGLM, or GPT series models), the system utilizes its core component—the self-attention mechanism—to take the core semantic information of the same compressed semantic block as input and perform a complete forward computation process of the large language model. During this process, when the input sequence flows through each Transformer layer of the model, the system intercepts and extracts the intermediate activation values calculated in the self-attention module, namely the Key matrix (K) and Value matrix (V). The Key and Value matrices of all layers extracted for the entire semantic block text sequence together constitute the Key-Value Cache of the semantic block corresponding to the specific large language model. This Cache is essentially a kind of "memory" or "encoding" state of the semantic block within the large language model. The system creates a structured data object to fully describe a semantic block. This data object integrates all the elements generated in the preceding steps to form the final semantic block data structure. This data structure typically includes the following fields: text_content: stores the compressed core semantic information text; timestamp: stores the time information associated with the semantic block; embedding_vector: stores the generated embedding vector; kv_cache: stores the pre-computed key-value cache. This complete data structure is stored in the context database, providing a complete data foundation for subsequent millisecond-level semantic retrieval and code-free real-time inference.
[0025] In one embodiment of the present invention, in step S3, establishing an index based on semantic embedding vectors and time sequence involves creating an inverted index for each semantic category partition, wherein the semantic embedding vector serves as the content index item and the timestamp of the semantic block serves as the time sequence index item.
[0026] In one embodiment of the present invention, step S4, performing a hybrid retrieval from the context database, includes the following steps: Based on the target semantic category of the new input query, locate the corresponding category partition in the context database; Within the category partition, the similarity between the semantic embedding vector of the query and the semantic block embedding vector in the context database is calculated, and the N2 semantic blocks with the highest similarity are selected. At the same time, across all category partitions, the N1 most recent dialogue records are selected directly based on the time-series index item.
[0027] Specifically, the system first uses the semantic classification model to determine the target semantic category (e.g., "project management") of the new input query. Then, based on this category label, the system directly locates the corresponding category storage partition in the context database. This step is equivalent to a highly efficient initial screening, narrowing the search scope from the entire database to a subset of topics most relevant to the current query, greatly improving search efficiency. Within the located target category partition, the system performs refined semantic similarity matching. Specifically, the system calculates the cosine similarity (or similarity measure such as the dot product) between the semantic embedding vector of the new input query and the embedding vector of each semantic block within that partition. Based on the calculated similarity score, the system sorts all semantic blocks within that partition in descending order and selects the N2 semantic blocks with the highest similarity (N2 is a preset positive integer). These semantic blocks... This represents the historical context that is most relevant to the current query in terms of deep semantics and is most likely to provide valuable reference information. In parallel, the system performs a retrieval operation independent of semantic categories, which spans all category partitions and accesses a global temporal index built based on timestamps. The system directly selects the N1 most recent dialogue records (N1 is a preset positive integer) according to the temporal index entries, in order from newest to oldest timestamps. These records are usually the latest interactions that have not yet been aggregated into semantic blocks, or independent semantic blocks that have just been formed in recent dialogues. They are crucial for maintaining the immediacy and coherence of the dialogue. Finally, the system merges the results of the above two steps to form a unified candidate context set, which includes both historical topics that are deeply semantically relevant (N2 semantic blocks) and recent records that maintain the fluency of the dialogue (N1 records).
[0028] In one embodiment of the present invention, step S4, which involves dynamic compression by deleting the semantic block with the lowest semantic similarity, includes the following steps: The N2 semantic blocks and N1 dialogue records are merged into a candidate context set; Calculate the semantic similarity between each element in the candidate context set and the new input query; The elements in the context set are sorted from low to high according to the semantic similarity. Remove elements that are sorted to the end in turn until the text length of the remaining content meets the input window limit of the large language model.
[0029] Specifically, the system merges the N2 semantic blocks and N1 dialogue records obtained from the hybrid retrieval to form a unified candidate context set. This set contains all relevant contexts initially selected in terms of semantics and temporal sequence. The system does not treat the entire set as a whole, but processes each element (i.e., each independent semantic block or each independent dialogue record) separately. For each element in the set, the system calculates the cosine similarity between its own semantic embedding vector and the semantic embedding vector of the new input query. This step assigns a quantified relevance score to each member of the set, directly reflecting the element's value in answering the current query. The system then uses the semantic similarity score calculated in the previous step to determine the final relevance score. The system sorts the elements in the entire candidate context set in ascending order (from low to high). After sorting, the elements at the end of the list are the most similar and relevant contexts, while the elements at the beginning of the list are the least similar and least relevant contexts. Starting from the beginning of the sorted list (i.e., the element with the lowest similarity), the system removes elements from the candidate set one by one. After each removal operation, the system immediately checks whether the total length of the text content of all elements in the remaining set meets the preset input window limit of the large language model. This process is repeated until the total length of the remaining text content meets or is lower than the input window limit, thereby maximizing the quality of the context input to the large language model and laying a solid foundation for the large model to generate accurate and useful responses.
[0030] Please see Figure 2 As shown, this invention is a dynamic management system for long dialogue contexts based on semantic block aggregation, comprising the following modules: Offline model preparation module: Acquire training corpus, which is a Chinese dataset generated by distillation technology based on the Qwen3-235B-Thinking-2507 model. Unsupervised clustering is performed on the training corpus to form multiple semantic categories, and each semantic category is manually semantically annotated. The clustering results and semantic annotations are used as supervision signals to train the m3e-small model as a semantic classification model for semantic classification of input text. Data processing and semantic block aggregation module: acquires real-time dialogue data stream, including input queries and their corresponding responses, generates sentence vectors for each dialogue data using a word embedding model, calculates the cosine similarity of adjacent sentence vectors, determines whether to merge adjacent sentences into the same semantic block based on a preset threshold, and performs semantic compression processing on the merged semantic block; Context database construction module: Classifies semantic blocks using the semantic classification model, generates an embedding vector and a corresponding Key-Value Cache for each semantic block; constructs a context database, associates and stores the semantic blocks, embedding vectors and their Key-Value Caches according to their semantic categories, and establishes an index based on semantic embedding vectors and time sequence; Online query response module: Receives new input queries from users, classifies them using the semantic classification model, determines their target semantic category, performs a hybrid retrieval from the context database, combines the retrieved semantic blocks with the dialogue record to form the context of the current query, and if the length of the selected context exceeds the input window limit of the large language model, dynamically compresses it by deleting the semantic blocks with the lowest semantic similarity, and generates a response by combining the retrieved context and its Key-Value Cache through the large language model; Dynamic update module: Adds newly generated dialogue data to the context database in real time, and repeats the data processing and semantic block aggregation module's steps to the context database construction module for semantic block aggregation and database updates. The above embodiments are merely illustrative of the technical methods of the present invention and are not intended to limit it. Although the present invention has been described in detail with reference to preferred embodiments, those skilled in the art should understand that modifications or equivalent substitutions can be made to the technical methods of the present invention without departing from the spirit and scope of the present invention.
Claims
1. A method for dynamic management of long dialogue context based on semantic block aggregation, characterized in that, Includes the following steps: S1: Obtain training corpus. The training corpus is a Chinese dataset generated by distillation technology based on the Qwen3-235B-Thinking-2507 model. The training corpus is unsupervised clustered using a clustering algorithm to form multiple semantic categories. Each semantic category is manually semantically annotated. The clustering results and semantic annotations are used as supervision signals to train the m3e-small model as a semantic classification model for semantic classification of input text. S2: Acquire real-time dialogue data stream, including input queries and their corresponding responses, generate sentence vectors for each dialogue data using a word embedding model, calculate the cosine similarity of adjacent sentence vectors, determine whether to merge adjacent sentences into the same semantic block based on a preset threshold, and perform semantic compression processing on the merged semantic block. S3: Use the semantic classification model to classify semantic blocks, generate an embedding vector and a corresponding Key-Value Cache for each semantic block; construct a context database, associate and store the semantic blocks, embedding vectors and their Key-Value Caches according to their semantic categories, and establish an index based on semantic embedding vectors and time sequence; S4: Receive a new input query from the user, classify it using the semantic classification model, determine its target semantic category, perform a hybrid retrieval from the context database, combine the retrieved semantic blocks with the dialogue record to form the context of the current query, if the length of the selected context exceeds the input window limit of the large language model, dynamically compress it by deleting the semantic block with the lowest semantic similarity, and generate a response through the large language model by combining the retrieved context and its Key-Value Cache. S5: Add the newly generated dialogue data to the context database in real time, and repeat steps S2-S3 to perform semantic block aggregation and database updates.
2. The method for dynamic management of long dialogue context based on semantic block aggregation according to claim 1, characterized in that, In step S2, determining whether to merge adjacent statements into the same semantic block based on a preset threshold includes the following steps: Calculate the cosine similarity of adjacent statement vectors. If the cosine similarity is greater than a preset threshold, then merge the adjacent statements into the same semantic block. If the cosine similarity is equal to or less than a preset threshold, a new semantic block is created for the current statement.
3. The method for dynamic management of long dialogue context based on semantic block aggregation according to claim 1, characterized in that, In step S2, semantic compression processing is performed on the merged semantic blocks, including the following steps: Framework regularization is performed on the text in semantic blocks through architectural structured representation; Identify and resolve conflicting texts in the semantic block, filter stop words in the text, and apply a sentence reduction algorithm to remove redundant modifiers while retaining the core subject-verb-object or subject-verb structure. The output is a compressed semantic block containing timestamps and core semantic information.
4. The method for dynamic management of long dialogue context based on semantic block aggregation according to claim 1, characterized in that, In step S3, generating an embedding vector and a corresponding Key-Value Cache for each semantic block includes the following steps: Using a preset embedding vector generation model, the core semantic information of the compressed semantic block is input, and the embedding vector corresponding to the semantic block is calculated and output through the model. The embedding vector is a vector representation used to characterize the overall semantic features of the semantic block. Based on the self-attention mechanism of a pre-trained large language model, the compressed semantic block is used as input. Through the forward computation process of the model, the corresponding key matrix and value matrix are extracted and saved. The key matrix and value matrix together constitute the key-value cache of the semantic block. The generated embedding vector, Key-Value Cache, and corresponding semantic block time information are associated to form a semantic block data structure.
5. The method for dynamic management of long dialogue context based on semantic block aggregation according to claim 1, characterized in that, In step S3, establishing an index based on semantic embedding vectors and time sequence involves creating an inverted index for each semantic category partition, where the semantic embedding vector serves as the content index item and the timestamp of the semantic block serves as the time sequence index item.
6. The method for dynamic management of long dialogue context based on semantic block aggregation according to claim 1, characterized in that, In step S4, a hybrid retrieval is performed from the context database, including the following steps: Based on the target semantic category of the new input query, locate the corresponding category partition in the context database; Within the category partition, the similarity between the semantic embedding vector of the query and the semantic block embedding vector in the context database is calculated, and the N2 semantic blocks with the highest similarity are selected. At the same time, across all category partitions, the N1 most recent dialogue records are selected directly based on the time-series index item.
7. The method for dynamic management of long dialogue context based on semantic block aggregation according to claim 1, characterized in that, In step S4, dynamic compression is performed by deleting the semantic blocks with the lowest semantic similarity, including the following steps: The N2 semantic blocks and N1 dialogue records are merged into a candidate context set; Calculate the semantic similarity between each element in the candidate context set and the new input query; The elements in the context set are sorted from low to high according to the semantic similarity. Remove elements that are sorted to the end in turn until the text length of the remaining content meets the input window limit of the large language model.
8. A long dialogue context dynamic management system based on semantic block aggregation, used to implement the long dialogue context dynamic management method based on semantic block aggregation as described in any one of claims 1-7, characterized in that, Includes the following modules: Offline model preparation module: Acquire training corpus, which is a Chinese dataset generated by distillation technology based on the Qwen3-235B-Thinking-2507 model. Unsupervised clustering is performed on the training corpus to form multiple semantic categories, and each semantic category is manually semantically annotated. The clustering results and semantic annotations are used as supervision signals to train the m3e-small model as a semantic classification model for semantic classification of input text. Data processing and semantic block aggregation module: acquires real-time dialogue data stream, including input queries and their corresponding responses, generates sentence vectors for each dialogue data using a word embedding model, calculates the cosine similarity of adjacent sentence vectors, determines whether to merge adjacent sentences into the same semantic block based on a preset threshold, and performs semantic compression processing on the merged semantic block; Context database construction module: Classifies semantic blocks using the semantic classification model, generates an embedding vector and a corresponding Key-Value Cache for each semantic block; constructs a context database, associates and stores the semantic blocks, embedding vectors and their Key-Value Caches according to their semantic categories, and establishes an index based on semantic embedding vectors and time sequence; Online query response module: Receives new input queries from users, classifies them using the semantic classification model, determines their target semantic category, performs a hybrid retrieval from the context database, combines the retrieved semantic blocks with the dialogue record to form the context of the current query, and if the length of the selected context exceeds the input window limit of the large language model, dynamically compresses it by deleting the semantic blocks with the lowest semantic similarity, and generates a response by combining the retrieved context and its Key-Value Cache through the large language model; Dynamic update module: Adds newly generated dialogue data to the context database in real time, and repeats the data processing and semantic block aggregation module to the context database construction module for semantic block aggregation and database update.
Citation Information
Cited By
Method, device, equipment, storage medium and program product for context query
CN122173642A