A large model accompanying empathetic text generation method, system, device and equipment with layered Hook vector intervention
Patent Information
- Application Number
- CN202611209654.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-11
- Publication Date
- 2026-10-30
AI Technical Summary
[0006]现有各类共情生成方案,要么依赖表层提示、后置过滤,无法深度调控模型生成过程;要么依赖重训练、外置分类器,部署成本高、在线可调性差;同时普遍存在情感识别粒度不足、无法联动长期陪伴记忆、混合架构引导失效、生成过程缺少实时情感护栏等多重缺陷,难以兼顾细粒度共情精准度、长期陪伴自适应、推理轻量化、生成安全性四大核心需求,无法适配陪伴式机器人长期、高频、个性化的交互使用场景
[0010]其有益效果在于:构建离线向量制备与在线闭环推理的两阶段技术体系。离线定义百余类细粒度情感,采用差异化采样策略分工构建感知向量集合与引导向量集合:前者覆盖多样表达以优化识别泛化性,后者选取高强度典型样本以强化生成驱动力。在线推理阶段,在基座模型目标解码层挂载感知Hook,读取残差流聚合隐状态,通过单次矩阵乘并行完成全类别细粒度情感识别;结合感知结果、人设配置与长期记忆强度,经共情映射与记忆调制生成动态引导系数;在覆盖全注意力与非全注意力层的连续解码层区间接力注入共情向量,配套逐层约束抵御信号稀释;生成阶段逐token复用感知向量做情感监测,命中不当情感阈值触发三级分级处置。全流程嵌入模型单次前向传播,形成感知、决策、引导、监测的完整闭环。
Smart Images

Figure CN123014188A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of large language model reasoning intervention and natural language generation technology, and in particular to a method, system, device and equipment for generating empathic text using a large model with hierarchical Hook vector intervention. Background Technology
[0002] Current technologies for generating empathy through large language models for companion robots can be broadly categorized into four mature implementation approaches: The prompt word constraint scheme writes empathic and reassuring instructions into the prompt words of the model system, and relies on the native instructions of the base model to output empathic responses without any additional hidden intervention logic.
[0003] The independent sentiment classifier paired with a response template solution involves adding an external coarse-grained sentiment classification model such as BERT to identify user emotions. The classification results are matched with a preset fixed empathy template to generate a response. However, sentiment recognition and text generation are independent of each other, and the confidence level of recognition cannot be integrated into the generation process.
[0004] Supervised fine-tuning / human feedback reinforcement learning schemes use manually labeled empathic dialogue samples to complete SFT fine-tuning, or optimize the model's empathic expression preferences through RLHF, and achieve empathic effects by changing the model weights.
[0005] The rule-based emotion mapping template scheme involves manually maintaining a mapping rule table between emotions and fixed phrases, and outputting standardized empathic statements through simple rule matching.
[0006] Existing empathy generation solutions either rely on surface prompts and post-filtering, making it impossible to deeply control the model generation process; or they rely on retraining and external classifiers, resulting in high deployment costs and poor online adjustability. At the same time, they generally suffer from multiple defects such as insufficient granularity of emotion recognition, inability to link with long-term companionship memory, failure of hybrid architecture guidance, and lack of real-time emotion guardrails in the generation process. They are unable to meet the four core requirements of fine-grained empathy accuracy, long-term companionship adaptation, lightweight reasoning, and generation security, and cannot adapt to the long-term, high-frequency, and personalized interactive use scenarios of companion robots. Summary of the Invention
[0007] To solve the above-mentioned technical problems, the present invention provides the following technical solution: A method for generating empathic text for a large-scale model with hierarchical hook vector intervention includes: offline pre-construction of a set of perceptual vectors and a set of guiding vectors; online inference stage acquisition of the latent state, dialogue history, long-term memory entries and memory strength, and persona configuration data of the base large-scale language model; defining N types of fine-grained emotions, and generating the set of perceptual vectors and the set of guiding vectors by using generalized samples covering diverse expressions and typical samples with higher emotion intensity, respectively, through mean difference and L2 normalization; attaching a perceptual hook to the target decoding layer to read the residual stream aggregated latent state, calculating all fine-grained emotion scores through single matrix multiplication and parallel projection, outputting the top-k emotion results, and reusing computing power for computation and model forward propagation; determining the target empathic emotion based on the perceptual top-k results, persona configuration, and current dialogue state through empathy mapping rules, and concluding... The intensity of long-term memory activated by the dialogue modulates the basic guidance coefficient, generating a dynamic guidance coefficient that allows the empathy intensity to adaptively increase or decrease with long-term companionship memory. Guidance hooks are attached to the continuous decoding layer intervals that simultaneously cover the full attention layer and the non-full attention layer, and target empathy guidance vectors are injected layer by layer with weighted injection. This is accompanied by layer-by-layer scale normalization and relative norm constraints, forming a relay-style guidance to solve the problem of interference signal dilution in the hybrid attention architecture. In the generation stage, the perception vector set is reused for real-time emotion projection on a token-by-token basis. When an inappropriate emotion threshold is hit, three levels of handling strategies are triggered according to priority: resampling of the current token, backtracking of the current sentence generation, and guidance enhancement with negative direction injection. The four stages of perception, decision-making, guidance, and monitoring are integrated into a closed-loop process within a forward propagation, ultimately outputting an empathic response text and a token-by-token emotion monitoring sequence.
[0008] A large-scale model companionship and empathy text generation system with hierarchical Hook vector intervention is provided. The system is used to execute executable instructions to perform the above-mentioned large-scale model companionship and empathy text generation method with hierarchical Hook vector intervention.
[0009] A large-scale model companionship and empathy text generation device equipped with hierarchical Hook vector intervention is characterized in that the device is used to execute executable instructions to perform the above-mentioned large-scale model companionship and empathy text generation method equipped with hierarchical Hook vector intervention.
[0010] Its beneficial effects lie in constructing a two-stage technical system of offline vector preparation and online closed-loop inference. Offline, over a hundred fine-grained emotion categories are defined, and a differentiated sampling strategy is used to construct a set of perceptual vectors and a set of guiding vectors: the former covers diverse expressions to optimize recognition generalization, while the latter selects high-intensity typical samples to strengthen the generation driving force. In the online inference stage, a perceptual hook is attached to the target decoding layer of the base model, reading the residual stream to aggregate the hidden state, and completing full-category fine-grained emotion recognition through single matrix multiplication in parallel. Combining the perceptual results, persona configuration, and long-term memory strength, dynamic guiding coefficients are generated through empathy mapping and memory modulation. Empathy vectors are injected intermittently in the continuous decoding layer area covering full-attention and non-full-attention layers, with layer-by-layer constraints to resist signal dilution. In the generation stage, perceptual vectors are reused for emotion monitoring on a token-by-token basis, triggering a three-level graded handling process when an inappropriate emotion threshold is hit. The entire process is embedded in the model's single forward propagation, forming a complete closed loop of perception, decision-making, guidance, and monitoring.
[0011] This application effectively addresses the pain points of existing solutions, such as coarse empathy granularity, easy failure of guidance, inability to link with long-term memory, and lack of security protection in generation. The intensity and type of empathy can be dynamically adjusted at the inference end without retraining the model; the memory linkage mechanism makes the intensity of empathy adaptive with the depth of companionship, which is in line with the attributes of long-term companionship; token-level pre-interception avoids inappropriate emotional output, making the interaction experience safer; non-intrusive Hook deployment is compatible with any base large language model, the marginal computing power overhead of the whole process is negligible, and it takes into account the advantages of recognition accuracy, style controllability and lightweight deployment. Attached Figure Description
[0012] Figure 1 A flowchart illustrating a method for generating empathic text for a large model with hierarchical Hook vector intervention, provided in an embodiment of the present invention; Figure 2 This is a schematic diagram of the overall architecture of a companion robot empathic response generation system provided in an embodiment of the present invention; Figure 3 This is a schematic diagram comparing the location and effect of cross-type multi-layer guided injection in a 32-layer hybrid attention stacking architecture according to an embodiment of the present invention. Figure 4 This is a schematic diagram illustrating the division of labor and cooperation between a set of perception vectors and a set of guidance vectors, provided in an embodiment of the present invention. Figure 5 A schematic diagram illustrating the calculation of a dynamic guidance coefficient coupled with long-term memory strength, provided in an embodiment of the present invention; Figure 6 This is a schematic diagram of the timing of a token-by-token sentiment monitoring and generation period rollback guardrail provided in an embodiment of the present invention; Figure 7This is a schematic diagram of a module for a large-scale model-based empathic text generation system with hierarchical Hook vector intervention, provided in an embodiment of the present invention. Detailed Implementation
[0013] The preferred embodiments of the present invention will be described below with reference to the accompanying drawings. It should be understood that the preferred embodiments described herein are for illustration and explanation only and are not intended to limit the present invention. Figure 1 This paper describes a method for generating empathic text for large models with hierarchical Hook vector intervention, according to an exemplary embodiment of this application.
[0014] In this application embodiment, a method for generating empathic text for large-scale models with hierarchical Hook vector intervention is provided, such as... Figure 1 As shown: S101: Offline pre-construction of perceptual vector set and guiding vector set; online inference stage to obtain the latent state, dialogue history, long-term memory items and memory strength, and persona configuration data of the base large language model.
[0015] In one implementation, the perception vector set and the guidance vector set are designed with separate functions. The two types of vectors are constructed based on the same set of emotion category definitions, but they adopt different sample sampling strategies to adapt to the different needs of emotion recognition and generation. The entire construction process is completed offline, and the vectors are directly loaded and used in the inference stage without dynamic calculation.
[0016] The construction of the perceptual vector set consists of five steps. The first step is to define a fine-grained emotion category system. Based on the characteristics of emotional expression in long-term companionship scenarios, 100 to 200 fine-grained emotion categories are set, covering complex emotions that frequently occur in daily interactions, such as gratitude, relief, heartache, guilt, acceptance, a sense of relief, and regret. Each emotion category corresponds to a clear semantic boundary. For example, for the low mood that users often experience during companionship, several independent categories such as grief, loss, grievance, self-blame, and anxiety are subdivided to replace the traditional coarse-grained category of sadness, ensuring the precision of emotion recognition.
[0017] The second step is to collect generalized sentiment samples. For each type of fine-grained emotion, multiple text samples covering diverse expressive scenarios are collected. These samples encompass different sentence structures, contexts, and intensities of the same emotion to ensure the generalization capability of the samples. For example, when constructing perception samples for the "heartache" category, texts describing different scenarios such as a family member being sick, a pet going missing, a friend experiencing setbacks, or one's own efforts not being rewarded are collected to avoid recognition bias caused by a single sample.
[0018] The third step is to calculate the mean hidden state of sentiment. Each sentiment sample text is input into the base large language model, and the residual stream hidden state output by the specified decoding layer of the model is obtained. Average pooling is performed on all valid token positions within a single sample to obtain the hidden state vector corresponding to that sample. Then, the mean of the hidden state vectors of all samples under the same sentiment category is calculated dimension-wise to obtain the mean vector of samples for that sentiment category. For example, after processing all samples in the "heartache" category, the average of the hidden states of all samples is calculated dimension-wise to finally obtain the mean sentiment vector for the "heartache" category.
[0019] The fourth step is to calculate the global neutral baseline vector. A batch of neutral texts without emotional bias is collected, such as objective descriptions of the attributes of things, explanations of how to use items, and statements of factual information. Using the same hidden state extraction and mean calculation process as the sentiment samples, a global neutral mean vector is obtained, which serves as the baseline zero point for sentiment direction calculation.
[0020] The fifth step involves performing mean difference and normalization operations. The global neutral mean vector is subtracted from the mean vector of each emotion category to obtain the directional difference vector corresponding to that emotion. This difference vector is then normalized to eliminate computational bias caused by differences in vector magnitude, ultimately yielding the perceptual vector corresponding to that emotion. The perceptual vectors of all emotion categories are then concatenated sequentially to form a complete set of perceptual vectors. The number of rows in this set matches the dimension of the model's hidden layers, and the number of columns matches the total number of emotion categories. This overall optimization of the perceptual vector set enhances the separability of fine-grained emotions, enabling precise differentiation between semantically similar emotions.
[0021] The guidance vector set corresponds to the exact same emotion categories as the perception vector set. It employs a differentiated sampling strategy and optimization objective, and its construction process is consistent with the computational logic of the perception vector set. The first step is to collect high-intensity typical samples. For each fine-grained emotion category, samples with higher emotional expression intensity and more typical style are prioritized. This can be achieved by initially scoring the emotional intensity and selecting the top proportion of content, or by using a smaller-scale but more rigorous sample strategy. For example, when constructing guidance samples for the "heartache" category, only typical expressions with the strongest emotional expression and the clearest empathy direction are selected to ensure the purity of the emotional direction of the samples. The second step is to perform mean difference and normalization calculations. Using the same computational process as the perception vectors, the sample mean vector for each emotion category is first calculated, then the global neutral mean vector is subtracted to obtain the difference vector, and finally normalization is performed to obtain the guidance vector for the corresponding emotion. All vectors are concatenated to form the guidance vector set. The perception vector and guidance vector for the same emotion are roughly in the same direction, but the guidance vector is purer and has a stronger driving force in the direction of causal text generation, specifically used to inject empathy direction into the model. The two types of vectors work together to avoid the performance imbalance that occurs when a single vector simultaneously undertakes both recognition and driving functions.
[0022] During the online inference phase, two pre-built vector sets of two types are directly loaded as static resources, while multi-dimensional initial input data is acquired to support the entire process of subsequent emotion perception, empathy decision-making, and guided generation. The latent state of the base large language model is obtained from the perceptual hooks during the forward propagation process and is an intermediate computational product.
[0023] First, dialogue history data acquisition. A fixed-window length of historical interaction records is read from the context of the current conversation, including past rounds of user input and robot responses. These are concatenated chronologically to form a context sequence, which serves as the input prefix for the base language model, ensuring that the response content fits the current dialogue context. For example, if the dialogue window is set to recent rounds of interaction, and the current user mentions that they didn't pass the interview they discussed last time, the system will automatically retrieve relevant historical content from previous conversations regarding the job application and preparation process, allowing the model to accurately understand the referent and context of the current statement.
[0024] Second, the system acquires long-term memory entries and their strength. Based on the semantic content of the current user input and the dialogue history, it retrieves semantically matching memory entries from the long-term memory bank. Each memory entry is accompanied by a corresponding current memory strength value, which ranges from 0 to 1. Significant emotional events that are repeatedly reinforced correspond to higher memory strengths, while those without relevant matching memories have a memory strength of zero. For example, if a user mentions that their cat is still missing, the system retrieves a corresponding pet-lost event entry from the long-term memory bank. This entry has been activated multiple times, and its current memory strength is 0.82. The system will then simultaneously send this memory entry and its corresponding memory strength to the subsequent empathy decision-making module.
[0025] Third, user persona configuration data acquisition. The user persona configuration parameters corresponding to the current companion robot are loaded. These parameters define the robot's response style and empathy tendency. For example, a gentle and caring older sister persona corresponds to a higher weight for empathy expression, leaning towards a soft and comforting response; a humorous older brother persona corresponds to a more relaxed empathy style, leaning towards an encouraging and supportive response. User persona configuration, as one of the input items for empathy mapping, influences the judgment result of the target empathy emotion.
[0026] Fourth, user input data acquisition. The system receives the text content of the user's current interaction, concatenates it with the dialogue history, and inputs it into the base language model, triggering forward propagation and subsequent sentiment perception processes.
[0027] S102 defines N types of fine-grained emotions, and uses generalized samples covering diverse expressions and typical samples with higher emotional intensity to generate a set of perception vectors and a set of guidance vectors through mean difference and L2 normalization.
[0028] In one implementation, based on the emotional interaction characteristics of long-term companionship scenarios, 100 to 200 fine-grained emotion categories are set, covering complex emotions that frequently occur in daily interactions, such as gratitude, relief, heartache, guilt, acceptance, a sense of relief, and regret. Each emotion category has a clear semantic boundary, replacing the traditional coarse-grained emotion classification of 5 to 10 categories. For example, for users' low mood, independent categories such as grief, loss, grievance, self-blame, and anxiety are subdivided; for positive emotions, independent categories such as joy, pride, warmth, and relief are subdivided. All categories together constitute a complete emotional labeling system, serving as a unified benchmark for constructing the two types of vectors.
[0029] The perceptual vector set is optimized for fine-grained emotion recognition tasks, pursuing accuracy and generalization in direction. The construction process consists of five steps. The first step is to collect generalized emotion samples. For each type of fine-grained emotion, multiple text samples covering diverse expressive scenarios are collected. These samples encompass different sentence structures, contexts, and intensities of the same emotion, ensuring the samples' generalization coverage. For example, when constructing perceptual samples for the "heartache" category, texts describing different scenarios such as a family member being sick, a pet going missing, a friend experiencing setbacks, or personal efforts not being rewarded are collected to avoid recognition bias caused by a single sample.
[0030] The second step is to extract the hidden states of the samples and perform average pooling. Each sentiment sample text is input into the base large language model, and the residual stream hidden states output by the specified decoding layer of the model are obtained. The average pooling process is then performed by averaging the values of all token positions within a single sample, resulting in a single-sample hidden state vector representing the sentiment semantics of the entire sample. For example, when processing a user text describing a lost pet, the hidden states corresponding to each token in the target decoding layer are read, and the average value of the hidden states across all positions in the sequence is calculated dimension-wise to obtain the hidden state vector corresponding to that sample.
[0031] The third step is to calculate the mean hidden state of the sentiment category. The mean of the hidden state vectors of all samples within the same sentiment category is calculated dimension-wise to obtain the mean vector of that sentiment category. For example, after processing all samples in the "heartache" category, the average of the hidden states of all samples is calculated dimension-wise, ultimately yielding the mean sentiment vector for the "heartache" category. This vector encapsulates the shared sentiment semantic features of all generalized samples.
[0032] The fourth step is to calculate the global neutral baseline vector. A batch of neutral texts without sentiment bias is collected, such as objective descriptions of object attributes, explanations of how to use items, and statements of factual information. Using the same latent state extraction and mean calculation process as the sentiment samples, a global neutral mean vector is obtained. This vector serves as the baseline zero point for sentiment direction calculation, used to strip away general semantic representations and extract the pure sentiment direction component.
[0033] The fifth step involves performing mean differencing and normalization operations. The mean vector of each sentiment category is subtracted from the global neutral mean vector to obtain the directional difference vector corresponding to that sentiment. This differencing operation eliminates interference from general semantics, retaining only the directional features relevant to sentiment. The difference vector is then normalized, scaling its length to unit length to eliminate computational bias caused by differences in vector magnitude, ultimately yielding the perceptual vector corresponding to that sentiment. The perceptual vectors of all sentiment categories are then concatenated sequentially to form a complete set of perceptual vectors. The number of rows in the perceptual vector set matches the hidden layer dimension of the base large language model, and the number of columns matches the total number of sentiment categories.
[0034] The guidance vector set is optimized for empathy-generating guidance tasks, pursuing the strength and purity of direction. It corresponds to the exact same emotion categories as the perception vector set, employing a differentiated sampling strategy, while maintaining a consistent computational process. The first step involves collecting high-intensity, typical samples. For each fine-grained emotion category, samples with higher emotional expression intensity and more typical style are prioritized. Samples can be selected based on a preliminary emotional intensity score, choosing the top proportions, or a smaller-scale but more rigorous sample strategy can be used. For example, when constructing guidance samples for the "heartache" category, only the most intense emotional expression and the clearest empathy direction are selected to ensure the purity of the emotional direction and avoid weak emotional samples diluting the driving force of the guidance direction. The second step involves performing mean difference and normalization calculations. Using the same computational process as the perception vector, the sample mean vector for each emotion category is first calculated, then the global neutral mean vector is subtracted to obtain the difference vector, and finally normalization is performed to obtain the corresponding emotion's guidance vector. All vectors are concatenated to form the guidance vector set.
[0035] The perceptual vector and the guidance vector corresponding to the same emotion are roughly in the same direction, but their optimization objectives differ. The perceptual vector is more separable among subdivided emotions, accurately distinguishing different emotions with similar semantics, making it suitable for emotion discrimination. The guidance vector is purer in its causal driving direction of text generation, with stronger driving force, making it suitable for injecting empathy into the model. The two types of vectors work together to avoid the performance imbalance that occurs when a single vector simultaneously undertakes both recognition and driving functions, balancing emotion recognition accuracy and generation guidance strength.
[0036] S103 loads a perception hook onto the target decoding layer to read the residual stream and aggregate the hidden state. It calculates all fine-grained sentiment scores through a single matrix multiplication and parallel projection, and outputs the top-k sentiment results. The computation and model forward propagation reuse computing power.
[0037] In one implementation, based on the requirements of fine-grained real-time emotion recognition, controllable marginal computational overhead, and compatibility with normal forward propagation of the model, perceptual hook attachment, header target token skipping, and hidden state average pooling are performed on the residual stream of the target decoding layer of the base large language model. A single matrix multiplication is used to complete the parallel projection operation of fine-grained emotions across all categories, generating aggregated hidden states, emotion scores for all categories, and top-k emotion ranking results. A read-only perceptual hook is attached to the target decoding layer of the base large language model. All emotion calculations are completed in one go during the model pre-filling stage after the user inputs and concatenates the dialogue history. The model's forward propagation computational power is reused throughout the process without modifying the original calculation results, ultimately outputting three standardized results.
[0038] The first step is the preprocessing of the perceptual hook attachment and residual stream aggregation. A perceptual hook is attached to the target decoding layer. This hook only has read permissions and does not modify the original residual stream data. When the model propagates forward to this layer, the hook automatically reads the complete residual stream data output by this layer. Then, it skips the special token positions at the beginning of the sequence to eliminate computational interference caused by special tags without sentimental semantics. Next, it performs average pooling on the hidden states corresponding to the remaining valid tokens, calculating the average value of the hidden states at all valid positions dimension-by-dimensional to generate standardized aggregated hidden states.
[0039] For example, a user inputs text describing setbacks in job hunting. After concatenating the dialogue history, the text is input into the base language model. When the model enters the pre-filling stage and propagates forward to the preset target decoding layer, the perceptual hook automatically captures the residual stream data of all tokens in that layer. The system removes special marker positions at the beginning of the sequence and averages the hidden states of the remaining valid text tokens dimension by dimension to obtain an aggregated hidden state vector with dimensions consistent with the model's hidden layers, fully representing the sentiment semantics of the entire input.
[0040] The second step involves parallel projection computation of fine-grained sentiment data across all categories. The aggregated latent states are subtracted from the global neutral mean vector to perform neutral baseline differencing. After removing interference from general semantics, a single matrix multiplication operation is performed with the offline pre-built set of perceptual vectors. This parallel computation calculates the matching scores for all fine-grained sentiment categories, generating the overall sentiment score. This operation shares computational resources with the model's forward propagation, resulting in extremely low marginal computational overhead; a single round of computation on a GPU takes less than 0.1 milliseconds.
[0041] For example, the sentiment feature vector that has undergone differential processing is multiplied by a set of perception vectors containing 100 to 200 fine-grained sentiments, and the matching scores of all sentiment categories are output simultaneously. No additional model inference is required throughout the process, and the original generation efficiency is not affected.
[0042] The third step is to output the top-k sentiment ranking results. All sentiment scores are sorted from highest to lowest, and the top k sentiment categories and their corresponding scores are selected to generate the top-k sentiment ranking results, which are then directly transmitted to the subsequent empathy decision-making stage.
[0043] For example, after all emotion scores are sorted, the system extracts the top k emotion categories and their scores, organizes them into structured results according to their scores, and sends them to the empathy decision-making module to provide fine-grained basis for determining the target empathy emotion.
[0044] Based on the architectural requirements of computing power reuse and deep coupling of sentiment perception and text generation, the fine-grained sentiment perception task is divided into lightweight modules. These modules are responsible for residual stream reading, neutral benchmark differencing, perceptual vector projection, and sentiment ranking output. The execution logic and data flow relationships of each module are clearly defined, forming a task execution information system that coordinates reading aggregation, projection calculation, and result output. Following the architectural requirements of computing power reuse and deep coupling of sentiment perception and text generation, the fine-grained sentiment perception task is broken down into four independent lightweight functional modules. Each module has a clearly defined division of labor and data flow logic, forming an execution chain that coordinates reading aggregation, projection calculation, and result output.
[0045] The first module is the residual stream reading module, which connects to the target decoding layer of the base large language model. It is responsible for the mounting and triggering of the perception hook, performing special token filtering in the header and average pooling operation of the hidden state, and outputting a standardized aggregated hidden state vector, which is the data entry point for the entire perception process.
[0046] The second module is the neutral benchmark difference module, which pre-stores the global neutral mean vector, receives the aggregated hidden state output by the residual stream reading module, performs dimension-wise difference operations, and outputs a sentiment feature vector that removes general semantic interference, providing a clean sentiment feature input for subsequent projection calculations.
[0047] The third module is the perceptual vector projection module, which loads the offline constructed perceptual vector set, receives the emotional feature vector, performs parallel matrix multiplication operations, and outputs a full-category emotional score vector with the same dimension as the total number of fine-grained emotional categories. It is the core execution unit of emotional computing.
[0048] The fourth module is the emotion ranking output module, which receives the score vectors of all categories, performs ranking and filtering and top-k truncation, and outputs the final perception result in a fixed format, which is then connected to the subsequent empathy decision-making module.
[0049] The four modules are connected in sequence according to the data flow direction and embedded into the model forward propagation process by hook mounting. They do not modify the original weights and calculation logic of the model and can flexibly adapt to base large language models with different structures.
[0050] Combining the requirements of accuracy in emotion recognition, avoidance of interference from target tokens, and computational efficiency, this paper sets the target mounting layer, the range of head skip positions, and the value of the emotion selection quantity k based on the semantic representation characteristics of the decoding layer. It also determines the hidden state aggregation method, the dimension of emotion score output, and the rules for top-k result output. Furthermore, based on the three core requirements of accuracy in emotion recognition, avoidance of interference from special tokens, and computational efficiency, three types of core parameters are set according to the semantic representation characteristics of the decoding layer, and corresponding execution rules are clearly defined to ensure a balance between perceptual effectiveness and performance.
[0051] The first type of parameter is the target mounting layer, which is set based on the semantic representation characteristics of the decoding layer. The intermediate decoding layer with a semantic abstraction level that matches the needs of emotion recognition is selected as the mounting position. This layer has completed shallow grammar and literal feature extraction, and the emotional semantic representation is sufficient, which can ensure the accuracy of fine-grained emotion discrimination.
[0052] The second type of parameter is the head skipping position range. It is set based on the need to avoid interference from special tokens. It defines the position range that the head of the sequence needs to skip, eliminates the dilution of the calculation results by special tags without actual emotional semantics, and improves the signal-to-noise ratio of sentiment calculation.
[0053] The third parameter is the number of emotions selected, k. The setting is based on the balance between the richness of recognition information and decision-making efficiency. If the k value is too small, it will miss the user's mixed emotional information. If the k value is too large, it will introduce low-confidence noise. By taking a reasonable value, the integrity of emotional information and the efficiency of subsequent decision-making can be taken into account.
[0054] Based on the parameter configuration, three execution rules were simultaneously determined. First, the hidden state aggregation method was determined, uniformly employing average pooling to process the hidden states of valid tokens, ensuring that input texts of different lengths can output aggregated vectors with consistent dimensions. Second, the sentiment score output dimensions were determined, with each output dimension corresponding one-to-one with the total number of fine-grained sentiment categories, and each dimension corresponding to the matching score of one sentiment category. Third, the top-k result output rules were determined, uniformly outputting k sentiment results in descending order of score, ensuring consistency in the reading format for subsequent modules.
[0055] S104 determines the target empathic emotion based on the perception top-k results, persona configuration, and current dialogue state through empathy mapping rules. It modulates the basic guidance coefficient by combining the strength of long-term memory activated by the dialogue, generating a dynamic guidance coefficient that allows the empathic intensity to adaptively increase or decrease with long-term companionship memory.
[0056] In one implementation, based on the accuracy of fine-grained emotion matching, consistency of empathic style persona, and the need for long-term companionship adaptability, empathic mapping matching is performed on the perceived top-k emotion results, persona configuration, and current dialogue state. Guidance coefficient modulation is then performed in conjunction with the strength of long-term memory activated by the dialogue, generating target empathic emotion category and dynamic guidance coefficient information. The process is divided into two stages: empathic mapping matching and guidance coefficient modulation. Emotional target determination and guidance intensity calculation are completed sequentially, ultimately outputting two types of results: target empathic emotion category and dynamic guidance coefficient.
[0057] The first stage is empathy mapping and matching. Using three types of data as input—the top-k emotion results from the perception stage, the robot's persona configuration, and the current dialogue state—a matching judgment is completed through preset empathy mapping rules to determine the target empathic emotion suitable for the current scenario. The mapping process is based on preset emotion correspondences, combined with persona style and dialogue context to complete a comprehensive judgment, ensuring that the empathic response fits the current interaction scenario.
[0058] For example, the top three emotions output by the perception stage are grief (0.86), loss (0.72), and guilt (0.35). The current robot persona is a gentle and caring older sister. The dialogue context is that the user is telling about the experience of the death of a family member. After the system completes the matching by combining the mapping rules, it determines that the target empathy emotion is heartache and comfort, and the response style is inclined to be gentle and soothing.
[0059] The second stage involves modulating the guidance coefficient. Based on the memory strength corresponding to the long-term memory entries activated in the current dialogue, dynamic modulation is performed on the base guidance coefficient to generate the final dynamic guidance coefficient. The modulation logic is that the higher the memory strength, the greater the increase in the guidance coefficient, and the stronger the corresponding empathy expression. This achieves adaptive adjustment of empathy intensity according to long-term companionship memories. During calculation, the dynamic guidance coefficient is obtained by multiplying the base guidance coefficient by one and summing the product of the memory modulation weight and the memory strength. The memory strength ranges from 0 to 1; when there is no matching memory, the memory strength is 0, and the dynamic guidance coefficient equals the base guidance coefficient. This mechanism allows similar perceptual emotions to exhibit different levels of empathy at different stages of companionship, naturally aligning with the long-term companionship attribute of companion robots.
[0060] For example, if the basic guidance coefficient is set to 0.15 and the memory modulation weight is 0.6, the system retrieves the event entry corresponding to the death of a user's relative from the long-term memory bank. After multiple activations, the current memory strength of this entry is 0.82. After substituting it into the modulation rules for calculation, the dynamic guidance coefficient is approximately 0.22. Compared to the basic state without memory association, the empathy guidance strength is significantly improved, which is suitable for the deep companionship needs of major emotional events.
[0061] Based on the hierarchical linkage architecture of empathic decision-making and memory modulation, the empathic decision-making task is modularized, dividing it into modules for empathic mapping and matching, long-term memory retrieval, and guided coefficient modulation. The execution logic and data flow relationships of each module are clearly defined, forming a task execution information system that coordinates emotion assessment, memory linkage, and coefficient generation. Following the hierarchical linkage architecture of empathic decision-making and memory modulation, the entire decision-making task is broken down into three independent functional modules. Each module has clearly defined execution logic and input / output boundaries, with data flowing unidirectionally according to business sequence, forming an execution chain for the coordinated operation of emotion assessment, memory linkage, and coefficient generation.
[0062] The first module is the empathy mapping and matching module, which is the core unit for emotion determination. This module receives the top-k emotion results from the perception stage, character configuration parameters, and current dialogue state information. It loads preset empathy mapping rules, completes the matching calculation, and finally outputs the target empathy emotion category. This module can independently update the mapping rules, adjusting the empathy style adaptation logic without altering other links.
[0063] The second module is the long-term memory retrieval module, which serves as the supporting unit for memory linkage. This module receives the user's current input and dialogue history data, retrieves semantically matching memory entries from the long-term memory bank, extracts the memory strength value of the corresponding entry, and outputs it to the subsequent coefficient modulation stage. When no matching memory entry is found, the output memory strength is 0, ensuring process compatibility.
[0064] The third module is the guidance coefficient modulation module, which is the execution unit for coefficient generation. The module receives three types of inputs: basic guidance coefficients, memory modulation weights, and long-term memory strength. It performs dynamic coefficient calculations according to a fixed modulation formula and outputs the final dynamic guidance coefficients, which are directly supplied to the subsequent guidance injection stage.
[0065] The three modules are connected sequentially according to their data dependencies. The outputs of the empathy mapping matching module and the long-term memory retrieval module are jointly fed into the guidance coefficient modulation module. The overall architecture has a high degree of decoupling and can flexibly adjust the rule parameters of each module to adapt to different companionship scenarios and user groups.
[0066] Combining the requirements of emotional response adaptability, reasonable linkage of memory intensity, and controllable empathy intensity, this study sets empathy mapping rules, memory modulation weights, and basic guidance coefficients based on fine-grained emotional correspondences and memory intensity value ranges. It also determines the target emotional judgment logic, coefficient modulation formulas, and memory intensity adaptability rules. Furthermore, by considering the three requirements of emotional response adaptability, reasonable linkage of memory intensity, and controllable empathy intensity, and based on fine-grained emotional correspondences and memory intensity value ranges, three types of core parameters are set, and three corresponding execution rules are clarified to ensure the stability and controllability of the decision-making logic.
[0067] The first type of parameter is the empathy mapping rule, which is set based on the semantic correspondence of fine-grained emotions, while incorporating the style weights of different personas. The rule covers the mapping logic from all fine-grained emotions to the target empathic emotion. Typical correspondences include: when a user expresses sadness, grief, or loss, the direction is one of compassion and comfort; when a user expresses anxiety, tension, or fear, the direction is one of composure and reassurance; when a user expresses joy, a sense of accomplishment, or pride, the direction is one of delight, resonance, and heartfelt praise; when a user expresses feelings of grievance or misunderstanding, the direction is one of compassion, understanding, and acceptance; when a user expresses feelings of guilt or self-blame, the direction is one of comfort and acceptance; and when a user expresses feelings of gratitude or warmth, the direction is one of gentle response and kindness. For the same user emotion, different personas will correspond to different expressive tendencies, ensuring consistency between the empathy style and the persona.
[0068] The second type of parameter is the memory modulation weight, which is set based on the required magnitude of the influence of memory on the level of empathy, and its value ranges from 0.3 to 1.0. The larger the weight value, the stronger the modulation effect of memory intensity on the guidance coefficient, and the more obvious the differences in long-term companionship; the smaller the weight value, the smaller the difference in the level of empathy under different memory intensities, and the more stable the overall style.
[0069] The third type of parameter is the basic guidance coefficient, which is set based on the strength requirements of basic empathy expression. It is the benchmark empathy strength when there is no memory association, ensuring a stable basic empathy effect in all scenarios and avoiding the lack of empathy in memory-free scenarios.
[0070] Based on three types of parameters, three execution rules are simultaneously determined. The first is the target emotion judgment logic, which uses the top-k emotion results as input, combines personality preferences and dialogue context, and completes a comprehensive judgment through an empathy mapping function to output a unique target empathy emotion category that matches the user's emotional state. The second is the coefficient modulation formula, which uses a calculation method of superimposing a base coefficient with memory gain to ensure that memory strength and empathy intensity are positively correlated, and that the gain amplitude is controllable to prevent intensity overflow. The third is the memory strength adaptation rule: when there is no matching memory, the intensity is 0, and the guiding coefficient maintains the baseline value; low-intensity memory corresponds to a small gain, and high-intensity memory corresponds to a large gain, realizing a smooth adaptive adjustment of empathy intensity with the depth of companionship.
[0071] S105 attaches a guiding hook to the continuous decoding layer interval that simultaneously covers the full attention layer and the non-full attention layer, injects the target empathy guiding vector layer by layer with weighted injection, and provides layer-by-layer scale normalization and relative norm constraints to form a relay-style guidance to solve the problem of interference signal dilution in the hybrid attention architecture.
[0072] In one implementation, a hybrid attention architecture is used to intervene in signal dilution resistance and guide intention. Figure 1To ensure consistency and controllability of model distribution, a relay-style guidance chain is constructed for continuous decoding layer intervals covering both full-attention and non-full-attention layers. This chain involves guiding hook attachment, weighted injection of target empathy guidance vectors, and layer-by-layer constraint verification. The resulting residual flow state and relative norm verification results for each layer are generated after adjustments. The entire execution process revolves around the construction of this relay-style guidance chain, sequentially completing four steps: layer interval selection and hook attachment, weighted injection of guidance vectors, layer-by-layer scaling normalization, and relative norm constraint verification. This process is fully compatible with normal forward propagation of the model and does not interrupt the original computational logic.
[0073] The first step involves selecting continuous decoding layer intervals and attaching guiding hooks. For the hybrid attention architecture of the base large language model, continuous or near-continuous decoding layer intervals that simultaneously cover both full attention layers and non-full attention layers are selected. A guiding hook is attached to each decoding layer within this interval. Non-full attention layers include various types such as gated attention, linear attention, and state space layers. If single-point injection is only performed in a few full attention layers, the guiding signal will be diluted and filtered during the state transitions of subsequent non-full attention layers, resulting in a very low energy proportion in the guiding direction when reaching the top layer of the model, making the generated result insensitive to the guiding intent. Continuous attachment across different types creates a relay link of alternating injection and dilution, ensuring that the guiding signal remains discernible deep within the model.
[0074] For example, a certain base language model adopts a 32-layer hybrid attention stacked structure, with 3 gated attention layers and 1 full attention layer in every 4 layers. The system selects a continuous decoding layer interval as the guidance attachment range. This interval simultaneously includes all full attention layers and gated attention layers within the interval. Each layer within the interval has a guidance hook attached, forming a continuous relay guidance link covering multiple layers, replacing the traditional single-point injection scheme that only attaches to a few full attention layers.
[0075] The second step is the weighted injection of the target empathy guidance vector. When each guidance hook is triggered, the guidance vector corresponding to the target empathy emotion is retrieved from the offline-built guidance vector set. Combined with the dynamic guidance coefficient and the scale normalization factor of that layer, the injection perturbation vector for the current layer is calculated. This perturbation vector is then superimposed dimension-by-dimensionally onto the original residual flow of that layer, completing the single-layer guidance injection. The injection operation is performed synchronously with the model's forward propagation, adding only a very small amount of addition overhead and not affecting the overall generation speed.
[0076] For example, in the empathy decision-making process, the target empathic emotion is determined to be "heartache," corresponding to the heartache direction vector in the guidance vector set, with a current dynamic guidance coefficient of 0.224. When the guidance hook of the intermediate full attention layer is triggered, the injection perturbation vector of that layer is calculated by combining the scale normalization factor of that layer, and then superimposed on the original residual flow of that layer dimension by dimension, causing the hidden state of that layer to shift towards the heartache empathy direction. The subsequently generated text will then present a corresponding empathic tone of heartache and comfort.
[0077] The third step is layer-by-layer scale normalization. An independent scale normalization factor is configured for each decoding layer. This factor is typically taken from the historical activation average norm of the residual flow of that layer. Its core function is to ensure the comparability of the guiding strength between different layers. Decoding layers at different depths naturally exhibit different activation amplitudes. A uniform injection coefficient would lead to inconsistent relative guiding strengths across different layers. The scale normalization factor is used to calibrate the injection strength of different layers, maintaining a balanced relative strength of the guiding signals across layers and preventing imbalances in guiding strength between layers.
[0078] For example, the historical activation mean norm of shallow decoding layers corresponds to a lower value, while the historical activation mean norm of deep decoding layers corresponds to a higher value. The system configures scale normalization factors for the corresponding values for both layers. Under the same dynamic guidance coefficient, the absolute amplitude of the injection perturbation in the deep layer will be adjusted synchronously with the normalization factor to ensure that the ratio of the guidance perturbation of the two layers to their own layer activation intensity is consistent, thus achieving comparability of guidance intensity between layers.
[0079] The fourth step is relative norm constraint verification and over-limit shrinkage. Immediately after each layer of guided injection, relative norm verification is performed. The norm of the perturbation vector introduced by the injection is calculated and compared with the result of multiplying the upper limit of the relative norm by the original residual flow norm. If the perturbation norm does not exceed the limit, the injection result is retained; if it exceeds the limit, the perturbation vector is shrunk proportionally until the final perturbation norm is exactly equal to the limit. This mechanism limits the perturbation amplitude of guided injection on the model's native distribution, preventing excessively strong guidance from causing generated content to deviate from normal semantics and resulting in text corruption.
[0080] For example, a relative norm upper limit can be set based on the stability requirements of the generation. The original residual flow of a certain layer corresponds to a fixed norm, and the maximum allowable perturbation norm is the product of the relative norm upper limit and the norm of the original residual flow. After the initial injection of this layer, if the actual norm of the perturbation vector exceeds the allowable limit, the system keeps the perturbation direction unchanged, scales all dimensions of the perturbation vector proportionally, shrinks its norm back to the maximum allowable value, and then superimposes it onto the original residual flow. This ensures that the guiding role is maximized while keeping the perturbation within a safe range, thus maintaining the stability of the model generation.
[0081] Based on the architectural requirement of compatibility between the guidance signal relay transmission, injection process, and model forward propagation, the empathy-guided injection task is modularized. This involves dividing the task into modules for layer interval selection, guidance vector injection, layer-by-layer scale normalization, and relative norm constraint verification. The execution logic and data flow relationships of each module are clearly defined, forming a task execution information system that coordinates cross-layer mounting, weighted injection, and constraint verification. Following the architectural requirement of compatibility between the guidance signal relay transmission, injection process, and model forward propagation, the empathy-guided injection task is broken down into four independent functional modules. Each module has clearly defined execution logic and data flow relationships, forming an execution chain that coordinates cross-layer mounting, weighted injection, and constraint verification.
[0082] The first module is the layer interval selection module, responsible for the overall planning of the guidance link. This module takes into account the decoding layer type distribution and residual flow activation characteristic data of the base large language model, and combines this with guidance strength requirements and computational constraints to output the optimal mounting layer intervals and guidance Hook configuration schemes for each layer. This module can flexibly adjust the mounting range according to the architectural characteristics of different base models, adapting to hybrid attention models with different parameter scales.
[0083] The second module is the guide vector injection module, which is responsible for the core execution of the guide signal. This module takes as input the target empathy emotion category, dynamic guide coefficients, and an offline guide vector set. When each guide hook is triggered, it retrieves the corresponding guide vector, performs weighted calculations and superimposes the residual stream, and outputs the injected intermediate residual stream state. This module is deeply coupled with the model's forward propagation and executes synchronously with the forward computation.
[0084] The third module is the layer-by-layer scale normalization module, which is responsible for the equalization calibration of the guidance intensity between layers. This module pre-stores the scale normalization factor corresponding to each decoding layer. During the injection calculation for each layer, the corresponding factor is passed in to adjust the injection perturbation amplitude of that layer and outputs the calibrated perturbation vector. This module ensures that the guidance intensity at different depths is relatively balanced, achieving comparability of guidance intensity between layers.
[0085] The fourth module is the relative norm constraint verification module, responsible for the safe management of injected disturbances. This module takes the original residual flow, the injected residual flow, and the upper limit of the relative norm as input, performs norm verification calculations, determines whether the disturbance exceeds the limit, performs rollback processing for those exceeding the limit, and outputs the final verified residual flow status and verification result information. This module acts as a safety barrier for guided injection, ensuring that all interventions are within a controllable range.
[0086] The four modules are connected in series according to the data flow. When the guiding hook is triggered at each layer, it goes through three processing stages in sequence: guiding vector injection, scale normalization calibration, and norm constraint verification. Finally, the adjusted residual stream is output and enters the next layer for decoding calculation. The overall architecture is connected to the model's forward flow through hook attachment without modifying the model's original weights and calculation logic.
[0087] Combining the requirements of deep discernibility of the guiding signal, balanced guiding between layers, controllable disturbances, and architectural adaptability, and based on the distribution of decoding layer types and the activation characteristics of residual flow, the continuous mounting layer interval, scale normalization factor, and upper limit of relative norm are set to determine the injection operation rules, over-limit shrinkage logic, and inter-layer force operation information. Combining the four requirements of deep discernibility of the guiding signal, balanced guiding between layers, controllable disturbances, and architectural adaptability, and based on the distribution of decoding layer types and the activation characteristics of residual flow, three types of core parameters are set, and three corresponding execution rules are defined to ensure a balance between guiding effect and model stability.
[0088] The first type of parameter is the continuous mounting layer interval, which is set based on the type distribution of the decoding layer and the guidance depth requirements. The interval needs to cover a sufficient number of non-full attention layers to offset the signal dilution effect, while also taking into account the computational overhead. If the interval is too short, it cannot effectively combat signal dilution, and if the interval is too long, it will increase unnecessary computation. It needs to be determined comprehensively based on the model architecture and business requirements.
[0089] The second type of parameter is the scaling normalization factor, which is set based on the residual flow activation characteristics of each decoding layer. The factor value corresponds to the average norm of the historical activation data of the corresponding layer, which is obtained by taking the average of the activation norms of the layer after forward propagation of a large number of samples. Different layers of the same model correspond to different factor values. Once the model architecture is fixed, the factor can be pre-calculated offline and directly loaded for use during the inference stage.
[0090] The third parameter is the upper limit of the relative norm, set based on the need to balance the controllability of perturbation and the intensity of guidance. A larger value allows for stronger guidance perturbation and a more pronounced empathy effect, but also increases the risk of generated content deviating from the original distribution; a smaller value results in gentler guidance and better generation stability, but the empathy tone may not be prominent enough. This parameter can be flexibly adjusted according to the empathy intensity requirements of different personas, adapting to diverse styles of companionship.
[0091] Based on three types of parameters, three execution rules are simultaneously determined. The first is the injection operation rule, which uses the product of the original residual flow, dynamic guidance coefficient, scale normalization factor, and target guidance vector to ensure that the guidance strength is positively correlated with the dynamic coefficient, and the empathy intensity is adaptively adjusted according to the decision result. The second is the over-limit shrinkage logic, which keeps the perturbation direction unchanged when the perturbation exceeds the limit, and uniformly scales the amplitude of all dimensions proportionally so that the final norm equals the upper limit value, thus preserving the effectiveness of the guidance direction while controlling the perturbation amplitude. The third is the inter-layer relay operation mode, which replenishes the guidance signal diluted by the non-full attention layer layer by layer through continuous multi-layer repeated injection, forming a relay transmission mode of injection, dilution, re-injection, and re-dilution, ensuring that the guidance signal maintains effective strength from shallow to deep layers, fundamentally solving the problem of intervention signal dilution in hybrid attention architectures.
[0092] S106, during the generation stage, the perception vector set is reused for real-time sentiment projection on a token-by-token basis. When an inappropriate sentiment threshold is hit, three handling strategies are triggered according to priority: resampling of the token, rollback of the sentence generation, and guidance enhancement with negative direction injection.
[0093] In one implementation, based on the requirements of pre-interception of inappropriate emotions during the generation period, real-time monitoring at the token level, security of empathic responses, and controllable marginal computational overhead, real-time emotion projection is performed on the hidden state of each newly added token at the corresponding position of the perception layer in the autoregressive decoding stage using the same set of perception vectors. When an inappropriate emotion threshold is hit, a hierarchical handling strategy is triggered according to priority, generating a token-by-token emotion score sequence, inappropriate emotion judgment results, and handling strategy execution status information. The first step is token-by-token real-time emotion projection calculation. In the autoregressive decoding stage, for each newly generated token, the hidden state of the newly added token at the corresponding position of the perception layer is read, taking only the currently added single position without re-aggregating the historical sequence. Subsequently, the projection calculation is performed using the same global neutral mean vector and perception vector set as in the perception stage. First, the hidden state of the newly added token is subtracted from the global neutral mean vector to remove interference from general semantic representations and extract pure emotion direction features. Then, the emotion features are projected onto the perception vector set to obtain the matching scores for all fine-grained emotion categories corresponding to the token in one go, summarizing to form a token-by-token emotion score sequence. The computation fully reuses the already loaded set of perceptual vectors, without the need to load additional model parameters. The single token operation takes less than 0.1 milliseconds, which has almost no impact on the overall generation speed.
[0094] For example, during the decoding and response generation process, when the 6th token is generated, the system reads the hidden state of the newly added token at the corresponding position in the perception layer, substitutes it into the global neutral mean vector for difference processing, and then performs a projection operation with the perception vector set containing 100 to 200 emotion categories to simultaneously obtain the matching score of all emotion categories. Among them, the score of the derogatory emotion is significantly higher than that of other categories, and the system records this score in the token-by-token emotion monitoring sequence.
[0095] The second step is the determination of the inappropriate emotion threshold. The system predefines a set of inappropriate emotions, covering emotion categories that are prone to causing negative experiences in companionship scenarios, including anger, belittling, contempt, displaced anger, and sarcasm. After each token's emotion projection calculation is completed, the system extracts the scores of all categories within the inappropriate emotion set from the total score, and compares the highest value among them with the preset inappropriate emotion threshold. If the highest score does not exceed the threshold, it is judged as normal, and the next token is generated; if the highest score exceeds the threshold, it is judged as hitting the inappropriate emotion peak, and the subsequent processing procedure is immediately triggered.
[0096] For example, if the inappropriate sentiment threshold is set to 0.7, and the current token's derogatory sentiment score is 0.82, which is the highest among all inappropriate sentiment categories and exceeds the preset threshold, the system determines that the token hits the inappropriate sentiment peak, immediately suspends the current generation and sampling process, and proceeds to the tiered handling stage.
[0097] The third step is the implementation of a tiered handling strategy. Upon hitting an inappropriate sentiment threshold, the system attempts three handling strategies from low to high priority. These strategies can be used independently or in combination, prioritizing the option with the least impact on generation fluency. If a lower-level handling strategy fails to eliminate the inappropriate sentiment tendency, the handling intensity is escalated according to the rules to ultimately mitigate the risk. The first tier involves resampling the current token, either by blocking the current candidate token in the original sampling distribution or by blocking tokens from the candidate set that match a preset blocked word list, and then resampling to obtain a replacement token. If multiple resampling attempts still result in hitting the inappropriate sentiment threshold, the system escalates to the second tier. The second tier involves backtracking the sentence generation. The backtracking begins after the previous period or line break. From this position, all generated content is deleted, the dynamic guidance coefficient is increased, and the entire sentence is regenerated from the beginning. The third level is a guidance enhancement with negative direction injection. It does not delete the generated content, but continues to generate from the current position. At the same time, it further enhances the dynamic guidance coefficient and injects the negative direction guidance vector corresponding to the inappropriate emotion into the residual stream of the decoding layer attached to the guidance Hook. This reverses the inappropriate emotion tendency and ensures that the subsequent generation returns to the normal empathy tone.
[0098] For example, when the system first encounters an inappropriate sentiment of denigration, it first performs a first-level resampling, filtering out inappropriate words in the current candidate and resampling semantically similar but sentimentally neutral alternative tokens from the remaining candidate distribution. This replacement token is then used to continue generation. If multiple resampling attempts consistently hit the inappropriate sentiment threshold, the system escalates to a second-level handling, reverting to the beginning of the sentence after the previous period, deleting all generated content, increasing the dynamic guidance coefficient by a certain percentage, and regenerating the entire sentence. If inappropriate sentiment tendencies repeatedly appear during generation, the system can combine this with a third-level handling, retaining the generated content, further increasing the guidance coefficient, and simultaneously overlaying a negative denigration guidance vector on the corresponding guidance layer to suppress this sentiment direction and guide subsequent generated content back to a normal, mild, and empathetic trajectory.
[0099] Based on the architectural requirements of reusing monitoring and perception computing power and the closed-loop linkage of emotion guardrails and guidance injection, the token-by-token emotion monitoring and rollback guardrail tasks are modularized. This involves dividing the work into modules for emotion projection calculation, inappropriate emotion threshold determination, and execution of multi-level handling strategies. The execution logic and data flow relationships of each module are clearly defined, forming task execution information that enables real-time monitoring, threshold determination, and tiered handling to operate collaboratively. Following the architectural requirements of reusing monitoring and perception computing power and the closed-loop linkage of emotion guardrails and guidance injection, the token-by-token emotion monitoring and rollback guardrail tasks are divided into three independent functional modules. Each module has clearly defined execution logic and input / output boundaries. Data flows unidirectionally according to business sequence, while simultaneously forming a feedback loop with the guidance injection module, achieving collaborative operation of monitoring judgment and guidance adjustment.
[0100] The first module is the sentiment projection calculation module, which is the core computing unit for real-time monitoring. The module takes as input the latent state of the perceptual layer for each new token during the autoregressive decoding process, the global neutral mean vector, and a pre-built set of perceptual vectors. It performs difference processing and matrix projection operations, outputting the sentiment score for each token across all categories, which are then aggregated to form a token-by-token sentiment score sequence. This module fully reuses the vector resources and computational logic from the perceptual stage, requiring no additional model parameters and ensuring manageable computational overhead.
[0101] The second module is the inappropriate sentiment threshold determination module, which is the decision-making unit for risk identification. The module takes token-by-token sentiment scores, a predefined set of inappropriate sentiments, and an inappropriate sentiment threshold as input. It extracts the highest score for each inappropriate sentiment category, compares it with the threshold, and outputs the inappropriate sentiment determination result, while also marking the specific inappropriate sentiment category that was matched. This module can flexibly adjust the threshold and the range of the inappropriate sentiment set to adapt to the security control intensity requirements of different scenarios.
[0102] The third module is the multi-level handling strategy execution module, which is the execution unit for risk mitigation. The module takes into account the inappropriate sentiment judgment result, the hit sentiment category, the current dynamic guidance coefficient, and the guidance vector set. It executes the corresponding handling operations according to the preset priority and escalation path, outputs the handling strategy execution status, and simultaneously feeds back the adjusted guidance coefficient and negative injection instructions to the guidance injection module, forming a closed-loop linkage between the guardrail and guidance. This module supports independent configuration of the trigger conditions and parameters for each level of strategy, balancing generation smoothness and security control strength.
[0103] The three modules are connected in series according to the data flow. The emotion projection calculation module outputs the score to the threshold judgment module, the judgment module outputs the result to the treatment strategy module, and the adjustment instructions of the treatment module act in reverse on the guidance injection link. The whole is embedded in the autoregressive decoding process in the form of Hook, without modifying the model's original weights and generation logic.
[0104] Combining the requirements of timely interception of inappropriate emotions, reasonable hierarchical handling strategies, controllable guidance disturbances, and lightweight deployment, this study sets inappropriate emotion sets, inappropriate emotion thresholds, consecutive failure escalation counts, guidance enhancement coefficients, and negative direction injection weights based on fine-grained emotion definitions and generation stability constraints. It also determines the emotion projection calculation rules, threshold triggering logic, three-tiered strategy priorities and escalation paths, and negative vector injection execution methods. Furthermore, by adhering to the four requirements of timely interception of inappropriate emotions, reasonable hierarchical handling strategies, controllable guidance disturbances, and lightweight deployment, and based on fine-grained emotion definitions and generation stability constraints, five core parameters are set, and four corresponding execution rules are defined to ensure a balance between interception effectiveness and generation experience.
[0105] The first type of parameter is the set of inappropriate emotions, which is set based on the types of emotional risks in companionship scenarios. It filters emotional categories from all fine-grained emotional categories that are likely to cause negative user experiences and violate the positioning of empathetic companionship, forming a fixed scope for inappropriate emotion control. The content of the set can be flexibly adjusted according to the control needs of the actual scenario.
[0106] The second type of parameter is the inappropriate emotion threshold, which is set based on the need to balance interception accuracy and generation fluency. Setting the threshold too high will lead to missed detections, preventing inappropriate emotions from being intercepted in a timely manner; setting it too low will cause frequent misjudgments, interrupting the normal generation process and reducing response fluency. This parameter can be flexibly adjusted according to different character styles; a lively character style can have its threshold appropriately increased, while a serious and gentle character style can have its threshold appropriately decreased.
[0107] The third parameter is the number of consecutive failures that can be escalated, set based on a balance between processing efficiency and generation stability. Setting the number too few times will frequently trigger high-level processing, increasing computational overhead and affecting generation smoothness; setting the number too many times will lead to untimely risk mitigation, increasing the probability of inappropriate content leaking. The specific value can be flexibly configured according to actual business needs. After resampling reaches the specified number of failures, it will automatically escalate to sentence rollback processing.
[0108] The fourth type of parameter is the guidance enhancement coefficient, which is set based on the controllability requirements of guidance perturbation. During each upgrade, the dynamic guidance coefficient increases by a fixed percentage. A coefficient that is too small will result in insufficient guidance enhancement, making it impossible to quickly correct inappropriate tendencies; a coefficient that is too large will cause guidance strength overflow, leading to semantic collapse in the generated data. This coefficient must be controlled within a reasonable range to ensure that the magnitude of each enhancement is gradual and controllable, avoiding sudden changes in guidance strength that could cause semantic anomalies in the generated data.
[0109] The fifth type of parameter is the negative direction injection weight, set based on the required intensity of suppression of inappropriate emotions. The weight coefficient when injecting the negative guiding vector in the third-level treatment is used to control the strength of the reverse suppression; a larger weight results in a stronger suppression effect, but also a greater perturbation to the model's original distribution. This parameter is used in conjunction with the guiding enhancement coefficient to achieve rapid suppression of inappropriate emotions.
[0110] Based on five types of parameters, four execution rules are simultaneously determined. The first is the emotion projection calculation rule, which uniformly adopts the differential projection logic completely consistent with the perception stage. It only performs calculations on the hidden state of a single newly added token, without reusing historical aggregation results, ensuring the timeliness of single-token emotion recognition, while maximizing the reuse of existing computing resources. The second is the threshold triggering logic, which uses a judgment method that compares the highest score of an inappropriate emotion category with a single threshold. As long as the score of any inappropriate emotion category exceeds the threshold, a handling action is triggered, ensuring that all risk categories are effectively covered. The third is the three-level strategy priority and upgrade path, which sets the priority in the order of token resampling, sentence generation rollback, and guidance enhancement superimposed with negative direction injection. After a specified number of failures in low-level handling, it automatically upgrades to a higher-level handling action. Each level of strategy can also be used in combination according to the scenario, prioritizing the smoothness of generation while ensuring that the risk can ultimately be resolved. The fourth method is the negative vector injection execution method. After hitting an inappropriate emotion, the guiding vector corresponding to the emotion is taken, multiplied by the negative direction injection weight, and then the vector is subtracted dimension by dimension in the residual stream of the corresponding decoding layer attached to the guiding hook. This achieves reverse suppression of the inappropriate emotion direction, forming a two-way regulation with the positive empathy guidance, and accelerating the speed of emotion returning to normal.
[0111] S107 integrates the four stages of perception, decision-making, guidance, and monitoring into a closed-loop process within a forward propagation, ultimately outputting an empathic response text and a token-by-token emotion monitoring sequence.
[0112] In one implementation, the model progresses through two native stages: pre-filling and autoregressive decoding, based on the large language model. The six steps of input initiation, perception embedding, decision calculation, guided injection, decoding monitoring, and result output are completed sequentially. All steps are embedded in native computing nodes, without interrupting the normal operation rhythm of the model.
[0113] The first step is input data concatenation and model pre-filling startup. The system first reads the dialogue history and the user's current input from a fixed window, concatenates them into a complete input sequence in chronological order, and sends it to the base large language model to start the forward propagation of the pre-filling stage. The pre-filling stage is the native process in which the model processes all the context and generates the initial hidden state at once. All the core computations of the entire closed loop are completed by relying on the computing power of this forward propagation, without increasing the number of model inferences.
[0114] For example, if a user inputs that they failed their job interview today and feel useless, the system concatenates the last three rounds of dialogue to form a complete input sequence, which is then fed into a 32-layer hybrid attention-based model to initiate pre-filled forward propagation. All subsequent emotion perception, guidance injection, and emotion monitoring operations reuse the computational resources from this forward propagation, without initiating any new model inference requests throughout the process.
[0115] The second step is the fine-grained sentiment perception embedding pre-filled forward propagation. When the model forward propagates to the preset target perception decoding layer, the attached perception hook is automatically triggered, sequentially completing five computational steps: skipping special tokens at the header, average pooling of effective positions, global neutral benchmark differencing, projection of the perception vector set matrix, and sorting of scores across all categories. The final output is the top-k fine-grained sentiment result. This step adds only one matrix multiplication operation, with a single round taking less than 0.1 milliseconds, fully reusing the forward computational power of the pre-filling stage.
[0116] For example, when the model propagates forward to the intermediate perception decoding layer, the perception hook automatically captures the residual stream data of all tokens in that layer. After skipping the two special marker positions at the beginning, it performs average pooling on the remaining text tokens to obtain the aggregated hidden state vector. Then, it subtracts the global neutral mean vector to remove general semantic interference, and performs matrix multiplication with the perception vector set containing 120 emotion categories to simultaneously obtain the matching scores of all emotion categories. After sorting from high to low, the top-3 results are output as loss 0.82, self-blame 0.76, and anxiety 0.41, which are directly sent to the empathy decision-making stage.
[0117] The third step is the calculation of the empathy decision-making order. After the perception results are output, the system first combines the persona configuration with the top-k perception results to determine the target empathy emotion and basic guidance coefficient through empathy mapping rules. Then, it retrieves memory entries from the long-term memory bank that match the current input semantics, reads the corresponding memory intensity, and substitutes them into the memory modulation formula to calculate the final dynamic guidance coefficient. All calculations are purely numerical operations, completed quickly after the perception layer outputs the results, which are then directly sent to the guidance injection module.
[0118] For example, if the robot's current persona is a gentle and caring older sister, and based on the top-3 emotional results matching empathy mapping rules, the target empathic emotion is determined to be sympathy and comfort, corresponding to a basic guidance coefficient of 0.15. The system retrieves a memory entry from three months ago where the user applied for the position and prepared for multiple interviews. This entry has been activated a total of 5 times, with a current memory strength of 0.65. Substituting this into a memory modulation weight of 0.6, the dynamic guidance coefficient is calculated to be 0.2085, which is then simultaneously sent to the guidance injection module.
[0119] The fourth step is cross-layer guided injection embedded in the inter-layer forward propagation. When the model forward propagates into a preset continuous decoding layer interval, the guided hook of each layer within the interval is triggered sequentially. Three operations are performed layer by layer: target guided vector weighted injection, scale normalization calibration, and relative norm constraint verification, relaying and supplementing the guided signal. All injection operations are performed at the output node of each layer's forward propagation, completed synchronously with the native computation, without interrupting the inter-layer data flow.
[0120] For example, the model forward propagates into a pre-defined continuous mounting interval, which simultaneously covers both full attention layers and gated attention layers. Upon reaching each layer, a corresponding guiding hook is triggered. The dynamic guiding coefficient, the layer's scale normalization factor, and the guiding vector of the desired direction are substituted into the calculated injected perturbation vector, which is then superimposed onto the original residual stream. A relative norm check is then performed; if the perturbation exceeds the limit, it is scaled back proportionally. Only after compliance is confirmed is the signal sent to the next layer for calculation. Through multiple layers of relay injection, the guiding signal maintains effective strength throughout and is not diluted by non-full attention layers.
[0121] Step 5: Autoregressive decoding and token-by-token monitoring are linked. After the pre-filling stage, the model enters the autoregressive decoding stage, generating response text token by token. For each new token generated, a monitoring hook is triggered at the corresponding location in the perception layer. The perception vector set is reused for real-time sentiment projection to determine if an inappropriate sentiment threshold has been hit. If so, the corresponding handling strategy is triggered according to priority. The monitoring computation is performed synchronously with the generation of each token, with a single token computation taking less than 0.1 milliseconds.
[0122] For example, when decoding the sixth token, the monitoring hook reads the hidden state of that token in the perception layer, reuses the same set of perception vectors to calculate the sentiment score for all categories, and finds that the derogatory sentiment score is 0.83, exceeding the preset inappropriate sentiment threshold of 0.7. The system immediately triggers the first level of token resampling, blocking the current inappropriate candidate tokens, and resampling from the remaining sampling distribution to obtain a semantically similar neutral alternative token. After confirming compliance, subsequent generation continues. Monitoring and handling are embedded in the decoding process throughout, without interrupting the overall generation rhythm.
[0123] Step 6: Unify the output of two types of results. After decoding, the central control module summarizes the two parts of the output: one is the complete empathic response text, and the other is the token-by-token sentiment monitoring sequence recorded throughout the process. Both types of results are simultaneously returned to the upper-level business. The sentiment monitoring sequence is arranged in the order of generation, with each position corresponding to a set of sentiment scores for all categories, which can be directly used for effect evaluation, log backtracking, and quality optimization.
[0124] For example, the final generated complete empathetic response might be: "I know you prepared for this interview for a long time, and you must be very disappointed that you didn't pass. But this is just an attempt, it doesn't mean you're not good enough. We can see together where we can adjust, and next time it will definitely be more successful." The synchronously outputted token-by-token sentiment monitoring sequence records the 120-dimensional sentiment score corresponding to each token in the response, fully presenting the changes in sentiment state at each step of the generation process. This can be used to statistically analyze core indicators such as empathy hit rate and inappropriate sentiment trigger rate, supporting continuous iterative optimization of the quality of companionship interaction.
[0125] Following the architectural requirements of a forward closed loop and deep computing power reuse, a central control orchestration module is set up to connect the four core functional modules, clarifying the triggering timing and data flow of each module, forming a closed-loop architecture with the main link flowing forward and the feedback link controlling backward. The entire architecture adopts a non-intrusive design, with all modules accessing the model's native process via Hooks, without modifying the base model's weights and computational logic.
[0126] The first module is the closed-loop inference orchestration module, which is the overall scheduling unit for the entire process. This module is responsible for input sequence concatenation, controlling the triggering timing of each stage, data forwarding between modules, and final result aggregation and output. It activates the corresponding modules sequentially according to the time sequence of pre-filling, perception, decision-making, guidance, and decoding monitoring, ensuring that each stage intervenes at the correct computational node, without initiating additional model forward requests throughout the process, and ensuring that all computations reuse the computing power of a single forward propagation.
[0127] The second module is the fine-grained sentiment perception module, which connects to the perception layer hook in the pre-filling stage. The module takes as input the residual stream data from the pre-filling stage, the global neutral mean vector, and the set of perception vectors, and outputs the top-k fine-grained sentiment results. The main data flow is directed to the empathy decision module, while the set of perception vectors is synchronously shared with the sentiment guardrail module in the generation stage, realizing the dual reuse of vector resources and computational logic.
[0128] The third module is the empathy coefficient decision module, which receives the output from the perception module. The module takes as input the top-k perception results, long-term memory strength, and persona configuration parameters, and sequentially completes the target emotion mapping and guidance coefficient modulation. The output is the target empathy emotion category and dynamic guidance coefficient. The results are sent to the cross-layer empathy guidance injection module via the main link, and simultaneously to the generation-period emotion guardrail module as benchmark parameters for guidance enhancement.
[0129] The fourth module is the cross-layer empathy guidance injection module, which connects to the guidance hook set of continuous decoding layer intervals. The module's inputs are the target empathy sentiment, dynamic guidance coefficients, and guidance vector set, and its output is the adjusted residual flow of each layer, which directly affects the model generation process. The module also receives feedback instructions from the sentiment guardrail module during the generation period, and can dynamically adjust the guidance coefficients and superimpose negative direction injections to form a closed-loop linkage between guidance and monitoring.
[0130] The fifth module is the sentiment guardrail module for the generation phase, which connects to the monitoring hook in the decoding phase. The module's inputs are the token-by-token hidden state, the set of perception vectors, inappropriate sentiment thresholds, and tiered handling rules. The output is a handling execution signal, which can reversely adjust the parameters injected into the module to quickly resolve inappropriate sentiment tendencies. The module fully reuses the vector resources from the perception phase, requiring no additional model parameters.
[0131] The five modules are uniformly scheduled by the central control and orchestration module. Data flows forward along the main link of pre-filled perception, decision calculation, inter-layer guidance, and decoding monitoring. At the same time, the feedback link of the guidance is monitored to form a reverse control closed loop. The whole is embedded in a single forward propagation process. The architecture is decoupled and the computing power is used efficiently. It can be adapted to large language models with different base structures.
[0132] Combining the three core requirements of controllable computing power consumption, accurate process timing, and traceable output, three types of orchestration rules and unified output specifications are set to ensure the stable and efficient operation of the closed-loop process, while retaining the flexibility and adaptability of the solution.
[0133] The first category is the computing power reuse rule. It explicitly perceives the reuse of forward computing power in the pre-filling stage, guides the reuse of inter-layer forward propagation computing power in the injected operations, and monitors the reuse of forward computing power for each token in the decoding stage. All new operations are vector addition / subtraction and matrix multiplication operations, without adding any model inference steps. The overall marginal computational overhead is controlled at the level of 0.1 milliseconds per round, which is essentially no different from the generation speed of the native model.
[0134] The second category is non-intrusive adaptation rules. This approach explicitly states that all functional modules are connected to the model via read-only or lightweight write Hook mounting, without modifying the native weight parameters of the base language model or altering the model's original forward propagation calculation logic. This rule ensures the entire solution can be quickly migrated to base models with different structures and parameter scales, without requiring customized modifications for individual models.
[0135] The third category is time-series coordination rules. The perception phase is triggered at a pre-filled specified layer, decision computation is executed after the perception results are output, guidance injection intervenes synchronously at the corresponding layer, and monitoring is executed immediately after each token is generated. The timing of each stage is strictly aligned with the model's native computation nodes, without generating additional waiting latency, ensuring that the overall generation smoothness is not affected by the closed-loop mechanism.
[0136] Based on the three types of rules, a unified output specification was simultaneously determined. The final output clearly includes two fixed results: one is a complete empathic response text, formatted exactly like a regular dialogue response, which is imperceptible to upper-level business logic; the other is a token-by-token emotion monitoring sequence, recording the full-category emotion score for each token in the order of its generation. This can be directly used to quantitatively evaluate core indicators such as empathy hit rate, inappropriate emotion trigger rate, and empathy intensity distribution, providing interpretable data support for the continuous optimization of the companion robot's long-term interaction quality.
[0137] This application can be lightweightly deployed on edge smart devices such as AI smart dolls and desktop companion robots, serving as the core empathic interaction engine for physical companion terminals. Relying on a closed-loop architecture that embeds perception, decision-making, guidance, and monitoring into a single forward propagation, the solution has extremely low overall marginal computing power overhead. It can run directly on the lightweight base large language model mounted locally on the device, without relying heavily on cloud inference. This ensures both real-time interaction response and local storage of user dialogue and memory data, enhancing interaction privacy and security. Considering the long-term use of smart companion devices, the solution's built-in long-term memory intensity modulation mechanism dynamically adjusts the intensity of empathic expression based on the depth of interaction. The longer the user's companionship period with the device and the richer the shared interaction memories, the higher the personalization and fit of the empathic response, creating a personalized companionship experience with a sense of growth. Simultaneously, token-based real-time emotion monitoring and a three-level graded handling mechanism can build a pre-emptive safety barrier for users of all ages, especially suitable for companion smart devices targeting children and individuals living alone. While ensuring the fineness of empathic expression at hundreds of levels, it blocks inappropriate emotional content throughout the process, balancing interaction warmth and usage compliance. The non-intrusive Hook's universally compatible design can also be quickly adapted to smart device terminals with different hardware configurations, supporting differentiated product forms and functional positioning.
[0138] like Figure 2 As shown, Figure 2 This is a schematic diagram of the overall architecture of a companion robot's empathic response generation system. It presents the entire process logic of a four-stage closed loop—perception, decision-making, guidance, and monitoring—based on a hybrid attention architecture-based large language model, completing a single forward propagation. The overall architecture uses the hybrid attention architecture-based large language model (marked with a red dashed box) as its core carrier, with data flowing from left to right throughout the entire process. The input side takes the user's input text U_t and enters the base model, serving as the trigger source for the entire process.
[0139] For the emotion perception unit: in the model's... The * layer is equipped with a perception hook, which reads the hidden state of the residual flow of that layer. It performs matrix projection operation with the perception vector set V^sense to identify N types of fine-grained emotions triggered by user input in parallel, and outputs the emotion distribution results to the empathy decision module.
[0140] For the empathy decision-making module: it receives the top-k emotion results from the perception output and the robot's human profile configuration P, while retrieving the current memory strength s_m of the corresponding memory entry from the long-term memory bank; it determines the target empathy emotion c* through the empathy mapping function, and calculates the final guidance coefficient α* by combining it with the memory strength modulation factor. The calculation formula is α*= (1+γ s_m) enables the empathy intensity to be adaptively adjusted according to the strength of long-term memory.
[0141] The architecture comprises two distinct sets of emotion vectors. The perception vector set, V^sense, has a dimension of d×N and focuses on the accuracy and generalization of fine-grained emotion recognition, serving both front-end perception and generation-stage monitoring. The guidance vector set, V^steer, also has a dimension of d×N and focuses on the strength and purity of emotion-driven actions, specifically for generating guidance injection.
[0142] For cross-type, multi-layered guiding units: in the model [ _a, Within the [b] layer interval, both fully attention layers and non-fully attention layers are simultaneously covered by a guiding hook. A guiding vector representing the target empathy direction is injected into the residual flow of each layer on a layer-weighted basis, with the operation rule being h_ '=h_ +α* η_ v_c^steer; through a relay design of "inject-dilution-re-injection", avoids the guide signal being diluted layer by layer in the hybrid attention architecture.
[0143] For the generation monitoring and guardrail unit: In the autoregressive decoding stage, the Hook reuses the perception vector set for each token and performs real-time sentiment projection on the hidden state of each newly generated token; once an inappropriate sentiment threshold τ_u is hit, intervention is immediately triggered by the backtracking guardrail, supporting three types of hierarchical processing strategies: resampling of this token, backtracking of this sentence generation, and enhancement of the guidance coefficient. The intervention signal acts in reverse on the decoding process.
[0144] The output side outputs the final empathic response text R_t after autoregressive decoding, along with a token-by-token sentiment monitoring sequence, making the generation process traceable and evaluable.
[0145] like Figure 3 As shown, Figure 3This diagram illustrates the comparison of the location and effect of cross-type multi-layer guided injection in a 32-layer hybrid attention stacking architecture. Taking a 32-layer hybrid attention stacking architecture (each 4 layers contain 3 layers of gated attention and 1 layer of full attention) as an example, it intuitively presents the layer design of the cross-type multi-layer guided injection of the present invention, and compares its defects with the existing scheme that only attaches a full attention layer. It clearly shows the cause of the dilution of the guided signal under the hybrid attention architecture and the solution of the present invention.
[0146] The left side shows the guidance coverage scheme of this invention, specifically as follows: the basic architecture consists of 32 decoding layers stacked alternately according to the pattern of "3 layers of gated attention + 1 layer of full attention". The full attention layer is marked in orange with an asterisk, and the gated attention layer is in blue, arranged from L0 to L31 from bottom to top. The full attention layer at L11 (labeled as the perception layer)... *) Attach a perception hook to read the hidden state of the residual flow in this layer and perform parallel recognition of fine-grained emotions.
[0147] This invention guides overlay [ _a, The continuous layer interval has a total of 24 layers, covering both full attention layers and gated attention layers, enabling cross-type full-level mounting. Through a relay-style guidance of "injection-dilution-re-injection-re-dilution", combined with the layer-by-layer relative norm constraint ρ_max, the guidance signal is ensured to remain discernible in the deep layers of the model, avoiding absorption and attenuation by the gated attention layers.
[0148] The right side shows a comparison of the shortcomings of existing solutions. Current hooking methods only attach guidance hooks at single points in a few full-attention layers such as L3 and L7. Subsequent gated attention layers continuously absorb and filter the guidance signal, with attenuation becoming more significant with deeper layers, showing signal attenuation of 30%, 50%, and 70% respectively. The guidance signal is gradually diluted by numerous gated layers, and by the time it reaches the top-level generator, the guidance energy has been greatly reduced. Ultimately, this leads to a disconnect between the generated result and the guidance intent, failing to effectively drive the model to output a response with the target empathetic tone.
[0149] like Figure 4 As shown, Figure 4 This is a schematic diagram illustrating the division of labor and cooperation between a set of perception vectors and a set of guidance vectors. It intuitively presents the core dual-vector set division of labor design of this invention. The set of perception vectors and the set of guidance vectors are constructed separately for different optimization objectives, covering the same category of fine-grained emotions but each performing its own function, jointly supporting the entire process of emotion perception and empathy generation.
[0150] The left side shows the perception vector set V^sense: the sampling strategy optimizes the accuracy and generalization of the direction, collecting a large number of samples for each emotion category, covering diverse expression forms, and ensuring the discriminative and generalizable ability of fine-grained emotion recognition. Emotional directions include N categories of fine-grained emotions such as gratitude, relief, guilt, relief, heartache, joy, regret, and loss, covering 100 to 200 common emotions in long-term companionship scenarios. The vector design emphasizes the separability between different emotions. It simultaneously performs two functions: first, it performs emotion perception on user input, distinguishing N emotion categories in parallel through a single matrix operation; second, it performs token-by-token emotion monitoring during the generation phase, serving as the basis for determining the fallback barrier. The core calculation formula is e = V^sense.
[0151] The two sets of vectors are connected by a "division of labor" arrow in the middle, which reflects the decoupling and collaborative relationship between the two at the vector set level, and each performs its own function. This avoids the problem of "precision and driving force pulling each other apart" when a single set of vectors simultaneously takes care of perception and guidance.
[0152] The right side shows the set of guiding vectors, V^steer: the sampling strategy optimizes the strength and purity of the direction, selecting a small number of samples with higher emotional intensity and greater representativeness to ensure the causal driving ability of the vectors in the generation process. The emotional direction corresponds one-to-one with the N types of emotions in the perception set; their directions are roughly the same, but the guiding vectors have a purer emotional direction and a stronger driving amplitude, identified by vectors marked with an asterisk for each emotion type. It is specifically used for causal guided generation, injecting the model residual flow through multi-layer mounting; the core computational rule is h_ '=h_ +α* η_ v_{c*}^steer; The injection process is protected by the relative norm constraint ρ_max to avoid causing uncontrollable disturbances to the original distribution of the model.
[0153] like Figure 5 As shown, Figure 5 This diagram illustrates the calculation of a dynamic guidance coefficient coupled with long-term memory strength, serving as an example of the computational logic in the empathy decision-making stage. It demonstrates how to combine user emotional perception results, long-term memory strength, and persona attributes to jointly deduce the target empathy emotion and the final guidance coefficient, intuitively presenting the dynamic modulation mechanism of memory strength on empathy intensity.
[0154] The input contains three types of parameters, as follows: Perceived Emotion Top-k Results: The user input was identified to contain fine-grained emotions such as grief (score 0.86), loss (score 0.72), and guilt (score 0.35), with grief being the dominant emotion. Long-Term Memory Retrieval Results: A matching memory entry was found to be the death of a loved one three months ago. This event was reinforced eight times, and the current memory strength s_m=0.82, indicating a high-weight long-term emotional memory. Persona Configuration P: The robot is set as a "gentle and understanding older sister" persona, with a high weight for empathy tendency, serving as the tone constraint for empathy mapping.
[0155] The intermediate processing consists of two core stages, as follows: Empathy Decision Mapping: Through the empathy mapping function Φ, the dominant emotion "grief" is mapped to the target empathy emotion c* = "heartache / comfort"; simultaneously, a basic guidance coefficient α0 = 0.15 is set, which can be automatically scaled according to the intensity of emotional perception. Memory Intensity Modulation: Memory intensity is used as a modulation factor and substituted into the formula to calculate the final guidance coefficient, the formula being α* = α0. (1+γ s_m); where the memory modulation weight γ is 0.6, and substituting the values into the calculation, we get α*=0.15×(1+0.6×0.82)=0.224.
[0156] The final output guidance injection parameters include the target empathic emotion "heartache", the corresponding guidance vector v_c*^steer, and the final guidance coefficient α*=0.224, which are uniformly delivered to the multi-layer guidance injection module to generate guidance. This design reflects the core logic: the greater the memory strength, the more repeatedly reinforced the significant life events, the higher the guidance coefficient, the more stable and stronger the empathic expression, which naturally aligns with the long-term companionship attribute of companion robots.
[0157] like Figure 6 As shown, Figure 6 This is a timeline diagram of token-by-token sentiment monitoring and generation-stage rollback guardrail. Based on the generation timeline of autoregressive decoding, it intuitively presents the operational logic of token-by-token sentiment monitoring during the generation stage, as well as the three-level graded rollback processing mechanism when an inappropriate sentiment threshold is hit, realizing an emotional safety guardrail that "intercepts words before they are spoken" during the generation stage.
[0158] Regarding the above token generation sequence: tokens y_1 to y_6 are generated sequentially along the generation time direction, corresponding to the text "I", "know", "this matter", "indeed", "makes people" and "serves them right". The 6th token "serves them right" is marked as y_6*, which is an abnormal token that triggers inappropriate emotions.
[0159] The diagram in the middle illustrates the intensity of emotion monitoring: the vertical axis represents the intensity of inappropriate emotions, distributed from low to high, and the red dashed line represents the inappropriate emotion threshold τ_u. The emotion intensity of the first 5 tokens gradually increases but remains below the threshold. At y_6, the emotion intensity exceeds the threshold, triggering an inappropriate emotion alarm, specifically identifying "degradation / displacement" type inappropriate emotions.
[0160] The bottom section outlines a three-tiered processing strategy, progressing in order of priority, as follows: Strategy I: Token Resampling: Blocks currently matched inappropriate tokens and reselects candidate tokens from the sampling distribution; if inappropriate sentiment is still triggered after n_r consecutive resampling iterations, it is upgraded to Strategy II. Strategy II: Sentence Generation Rollback: Deletes all currently generated content, proportionally increases the guidance coefficient α*, and regenerates from the sentence's starting position. Strategy III: Continued Guidance Enhancement: Does not delete already generated content, continues generating at the current position; synchronously increases the guidance coefficient α* and injects a negative direction vector corresponding to the inappropriate sentiment to suppress inappropriate sentiment tendencies.
[0161] like Figure 7 As shown, a large-scale model-based empathic text generation system with hierarchical Hook vector intervention includes: The vector offline construction module 701 is used to define N types of fine-grained emotions. It collects latent states using generalized samples and high-intensity typical samples, and generates a set of perceptual vectors for fine-grained recognition and a set of guiding vectors for generation through neutral mean difference and normalization operations, thus completing the preparation of static resources before inference. The online data loading module 702 is used to read the hidden state of the base large language model, historical dialogues, long-term memory entries with memory strength, and robot human configuration after the inference process is started, and to supply the entire chain of subsequent perception and decision-making. The fine-grained sentiment perception module 703 is used to attach a perception hook to a specified decoding layer to read the aggregated residual stream, reuse the forward propagation computing power of the model, and output the top-k fine-grained sentiment score results through single matrix parallel projection. Empathy coefficient decision module 704 is used to load empathy mapping rules, combine perception results, persona, and dialogue status to determine the target empathy emotion, match the corresponding long-term memory strength to modulate the basic coefficient, and output an adaptive dynamic guidance coefficient. The cross-layer empathy-guided injection module 705 is used to continuously attach guide hooks in multiple layers of hybrid attention, inject target guide vectors by layer with weighted injection, and achieve relay-style guidance by scale normalization and norm constraints to eliminate signal dilution problems. The generation-period emotion guardrail module 706 is used to reuse the perceptual vector for token-by-token emotion projection during decoding. When the inappropriate emotion threshold is exceeded, three types of handling strategies are executed according to priority: resampling, sentence backoff, and enhancement suppression. The closed-loop reasoning orchestration module 707 is used to connect the four major stages of perception, decision-making, guidance, and monitoring, integrating them into a single model forward propagation closed loop, and finally outputting empathic response text and token-by-token sentiment monitoring sequence.
[0162] A device for generating empathic text for a large model with hierarchical Hook vector intervention is provided. The device is used to execute executable instructions to perform the aforementioned method for generating empathic text for a large model with hierarchical Hook vector intervention.
[0163] A computing device includes a memory for storing computer program instructions and a processor for executing the computer program instructions, wherein when the computer program instructions are executed by the processor, the device is triggered to execute any of the large-model companion empathy text generation methods equipped with hierarchical Hook vector interventions.
[0164] The methods and / or embodiments in this application can be implemented as computer software programs. For example, embodiments of this disclosure include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowchart. When the computer program is executed by a processing unit, it performs the functions defined in the methods of this application.
[0165] It should be noted that the computer-readable medium described in this application can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this application, a computer-readable medium can be any tangible medium that contains or stores a program that can be used by or in conjunction with an instruction execution system or device.
[0166] It will be apparent to those skilled in the art that this application is not limited to the details of the exemplary embodiments described above, and that this application can be implemented in other specific forms without departing from the spirit or essential characteristics of this application.
Claims
1. A method for generating empathic text for large-scale models with hierarchical Hook vector intervention, characterized in that, include: Offline pre-constructed sets of perception vectors and guidance vectors; During the online reasoning phase, acquire the latent state, dialogue history, long-term memory entries and memory strength, and persona configuration data of the base large language model; We define N types of fine-grained emotions, and use generalized samples covering diverse expressions and typical samples with higher emotion intensity to generate a set of perception vectors and a set of guidance vectors through mean difference and L2 normalization. A perception hook is attached to the target decoding layer to read the residual stream and aggregate the hidden state. All fine-grained sentiment scores are calculated through single matrix multiplication and parallel projection, and the top-k sentiment results are output. The computation and model forward propagation reuse computing power. Based on the top-k results of perception, persona configuration and current dialogue state, the target empathic emotion is determined through empathy mapping rules. The basic guidance coefficient is modulated by combining the strength of long-term memory activated by the dialogue to generate a dynamic guidance coefficient, so that the empathy intensity increases or decreases adaptively with long-term companionship memory. By attaching a guiding hook in the continuous decoding layer interval that simultaneously covers both the full attention layer and the non-full attention layer, and injecting the target empathy guiding vector layer by layer, and matching it with layer-by-layer scale normalization and relative norm constraints, a relay-style guidance is formed to solve the problem of interference signal dilution in the hybrid attention architecture. During the generation phase, the perception vector set is reused for real-time sentiment projection on each token. When an inappropriate sentiment threshold is hit, three handling strategies are triggered according to priority: resampling of the token, backtracking of the sentence generation, and guidance enhancement with negative direction injection. The four stages of perception, decision-making, guidance, and monitoring are integrated into a closed-loop process within a forward propagation, ultimately outputting empathic response text and a token-by-token sentiment monitoring sequence.
2. The method for generating empathic text for large-scale models with hierarchical Hook vector intervention as described in claim 1, characterized in that, A perceptual hook is attached to the target decoding layer to read the residual stream and aggregate the hidden states. All fine-grained sentiment scores are calculated through a single matrix multiplication and parallel projection, outputting the top-k sentiment results. The computation and model forward propagation reuse computational power, including: Based on the requirements of real-time fine-grained emotion recognition, controllable marginal computational overhead, and compatibility with normal forward propagation of the model, the residual stream of the target decoding layer of the base large language model is subjected to perceptual hook mounting, header target token skipping and hidden state average pooling processing. The parallel projection operation of fine-grained emotion for all categories is completed by a single matrix multiplication, generating aggregated hidden state, all category emotion score and top-k emotion ranking results. Based on the architectural requirements of computing power reuse and deep coupling of emotion perception and text generation, the fine-grained emotion perception task is divided into lightweight modules, namely residual stream reading, neutral benchmark difference, perception vector projection, and emotion ranking output. The execution logic and data flow relationship of each module are clarified, forming task execution information that coordinates reading aggregation, projection calculation, and result output. Combining the requirements of emotion recognition accuracy, target token interference avoidance, and computational efficiency, the target mounting layer, head skip position range, and emotion selection quantity k value are set based on the semantic representation characteristics of the decoding layer. The hidden state aggregation method, emotion score output dimension, and top-k result output rule information are determined.
3. The method for generating empathic text for large-scale models with hierarchical Hook vector intervention as described in claim 1, characterized in that, Based on the top-k results of perception, persona configuration, and current dialogue state, the target empathic emotion is determined using empathy mapping rules. This, combined with the strength of long-term memory activated by the dialogue, modulates the basic guidance coefficient to generate a dynamic guidance coefficient. This allows the empathic intensity to adaptively increase or decrease with long-term companionship memory, including: Based on the requirements of fine-grained emotion matching accuracy, consistency of empathy style persona, and long-term companionship adaptability, we perform empathy mapping matching on the top-k perceived emotion results, persona configuration and current dialogue state, and perform guidance coefficient modulation in combination with the long-term memory intensity activated by the dialogue to generate target empathy emotion category and dynamic guidance coefficient information. Based on the architecture requirements of hierarchical linkage between empathic decision-making and memory modulation, the empathic decision-making task is modularized, dividing the work into modules for empathic mapping and matching, long-term memory retrieval, and guided coefficient modulation. The execution logic and data flow relationship of each module are clarified, forming task execution information for the coordinated operation of emotion judgment, memory linkage, and coefficient generation. Combining the requirements of emotional response adaptability, memory intensity linkage rationality, and empathy strength controllability, based on fine-grained emotional correspondence and memory intensity value range, empathy mapping rules, memory modulation weights, and basic guidance coefficients are set to determine the target emotion judgment logic, coefficient modulation formula, and memory intensity adaptability rules.
4. The method for generating empathic text for large-scale models with hierarchical Hook vector intervention as described in claim 1, characterized in that, A guiding hook is attached to the continuous decoding layer interval that simultaneously covers both the full attention layer and the non-full attention layer. A target empathy guiding vector is injected layer-by-layer with weighted increments, along with layer-by-layer scale normalization and relative norm constraints, forming a relay-style guiding mechanism to address the dilution of intervention signals in the hybrid attention architecture. This includes: Based on the requirements of anti-dilution intervention signal, consistency of guidance intent, and controllability of model distribution in the hybrid attention architecture, guidance hook mounting, target empathy guidance vector weighted injection, and layer-by-layer constraint verification are performed on the continuous decoding layer intervals covering the full attention layer and non-full attention layer to construct a relay-style guidance link and generate information on the adjusted residual flow state and relative norm verification results of each layer. Based on the architectural requirements of relay transmission of guiding signals, and compatibility between the injection process and model forward propagation, the empathy-guided injection task is modularized, with modules for selecting layer intervals, injecting guiding vectors, normalizing scales layer by layer, and verifying relative norm constraints. The execution logic and data flow relationship of each module are clarified, forming task execution information that coordinates cross-layer mounting, weighted injection, and constraint verification. Combining the requirements of deep discernibility of the guiding signal, inter-layer guiding balance, perturbation controllability and architecture adaptability, and based on the distribution of decoding layer types and residual flow activation characteristics, the continuous mounting layer interval, scale normalization factor, and relative norm upper limit are set to determine the injection operation rules, over-limit shrinkage logic, and inter-layer force operation mode information.
5. The method for generating empathic text for large-scale models with hierarchical Hook vector intervention as described in claim 4, characterized in that, During the generation phase, the perception vector set is reused per token to perform real-time sentiment projection. When an inappropriate sentiment threshold is hit, three handling strategies are triggered according to priority: resampling of the current token, backtracking of the current sentence generation, and guidance enhancement with negative injection. These include: Based on the requirements of intercepting inappropriate emotions in advance during the generation period, real-time monitoring at the token level, and controllable security and marginal computational overhead of empathic response, real-time emotion projection is performed on the set of reusable perception vectors for each newly added token hidden state at the corresponding position of the perception layer in the autoregressive decoding stage. When an inappropriate emotion threshold is hit, a hierarchical handling strategy is triggered according to priority, generating a token-by-token emotion score sequence, inappropriate emotion judgment result, and handling strategy execution status information. Based on the architectural requirements of monitoring and perception computing power reuse, and emotional guardrail and guidance injection closed-loop linkage, the token-by-token emotional monitoring and rollback guardrail tasks are modularized, dividing the tasks into modules for emotional projection calculation, inappropriate emotional threshold judgment, and multi-level handling strategy execution. The execution logic and data flow relationship of each module are clarified, forming task execution information for real-time monitoring, threshold judgment, and hierarchical handling collaborative operation. Combining the timeliness of inappropriate emotion interception, the rationality of graded handling strategies, the controllability of guidance disturbances, and the requirements of lightweight deployment, this paper sets inappropriate emotion sets, inappropriate emotion thresholds, consecutive failure upgrade counts, guidance enhancement coefficients, and negative direction injection weights based on fine-grained emotion definitions, generation stability constraints, and determines emotion projection calculation rules, threshold triggering logic, three-level strategy priorities and upgrade paths, and negative vector injection execution methods.
6. A large-scale model-based empathic text generation system with hierarchical Hook vector intervention, characterized in that, The system is used to execute executable instructions to perform the large-model companion empathy text generation method with hierarchical Hook vector intervention as described in any one of claims 1 to 5.
7. A large-scale model-based empathic text generation device equipped with hierarchical Hook vector intervention, characterized in that, The device is used to execute executable instructions to perform the large-model companion empathy text generation method with hierarchical Hook vector intervention as described in any one of claims 1 to 5.
8. An electronic device, characterized in that, include: First processor; And a memory for storing executable instructions of the first processor; wherein the first processor is configured to execute the large model companion empathy text generation method with hierarchical Hook vector intervention as described in any one of claims 1 to 5 by executing the executable instructions.
9. A computing device, the device comprising a memory for storing computer program instructions and a processor for executing the computer program instructions, wherein, When the computer program instructions are executed by the processor, the device is triggered to execute the large-model companion empathy text generation method with layered Hook vector intervention as described in any one of claims 1 to 5.
