OpenAI API拒绝设置不安全的标题“用户代理”
我不明白为什么我会收到此错误。
拒绝设置不安全的标题“用户代理”
我正在尝试将OpenAI的API用于个人项目。我不明白为什么它拒绝设置这个“不安全的标题”,以及如何或是否可以使其安全。我已经尝试搜索此问题,最佳链接是一个GitHub论坛,该论坛解释了Chrome可能是如何使用的,但是我试图在Safari中使用该应用程序,但也无法使用。
const onFormSubmit = (e) => {
e.preventDefault();
const formData = new FormData(e.target),
formDataObj = Object.fromEntries(formData.entries())
console.log(formDataObj.foodDescription);
//////OPENAI
const configuration = new Configuration({
apiKey: process.env.REACT_APP_OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
openai.createCompletion("text-curie-001", {
prompt: `generate food suggestions from the following flavor cravings: ${formDataObj.foodDescription}`,
temperature: 0.8,
max_tokens: 256,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
})
.then((response) => {
setState({
heading: `AI Food Suggestions for: ${formDataObj.foodDescription}`,
response: `${response.data.choices[0].text}`
});
})
}
I don't understand why I am receiving this error.
Refused to set unsafe header "User-Agent"
I am trying to use OpenAI's API for a personal project. I don't understand why it's refusing to set this "unsafe header" and how, or if, I can make it safe. I've tried googling this issue and the top link is for a GitHub forum that explains how it might be something that Chrome does but, I tried to use the app in Safari and it wouldn't work either.
const onFormSubmit = (e) => {
e.preventDefault();
const formData = new FormData(e.target),
formDataObj = Object.fromEntries(formData.entries())
console.log(formDataObj.foodDescription);
//////OPENAI
const configuration = new Configuration({
apiKey: process.env.REACT_APP_OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
openai.createCompletion("text-curie-001", {
prompt: `generate food suggestions from the following flavor cravings: ${formDataObj.foodDescription}`,
temperature: 0.8,
max_tokens: 256,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
})
.then((response) => {
setState({
heading: `AI Food Suggestions for: ${formDataObj.foodDescription}`,
response: `${response.data.choices[0].text}`
});
})
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如您所述,您正在收到错误,因为OpenAI API客户端“拒绝设置不安全的标题”“用户代理”。由于使用它需要访问敏感信息(API键),因此NodeJS客户端有意将交叉原点限制为防止意外揭示
解决方法,请参阅 https://github.com/ 请求完成。
-Node/essugn/6 Amankishore手动
As you stated, you're recieving the error because the openai API client "Refused to set unsafe header "User-Agent". Since using it requires access to sensitive information (the API key), the nodejs client intentionally restricts cross origin requests to prevent accidentally revealing secrets.
For a workaround, see https://github.com/openai/openai-node/issues/6 where AmanKishore manually requests completions.
I ended up writing my own completion function like so:
这对我有用,但这取决于配置类的实现详细信息:
This worked for me, but it depends on implementation details of the Configuration class:
使用Jacobs答案作为参考,这是GPT 3.5 Turbo API的解决方法。
需要包含一个函数fetchstream(),因为OpenAPI响应返回了需要通过递归函数来处理的readableSream。
Using Jacobs answer as reference, here is the workaround for the GPT 3.5 Turbo API.
There was a need to include a function fetchStream() as the openapi response returned a readableStream which needed to be handled through a recursive function.
我有一些问题。这个代码对我有用!
I have some problem. And this code work for me!
如果我们从前端 /客户端而不是安全的后端 /服务器端调用OpenAI,则会发生此错误。
This error occurs if we call OpenAI from the frontend / client-side instead of the secure backend / server-side.