X Tutup
Skip to content

feat: 添加 Ollama 关闭思考模式开关 #5714#5941

Open
catDforD wants to merge 1 commit intoAstrBotDevs:masterfrom
catDforD:feat/5714-ollama-thinking-mode
Open

feat: 添加 Ollama 关闭思考模式开关 #5714#5941
catDforD wants to merge 1 commit intoAstrBotDevs:masterfrom
catDforD:feat/5714-ollama-thinking-mode

Conversation

@catDforD
Copy link
Contributor

@catDforD catDforD commented Mar 9, 2026

Feat #5714

当前 AstrBot 中的 Ollama 提供商走的是 OpenAI 兼容接口,但在该接口下直接使用 think:false 不能稳定关闭 thinking。这个 PR 为 Ollama 提供商增加了一个专用开关,在启用时内部改为注入 reasoning_effort=none,从而更稳定地关闭思考模式,减少模型响应延迟。顺带一提,#5769 其实也是相似的问题,这里应该都能解决。

Modifications / 改动点

  • 为 Ollama 提供商源的高级配置新增 关闭思考模式 开关
  • 在启用该开关时,对 Ollama 的 OpenAI 兼容请求注入 reasoning_effort=none
  • 在开关开启时,移除冲突的 reasoning / think 额外请求参数
  • 为旧的 Ollama provider source 配置补默认值,确保新字段能正常显示
  • 补充该配置项的中英文文案
  • 增加对应测试,覆盖 Ollama 请求覆盖逻辑

文件变更说明

  • astrbot/core/config/default.py

    • 为 Ollama source 增加默认配置项和元数据定义
  • astrbot/core/provider/sources/openai_source.py

    • 增加 Ollama 专用的 thinking 关闭逻辑,映射到 reasoning_effort=none
  • dashboard/src/composables/useProviderSources.ts

    • 修复高级配置条件渲染所需的上下文保留问题,并兼容旧配置
  • dashboard/src/i18n/locales/en-US/features/config-metadata.json

    • 增加英文文案
  • dashboard/src/i18n/locales/zh-CN/features/config-metadata.json

    • 增加中文文案
  • tests/test_openai_source.py

    • 增加相关单元测试
  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

配置界面截图如下:

image

测试视频如下:

freecompress-202603091727.mp4

视频中未显示 工具调用 是否会有影响,但是我实际测试过,开关思考模式不会对工具调用产生影响。

本地已完成以下验证:

ruff check .
uv run pytest tests/test_openai_source.py
cd dashboard && pnpm build

验证结果:

  • ruff check . 通过
  • uv run pytest tests/test_openai_source.py 通过,共 12 passed
  • pnpm build 通过
  • 在运行中的 Dashboard 中手动确认:
    • 已加载最新前端产物
    • Ollama 高级配置中已显示“关闭思考模式”开关
    • 开关可以正常勾选
  • 对本地 Ollama 服务验证确认:
    • reasoning_effort=none 能在 OpenAI 兼容接口下更稳定地关闭 thinking
    • think:false 在该接口下不稳定,不适合作为 AstrBot 的实现方案

Checklist / 检查清单

  • 😊 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。/ If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
  • 👀 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。/ My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
  • 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了 requirements.txtpyproject.toml 文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
  • 😮 我的更改没有引入恶意代码。/ My changes do not introduce malicious code.

Summary by Sourcery

通过添加一个 Ollama 专用开关,更可靠地在 OpenAI 兼容提供方中禁用模型的思考模式,并确保现有的 Ollama 来源拥有安全的默认值。

New Features:

  • 引入一个仅适用于 Ollama 的高级配置选项,用于禁用思考模式,其方式是通过 OpenAI 兼容端点将请求映射为 reasoning_effort=none

Enhancements:

  • 在 OpenAI 提供方中规范化并应用针对不同提供方的 extra_body 覆盖逻辑,使得在禁用思考模式时,Ollama 请求会移除冲突的 reasoning/think 参数。
  • 确保仪表盘中的 Ollama 提供方来源能够为新的禁用思考标志回填默认值,并调整高级配置处理逻辑,在隐藏被排除的键以避免渲染的同时,仍然保留字段访问能力。

Tests:

  • 添加单元测试,以验证在启用禁用思考选项时,Ollama 请求会注入 reasoning_effort=none,并移除冲突的 reasoning/think 字段。
Original summary in English

Summary by Sourcery

Add an Ollama-specific toggle to more reliably disable the model’s thinking mode via the OpenAI-compatible provider, and ensure existing Ollama sources get a safe default.

New Features:

  • Introduce an Ollama-only advanced configuration option to disable thinking mode by mapping requests to reasoning_effort=none via the OpenAI-compatible endpoint.

Enhancements:

  • Normalize and apply provider-specific overrides to extra_body in the OpenAI provider so Ollama requests remove conflicting reasoning/think parameters when thinking is disabled.
  • Ensure dashboard provider sources for Ollama backfill a default value for the new disable-thinking flag and adjust advanced-config handling to preserve field accessors while hiding excluded keys from rendering.

Tests:

  • Add unit tests to validate that Ollama requests inject reasoning_effort=none and strip conflicting reasoning/think fields when the disable-thinking option is enabled.

@auto-assign auto-assign bot requested review from Fridemn and advent259141 March 9, 2026 09:37
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Mar 9, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在解决 Ollama 提供商在使用 OpenAI 兼容接口时,通过 think:false 无法稳定关闭思考模式的问题。通过引入一个新的专用开关,并在启用时注入 reasoning_effort=none,本变更提供了一种更可靠的方式来控制模型的思考行为,从而减少响应延迟并提高稳定性。

Highlights

  • Ollama 思考模式开关: 为 Ollama 提供商添加了“关闭思考模式”开关,以提供更稳定的控制方式。
  • 注入 reasoning_effort=none: 当该开关启用时,通过 OpenAI 兼容接口注入 reasoning_effort=none,以更稳定地关闭思考模式,解决 think:false 不稳定的问题。
  • 移除冲突参数: 在开关开启时,移除了冲突的 reasoningthink 请求参数。
  • 配置默认值与国际化: 为旧的 Ollama 提供商配置补充了 ollama_disable_thinking 字段的默认值,并增加了新配置项的中英文文案。
  • 单元测试: 补充了相关的单元测试,覆盖了 Ollama 请求覆盖逻辑。
Changelog
  • astrbot/core/config/default.py
    • 为 Ollama 提供商配置添加了 ollama_disable_thinking 字段的默认值。
    • 定义了 ollama_disable_thinking 配置项的元数据,包括描述、类型、提示和条件。
  • astrbot/core/provider/sources/openai_source.py
    • 新增了 _ollama_disable_thinking_enabled 方法,用于检查是否启用了 Ollama 的思考模式关闭功能。
    • 新增了 _apply_provider_specific_extra_body_overrides 方法,用于在 Ollama 提供商启用关闭思考模式时,从 extra_body 中移除 reasoningthink 参数,并注入 reasoning_effort=none
    • _query_query_stream 方法中调用了 _apply_provider_specific_extra_body_overrides
  • dashboard/src/composables/useProviderSources.ts
    • 修改了 advancedSourceConfig 计算属性,以更灵活地处理高级配置项的枚举性,并兼容旧配置。
    • 新增了 ensureProviderSourceDefaults 函数,用于为提供商源(特别是 Ollama)设置默认值,确保 ollama_disable_thinking 字段的正确显示。
    • 在加载现有提供商源和创建新提供商源时,应用了 ensureProviderSourceDefaults 函数。
  • dashboard/src/i18n/locales/en-US/features/config-metadata.json
    • 添加了 ollama_disable_thinking 配置项的英文描述和提示。
  • dashboard/src/i18n/locales/zh-CN/features/config-metadata.json
    • 添加了 ollama_disable_thinking 配置项的中文描述和提示。
  • tests/test_openai_source.py
    • 导入了 ChatCompletion 类型。
    • 新增了 test_apply_provider_specific_extra_body_overrides_disables_ollama_thinking 测试,验证当 ollama_disable_thinking 启用时,reasoningthink 参数被移除,reasoning_effort 被设置为 none
    • 新增了 test_query_injects_reasoning_effort_none_for_ollama 测试,验证在实际 _query 调用中,当 ollama_disable_thinking 启用时,reasoning_effort=none 被正确注入。
Activity
  • 作者提供了配置界面的截图和测试视频,展示了新功能的 UI 和行为。
  • 作者进行了本地验证,包括 ruff checkpytestpnpm build,并确认所有测试通过。
  • 作者手动验证了 Dashboard 中“关闭思考模式”开关的显示和功能,并确认 reasoning_effort=none 在本地 Ollama 服务下能更稳定地关闭思考模式。
  • 作者确认此变更为非破坏性变更。
  • 作者完成了 PR 检查清单中的所有项。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗨——我已经审阅了你的改动,看起来非常不错!


Sourcery 对开源项目是免费的——如果你觉得我们的审查有帮助,请考虑分享给更多人 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的审查。
Original comment in English

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@dosubot dosubot bot added the area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. label Mar 9, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

这个 PR 实现得很好,为 Ollama 提供商添加了一个关闭思考模式的专用开关。代码改动清晰,覆盖了后端逻辑、前端配置、国际化文案和单元测试,确保了功能的完整性和稳定性。特别是,后端对 extra_body 的处理很健壮,前端通过 ensureProviderSourceDefaults 确保了向后兼容性,并且测试用例覆盖了核心逻辑。我只发现了一个可以改进的小问题,即 _query_query_stream 方法中参数合并逻辑存在不一致,具体请看我的评论。

to_del.append(key)
for key in to_del:
del payloads[key]
self._apply_provider_specific_extra_body_overrides(extra_body)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

您好,此处的改动是正确的,但在审查代码时,我注意到 _query_stream 方法中构造 extra_body 的逻辑与 _query 方法中不一致。

_query_stream 中(当前方法):

  1. custom_extra_body 更新 extra_body
  2. payloads 中的参数再次更新 extra_body
    这意味着 payloads 中的参数会覆盖 custom_extra_body 中的同名参数。

_query 中:

  1. payloads 中的参数构造 extra_body
  2. custom_extra_body 更新 extra_body
    这意味着 custom_extra_body 会覆盖 payloads 中的同名参数。

通常 custom_extra_body(来自静态配置)的优先级应该更高。为了保持行为一致性并避免潜在的 bug,建议将 _query_stream 中的逻辑调整为与 _query 一致。

虽然这超出了本次变更的核心范围,但由于您接触了这部分代码,这是一个很好的改进机会。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

X Tutup