feat: implement Aliyun Bailian Chat provider and coding plan support#5718
feat: implement Aliyun Bailian Chat provider and coding plan support#5718WintryWind7 wants to merge 1 commit intoAstrBotDevs:masterfrom
Conversation
Summary of ChangesHello, 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! 此拉取请求旨在通过集成阿里云百炼对话模型来扩展平台的AI提供商能力。它引入了对百炼特有功能的全面支持,例如 Coding Plan 模式和 Thinking 深度推理模式,从而降低了用户上手难度并提供了更灵活的AI交互选项。 Highlights
Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
你好——我发现了 1 个问题,并给出了一些整体性的反馈:
- Bailian 的 API 基础 URL 同时在
update_provider_source和ProviderBailianChat.__init__中被设置;建议将这部分逻辑集中到一个地方(例如只放在 provider adapter 中),以避免配置分叉或引起混淆。 - 在
ProviderBailianChat.__init__中,你会修改传入的provider_config字典(覆盖其中的api_base和model);如果调用方在其他地方还依赖原始配置对象,建议基于副本进行操作,以避免产生意料之外的副作用。 - DashScope 的端点(
https://coding.dashscope.aliyuncs.com/v1和https://dashscope.aliyuncs.com/compatible-mode/v1)在多个地方被硬编码;将它们抽取为共享常量可以减少重复,并让未来的端点调整更加容易。
给 AI 代理的提示词
Please address the comments from this code review:
## Overall Comments
- The API base URL for Bailian is being set both in `update_provider_source` and in `ProviderBailianChat.__init__`; consider centralizing this logic in one place (e.g., only in the provider adapter) to avoid divergence or confusion.
- In `ProviderBailianChat.__init__` you mutate the incoming `provider_config` dict (overwriting `api_base` and `model`); if callers rely on the original configuration object elsewhere, consider working on a copy to prevent unexpected side effects.
- The DashScope endpoints (`https://coding.dashscope.aliyuncs.com/v1` and `https://dashscope.aliyuncs.com/compatible-mode/v1`) are hard-coded in multiple places; extracting them into shared constants would reduce duplication and make future endpoint changes easier.
## Individual Comments
### Comment 1
<location path="astrbot/dashboard/routes/config.py" line_range="447-452" />
<code_context>
return Response().error("缺少或错误的配置数据").__dict__
+ # 百炼 Chat Completion: 根据 bl_coding_plan 强制设置正确的 api_base
+ if new_source_config.get("type") == "bailian_chat_completion":
+ is_coding_plan = new_source_config.get("bl_coding_plan", False)
+ if is_coding_plan:
+ new_source_config["api_base"] = "https://coding.dashscope.aliyuncs.com/v1"
+ else:
+ new_source_config["api_base"] = "https://dashscope.aliyuncs.com/compatible-mode/v1"
+
# 确保配置中有 id 字段
</code_context>
<issue_to_address>
**suggestion:** Avoid hard-coding the 百炼 base URLs in multiple places to reduce drift risk.
These dashscope base URLs are now defined both here and in `ProviderBailianChat.__init__`. If one is updated (e.g., versioned path, regional endpoint, proxy), the other may be missed. Please centralize them in a shared constant or helper (e.g., provider/config module) and reference that from both places to avoid drift.
Suggested implementation:
```python
# 百炼 Chat Completion: 根据 bl_coding_plan 强制设置正确的 api_base
if new_source_config.get("type") == "bailian_chat_completion":
is_coding_plan = new_source_config.get("bl_coding_plan", False)
if is_coding_plan:
new_source_config["api_base"] = BAILIAN_CODING_API_BASE
else:
new_source_config["api_base"] = BAILIAN_COMPATIBLE_MODE_API_BASE
```
To fully implement the suggestion and avoid URL drift:
1. Define shared constants in a central provider/config module (for example, in `astrbot/providers/bailian_chat.py` or a dedicated `astrbot/providers/bailian_config.py`):
```python
BAILIAN_CODING_API_BASE = "https://coding.dashscope.aliyuncs.com/v1"
BAILIAN_COMPATIBLE_MODE_API_BASE = "https://dashscope.aliyuncs.com/compatible-mode/v1"
```
2. Update `ProviderBailianChat.__init__` to use these constants instead of hard-coded strings.
3. Add an import at the top of `astrbot/dashboard/routes/config.py` to bring the constants into scope, for example:
```python
from astrbot.providers.bailian_chat import (
BAILIAN_CODING_API_BASE,
BAILIAN_COMPATIBLE_MODE_API_BASE,
)
```
If the actual provider module path or naming differs in your codebase, adjust the module name in the import accordingly.
</issue_to_address>帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的评审。
Original comment in English
Hey - I've found 1 issue, and left some high level feedback:
- The API base URL for Bailian is being set both in
update_provider_sourceand inProviderBailianChat.__init__; consider centralizing this logic in one place (e.g., only in the provider adapter) to avoid divergence or confusion. - In
ProviderBailianChat.__init__you mutate the incomingprovider_configdict (overwritingapi_baseandmodel); if callers rely on the original configuration object elsewhere, consider working on a copy to prevent unexpected side effects. - The DashScope endpoints (
https://coding.dashscope.aliyuncs.com/v1andhttps://dashscope.aliyuncs.com/compatible-mode/v1) are hard-coded in multiple places; extracting them into shared constants would reduce duplication and make future endpoint changes easier.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The API base URL for Bailian is being set both in `update_provider_source` and in `ProviderBailianChat.__init__`; consider centralizing this logic in one place (e.g., only in the provider adapter) to avoid divergence or confusion.
- In `ProviderBailianChat.__init__` you mutate the incoming `provider_config` dict (overwriting `api_base` and `model`); if callers rely on the original configuration object elsewhere, consider working on a copy to prevent unexpected side effects.
- The DashScope endpoints (`https://coding.dashscope.aliyuncs.com/v1` and `https://dashscope.aliyuncs.com/compatible-mode/v1`) are hard-coded in multiple places; extracting them into shared constants would reduce duplication and make future endpoint changes easier.
## Individual Comments
### Comment 1
<location path="astrbot/dashboard/routes/config.py" line_range="447-452" />
<code_context>
return Response().error("缺少或错误的配置数据").__dict__
+ # 百炼 Chat Completion: 根据 bl_coding_plan 强制设置正确的 api_base
+ if new_source_config.get("type") == "bailian_chat_completion":
+ is_coding_plan = new_source_config.get("bl_coding_plan", False)
+ if is_coding_plan:
+ new_source_config["api_base"] = "https://coding.dashscope.aliyuncs.com/v1"
+ else:
+ new_source_config["api_base"] = "https://dashscope.aliyuncs.com/compatible-mode/v1"
+
# 确保配置中有 id 字段
</code_context>
<issue_to_address>
**suggestion:** Avoid hard-coding the 百炼 base URLs in multiple places to reduce drift risk.
These dashscope base URLs are now defined both here and in `ProviderBailianChat.__init__`. If one is updated (e.g., versioned path, regional endpoint, proxy), the other may be missed. Please centralize them in a shared constant or helper (e.g., provider/config module) and reference that from both places to avoid drift.
Suggested implementation:
```python
# 百炼 Chat Completion: 根据 bl_coding_plan 强制设置正确的 api_base
if new_source_config.get("type") == "bailian_chat_completion":
is_coding_plan = new_source_config.get("bl_coding_plan", False)
if is_coding_plan:
new_source_config["api_base"] = BAILIAN_CODING_API_BASE
else:
new_source_config["api_base"] = BAILIAN_COMPATIBLE_MODE_API_BASE
```
To fully implement the suggestion and avoid URL drift:
1. Define shared constants in a central provider/config module (for example, in `astrbot/providers/bailian_chat.py` or a dedicated `astrbot/providers/bailian_config.py`):
```python
BAILIAN_CODING_API_BASE = "https://coding.dashscope.aliyuncs.com/v1"
BAILIAN_COMPATIBLE_MODE_API_BASE = "https://dashscope.aliyuncs.com/compatible-mode/v1"
```
2. Update `ProviderBailianChat.__init__` to use these constants instead of hard-coded strings.
3. Add an import at the top of `astrbot/dashboard/routes/config.py` to bring the constants into scope, for example:
```python
from astrbot.providers.bailian_chat import (
BAILIAN_CODING_API_BASE,
BAILIAN_COMPATIBLE_MODE_API_BASE,
)
```
If the actual provider module path or naming differs in your codebase, adjust the module name in the import accordingly.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| if new_source_config.get("type") == "bailian_chat_completion": | ||
| is_coding_plan = new_source_config.get("bl_coding_plan", False) | ||
| if is_coding_plan: | ||
| new_source_config["api_base"] = "https://coding.dashscope.aliyuncs.com/v1" | ||
| else: | ||
| new_source_config["api_base"] = "https://dashscope.aliyuncs.com/compatible-mode/v1" |
There was a problem hiding this comment.
suggestion: 避免在多个地方硬编码百炼的基础 URL,以降低配置漂移风险。
这些 dashscope 的基础 URL 目前在这里以及 ProviderBailianChat.__init__ 中都有定义。如果其中一个被更新(例如版本路径、区域端点或代理发生变化),另一个可能会被遗漏。请将它们集中到共享常量或辅助方法中(例如 provider/config 模块),并在两个地方都通过该常量/辅助方法引用,以避免配置漂移。
建议的实现方式:
# 百炼 Chat Completion: 根据 bl_coding_plan 强制设置正确的 api_base
if new_source_config.get("type") == "bailian_chat_completion":
is_coding_plan = new_source_config.get("bl_coding_plan", False)
if is_coding_plan:
new_source_config["api_base"] = BAILIAN_CODING_API_BASE
else:
new_source_config["api_base"] = BAILIAN_COMPATIBLE_MODE_API_BASE为了完整落实这一建议并避免 URL 漂移:
-
在一个中心化的 provider/config 模块中定义共享常量(例如在
astrbot/providers/bailian_chat.py或单独的astrbot/providers/bailian_config.py中):BAILIAN_CODING_API_BASE = "https://coding.dashscope.aliyuncs.com/v1" BAILIAN_COMPATIBLE_MODE_API_BASE = "https://dashscope.aliyuncs.com/compatible-mode/v1"
-
更新
ProviderBailianChat.__init__,让其使用这些常量,而不是硬编码字符串。 -
在
astrbot/dashboard/routes/config.py文件顶部添加 import,将这些常量引入当前作用域,例如:from astrbot.providers.bailian_chat import ( BAILIAN_CODING_API_BASE, BAILIAN_COMPATIBLE_MODE_API_BASE, )
如果你们代码库中实际的 provider 模块路径或命名不同,请相应调整上述 import 中的模块名。
Original comment in English
suggestion: Avoid hard-coding the 百炼 base URLs in multiple places to reduce drift risk.
These dashscope base URLs are now defined both here and in ProviderBailianChat.__init__. If one is updated (e.g., versioned path, regional endpoint, proxy), the other may be missed. Please centralize them in a shared constant or helper (e.g., provider/config module) and reference that from both places to avoid drift.
Suggested implementation:
# 百炼 Chat Completion: 根据 bl_coding_plan 强制设置正确的 api_base
if new_source_config.get("type") == "bailian_chat_completion":
is_coding_plan = new_source_config.get("bl_coding_plan", False)
if is_coding_plan:
new_source_config["api_base"] = BAILIAN_CODING_API_BASE
else:
new_source_config["api_base"] = BAILIAN_COMPATIBLE_MODE_API_BASETo fully implement the suggestion and avoid URL drift:
-
Define shared constants in a central provider/config module (for example, in
astrbot/providers/bailian_chat.pyor a dedicatedastrbot/providers/bailian_config.py):BAILIAN_CODING_API_BASE = "https://coding.dashscope.aliyuncs.com/v1" BAILIAN_COMPATIBLE_MODE_API_BASE = "https://dashscope.aliyuncs.com/compatible-mode/v1"
-
Update
ProviderBailianChat.__init__to use these constants instead of hard-coded strings. -
Add an import at the top of
astrbot/dashboard/routes/config.pyto bring the constants into scope, for example:from astrbot.providers.bailian_chat import ( BAILIAN_CODING_API_BASE, BAILIAN_COMPATIBLE_MODE_API_BASE, )
If the actual provider module path or naming differs in your codebase, adjust the module name in the import accordingly.
| if new_source_config.get("type") == "bailian_chat_completion": | ||
| is_coding_plan = new_source_config.get("bl_coding_plan", False) | ||
| if is_coding_plan: | ||
| new_source_config["api_base"] = "https://coding.dashscope.aliyuncs.com/v1" | ||
| else: | ||
| new_source_config["api_base"] = "https://dashscope.aliyuncs.com/compatible-mode/v1" |
There was a problem hiding this comment.
您好,这部分用于确定阿里云百炼 api_base 的逻辑与 astrbot/core/provider/sources/bailian_chat_source.py 中的逻辑重复了。这可能会在未来导致维护困难,因为如果 URL 发生变化,需要同时修改两个地方。
为了提高代码的可维护性,建议将这些 URL 定义为常量,并在两处复用。一个好的实践是将常量定义在 bailian_chat_source.py 中,然后在这里导入使用。
建议修改:
-
在
astrbot/core/provider/sources/bailian_chat_source.py文件顶部添加常量定义:BAILIAN_CODING_PLAN_API_BASE = "https://coding.dashscope.aliyuncs.com/v1" BAILIAN_COMPATIBLE_API_BASE = "https://dashscope.aliyuncs.com/compatible-mode/v1"
-
在
astrbot/core/provider/sources/bailian_chat_source.py的__init__方法中使用这些常量:if is_coding_plan: provider_config["api_base"] = BAILIAN_CODING_PLAN_API_BASE else: provider_config["api_base"] = BAILIAN_COMPATIBLE_API_BASE
-
在此文件 (
astrbot/dashboard/routes/config.py) 中,也导入并使用这些常量。
| if new_source_config.get("type") == "bailian_chat_completion": | |
| is_coding_plan = new_source_config.get("bl_coding_plan", False) | |
| if is_coding_plan: | |
| new_source_config["api_base"] = "https://coding.dashscope.aliyuncs.com/v1" | |
| else: | |
| new_source_config["api_base"] = "https://dashscope.aliyuncs.com/compatible-mode/v1" | |
| if new_source_config.get("type") == "bailian_chat_completion": | |
| from astrbot.core.provider.sources.bailian_chat_source import ( | |
| BAILIAN_CODING_PLAN_API_BASE, | |
| BAILIAN_COMPATIBLE_API_BASE, | |
| ) | |
| is_coding_plan = new_source_config.get("bl_coding_plan", False) | |
| if is_coding_plan: | |
| new_source_config["api_base"] = BAILIAN_CODING_PLAN_API_BASE | |
| else: | |
| new_source_config["api_base"] = BAILIAN_COMPATIBLE_API_BASE |
| def _extract_reasoning_content(self, completion) -> str: | ||
| """提取推理内容""" | ||
| return super()._extract_reasoning_content(completion) |
因为我刚刚订阅了阿里云百炼的coding plan,且这里没有预设,为了降低其他用户的上手难度。添加对阿里云百炼 (Aliyun Bailian / DashScope) 对话模型的原生支持,包括专用的 Coding Plan 模式和 Thinking 深度推理模式。
Modifications / 改动点
astrbot/core/provider/sources/bailian_chat_source.py,实现百炼对话模型适配器,继承自 OpenAI 适配器并针对百炼特性进行优化。astrbot/core/config/default.py,注册百炼配置模板,新增bl_coding_plan(Coding Plan 开关) 和bl_thinking(深度推理开关) 配置项。astrbot/core/provider/manager.py,注册bailian_chat_completion提供商类型及其适配器映射。astrbot/dashboard/routes/config.py,在更新配置时针对百炼提供商增加逻辑,dashboard/src/i18n/locales/下的zh-CN和en-US国际化文件,支持百炼专有配置项的中文/英文描述显示,并补全了前端切换通知词条。Screenshots or Test Results / 运行截图或测试结果
有一个问题:当点击按钮进行切换的时候,理论上应实时变化url地址,然而我尝试了多种修改方式依然无法实现实时变化,但是存储的数据是正确的,点下按钮后保存的时候后端会正确匹配,重新点击下这个provider就会正常显示。主要是我debug的时候发现前端写console.log显示不出来,我不太懂,可能我的调试方式有问题吧。因为我精力有限,一段时间内无法再修改了。如果您认为此功能有用但我的代码有问题,可以在这基础上修改一下,,或者等我后续有空。
Checklist / 检查清单
requirements.txt和pyproject.toml文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations inrequirements.txtandpyproject.toml.Summary by Sourcery
为阿里云百炼聊天补全新增原生 Provider 支持,包括 Coding Plan 和 Thinking 模式,并将其集成到配置和仪表盘流程中。
新功能:
bl_coding_plan和bl_thinking配置选项。增强改进:
Original summary in English
Summary by Sourcery
Add native provider support for Aliyun Bailian chat completion, including Coding Plan and Thinking modes, and integrate it into configuration and dashboard flows.
New Features:
Enhancements: