Skip to content

Commit 86f2d36

Browse files
committed
feat: 为 OpenAI provider 添加可配置的最大重试次数
- 在 openai_source.py 中添加 _get_max_retries() 方法,支持 1-50 范围验证 - 在 default.py 的 OpenAI provider 模板中添加 max_retries: 10 - 在 config-metadata.json 中添加中英俄三语翻译(含范围提示) - 无效配置时记录 warning 日志
1 parent dee4f14 commit 86f2d36

5 files changed

Lines changed: 47 additions & 2 deletions

File tree

astrbot/core/config/default.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,7 @@
11501150
"key": [],
11511151
"api_base": "https://api.openai.com/v1",
11521152
"timeout": 120,
1153+
"max_retries": 10,
11531154
"proxy": "",
11541155
"custom_headers": {},
11551156
},
@@ -2510,6 +2511,11 @@
25102511
"type": "int",
25112512
"hint": "超时时间,单位为秒。",
25122513
},
2514+
"max_retries": {
2515+
"description": "最大重试次数",
2516+
"type": "int",
2517+
"hint": "API 调用失败时的最大重试次数,范围 1-50,默认为 10。",
2518+
},
25132519
"mimo-stt-system-prompt": {
25142520
"description": "系统提示词",
25152521
"type": "string",

astrbot/core/provider/sources/openai_source.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,30 @@ def __init__(self, provider_config, provider_settings) -> None:
495495

496496
self.reasoning_key = "reasoning_content"
497497

498+
_MAX_RETRIES_DEFAULT = 10
499+
_MAX_RETRIES_UPPER_BOUND = 50
500+
501+
def _get_max_retries(self) -> int:
502+
"""获取并验证最大重试次数,确保为 1 到 _MAX_RETRIES_UPPER_BOUND 之间的整数。"""
503+
raw = self.provider_config.get("max_retries", self._MAX_RETRIES_DEFAULT)
504+
try:
505+
value = int(raw)
506+
except (TypeError, ValueError):
507+
logger.warning(
508+
"max_retries 配置无效 (%s),使用默认值 %d。",
509+
raw,
510+
self._MAX_RETRIES_DEFAULT,
511+
)
512+
return self._MAX_RETRIES_DEFAULT
513+
clamped = max(1, min(value, self._MAX_RETRIES_UPPER_BOUND))
514+
if clamped != value:
515+
logger.warning(
516+
"max_retries 配置值 %d 超出范围,已调整为 %d。",
517+
value,
518+
clamped,
519+
)
520+
return clamped
521+
498522
def _ollama_disable_thinking_enabled(self) -> bool:
499523
value = self.provider_config.get("ollama_disable_thinking", False)
500524
if isinstance(value, str):
@@ -1189,7 +1213,7 @@ async def text_chat(
11891213
payloads["tool_choice"] = tool_choice
11901214

11911215
llm_response = None
1192-
max_retries = 10
1216+
max_retries = self._get_max_retries()
11931217
available_api_keys = self.api_keys.copy()
11941218
chosen_key = random.choice(available_api_keys)
11951219
image_fallback_used = False
@@ -1260,7 +1284,7 @@ async def text_chat_stream(
12601284
if func_tool and not func_tool.empty():
12611285
payloads["tool_choice"] = tool_choice
12621286

1263-
max_retries = 10
1287+
max_retries = self._get_max_retries()
12641288
available_api_keys = self.api_keys.copy()
12651289
chosen_key = random.choice(available_api_keys)
12661290
image_fallback_used = False

dashboard/src/i18n/locales/en-US/features/config-metadata.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,11 @@
15141514
"description": "Timeout",
15151515
"hint": "Timeout in seconds."
15161516
},
1517+
"max_retries": {
1518+
"description": "Max Retries",
1519+
"type": "int",
1520+
"hint": "Maximum number of retries when API call fails, range 1-50, default is 10."
1521+
},
15171522
"mimo-stt-system-prompt": {
15181523
"description": "System prompt",
15191524
"hint": "System prompt used to guide MiMo STT transcription behavior."

dashboard/src/i18n/locales/ru-RU/features/config-metadata.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,11 @@
15111511
"description": "Таймаут (сек)",
15121512
"hint": "Максимальное время ожидания ответа."
15131513
},
1514+
"max_retries": {
1515+
"description": "Макс. попыток",
1516+
"type": "int",
1517+
"hint": "Максимальное количество повторных попыток при ошибке API, диапазон 1-50, по умолчанию 10."
1518+
},
15141519
"mimo-stt-system-prompt": {
15151520
"description": "Системный промпт",
15161521
"hint": "System prompt, который управляет поведением MiMo STT при распознавании."

dashboard/src/i18n/locales/zh-CN/features/config-metadata.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,11 @@
15161516
"description": "超时时间",
15171517
"hint": "超时时间,单位为秒。"
15181518
},
1519+
"max_retries": {
1520+
"description": "最大重试次数",
1521+
"type": "int",
1522+
"hint": "API 调用失败时的最大重试次数,范围 1-50,默认为 10。"
1523+
},
15191524
"mimo-stt-system-prompt": {
15201525
"description": "系统提示词",
15211526
"hint": "用于指导 MiMo STT 转录行为的 system prompt。"

0 commit comments

Comments
 (0)