All models failed (4): zhipu/glm-4.7: ⚠️ API rate limit reached. Please try agai...

2026年04月10日 15:01 processing

错误信息

FallbackSummaryError: All models failed (4): zhipu/glm-4.7: ⚠️ API rate limit reached. Please try again later. (rate_limit) | zhipu/GLM-5-Turbo: ⚠️ API rate limit reached. Please try again later. (rate_limit) | minimax/MiniMax-M2.7: HTTP 401 authentication_error: login fail: Please carry the API secret key in the 'Authorization' field of the request header (auth) | minimax/MiniMax-M2.5: HTTP 401 authentication_error: login fail: Please carry the API secret key in the 'Authorization' field of the request header (auth)

AI 解决方案

报错分析

1.错误翻译


原文:FallbackSummaryError: All models failed (4): zhipu/glm-4.7: ⚠️ API rate limit reached. Please try again later. (rate_limit) | zhipu/GLM-5-Turbo: ⚠️ API rate limit reached. Please try again later. (rate_limit) | minimax/MiniMax-M2.7: HTTP 401 authentication_error: login fail: Please carry the API secret key in the 'Authorization' field of the request header (auth) | minimax/MiniMax-M2.5: HTTP 401 authentication_error: login fail: Please carry the API secret key in the 'Authorization' field of the request header (auth)
译文:回退汇总错误:所有模型均失败(4个):智谱glm-4.7:⚠️ 达到API速率限制,请稍后重试。(rate_limit) | 智谱GLM-5-Turbo:⚠️ 达到API速率限制,请稍后重试。(rate_limit) | Minimax/MiniMax-M2.7:HTTP 401认证错误:登录失败:请在请求头的'Authorization'字段中携带API密钥(auth) | Minimax/MiniMax-M2.5:HTTP 401认证错误:登录失败:请在请求头的'Authorization'字段中携带API密钥(auth)

2.原因分析


  • 根本原因:① 智谱2个模型因短时间请求超量触发速率限制;② Minimax2个模型因未在请求头Authorization字段携带API密钥导致401认证失败。

  • 发生位置:调用模型的API请求逻辑处,上下文为批量请求多模型的错误汇总。
  • 3. 解决方案


    import requests
    from tenacity import retry, wait_exponential, stop_after_attempt
    
    # 1. 修复认证错误:统一设置Authorization头(从环境变量/配置文件取密钥)
    API_KEYS = {
        "zhipu": "YOUR_ZHIPU_API_KEY",  # 替换为实际密钥
        "minimax": "YOUR_MINIMAX_API_KEY"  # 替换为实际密钥
    }
    
    # 2. 修复速率限制:添加指数退避重试
    @retry(wait=wait_exponential(min=4, max=10), stop=stop_after_attempt(3))
    def call_model(model_name, url, payload):
        headers = {"Content-Type": "application/json"}
        # 动态设置认证头
        if model_name.startswith("zhipu/"):
            headers["Authorization"] = f"Bearer {API_KEYS['zhipu']}"
        elif model_name.startswith("minimax/"):
            headers["Authorization"] = f"Bearer {API_KEYS['minimax']}"
        
        resp = requests.post(url, json=payload, headers=headers, timeout=30)
        resp.raise_for_status()  # 抛出HTTP错误(如401/429)
        return resp.json()
    
    # 示例调用(替换为实际URL和参数)
    try:
        result = call_model(
            model_name="zhipu/glm-4.7",
            url="https://open.bigmodel.cn/api/paas/v4/chat/completions",
            payload={"messages": [{"role": "user", "content": "Hello"}]}
        )
    except Exception as e:
        print(f"调用失败:{str(e)}")
    

    4. 预防措施


  • 最佳实践1:用指数退避算法(如tenacity库)处理速率限制,避免硬编码重试间隔;

  • 最佳实践2:API密钥通过环境变量/配置中心管理(如python-dotenv),禁止硬编码,请求前校验密钥非空。

  • 推荐工具:速率限制监控用Prometheus+Grafana,密钥管理用HashiCorp Vault