OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Using fetch to call the OpenAi API throws error 400 "you must provide a model parameter"

  • Thread starter Thread starter Juno
  • Start date Start date
J

Juno

Guest
Followed the OpenAI API docs and but there is something wrong with this POST request... I get an "you must provide a model parameter" error ... what is wrong with the request body?

Code:
try {
        const response = await fetch(
            `https://api.openai.com/v1/completions`,
            {
                body: JSON.stringify({"model": "text-davinci-003", "prompt": "Say this is a test", "temperature": 0, "max_tokens": 7}),
                method: "POST",
                headers: {
                    Accept: "application/json",
                    Authorization: "Bearer [API-KEY]",
                },
                    }
        ).then((response) => {
            if (response.ok) {
                response.json().then((json) => {
                    terminal.echo(json);
                });
            }
        });
      
        console.log("Completed!");
    } catch (err) { console.error(`Error: ${err}`) }
}```

<p>Followed the OpenAI API docs and but there is something wrong with this POST request... I get an "you must provide a model parameter" error ... what is wrong with the request body?</p>
<pre><code>try {
const response = await fetch(
`https://api.openai.com/v1/completions`,
{
body: JSON.stringify({"model": "text-davinci-003", "prompt": "Say this is a test", "temperature": 0, "max_tokens": 7}),
method: "POST",
headers: {
Accept: "application/json",
Authorization: "Bearer [API-KEY]",
},
}
).then((response) => {
if (response.ok) {
response.json().then((json) => {
terminal.echo(json);
});
}
});

console.log("Completed!");
} catch (err) { console.error(`Error: ${err}`) }
}```
</code></pre>
 
Top