From 7695435faa9eeba6838b495ae24ec7559bade726 Mon Sep 17 00:00:00 2001 From: Blueteemo Date: Sun, 3 May 2026 01:29:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=BA=20OpenAI=20provider=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8F=AF=E9=85=8D=E7=BD=AE=E7=9A=84=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E9=87=8D=E8=AF=95=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 openai_source.py 中添加 _get_max_retries() 方法,支持 1-50 范围验证 - 在 __init__ 中计算一次 max_retries,避免重复调用 - 在 default.py 的 OpenAI provider 模板中添加 max_retries: 10 - 在 config-metadata.json 中添加中英俄三语翻译(含范围提示) - 无效配置时记录 warning 日志 --- astrbot/core/config/default.py | 6 ++++ .../core/provider/sources/openai_source.py | 29 +++++++++++++++++-- .../en-US/features/config-metadata.json | 5 ++++ .../ru-RU/features/config-metadata.json | 5 ++++ .../zh-CN/features/config-metadata.json | 5 ++++ 5 files changed, 48 insertions(+), 2 deletions(-) diff --git a/astrbot/core/config/default.py b/astrbot/core/config/default.py index 227e0b0242..ed1fb31fb9 100644 --- a/astrbot/core/config/default.py +++ b/astrbot/core/config/default.py @@ -1150,6 +1150,7 @@ "key": [], "api_base": "https://api.openai.com/v1", "timeout": 120, + "max_retries": 10, "proxy": "", "custom_headers": {}, }, @@ -2510,6 +2511,11 @@ "type": "int", "hint": "超时时间,单位为秒。", }, + "max_retries": { + "description": "最大重试次数", + "type": "int", + "hint": "API 调用失败时的最大重试次数,范围 1-50,默认为 10。", + }, "mimo-stt-system-prompt": { "description": "系统提示词", "type": "string", diff --git a/astrbot/core/provider/sources/openai_source.py b/astrbot/core/provider/sources/openai_source.py index 64e3a6645a..3022793bb4 100644 --- a/astrbot/core/provider/sources/openai_source.py +++ b/astrbot/core/provider/sources/openai_source.py @@ -494,6 +494,31 @@ def __init__(self, provider_config, provider_settings) -> None: self.set_model(model) self.reasoning_key = "reasoning_content" + self.max_retries = self._get_max_retries() + + _MAX_RETRIES_DEFAULT = 10 + _MAX_RETRIES_UPPER_BOUND = 50 + + def _get_max_retries(self) -> int: + """获取并验证最大重试次数,确保为 1 到 _MAX_RETRIES_UPPER_BOUND 之间的整数。""" + raw = self.provider_config.get("max_retries", self._MAX_RETRIES_DEFAULT) + try: + value = int(raw) + except (TypeError, ValueError): + logger.warning( + "max_retries 配置无效 (%s),使用默认值 %d。", + raw, + self._MAX_RETRIES_DEFAULT, + ) + return self._MAX_RETRIES_DEFAULT + clamped = max(1, min(value, self._MAX_RETRIES_UPPER_BOUND)) + if clamped != value: + logger.warning( + "max_retries 配置值 %d 超出范围,已调整为 %d。", + value, + clamped, + ) + return clamped def _ollama_disable_thinking_enabled(self) -> bool: value = self.provider_config.get("ollama_disable_thinking", False) @@ -1189,7 +1214,7 @@ async def text_chat( payloads["tool_choice"] = tool_choice llm_response = None - max_retries = 10 + max_retries = self.max_retries available_api_keys = self.api_keys.copy() chosen_key = random.choice(available_api_keys) image_fallback_used = False @@ -1260,7 +1285,7 @@ async def text_chat_stream( if func_tool and not func_tool.empty(): payloads["tool_choice"] = tool_choice - max_retries = 10 + max_retries = self.max_retries available_api_keys = self.api_keys.copy() chosen_key = random.choice(available_api_keys) image_fallback_used = False 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 689c460d83..8a1552dfef 100644 --- a/dashboard/src/i18n/locales/en-US/features/config-metadata.json +++ b/dashboard/src/i18n/locales/en-US/features/config-metadata.json @@ -1514,6 +1514,11 @@ "description": "Timeout", "hint": "Timeout in seconds." }, +"max_retries": { + "description": "Max Retries", + "type": "int", + "hint": "Maximum number of retries when API call fails, range 1-50, default is 10." + }, "mimo-stt-system-prompt": { "description": "System prompt", "hint": "System prompt used to guide MiMo STT transcription behavior." diff --git a/dashboard/src/i18n/locales/ru-RU/features/config-metadata.json b/dashboard/src/i18n/locales/ru-RU/features/config-metadata.json index ec124eeeec..1c26cc1f9d 100644 --- a/dashboard/src/i18n/locales/ru-RU/features/config-metadata.json +++ b/dashboard/src/i18n/locales/ru-RU/features/config-metadata.json @@ -1511,6 +1511,11 @@ "description": "Таймаут (сек)", "hint": "Максимальное время ожидания ответа." }, +"max_retries": { + "description": "Макс. попыток", + "type": "int", + "hint": "Максимальное количество повторных попыток при ошибке API, диапазон 1-50, по умолчанию 10." + }, "mimo-stt-system-prompt": { "description": "Системный промпт", "hint": "System prompt, который управляет поведением MiMo STT при распознавании." 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 73f6903bbe..f92f13f615 100644 --- a/dashboard/src/i18n/locales/zh-CN/features/config-metadata.json +++ b/dashboard/src/i18n/locales/zh-CN/features/config-metadata.json @@ -1516,6 +1516,11 @@ "description": "超时时间", "hint": "超时时间,单位为秒。" }, +"max_retries": { + "description": "最大重试次数", + "type": "int", + "hint": "API 调用失败时的最大重试次数,范围 1-50,默认为 10。" + }, "mimo-stt-system-prompt": { "description": "系统提示词", "hint": "用于指导 MiMo STT 转录行为的 system prompt。"