Hello, apologies if this is asked in another place, but couldn’t find the answer regarding node.js.
I am using OpenAI Quickstart Node with the model text-davinci-003
and it works perfectly fine.
Now, I want to use the model gpt-3.5-turbo
. Attending to the example request documentation, I need to use openai.createChatCompletion
instead of openai.createCompletion
, and also add the parameter messages
. However, I get the error ‘An error occurred during your request’.
The version of openai
is ^3.1.0
.
In any idea why is not working? Thanks
import { Configuration, OpenAIApi } from "openai";
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
export default async function (req, res) {
if (!configuration.apiKey) {
res.status(500).json({
error: {
message: "OpenAI API key not configured, please follow instructions in README.md",
}
});
return;
}
const city = req.body.city || '';
if (city.trim().length === 0) {
res.status(400).json({
error: {
message: "Please enter a valid city",
}
});
return;
}
try {
const completion = await openai.createChatCompletion({
model: "gpt-3.5-turbo",
messages: [
{ role: "system", content: generatePrompt(city) },
{ role: "user", content: city },
],
temperature: 0.5,
max_tokens: 2000,
});
res.status(200).json({ result: completion.data.choices[0].text });
} catch(error) {
if (error.response) {
console.error(error.response.status, error.response.data);
res.status(error.response.status).json(error.response.data);
} else {
console.error(`Error with OpenAI API request: ${error.message}`);
res.status(500).json({
error: {
message: 'An error occurred during your request.',
}
});
}
}
}
function generatePrompt(city) {
return `Suggest a plan in ${city}.
`;
}