Skip to main content

Module ollama

Module ollama 

Source
Expand description

OllamaCtx — 为本地小窗口 Ollama 模型优化的上下文策略。

§与 [DefaultCtx] 的三点差异

  1. 更早触发压缩:总 tokens 超过窗口 35% 就压,而非默认的 50%。 8K 窗口下 ~2800 tokens 即启动压缩,给后续 turn 留呼吸空间。
  2. 工具输出更紧:单条 tool_result 上限 = ctx/8 clamp [2K, 6K] 字节,显著低于 Default 的 [8K, 32K]。本地模型 8K 窗口下一条 bash 输出占一半预算是主要失败模式。
  3. 窗口默认值降低:若 provider.context_window 未设,fallback 到 8000(Default 是 128000)。匹配 Ollama CLI 的 num_ctx 常见值。

§不做的事(明确范围)

  • 不砍 system prompt:tool schema 作为独立参数传给 LLM API, 不在 system_prompt: &str 里。真要简化 system prompt 需要在 [crate::agent::prompt] 层面做。
  • 不改工具集筛选:哪些工具暴露给模型是 crate::tool::ToolRegistry 的职责,与 ctx 无关。
  • 不重写 render/microcompact/replace_stale_readsbuild_messages 直接透传给 crate::ctx::render::build_messages —— 与默认行为同 pipeline,只是 ctx_window 更小、配合更紧的 tool-output 截断。 想要 render pipeline 级别的定制,完全重写自己的 build_messages 即可,不必受这里影响。

需要以上行为时,在上层扩展相应模块,不在 ctx 里做。

Structs§

OllamaCtx
本地 Ollama 模型的上下文策略。