No clue why you are getting an error, it would probably be easier to debug with vanilla JS
Here is a low tech example:
async function openAi(apiKey, method, data) {
let requestOptions = {
method : 'POST',
headers : {
'Content-Type' : 'application/json',
'Authorization': `Bearer ${apiKey}`,
},
body: JSON.stringify(data)
};
return await fetch(`https://api.openai.com/v1/${method}`, requestOptions).then((response) => response.json());
}
let request = {
"model" : "text-davinci-003",
"prompt" : "<input text>",
"temperature": 0,
"max_tokens" : 1000
}
let result = await openAi(localStorage.getItem('myApiKey'), 'completions', request);
console.log(result.choices[0].text);
PS: didn’t test this code, i ripped it out of one of my abstraction layers and edited it for easy understanding …milage may vary ^^