Error message from OpenAI GPT-3 API: "openai.completions function not found"

I encountered an issue while trying to execute the test code from a tutorial on building a chat app with GPT-3, ReactJS, and Next.js. The error message I received was:

TypeError: openai.completions is not a function

This occurred when running the following code in my.js using "node my.js" in Git Bash on Windows 10:


    const openai = require('openai');
    openai.apiKey = "api-key";
    openai.completions({
         engine: "text-davinci-003",
                   prompt: "Hello, how are you?",
                   max_tokens: 32,
                   n: 1,
                   stop: ".",
                   temperature: 0.5,
                  }).then((response) => {
                      console.log(response.data.choices[0].text);
    });

I have attempted multiple alternative code snippets from OpenAI docs and other sources but have been unsuccessful in resolving the issue.

Answer №1

To resolve the issue, make sure to update the OpenAI package.

For Python:

pip install --upgrade openai

For NodeJS:

npm update openai

After updating, close the terminal and reopen it. Run the code again, and the error should no longer be present.

Answer №2

const ai = require('ai-engine');

async function generateResponse() {
  try {
    const result = await ai.generate({
         model: "AI-text-generator-005",
                   prompt: "Hi there, how's it going?",
                   max_words: 50,
                   response_count: 1,
                   stop_sequence: ".",
                   temperature: 0.6,
                  }).then((result) => {
                      console.log(result.data.responses[0].content);
    });
  } catch (err) {
    console.error('Oops! Something went wrong:', err);
  }
}

generateResponse();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are the steps to validate a form control in Angular 13?

My Angular 13 application has a reactive form set up as follows: I am trying to validate this form using the following approach: However, I encountered the following error messages: Additionally, it is important to note that within the component, there ...

Resolving NestJS Custom Startup Dependencies

In my setup, I have a factory responsible for resolving redis connections: import {RedisClient} from "redis"; export const RedisProvider = { provide: 'RedisToken', useFactory: async () => { return new Promise((resolve, reject ...

Using webpack to generate sourcemaps for converting Typescript to Babel

Sharing code snippets from ts-loader problems may be more suitable in this context: Is there a way to transfer TypeScript sourcemaps to Babel so that the final sourcemap points to the original file rather than the compiled TypeScript one? Let's take ...

Navigating to an external website in NextJs using Strapi

I am facing an issue where I need to redirect the user to a specific link stored as a data value in my Strapi CMS. When using <a href> tag, it redirects me to http://localhost:3000/www.google.com. How can I remove the localhost part from the URL? I h ...

NextJS introduces a unique functionality to Typescript's non-null assertion behavior

As per the typescript definition, the use of the non-null assertion operator is not supposed to impact execution. However, I have encountered a scenario where it does. I have been struggling to replicate this issue in a simpler project. In my current proj ...

The issue causing "ReferenceError: fetch is not defined" is causing the test to fail

There seems to be an issue with my project where 'node-fetch' is installed, but the rest of the files are not importing it and the tests are not failing import { IQuery } from 'models/IQuery.interface'; import { NextApiRequest, NextApiR ...

What is the method of duplicating an array using the array.push() function while ensuring no duplicate key values are

In the process of developing a food cart feature, I encountered an issue with my Array type cart and object products. Whenever I add a new product with a different value for a similar key, it ends up overwriting the existing values for all products in the ...

Headers cannot be sent to the client after they have already been set in Axios within Next.js

For additional discussion on this issue, please refer to the GitHub thread at - https://github.com/axios/axios/issues/2743 In my Next.js project, I am using Axios and occasionally encounter an error related to interceptors when returning a Promise.reject. ...

Angular 2: The linting error shows up as "Anticipated operands need to be of the same type or any"

So, I have this shared service file where a variable is defined like so: export class SharedService { activeModal: String; } Then, in my component file, I import the service and define it as follows: constructor(public sharedService: SharedService) ...

What is the process of customizing a Button component from Ant Design using styled components and TypeScript?

I'm trying to customize a Button from Ant Design using TypeScript and styled-components to give it a personalized style. However, I haven't been successful in achieving the desired result. I have experimented with various tests but none of them ...

"Exploring the process of extracting data from a JSON URL using NextJS, TypeScript, and presenting the

After tirelessly searching for a solution, I found myself unable to reach a concrete conclusion. I was able to import { useRouter } from next/router and successfully parse a local JSON file, but my goal is to parse a JSON object from a URL. One example of ...

Steps to resolve the 'Cannot assign value to userInfo$ property of [object Object] that only has getter' issue in Angular

I am currently in the process of building a web application using NGXS, and I'm encountering a specific error that I'm trying to troubleshoot. The issue arises when I attempt to fetch data from an API and display it within a column on the page. D ...

Steps to fix the issue of 'Property 'foo' lacks an initializer and is not definitely assigned in the constructor' while utilizing the @Input decorator

What is the proper way to initialize a property with an @Input decorator without compromising strict typing? The code snippet below is triggering a warning: @Input() bar: FormGroup = new FormGroup(); ...

Tips for resolving type inference for a component variable in a jest Mount test created using reactjs

I am currently working on a React project that is built in Typescript, specifically dealing with a unit test involving the use of mount from Enzyme. As I strive to align the project with the tsconfig parameter "noImplicitAny": true, I am faced with the cha ...

Issue with Destructuring Assignment Syntax in TypeScript

interface User extends Function { player: number, units: number[], sites: string[], } class User extends Function { constructor() { super('return this.player') [this.player, this.units, this.sites] = getBelongings( ...

When I integrated next-i18next into my project on Vercel, I encountered a 404 error page

I recently integrated the app next-i18next for internationalization, but now all pages on Vercel are showing up as 404 errors. Oddly enough, everything works perfectly fine locally. Why is this happening? const { i18n } = require("./next-i18next.conf ...

Utilize React to transform PDF files into PNG images and seamlessly upload them to Cloudinary

Currently, I am utilizing react typescript and looking to select a PDF file, transform the PDF into an image, and then upload the final image onto Cloudinary. Although I have a service set up for uploading images in my Cloudinary media library, I am unsu ...

Implementing the same concept yet yielding diverse variations?

I'm a bit confused as to why a1 is evaluated as false while a2 is considered to be of type boolean. Can someone explain the reasoning behind this discrepancy? type Includes<T extends readonly any[], U> = U extends T[number] ? true : false; type ...

Discover properties of a TypeScript class with an existing object

I am currently working on a project where I need to extract all the properties of a class from an object that is created as an instance of this class. My goal is to create a versatile admin page that can be used for any entity that is associated with it. ...