(http_error_msg, response=self) requests.exceptions.HTTPError: 400 Client Error:...

2026年03月19日 21:23 状态: processing

🚨 错误信息

File "c:\Users\user\Desktop\RAG_test\test1.py", line 138, in <module> m=C1("2024年2月,OpenAI正式发布的首个能够根据文本提示生成一分钟高清视频的AI模型叫什么名字?") ~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\user\Desktop\RAG_test\test1.py", line 78, in C1 l2=text_rerank(query_text=s,text_list=l1,top_n=t1) File "C:\Users\user\Desktop\Signal Refinery\KAG_system\RAG_SYSTEM\query_doument.py", line 237, in text_rerank response.raise_for_status() # 检查请求是否成功 ~~~~~~~~~~~~~~~~~~~~~~~~~^^ File "E:\anaconda3\envs\signal_refinery\Lib\site-packages\requests\models.py", line 1026, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://www.dmxapi.cn/v1/rerank

🤖 AI解决方案

报错分析报告

1.错误翻译



原文:requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://www.dmxapi.cn/v1/rerank
译文:requests.exceptions.HTTPError:400客户端错误:对URL https://www.dmxapi.cn/v1/rerank 的请求无效(Bad Request)

2.原因分析


  • 根本原因:调用https://www.dmxapi.cn/v1/rerank接口时,请求参数不符合API规范,导致服务器返回400坏请求。

  • 具体位置query_doument.py第237行response.raise_for_status()触发异常,源于text_rerank函数向该URL发送的POST请求参数(如query_text/text_list/top_n)格式或内容错误。
  • 3.解决方案


    需按API文档修正请求参数构造逻辑(以下为通用修复示例):
    python
    import requests

    def text_rerank(query_text, text_list, top_n):
    url = "https://www.dmxapi.cn/v1/rerank"
    headers = {"Content-Type": "application/json"}
    # 按API要求构造请求体(需替换为实际文档的参数名)
    payload = {
    "query": query_text, # 对应API的查询字段
    "documents": text_list, # 对应API的文档列表字段
    "top_n": top_n # 对应API的返回条数字段
    }
    response = requests.post(url, json=payload, headers=headers)
    response.raise_for_status()
    return response.json()

    4. 预防措施


  • 参数校验:调用API前,对照官方文档验证参数的名称、类型、必填性(如text_list不能为空、top_n需为整数)。

  • 错误调试:在raise_for_status()前添加print(response.text),打印服务器返回的详细错误原因(如“缺少query字段”)。
  • ---
    :若仍报错,需检查API密钥是否有效、网络连通性或接口版本兼容性。