( ^^^^^^^^^^^^^^^^^^ File "C:\Users\Administrator\AppData\Local\Programs\Python\...
错误信息
Traceback (most recent call last):
File "E:\ICLR\manuscript-1\steps\01_flatten_semantic_acts\flatten_semantic_acts.py", line 805, in <module>
main()
File "E:\ICLR\manuscript-1\steps\01_flatten_semantic_acts\flatten_semantic_acts.py", line 596, in main
issue_embeddings = embed_texts(
^^^^^^^^^^^^
File "E:\ICLR\manuscript-1\steps\01_flatten_semantic_acts\flatten_semantic_acts.py", line 175, in embed_texts
batch_embeddings = call_embeddings(
^^^^^^^^^^^^^^^^
File "E:\ICLR\manuscript-1\steps\01_flatten_semantic_acts\flatten_semantic_acts.py", line 119, in call_embeddings
with url_request.urlopen(req, timeout=360) as resp:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\urllib\request.py", line 215, in urlopen
return opener.open(url, data, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\urllib\request.py", line 521, in open
response = meth(req, response)
^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\urllib\request.py", line 630, in http_response
response = self.parent.error(
^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\urllib\request.py", line 559, in error
return self._call_chain(*args)
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\urllib\request.py", line 492, in _call_chain
result = func(*args)
^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\urllib\request.py", line 639, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 429: Too Many Requests
AI 解决方案
报错分析报告
1. 错误翻译
原文:urllib.error.HTTPError: HTTP Error 429: Too Many Requests
译文:urllib.error.HTTPError: HTTP 错误 429: 请求过多
2. 原因分析
- 文件:
flatten_semantic_acts.py 第119行- 函数:
call_embeddings() 中的 urllib.request.urlopen() 调用- 上下文:尝试获取文本嵌入(embedding)时触发了API限制
3. 解决方案
#### 方案一:添加指数退避重试机制(推荐)
# 修改 flatten_semantic_acts.py 中第119行附近的代码
import time
import urllib.error
def call_embeddings_with_retry(req, max_retries=5, initial_delay=1):
"""带有重试机制的API调用函数"""
for attempt in range(max_retries):
try:
with url_request.urlopen(req, timeout=360) as resp:
# 处理成功响应
return resp.read()
except urllib.error.HTTPError as e:
if e.code == 429 and attempt < max_retries - 1:
# 计算指数退避延迟
delay = initial_delay * (2 ** attempt)
print(f"遇到速率限制,等待 {delay} 秒后重试 (尝试 {attempt + 1}/{max_retries})")
time.sleep(delay)
continue
else:
raise # 重新抛出异常
except Exception as e:
raise # 处理其他异常
# 替换原调用
# 将第119行的 with url_request.urlopen(req, timeout=360) as resp:
# 替换为:
response_data = call_embeddings_with_retry(req)
# 然后根据原代码继续处理 response_data
#### 方案二:降低请求频率(简单但效率低)
# 在 embed_texts 函数中,批量处理时添加延迟
def embed_texts(texts, batch_size=10, delay_seconds=0.5):
batch_embeddings = []
for i in range(0, len(texts), batch_size):
batch = texts[i:i+batch_size]
# 调用原嵌入函数
embedding = call_embeddings(batch)
batch_embeddings.append(embedding)
# 在批次之间添加延迟
if i + batch_size < len(texts):
time.sleep(delay_seconds)
return batch_embeddings
4. ️ 预防措施
#### 最佳实践建议:
- 查询API文档了解具体的速率限制策略
- 使用
ratelimit 或 tenacity 库实现更专业的速率控制- 记录请求次数,避免接近限制阈值
- 批处理优化:
- Python库:
tenacity(重试库)、ratelimit(速率限制装饰器) - 监控:添加日志记录,监控API调用频率和失败情况
- 配置化:将速率限制参数(重试次数、延迟时间)提取到配置文件中
- 增大单次请求的批量大小(如果API支持)
- 使用异步请求提高效率
- 实现请求队列和优先级调度
#### 推荐工具:
关键提醒:429错误是保护性限制,合理的重试策略通常能解决,但需确保不形成"重试风暴"进一步加重服务器负担。