Skip to content

feat(provider): add DashScope chat completion providers - #9857

Closed
mantoujun12 wants to merge 2 commits into
AstrBotDevs:masterfrom
mantoujun12:feat/add-dashscope-llm-provider
Closed

feat(provider): add DashScope chat completion providers#9857
mantoujun12 wants to merge 2 commits into
AstrBotDevs:masterfrom
mantoujun12:feat/add-dashscope-llm-provider

Conversation

@mantoujun12

@mantoujun12 mantoujun12 commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Modifications / 改动点

Linked #9817

本次 Pull Request 做了:

  • 添加 阿里云百炼 作为 对话模型 提供商,包括 按量付费 (pay-as-you-go) 和 Token Plan
  • DashBoardproviderUtils.js 中,图标添加了 dashscope-token-plan ,图标使用 阿里云

本次 Pull Request 修改 / 添加 了下列文件:

  • 添加 dashscope_token_plan_source.py - 适配 阿里云百炼 Token Plan
  • 添加 test_dashscope_token_plan_source.py - 添加 阿里云百炼 Token Planpytest (不需要可移除)
  • [核心文件]修改 core/configdefault.py ,添加了 DashScopeDashScope Token Plan 提供商
  • [核心文件]修改 core/providermanager.py ,添加了 "dashscope_token_plan_chat_completion"

Question & Ask / 问题与解答

Q: 为什么要添加 阿里云百炼 作为 对话 模型提供商

A: 百炼阿里云LLM 平台(核心是 qwen 系列),AstrBot 已有百炼embedding/tts/rerank ,但缺 LLM 对话这一核心入口。补上后百炼用户有完整生态(对话+知识库+语音+搜索一站式),且和已有的 modelscope (阿里达摩院) 生态呼应。

Q: 为什么要额外添加 DashScope Token Plan

A: Token Plan 是百炼的订阅套餐,走 Anthropic 兼容端点 + 需伪装 Claude CodeUser-Agent 。和 Kimi/MiniMax/XiaomiToken Plan adapter 同款模式,让订阅用户能用便宜套餐qwen3.8-max 对话。按量计费那条 (复用 openai_chat_completion) 是合规付费选项, Token Plan 这条是订阅用户专属。

Important

目前本人没有订阅 阿里云百炼Token Plan ,如果你订阅了且是 开发成员 / 贡献者 ,可以 checkoutPull Request ,查看是否有问题 (比如请求失败、运行异常) ,如果有问题,请在此处发表 评论 (comment) 或 更改 (commit) 帮助改进。

Q: 为什么会有 DashScope Token Plan 的 pytest

A: 项目惯例——tests/ 里几乎每个 source 都有对应测试(参考 test_anthropic_kimi_code_provider.py / test_dashscope_embedding_source.py)。覆盖默认值解析、UA 注入与空白兜底恢复、用户自定义配置保留(api_base/model/headers),确保 adapter 行为正确且后续重构不回归。

Note

如果不需要此 pytest 可以移除

Q: 为什么要在 DashBoard 中额外为一个 Token Plan 添加图标

A: 前端 getProviderIconconfig_templateprovider 字段查图标字典。DashScope Token Planprovider="dashscope-token-plan" ,但字典里只有 dashscope ,没有 dashscope-token-plan → 返回空 → WebUI 下拉没图标。Kimi/MiniMax/XiaomiToken Plan 都在字典里有对应 key ,补上才会视觉一致。

Q: 会不会对 AstrBot 产生实质性的影响

A: 不会。纯新增,零侵入:

  • 用独立 type dashscope_token_plan_chat_completion ,不修改任何现有 provider 的行为或 type

  • 按量计费那条复用现有 openai_chat_completion ,只加一条 config_template

  • 用户不在 WebUIDashScope 就完全不受影响,现有配置和 provider 全部照常

  • 唯一前置依赖是 dashscope_token_plan_source.py 装饰器注册(项目标准机制,启动时自动加载)

  • This is NOT a breaking change. / 这不是一个破坏性变更。

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

Command Check / 命令检查

已经使用 ruff 代码检查 + 格式化,使用 pytest 测试了无问题。

uv run ruff check . --fix
All checks passed!

uv run ruff format .
502 files left unchanged

uv run pytest tests/test_dashscope_token_plan_source.py -v
5 passed, 1 warning in 3.72s

Screenshots / 截图

下面是 模型提供商 界面的截图,并添加了 DashScopeDashScope Token Plan 的效果

Snipaste_2026-08-28_10-45-16
打开 新建提供商源 显示的下拉菜单
Snipaste_2026-08-28_10-45-30
DashScope
Snipaste_2026-08-28_10-45-34
DashScope Token Plan

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 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.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Add DashScope and DashScope Token Plan chat-completion providers for Alibaba Cloud Bailian users.

New Features:

  • Add DashScope as a chat-completion provider using its OpenAI-compatible endpoint.
  • Add a DashScope Token Plan chat-completion provider using the Anthropic-compatible service with required defaults and headers.

Enhancements:

  • Expose the DashScope Token Plan provider through dynamic provider loading and preserve user-configured endpoints, models, and headers.
  • Add the Alibaba Cloud icon for DashScope Token Plan entries in the dashboard.

Tests:

  • Add unit coverage for DashScope Token Plan defaults, header handling, and custom configuration preservation.

Add two DashScope (Aliyun Bailian) LLM config templates:

- DashScope (pay-as-you-go): reuses the existing openai_chat_completion
  type with the OpenAI-compatible endpoint and a standard sk- API key.
- DashScope Token Plan: new ProviderDashScopeTokenPlan adapter that
  routes Token Plan subscriptions through the Anthropic-compatible
  endpoint with a Claude Code User-Agent.

Register the adapter in ProviderManager.dynamic_import_provider, add
five unit tests covering default resolution, custom header preservation,
blank User-Agent fallback, and api_base/model override behavior, and map
the dashscope-token-plan icon in the WebUI provider icon registry.
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. area:webui The bug / feature is about webui(dashboard) of astrbot. labels Aug 28, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="astrbot/core/provider/sources/dashscope_token_plan_source.py" line_range="29-30" />
<code_context>
+        provider_settings: dict,
+    ) -> None:
+        merged_provider_config = dict(provider_config)
+        merged_provider_config.setdefault("api_base", DASHSCOPE_API_BASE)
+        merged_provider_config.setdefault("model", DASHSCOPE_DEFAULT_MODEL)
+        merged_provider_config["custom_headers"] = self._resolve_custom_headers(
+            merged_provider_config,
</code_context>
<issue_to_address>
**issue (bug_risk):** When a persisted provider configuration contains an empty `api_base` or `model` value, `setdefault` does not apply the DashScope defaults. The inherited Anthropic provider therefore falls back to `https://api.anthropic.com` for an empty base URL and sends an empty model name for an empty model, so the Token Plan provider does not reach DashScope or fails model validation.

**Triggers:** When the WebUI or an existing configuration serializes an unset field as an empty string rather than omitting the key.

**Suggested fix:** Apply the defaults when the values are blank as well, for example with `if not str(...).strip(): ...` before constructing the parent provider.

```suggestion
        if not str(merged_provider_config.get("api_base", "")).strip():
            merged_provider_config["api_base"] = DASHSCOPE_API_BASE
        if not str(merged_provider_config.get("model", "")).strip():
            merged_provider_config["model"] = DASHSCOPE_DEFAULT_MODEL
```
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: astrbot/core/provider/sources/dashscope_token_plan_source.py:30


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.

Comment thread astrbot/core/provider/sources/dashscope_token_plan_source.py Outdated
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sourcery assessment

Approved.

@Soulter Soulter closed this Aug 28, 2026
@mantoujun12
mantoujun12 deleted the feat/add-dashscope-llm-provider branch August 28, 2026 09:24
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. area:webui The bug / feature is about webui(dashboard) of astrbot. size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants