/*
usage dashboard : https://platform.openai.com/account
billing settings : https://platform.openai.com/account/billing
$ npm install openai
*/
process.env.OPENAI_KEY = 'sk-xxxxxx'
process.env.OPENAI_MODEL = 'text-davinci-003'
process.env.OPENAI_TEMPERATURE = 0.6
// https://platform.openai.com/docs/models/gpt-3
// text-davinci-003
// gpt-3.5-turbo
// gpt-4
import { Configuration, OpenAIApi } from "openai";
const configuration = new Configuration({
apiKey: process.env.OPENAI_KEY,
});
const openai = new OpenAIApi(configuration);
try {
const completion = await openai.createCompletion({
model: process.env.OPENAI_MODEL,
prompt: "can you generate me a horse name ?",
temperature: process.env.OPENAI_TEMPERATURE,
});
console.log(completion.data.choices[0].text)
} catch(error) {
if (error.response) {
console.error(error.response.status, error.response.data);
} else {
console.error(`Error with OpenAI API request: ${error.message}`);
}
}