---#299
Open
yangzhen233 wants to merge 3 commits intoageerle:mainfrom
Open
Conversation
feat(chat): 优化上下文管理 - 实现内存管理策略 基于 LangChain4j 的 ChatMemory 机制,实现智能的 Token 窗口内存管理, 支持策略模式的压缩机制,包括摘要压缩、Token 截断和滑动窗口三种策略。 主要功能: - 支持三种顶层策略:message(固定消息数)、token(Token窗口)、hybrid(Token+摘要) - 支持 100+ 主流 AI 模型的 Token 限制自动识别 - 策略框架按优先级执行:摘要(50) → 截断(100) → 滑动窗口(150) - 多层降级保障:策略失败时强制截断兜底 - 摘要模型策略:current/smart/custom 三种选择 - 系统消息保护:所有策略保留系统消息 核心组件: - ChatMemoryFactory: 内存工厂,根据配置创建 ChatMemory 实例 - TokenBasedChatMemory: Token 窗口内存核心实现 - CompressionStrategyManager: 策略管理器 - ModelTokenLimits: 模型 Token 限制映射 - TokenCounter: Token 计数器 修复问题: - SlidingWindowStrategy 不保留系统消息 → 分离系统消息处理 - effectiveMaxTokens 边界检查 → 确保返回值 >= 1 - 策略执行后仍超限 → 添加 forceTruncate() 强制截断兜底 配置优化: - 清理无效配置参数,保留有效配置项 - 添加策略配置注释说明,区分通用配置和策略专属配置 - 明确 message 策略下不生效的配置列表
重命名文档名称,优化策略描述为表格形式,修复模型Token限制映射中的重复key问题, 删除废弃的 SlidingWindowStrategy 类,新增 message 策略测试用例。 文档优化: - 重命名:Token窗口内存管理实现文档.md → 聊天上下文管理实现文档.md - 概述部分策略描述改为表格形式,更清晰易懂 - 压缩策略描述改为表格形式,展示功能与触发条件 代码修复: - 删除废弃的 SlidingWindowStrategy 类 - 默认策略改为 message(固定消息数量) 测试验证: - 新增 message 策略测试用例,验证滑动窗口功能 - 三种策略(message/token/hybrid)均测试通过 - message: 固定消息数量,滑动窗口移除旧消息 - token: Token超限时截断(15→7条, 1184→764 tokens) - hybrid: Token达到阈值时摘要压缩,保留语义
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
● Summary
基于 LangChain4j 的 ChatMemory 机制,实现智能的聊天上下文管理,支持三种策略模式,可根据不同场景灵活选择。
Features
三种顶层策略
两种压缩策略
核心组件
ChatMemoryFactory- 内存工厂,根据配置创建 ChatMemory 实例TokenBasedChatMemory- Token 窗口内存核心实现CompressionStrategyManager- 策略管理器,按优先级执行压缩策略ModelTokenLimits- 100+ 主流 AI 模型的 Token 限制映射TokenCounter- Token 计数器,估算文本和消息的 Token 数量PersistentChatMemoryStore- 持久化存储,将消息存储到数据库核心功能
Architecture
请求流程:
● Configuration
新增 application.yml 配置:
详情见文档:ruoyi-modules/ruoyi-chat/docs/聊天上下文管理实现文档.md
Test Plan
Files Changed