Service Unavailable for url: https://www.dmxapi.cn/v1/chat/completions. Skipping...
错误信息
Phase 1 for poem #178 failed: DMX request failed: 503 Server Error: Service Unavailable for url: https://www.dmxapi.cn/v1/chat/completions. Skipping this poem.
AI 解决方案
🔍 报错分析
1. 🌐 错误翻译
原文:Phase 1 for poem #178 failed: DMX request failed: 503 Server Error: Service Unavailable for url: https://www.dmxapi.cn/v1/chat/completions. Skipping this poem.
译文:诗歌#178的第一阶段失败:DMX请求失败:503服务器错误:服务不可用,请求URL:https://www.dmxapi.cn/v1/chat/completions。跳过此诗歌。
2. 🎯 原因分析
https://www.dmxapi.cn/v1/chat/completions。3. 💡 解决方案
由于503错误是服务器端问题,客户端无法直接修复,但可以通过添加重试机制增强代码的健壮性:
import time
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
def make_dmx_request_with_retry(url, max_retries=3, backoff_factor=1):
"""
带重试机制的DMX API请求函数
"""
session = requests.Session()
# 配置重试策略
retry_strategy = Retry(
total=max_retries,
status_forcelist=[429, 500, 502, 503, 504], # 覆盖503等常见服务端错误
backoff_factor=backoff_factor, # 重试间隔:1, 2, 4秒...
allowed_methods=["HEAD", "GET", "OPTIONS", "POST"]
)
adapter = HTTPAdapter(max_retries=retry_strategy)
session.mount("http://", adapter)
session.mount("https://", adapter)
try:
response = session.post(url, timeout=30)
response.raise_for_status()
return response
except requests.exceptions.RequestException as e:
print(f"请求失败,已重试{max_retries}次,最终错误:{e}")
raise
# 使用示例
try:
response = make_dmx_request_with_retry("https://www.dmxapi.cn/v1/chat/completions")
# 处理响应
except Exception as e:
print("DMX服务持续不可用,请稍后重试")