调用chatgpt接口出现403报错,如何解决呢?

整体代码:
image.png

import fetch from 'node-fetch';
import HttpProxyAgent from "http-proxy-agent"
import { ChatGPTAPI } from 'chatgpt';

async function example() {
    const api = new ChatGPTAPI({
        apiKey: 'xxx',
        completionParams: {
            model: 'text-davinci-003'
        },
        fetch: (url, options) => {
            console.log(url, options);
            return fetch('https://api.openai.com/v1/chat/completions', {
                ...options,
                agent: new HttpProxyAgent("socks5://127.0.0.1:7890")
            })
        }
    })
    const res = await api.sendMessage('Hello World!')
    console.log(res.text)
}
example()

其中agent: new HttpProxyAgent("http://127.0.0.1:7890")是代理地址
其地址来自vpn应用:
image.png

如果不配agent: new HttpProxyAgent("http://127.0.0.1:7890")这一行会出现一直请求但没有结果,属于连接不上去

配了后请求会报错403:
image.png
错误详情:

ChatGPTError: OpenAI error 403: {
  "error": {
    "message": "The OpenAI API can only be accessed over HTTPS. You should access https://api.openai.com rather than the current URL.",
    "type": "invalid_request_error"
  }
}

我应该怎么解决这个问题?麻烦大佬们指点一下

我把http换成https后,报验证不过去;应该使用一个https证书,这个我应该如何操作
image.png

阅读 5.6k
2 个回答
✓ 已被采纳

解决方法:配置https代理

import fetch from 'node-fetch';
import HttpsProxyAgent from "https-proxy-agent"
import { ChatGPTAPI } from 'chatgpt';

async function example() {
    const api = new ChatGPTAPI({
        apiKey: 'xx',
        fetch: (url, options) => {
            console.log(url,options);
            return fetch(url, {
                ...options,
                agent: new HttpsProxyAgent("http://127.0.0.1:7890")
            })
        }
    })
    const res = await api.sendMessage('Hello World!')
    console.log(res.text)
}

example()

注意:import HttpsProxyAgent from "https-proxy-agent"使用https-proxy-agent,而不是http-proxy-agent

看报错只支持https,你的代理地址是http

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题