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。"