- Request Blocked</title> <style> body { font-family: 'Arial', sans-serif; backg...
错误信息
https://platform.deepseek.com/v1/chat/completions 返回 HTTP 429:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Error - Request Blocked</title> <style> body { font-family: 'Arial', sans-serif; background-color: #f3f7f9; margin: 0;
AI 解决方案
🌐 错误翻译
原文:https://platform.deepseek.com/v1/chat/completions returned HTTP 429
译文:https://platform.deepseek.com/v1/chat/completions 返回 HTTP 429
🎯 原因分析
💡 解决方案
需在代码中加入请求频率控制,例如使用指数退避重试策略,以下是 Python 示例:
import time
import requests
from tenacity import retry, stop_after_attempt, wait_exponential
# 配置重试策略:最多重试5次,间隔时间按指数递增
@retry(
stop=stop_after_attempt(5),
wait=wait_exponential(multiplier=1, min=1, max=10)
)
def call_api():
url = "https://platform.deepseek.com/v1/chat/completions"
headers = {"Authorization": "Bearer YOUR_API_KEY"}
data = {"messages": [{"role": "user", "content": "Hello"}]}
response = requests.post(url, headers=headers, json=data)
# 若返回429,自动触发重试
response.raise_for_status()
return response.json()
# 调用API
try:
result = call_api()
print(result)
except Exception as e:
print(f"API调用失败:{e}")
若无tenacity库,可手动实现重试逻辑(如for循环配合time.sleep())
🛡️ 预防措施
time.sleep(间隔时间))。