2

I am sorry for basic question but getting no where with what seems to be a very basic piece of code. I have npm installed latest version of openai. I am getting a constant error in my terminal:

TypeError: Cannot read properties of undefined (reading 'create')
    at Object.<anonymous> (/Users/michalchojnacki/Desktop/Coding/OpenAi2/code.js:9:20)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Code:

const openai = require('openai');

openai.apiKey = "my API here";

const prompt = "What is the capital of France?";

const model = "davinci";

openai.completions.create({
  engine: model,
  prompt: prompt,
  max_tokens: 2048,
  n: 1,
  stop: '.',
  temperature: 0.5,
}, (error, response) => {
  if (error) {
    console.log(error);
  } else {
    console.log(response.choices[0].text);
  }
});

Would be grateful for any help!

I was expecting the terminal to give me the response to the prompt

1
  • Your code doesn't look anything like the example in the README, did you try that? Commented Dec 31, 2022 at 14:22

1 Answer 1

3

Based on the docs in the library it looks like you want something like this instead:

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

const configuration = new Configuration({
  apiKey: "YOUR_API_KEY",
});

async function getAiResponse(topic) {
  const openai = new OpenAIApi(configuration);
  const completion = await openai.createCompletion({
    model: "text-davinci-003",
    prompt: topic,
    max_tokens: 1024,
    n: 1,
    stop: null,
    temperature: 0.7
  });
  console.log(completion.data.choices[0].text);
}
getAiResponse("Your Prompt here");

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.