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:
1 parent
c87a07706e
commit
5fe1a7c868
6 files changed
+104
-7
No files matched your search
@@ -44,6 +44,8 @@ class ManualAI:
|
||||
self.conversation_history = []
|
||||
self.max_history = 10
|
||||
self._subprocess = None
|
||||
# 最近一次请求的 prompt token 数(手动引擎无真实模型,恒为 None)
|
||||
self.last_prompt_tokens = None
|
||||
self._session_id = str(int(time.time()))
|
||||
self._started = False
|
||||
|
||||
@@ -240,6 +242,11 @@ while True:
|
||||
time.sleep(0.3)
|
||||
'''
|
||||
|
||||
@property
|
||||
def max_input_tokens(self):
|
||||
"""当前引擎支持的最大输入 token 数(统一 API)。手动引擎无真实模型,给名义上限。"""
|
||||
return 128000
|
||||
|
||||
def generate_response(self, user_input, tool_results=None, system_prompt=None):
|
||||
"""生成响应 — 转发给另一个终端的人工"""
|
||||
paths = self._get_paths()
|
||||
|
||||
@@ -113,6 +113,9 @@ Ollama AI引擎插件帮助信息
|
||||
|
||||
# 共享对话历史引用(由CLI设置)
|
||||
self.shared_conversation_history = None
|
||||
|
||||
# 最近一次请求的 prompt token 数(供 token 感知压缩使用)
|
||||
self.last_prompt_tokens = None
|
||||
|
||||
# 直接启用思考标记,由主程序自动处理深度思考模型的响应
|
||||
self.thinking_start_marker = "<thinking>"
|
||||
@@ -156,7 +159,13 @@ Ollama AI引擎插件帮助信息
|
||||
except Exception as e:
|
||||
# 如果出现异常,说明服务未运行
|
||||
return False
|
||||
|
||||
|
||||
@property
|
||||
def max_input_tokens(self):
|
||||
"""当前引擎支持的最大输入 token 数(统一 API,供上下文压缩使用)。
|
||||
本地 Ollama 由配置 max_token_k 换算的 num_ctx 决定。"""
|
||||
return self.num_ctx
|
||||
|
||||
def generate_response(self, user_input, tool_results=None, system_prompt=None, tools=None):
|
||||
"""
|
||||
生成AI响应 - 使用Ollama API
|
||||
@@ -203,6 +212,15 @@ Ollama AI引擎插件帮助信息
|
||||
|
||||
# ── Function Calling: 检查 tool_calls ──
|
||||
message = response.get("message", {})
|
||||
# 上报本轮 prompt token 数(Ollama 返回 prompt_eval_count),供 token 感知压缩
|
||||
try:
|
||||
if isinstance(response, dict):
|
||||
_pet = response.get("prompt_eval_count")
|
||||
else:
|
||||
_pet = getattr(response, "prompt_eval_count", None)
|
||||
self.last_prompt_tokens = int(_pet) if _pet else None
|
||||
except Exception:
|
||||
self.last_prompt_tokens = None
|
||||
tool_calls_raw = message.get("tool_calls")
|
||||
if tool_calls_raw:
|
||||
# Ollama 的 tool_calls 格式: [{"function": {"name": ..., "arguments": {...}}}]
|
||||
|
||||
@@ -582,6 +582,11 @@ OpenAI 兼容格式引擎帮助信息
|
||||
return win
|
||||
return 128000
|
||||
|
||||
@property
|
||||
def max_input_tokens(self):
|
||||
"""当前引擎支持的最大输入 token 数(统一 API,供上下文压缩使用)。"""
|
||||
return self.context_window()
|
||||
|
||||
def list_models(self):
|
||||
"""列出可用模型(调用 API 的 /models 端点)"""
|
||||
if not self.base_url:
|
||||
|
||||
Reference in new issue
Block a user