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

Sending the :id parameter to the Service component

In the early days of my Angular journey, I have a simple question. Currently, I am utilizing the WordPress REST API to showcase a list of posts from a specific category by using posts?categories={ID HERE}. However, I am facing an issue in passing the ID f ...

Looking to incorporate ipcRenderer from Electron into your Angular project? Having trouble accessing variables passed from the preload script?

I am struggling with incorporating ipcRenderer into the 'frontend' code of my electron app. Although I found examples in the documentation that use require, this method is not accessible on the frontend side where I am utilizing Angular. In the ...

Customizable parameters in a React component

I am encountering two issues with the code provided below: interface MyForm { age: number; email: string; name: string; } function Form< T, ComponentProps extends { name: string; onChange: (event: React.ChangeEvent) => void; } &g ...

I noticed that my API call is being executed twice within the router function

In my NextJs project, I am utilizing Express for routing. I have implemented a router with a dynamic :id parameter which triggers an axios call to check the ID in the database. However, I am facing an issue where the API is being called twice when the :id ...

Initiate a new TypeScript project using Create-Next-App

Currently, I'm attempting to create a new nextJs project using javascript. However, whenever I execute the following command npx create-next-app -e with-tailwindcss ./, it ends up generating a typescript project instead. If anyone could provide assis ...

Is there a way to adjust React states directly in the browser without relying on React dev tools?

Recently, my website was compromised and an individual was able to manipulate the react states, gaining access to sensitive information such as an admin panel. In addition, they were able to alter the userID within the useState function, resulting in a co ...

Uploading Files with Typescript Promises

Hello everyone, I'm facing an issue where a dialog window is opening before all the files are uploaded to the server. Can anyone please guide me on what might be going wrong in my code? public UploadAll() { this.doAsyncTask().then(() => ...

Is it possible to utilize Angular's structural directives within the innerHtml?

Can I insert HTML code with *ngIf and *ngFor directives using the innerHtml property of a DOM element? I'm confused about how Angular handles rendering, so I'm not sure how to accomplish this. I attempted to use Renderer2, but it didn't wor ...

How to assign a value to an array within a form in Angular 8

I'm facing an issue with my Angular 8 edit form that utilizes a form array. When I navigate to the page, the form array is not populated with values as expected. Can anyone help me identify and solve this problem? ngOnInit(): void { // Fetc ...

What is the proper way to utilize queries in BlitzJS?

I am attempting to extract data from a table by filtering based on the relationship with the Blitzjs framework. However, I am facing difficulties using queries as it seems to be the only option available. Every time I try to call the quer ...

For the past two days, there has been an ongoing issue that I just can't seem to figure out when running npm start

After multiple failed attempts, I have exhausted all troubleshooting steps including executing npm clear cache --force, deleting node_modules/ and package-lock.json, followed by running npm install, npm build, and eventually npm run dev. The errors encoun ...

Storage can be shared globally in a React/Nextjs application

My current situation involves retrieving data through an application, however I am encountering 429 errors due to server limits restricting infinite requests. To address this issue, my plan is to fetch data nightly and store it in a centralized storage ac ...

Managing errors with the RxJS retry operator

I'm facing an issue with my RxJS code where I need to continuously retry a data request upon failure while also handling the error. Currently, I am using the retry operator for this purpose. However, when attempting to subscribe to the retry operator ...

Is there a way to ensure that in React (Typescript), if a component receives one prop, it must also receive another prop?

For instance, consider a component that accepts the following props via an interface: interface InputProps { error?: boolean; errorText?: string; } const Input = ({error, errorText}: InputProps) => { etc etc } How can I ensure that when this com ...

How to implement ngx-infinite-scroll in Angular 4 by making a vertically scrollable table

Looking for a way to make just the table body scrollable in Angular 4 using ngx-infinite-scroll. I've tried some CSS solutions but haven't found one that works. Any help or documentation on this issue would be greatly appreciated. I attempted th ...

What is the best way to implement useState within a loop?

I'm currently working on a mini app that allows users to select multiple music albums using Next.js. My album display is similar to the image below, and I want to include a check mark when an album is clicked, and remove it when clicked again. Here& ...

What is the reason behind the asynchronous nature of getStaticProps() in Next.js?

What is the reasoning behind getStaticProps() being designed as an asynchronous function in Next.js? My understanding is that an async function is moved off the main thread during execution to prevent it from blocking while running tasks that might take so ...

Obtain document via Angular 2

Is it possible to use TypeScript to download an image that is already loaded? <div *ngIf="DisplayAttachmentImage" class="fixed-window-wrapper_dark"> <button class="btn btn-close-window" (wslClick)="HideAttachmentImage()"> & ...

What could be causing TypeScript to throw errors when attempting to utilize refs in React?

Currently, I am utilizing the ref to implement animations on scroll. const foo = () => { if (!ref.current) return; const rect = ref.current.getBoundingClientRect(); setAnimClass( rect.top >= 0 && rect.bottom <= window.i ...

React: Implement a feature to execute a function only after the user finishes typing

Currently, I am using react-select with an asynchronous create table and have integrated it into a Netsuite custom page. A issue I am facing is that I would like the getAsyncOptions function to only trigger when the user stops typing. The problem right now ...