HTTP Error 401: Unauthorized 0.7 https://www.dmxapi.cn/v1 TimeoutError The read ...

2026年09月10日 13:36 30.36 秒 success

错误信息

Ran TIANDAO_CONFIG=/home/gongbo/srpg/config.example.json python3 -c 'import json, time; from main import load_config; from llm_doubao import DoubaoLLM; payload="events = "+json.dumps([{ │ "type": "NPC_TALK", "npc": "老张", "memory": "今夜风凉,酒馆里尚有温酒"}], ensure_ascii=False); cfg=load_config(); urls=["https://api.openai.com/v1", "https://www.dmxapi.cn/v1"]; │ [((lambda c,u: (c.update(base_url=u, provider="doubao", model="doubao-seed-2-1-turbo-260628", temperature=0.75, max_tokens=64, timeout=12), c))(dict(cfg), url)) for url in []]; │ … +4 lines └ https://api.openai.com/v1 HTTPError HTTP Error 401: Unauthorized 0.7 https://www.dmxapi.cn/v1 TimeoutError The read operation timed out 12.1

AI 解决方案

🔍 报错分析

1. 🌐 错误翻译


原文:https://api.openai.com/v1 HTTPError HTTP Error 401: Unauthorized 0.7
译文:https://api.openai.com/v1 HTTP错误 HTTP错误401:未授权 0.7

原文:https://www.dmxapi.cn/v1 TimeoutError The read operation timed out 12.1
译文:https://www.dmxapi.cn/v1 超时错误 读取操作超时 12.1

2. 🎯 原因分析


  • API认证失败:访问 OpenAI API 时返回 401 未授权错误,说明 API 密钥无效或未提供认证信息,请求被服务端拒绝。

  • API请求超时:访问 DMXAPI 服务时出现读取超时,通常是网络连接不稳定、服务端响应过慢或请求超时时间设置过短导致。
  • 3. 💡 解决方案


    针对两个不同的 API 问题分别处理,修复核心认证和超时配置:

    import json, time
    from main import load_config
    from llm_doubao import DoubaoLLM
    
    payload = "events = " + json.dumps([{
        "type": "NPC_TALK", "npc": "老张", "memory": "今夜风凉,酒馆里尚有温酒"
    }], ensure_ascii=False)
    
    cfg = load_config()
    urls = ["https://api.openai.com/v1", "https://www.dmxapi.cn/v1"]
    
    # 修复1:补充API密钥,解决OpenAI 401未授权问题
    # 修复2:增加重试机制和合理超时配置,解决DMXAPI超时问题
    def safe_api_init(c, url, api_key=None):
        config = c.copy()
        # 针对不同API设置认证和超时
        if "api.openai.com" in url:
            if not api_key:
                raise ValueError("需提供OpenAI API密钥")
            config.update(
                base_url=url,
                provider="openai",
                model="gpt-3.5-turbo",
                api_key=api_key,
                temperature=0.75,
                max_tokens=64,
                timeout=12
            )
        elif "dmxapi.cn" in url:
            config.update(
                base_url=url,
                provider="doubao",
                model="doubao-seed-2-1-turbo-260628",
                api_key=config.get("dmx_api_key"),  # 确保DMXAPI密钥已配置
                temperature=0.75,
                max_tokens=64,
                timeout=30,  # 延长超时时间,避免读取超时
                max_retries=3  # 增加重试机制
            )
        return config
    
    # 使用时补充有效的API密钥(示例需替换为真实密钥)
    openai_api_key = "你的有效OpenAI_API_密钥"
    dmx_api_key = "你的有效DMXAPI_密钥"
    
    # 初始化配置时传入对应密钥
    [(safe_api_init(dict(cfg), url, openai_api_key if "openai" in url else dmx_api_key)) for url in urls]
    

    4. 🛡️ 预防措施


  • 严格管理API密钥:使用环境变量存储 API 密钥,避免硬编码在代码中,同时确保密钥未过期且权限有效,定期检查密钥有效性。

  • 优化请求配置:根据服务端响应速度调整超时时间,核心 API 配置重试机制和超时重试策略,针对不稳定的网络设置合理的重试次数和间隔。

  • 使用API诊断工具:推荐用 Postman 提前测试 API 连通性和认证信息,或使用 curl 命令快速验证接口是否可正常访问,提前排查认证和网络问题。