我是相当新的API的,我试图创建一个小的网络应用程序利用达芬奇文本模型。
我觉得我在设置API时每一步都做对了,但是我得到了一个错误消息:
{
"error": {
"message": "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://beta.openai.com.",
"type": "invalid_request_error",
"param": null,
"code": null
}
}
下面是我为它设置的函数:
const [input, setInput] = useState("");
async function fetchAnswer() {
const res = await fetch("https://api.openai.com/v1/completions", {
method: "POST",
headers: {
"Content-Type": "applications/json",
Authorization:
"Bearer MY_API_KEY",
},
body: JSON.stringify({
model: "text-davinci-003",
prompt: "Create a lyric that rhymes with:" + input,
temperature: 1,
}),
});
const json = await res.json();
console.log(json);
}
显然,我不会共享API密钥,但我可以确认我复制并粘贴到函数中的API密钥是正确的,并且直接来自OpenAI提供给我的内容。
我还漏掉了什么吗?
1条答案
按热度按时间fslejnso1#
您可能已达到限额,或者由于帐户中缺少支付设置,您的API密钥不再有效。
如果您更新了它并生成了一个新的API密钥,那么一切都应该正常。