feat: 统一 max_input_tokens API,所有引擎必须返回最大输入 token 数

- openai/ollama/manual 三引擎均暴露 max_input_tokens 属性(统一契约)
- ollama 额外上报 prompt_eval_count 到 last_prompt_tokens,使本地模型也走 token 感知压缩
- should_compress 改用 max_input_tokens,系统依当前引擎上限的 70%(compress_ratio)自动触发
- 压缩始终调用当前引擎(current_engine)做总结,符合"调用当前引擎总结压缩"
- 新增 tests/test_max_input_tokens.py 覆盖契约 + 70% 触发 + 消息数兜底

Co-Authored-By: WorkBuddy <workbuddy@tencent.com>
This commit is contained in:
shuiyu67andWorkBuddy committed 2026-08-06 22:50:18 +08:00
1 parent c87a07706e
commit 5fe1a7c868
6 files changed
+104 -7

No files matched your search

+3 -2
View File
@@ -760,10 +760,11 @@ multi 操作支持一次修改多处:
if hasattr(self, 'memory_manager') and self.memory_manager:
engine = self.current_engine
prompt_tokens = getattr(engine, 'last_prompt_tokens', None) if engine else None
# 统一 API:所有引擎必须返回 max_input_tokens(最大输入 token 数)
ctx = None
if engine and hasattr(engine, 'context_window'):
if engine:
try:
ctx = engine.context_window()
ctx = engine.max_input_tokens
except Exception:
ctx = None
ratio = get_system_config('compress_ratio', 0.7)
+4 -4
View File
@@ -307,18 +307,18 @@ class MemoryManager:
except (TypeError, ValueError):
pass
def should_compress(self, conversation: list, prompt_tokens=None, context_window=None, ratio=None) -> bool:
def should_compress(self, conversation: list, prompt_tokens=None, max_input_tokens=None, ratio=None) -> bool:
"""
判断是否需要压缩上下文。
优先级:
1. token 感知: prompt_tokens/context_window >= ratio 触发(OpenAI 格式可拿到真实 token 数)
1. token 感知: prompt_tokens/max_input_tokens >= ratio 触发(引擎需返回 max_input_tokens)
2. 兜底: 消息数超过 compress_threshold
"""
if ratio is None:
ratio = self.compress_ratio
if prompt_tokens and context_window:
if prompt_tokens and max_input_tokens:
try:
if int(context_window) > 0 and (int(prompt_tokens) / int(context_window)) >= ratio:
if int(max_input_tokens) > 0 and (int(prompt_tokens) / int(max_input_tokens)) >= ratio:
return True
except (TypeError, ValueError):
pass