整体代码:
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应用:
如果不配agent: new HttpProxyAgent("http://127.0.0.1:7890")
这一行会出现一直请求但没有结果,属于连接不上去
配了后请求会报错403:
错误详情:
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证书,这个我应该如何操作
解决方法:配置https代理
注意:
import HttpsProxyAgent from "https-proxy-agent"
使用https-proxy-agent
,而不是http-proxy-agent