You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用OpenAI API时出现“Maximum number of redirects exceeded”错误排查

可能原因及修复方案

1. 调整HTTP请求重定向参数

Chainlink Functions的makeHttpRequest默认重定向次数限制可能无法适配OpenAI API的请求流程,可显式配置重定向相关参数:

修改后的代码示例:

const prompt = args[0]

if (!secrets.openaiKey) {
  throw Error("Need to set OPENAI_KEY environment variable")
}

const openAIRequest = Functions.makeHttpRequest({
  url: "https://api.openai.com/v1/completions",
  method: "POST",
  headers: {
    Authorization: `Bearer ${secrets.openaiKey}`,
    "Content-Type": "application/json"
  },
  data: {
    model: "text-davinci-003",
    prompt: prompt,
    temperature: 1,
    max_tokens: 512,
  },
  maxRedirects: 5,
  followRedirects: true
})

const [openAiResponse] = await Promise.all([openAIRequest])
console.log("raw response", openAiResponse)

if (openAiResponse.error) {
  throw Error(`OpenAI API request failed: ${openAiResponse.error.message}`)
}

const result = openAiResponse.data.choices[0].text.trim()
return Functions.encodeString(result)

2. 补充必要请求头

原代码缺少Content-Type: application/json头,OpenAI API可能因该缺失返回重定向而非直接处理请求,添加该头可避免不必要的重定向触发。

3. 验证基础配置与环境

  • 确认secrets.openaiKey已正确配置,密钥无过期或权限问题
  • 检查Chainlink Functions运行环境是否能正常访问OpenAI API,部分受限环境可能需要额外代理配置

内容的提问来源于stack exchange,提问作者user7424538

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 13:15:02