Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions astrbot/core/config/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,20 @@ class ChatProviderTemplate(TypedDict):
"proxy": "",
"custom_headers": {},
},
"百炼": {
"id": "dashscope",
"provider": "dashscope",
"type": "bailian_chat_completion",
"provider_type": "chat_completion",
"enable": True,
"key": [],
"api_base": "https://dashscope.aliyuncs.com/compatible-mode/v1",
"bl_coding_plan": False,
"bl_thinking": False,
"timeout": 120,
"proxy": "",
"custom_headers": {},
},
"AIHubMix": {
"id": "aihubmix",
"provider": "aihubmix",
Expand Down Expand Up @@ -1915,6 +1929,12 @@ class ChatProviderTemplate(TypedDict):
"type": "bool",
"hint": "启用后所有函数工具将全部失效,免费次数限制请查阅官方文档",
},
"bl_coding_plan": {
"type": "bool",
},
"bl_thinking": {
"type": "bool",
},
"gm_native_coderunner": {
"description": "启用原生代码执行器",
"type": "bool",
Expand Down
4 changes: 4 additions & 0 deletions astrbot/core/provider/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@ def dynamic_import_provider(self, type: str) -> None:
from .sources.bailian_rerank_source import (
BailianRerankProvider as BailianRerankProvider,
)
case "bailian_chat_completion":
from .sources.bailian_chat_source import (
ProviderBailianChat as ProviderBailianChat,
)

def get_merged_provider_config(self, provider_config: dict) -> dict:
"""获取 provider 配置和 provider_source 配置合并后的结果
Expand Down
45 changes: 45 additions & 0 deletions astrbot/core/provider/sources/bailian_chat_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from ..register import register_provider_adapter
from .openai_source import ProviderOpenAIOfficial


@register_provider_adapter(
"bailian_chat_completion",
"阿里云百炼 Chat Completion 提供商适配器",
provider_display_name="百炼",
)
class ProviderBailianChat(ProviderOpenAIOfficial):
def __init__(
self,
provider_config: dict,
provider_settings: dict,
) -> None:
# 根据 Coding Plan 模式自动切换 API Base URL
is_coding_plan = provider_config.get("bl_coding_plan", False)
is_thinking = provider_config.get("bl_thinking", False)

if is_coding_plan:
provider_config["api_base"] = "https://coding.dashscope.aliyuncs.com/v1"
else:
provider_config["api_base"] = (
"https://dashscope.aliyuncs.com/compatible-mode/v1"
)

if is_thinking and not provider_config.get("model"):
if is_coding_plan:
provider_config["model"] = "qwen3.5-plus"
else:
provider_config["model"] = "qwen-plus"

super().__init__(provider_config, provider_settings)
self.is_thinking = is_thinking

def _finally_convert_payload(self, payloads: dict) -> None:
"""添加百炼特定参数"""
super()._finally_convert_payload(payloads)

if self.is_thinking:
payloads["enable_thinking"] = True

def _extract_reasoning_content(self, completion) -> str:
"""提取推理内容"""
return super()._extract_reasoning_content(completion)
Comment on lines +43 to +45
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

_extract_reasoning_content 方法是对父类方法的空覆写,它只是简单地调用了 super()。这个方法是多余的,可以安全地移除,以使代码更简洁。移除后,程序将自动调用父类的实现,行为不会改变。

8 changes: 8 additions & 0 deletions astrbot/dashboard/routes/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,14 @@ async def update_provider_source(self):
if not isinstance(new_source_config, dict):
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"
Comment on lines +447 to +452
Copy link
Contributor

Choose a reason for hiding this comment

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

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 漂移:

  1. 在一个中心化的 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"
  2. 更新 ProviderBailianChat.__init__,让其使用这些常量,而不是硬编码字符串。

  3. 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_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):

    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:

    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.

Comment on lines +447 to +452
Copy link
Contributor

Choose a reason for hiding this comment

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

high

您好,这部分用于确定阿里云百炼 api_base 的逻辑与 astrbot/core/provider/sources/bailian_chat_source.py 中的逻辑重复了。这可能会在未来导致维护困难,因为如果 URL 发生变化,需要同时修改两个地方。

为了提高代码的可维护性,建议将这些 URL 定义为常量,并在两处复用。一个好的实践是将常量定义在 bailian_chat_source.py 中,然后在这里导入使用。

建议修改:

  1. 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"
  2. 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
  3. 在此文件 (astrbot/dashboard/routes/config.py) 中,也导入并使用这些常量。

Suggested change
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


# 确保配置中有 id 字段
if not new_source_config.get("id"):
new_source_config["id"] = original_id
Expand Down
10 changes: 9 additions & 1 deletion dashboard/src/i18n/locales/en-US/features/config-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,14 @@
"description": "Enable native search",
"hint": "When enabled, uses xAI Chat Completions native Live Search for web queries (billed on demand). Only applies to xAI providers."
},
"bl_coding_plan": {
"description": "Enable Coding Plan Mode",
"hint": "Uses Aliyun Bailian Coding Plan dedicated API. Requires sk-sp- prefixed API keys. Note: This mode doesn't support automatic model list fetching; please refer to [Supported Models](https://bailian.console.aliyun.com/cn-beijing/?tab=doc#/doc/?type=model&url=3005961) and add models manually."
},
"bl_thinking": {
"description": "Enable Thinking",
"hint": "Enables deep reasoning for detailed thought processes. Suitable for complex reasoning tasks."
},
"rerank_api_base": {
"description": "Rerank Model API Base URL",
"hint": "AstrBot appends /v1/rerank to the request URL."
Expand Down Expand Up @@ -1436,4 +1444,4 @@
"helpMiddle": "or",
"helpSuffix": "."
}
}
}
10 changes: 9 additions & 1 deletion dashboard/src/i18n/locales/zh-CN/features/config-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,14 @@
"description": "启用原生搜索功能",
"hint": "启用后,将通过 xAI 的 Chat Completions 原生 Live Search 进行联网检索(按需计费)。仅对 xAI 提供商生效。"
},
"bl_coding_plan": {
"description": "启用 Coding Plan 模式",
"hint": "启用后将使用阿里云百炼 Coding Plan 专用 API,需要使用 sk-sp- 开头的专用 API Key。注意:Coding Plan 模式不支持自动获取模型列表,请参考 [支持模型列表](https://bailian.console.aliyun.com/cn-beijing/?tab=doc#/doc/?type=model&url=3005961) 手动添加。"
},
"bl_thinking": {
"description": "启用 Thinking",
"hint": "启用后模型将进行深度推理,提供更详细的思考过程。适用于需要复杂推理的任务。"
},
"rerank_api_base": {
"description": "重排序模型 API Base URL",
"hint": "AstrBot 会在请求时在末尾加上 /v1/rerank。"
Expand Down Expand Up @@ -1439,4 +1447,4 @@
"helpMiddle": "或",
"helpSuffix": "。"
}
}
}
Loading