Error Accessing completions Property of openai in Node.js Module

35 Views Asked by At

I'm working on a Node.js project where I'm trying to integrate OpenAI's API to process some files and use the chat completion feature. My module handles file uploads, processes these files, and is supposed to interact with OpenAI's API for further processing.

I've initialized the openai SDK as follows:

const openai = require("openai");
const openAiApiKey = "sk-API-KEY"; 
openai.apiKey = openAiApiKey;

Later in the same module, I attempt to call the chat.completions.create method to start a chat completion task:

const stream = await openai.chat.completions.create({
  model: "gpt-3.5-turbo",
  messages: [{"role": "user", "content": "Hello!"}],
  stream: true,
});
for await (const part of stream) {
  console.log(part.choices[0].delta);
}

However, when I run my application, I encounter the following error:

TypeError: Cannot read properties of undefined (reading 'completions')

This error suggests that the completions property is not recognized as part of the openai object.

Here are the steps I've taken to troubleshoot:

  1. Ensured that the openai SDK is correctly installed and can be required in the module without issues.
  2. Verified that the API key is correct and active.
  3. Looked into the openai SDK documentation to ensure that the chat.completions.create method exists and is used correctly.

Despite these checks, the error persists. Could someone help me understand why the completions property is undefined, and how I can resolve this issue to successfully call the chat.completions.create method?

0

There are 0 best solutions below