Asked 1 years ago
17 Mar 2023
Views 506
jqueryLearner

jqueryLearner posted

Error - cant read property of 'create' undefined in openai library

const { Configuration, OpenAIApi } = require("openai");

const configuration = new Configuration({
  apiKey: 'api key here',
});
 
const openai = new OpenAIApi(configuration);
async function a(){
try {
const prompt = "Please write a brief summary openai.";

 const completion = await openai.Completion.create({
  engine: 'text-davinci-003',
  prompt: prompt,
  max_tokens: 60,
  n: 1 
});

const summary = completion.choices[0].text.trim();
console.log(summary);
 
   
  } catch (error) {
  if (error.response) {
    console.log(error.response.status);
    console.log(error.response.data);
  } else {
    console.log(error.message);
  }
}

}

a();
ajamil

ajamil
answered Mar 17 '23 00:00

One possible reason for this error could be that the openai library is not properly installed or imported. Make sure that you have installed the openai library using npm or yarn, and that you have imported the necessary classes and methods in your code. You can do this by adding the following line at the beginning of your file:


const { Configuration, OpenAIApi, Completion } = require("openai");


This line imports the Completion class along with the Configuration and OpenAIApi classes, which are already included in your code. Once you have imported the Completion class, you can call the Completion.create() method to generate text using the OpenAI API.



Post Answer