OpenAI Quickstart tutorial issue running a code

I am running an OpenAI Quickstart tutorial: https://platform.openai.com/docs/quickstart?context=node
Having an issue in Step 3, Making an API request.
When I run: node openai-test.js I get this error:
“TypeError: Cannot read properties of undefined (reading ‘completions’)
at main (C:\Users\leonm\Documents\Study\OpenAi\openai-test.js:7:40)
at Object. (C:\Users\leonm\Documents\Study\OpenAi\openai-test.js:15:1)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
at Module._extensions…js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
at node:internal/main/run_main_module:28:49”

Here is my full code:

const openai = require('openai');

async function main() {
  const completion = await openai.chat.completions.create({
    messages: [{ role: "system", content: "You are a helpful assistant." }],
    model: "gpt-3.5-turbo",
  });
  console.log(completion.choices[0]);
}
main();`Preformatted text`

I am running the code in VSC terminal.
openai-test.js file is created in local folder, in OpenAi directory.
Node is installed using Command Prompt in C:\WINDOWS\system32

How do I resolve the issue?

Hi! Welcome to the forums!

when I look at the docs it tells you to import it with modules: :thinking:

import OpenAI from "openai";

const openai = new OpenAI();

async function main() {
  const completion = await openai.chat.completions.create({
    messages: [{ role: "system", content: "You are a helpful assistant." }],
    model: "gpt-3.5-turbo",
  });

  console.log(completion.choices[0]);
}

main();

i’m thinking there could be multiple reasons; is there a particular reason why you didn’t stick with the example?

but it’s quite possible that you simply need to instantiate the class

const openai = new OpenAI();

caveat

even when following the docs, there’s still a possibility that it might not work, but let’s take it step by step :laughing:

Hi Diet! Thank you!

When I run the code as it is at the docs, I get another error.

import OpenAI from "openai";

const openai = new OpenAI();

async function main() {
  const completion = await openai.chat.completions.create({
    messages: [{ role: "system", content: "You are a helpful assistant." }],
    model: "gpt-3.5-turbo",
  });

  console.log(completion.choices[0]);
}

main();

I get the error:
SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:77:18)
at wrapSafe (node:internal/modules/cjs/loader:1288:20)
at Module._compile (node:internal/modules/cjs/loader:1340:27)
at Module._extensions…js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
at node:internal/main/run_main_module:28:49

:thinking:

We can try two things:

  1. we can stick with your original code, and properly instantiate the openai class.
const OpenAI= require('openai'); //<----

const openai = new OpenAI(); //<----

async function main() {
  const completion = await openai.chat.completions.create({
    messages: [{ role: "system", content: "You are a helpful assistant." }],
    model: "gpt-3.5-turbo",
  });
  console.log(completion.choices[0]);
}
main();`Preformatted text`

or

  1. we take a look at your package json and how you’re launching your project

but in any case, these are starting issues. once you get a basic project working, it should be smoother sailing :slight_smile:

2 Likes

I got it fixed and now both versions of code are working. :+1:

This instruction in Quickstart tutorial threw me off:

npm install --save openai
# or
yarn add openai

It should say:

npm install --save openai
# or/and
npm add openai

However, I get a new error:
“RateLimitError: 429 You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs”

Is there any sandbox account to try API or do I necessary have to pay for it even when developing and testing?

Thanks!

Yes, the API is pay per use, paying for the data sent and received, billed at the rate of the AI model. OpenAI Pricing

Outside of free trial monetary grants that may have been issued to accounts under different programs, there is no differentiation of “developing and testing”.

$5 prepaid to your account after adding a payment method will get you a LOT of gpt-3.5-turbo. https://platform.openai.com/account/billing/overview