diff --git a/astrbot/core/config/default.py b/astrbot/core/config/default.py index 37edeb312d..b7caca68b3 100644 --- a/astrbot/core/config/default.py +++ b/astrbot/core/config/default.py @@ -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", @@ -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", diff --git a/astrbot/core/provider/manager.py b/astrbot/core/provider/manager.py index 2359a81371..52243a7da6 100644 --- a/astrbot/core/provider/manager.py +++ b/astrbot/core/provider/manager.py @@ -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 配置合并后的结果 diff --git a/astrbot/core/provider/sources/bailian_chat_source.py b/astrbot/core/provider/sources/bailian_chat_source.py new file mode 100644 index 0000000000..06a5815fd5 --- /dev/null +++ b/astrbot/core/provider/sources/bailian_chat_source.py @@ -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) diff --git a/astrbot/dashboard/routes/config.py b/astrbot/dashboard/routes/config.py index 823d0fb9dd..94fb322e2a 100644 --- a/astrbot/dashboard/routes/config.py +++ b/astrbot/dashboard/routes/config.py @@ -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" + # 确保配置中有 id 字段 if not new_source_config.get("id"): new_source_config["id"] = original_id diff --git a/dashboard/src/i18n/locales/en-US/features/config-metadata.json b/dashboard/src/i18n/locales/en-US/features/config-metadata.json index 2f7db9b429..f2c79a0e62 100644 --- a/dashboard/src/i18n/locales/en-US/features/config-metadata.json +++ b/dashboard/src/i18n/locales/en-US/features/config-metadata.json @@ -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." @@ -1436,4 +1444,4 @@ "helpMiddle": "or", "helpSuffix": "." } -} +} \ No newline at end of file diff --git a/dashboard/src/i18n/locales/zh-CN/features/config-metadata.json b/dashboard/src/i18n/locales/zh-CN/features/config-metadata.json index 57e092960b..50154e5671 100644 --- a/dashboard/src/i18n/locales/zh-CN/features/config-metadata.json +++ b/dashboard/src/i18n/locales/zh-CN/features/config-metadata.json @@ -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。" @@ -1439,4 +1447,4 @@ "helpMiddle": "或", "helpSuffix": "。" } -} +} \ No newline at end of file