Reputation: 129
I am making a chrome extension that uses Chat GPT 3.5 and have coded a simple prompt to send to the API using openai api and returns a value in the console.
I have my code below and keep getting this error...
error:
code: null
message: "you must provide a model parameter"
param: null
type: "invalid_request_error"
event though i have a model parameter.
// Define the API key
const API_KEY = "API KEY";
// Define the endpoint URL
const endpointUrl = "https://api.openai.com/v1/chat/completions";
// Define the headers
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${API_KEY}`,
};
// Define the maximum number of completions to return
const maxCompletions = 1;
// Define the prompt to send to the API
const prompt = {
model: "gpt-3.5-turbo",
prompt: "Hello, world!",
temperature: 0.5,
};
// Send a POST request to the endpoint with the prompt and headers
fetch(endpointUrl, {
method: "POST",
headers,
body: JSON.stringify({
prompt,
max_completions: maxCompletions,
}),
})
.then((response) => response.json())
.then((data) => {
// Log the response data to the console
console.log(data);
})
.catch((error) => {
console.error(error);
});
Upvotes: 3
Views: 1185
Reputation: 132
I used your code and experienced the same error. I investigated the network request and saw that the payload was malformed:
{
prompt: {
model: "gpt-3.5-turbo",
prompt: "Hello, world!",
temperature: 0.5},
max_completions: 1
}
So, as far as ChatGPT's api is concerned, you're only sending prompt
and max_completions
. The reason you request is formed this way is because you're passing an object filled with other objects into JSON.stringify()
.
Also, I'm not sure where you're getting the max_completions
property from, as it is not in the API doc, so I left that out. Here is the change you need to make:
// in your fetch call:
fetch(endpointUrl, {
method: "POST",
headers,
body: JSON.stringify(prompt), // prompt is an object, so no need to wrap it in another object.
}).then...
Another issue is that you're calling the create chat completion endpoint, but the properties you are sending are not correct. You are required to send:
So, you would also need to make an edit here:
// in your prompt variable:
const prompt = {
model: "gpt-3.5-turbo",
messages: [{role: "user", content: "Hello, World!"}], // change prompt to messages array
temperature: 0.5,
};
Cheers!
Upvotes: 1