feat(provider): add DashScope chat completion providers - #9857
Closed
mantoujun12 wants to merge 2 commits into
Closed
feat(provider): add DashScope chat completion providers#9857mantoujun12 wants to merge 2 commits into
mantoujun12 wants to merge 2 commits into
Conversation
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.
Contributor
There was a problem hiding this comment.
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
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Modifications / 改动点
Linked #9817
本次 Pull Request 做了:
pay-as-you-go) 和 Token PlanproviderUtils.js中,图标添加了dashscope-token-plan,图标使用 阿里云本次 Pull Request
修改 / 添加了下列文件:dashscope_token_plan_source.py- 适配 阿里云百炼 Token Plantest_dashscope_token_plan_source.py- 添加 阿里云百炼 Token Plan 的pytest(不需要可移除)core/config的default.py,添加了DashScope和DashScope Token Plan提供商core/provider的manager.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 Code的User-Agent。和Kimi/MiniMax/Xiaomi的Token Plan adapter同款模式,让订阅用户能用便宜套餐跑qwen3.8-max对话。按量计费那条 (复用openai_chat_completion) 是合规付费选项, Token Plan 这条是订阅用户专属。Important
目前本人没有订阅
阿里云百炼的Token Plan,如果你订阅了且是 开发成员 / 贡献者 ,可以checkout此Pull Request,查看是否有问题 (比如请求失败、运行异常) ,如果有问题,请在此处发表 评论 (comment) 或 更改 (commit) 帮助改进。Q: 为什么会有 DashScope Token Plan 的
pytestA: 项目惯例——
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: 前端
getProviderIcon按config_template的provider字段查图标字典。DashScope Token Plan 的provider="dashscope-token-plan",但字典里只有dashscope,没有dashscope-token-plan→ 返回空 → WebUI 下拉没图标。Kimi/MiniMax/Xiaomi的 Token Plan 都在字典里有对应key,补上才会视觉一致。Q: 会不会对 AstrBot 产生实质性的影响
A: 不会。纯新增,零侵入:
用独立
typedashscope_token_plan_chat_completion,不修改任何现有provider的行为或type按量计费那条复用现有
openai_chat_completion,只加一条config_template用户不在
WebUI选DashScope就完全不受影响,现有配置和provider全部照常唯一前置依赖是
dashscope_token_plan_source.py装饰器注册(项目标准机制,启动时自动加载)This is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果
Command Check / 命令检查
已经使用
ruff代码检查 + 格式化,使用pytest测试了无问题。Screenshots / 截图
下面是 模型提供商 界面的截图,并添加了
DashScope和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.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.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:
Enhancements:
Tests: