JavTe
1
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}.
`;
}
PJK
2
I can’t see what wrong with your code but I’m not a great de-bugger!
I’ve just tested UsingGPT3.5.js and it works. Hopefully you can pick out the bits you need.
Good luck 
Save file as UsingGPT3.5.js
// Using GPT-3.5-Turbo
const { Configuration, OpenAIApi } = require(“openai”);
const API_KEY = “put your API key here”;
const configuration = new Configuration({ apiKey: API_KEY });
const openai = new OpenAIApi(configuration);
//const model = “gpt-3.5-turbo”;
const model = “gpt-3.5-turbo-0301”;
const temp = 0.5;
const tokens = 1024;
const TurboGPT = async (prompt) => {
try {
const completion = await openai.createChatCompletion({
model: model,
messages: [
{ role: “system”, content: “You are omnipotent and always compassionate” },
{ role: “assistant”, content: “How else may I help” },
{ role: “user”, content: prompt },
],
temperature: temp,
max_tokens: tokens,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
n: 1,
stop: “”,
});
const response = completion.data.choices[0].message.content;
// Show the response
console.log("==++==++==++==++==++====++==++==++==++==++==");
console.log(response);
console.log("==++==++==++==++==++====++==++==++==++==++==");
return response;
} catch (error) {
// added error handling with try/catch block
console.error(error); // log the error message
return null; // return null if there is an error
}
};
// execute TurboGPT(prompt) from the Terminal
TurboGPT(“Who were the Etruscans”);
In the Terminal directory where the file UsingGPT3.5.js is saved run
node UsingGPT3.5.js
to find out “Who were the Etruscans”
JavTe
3
Thanks PJK. Indeed it works. My bad, I only had to update openai (I am an idiot!)