Sascha Ehrentraut
Sascha Ehrentraut

Reputation: 37

OpenAI GPT-3 API error: "Cannot find module '@openai/api'"

I am having trouble using the OpenAI API with Node.js. Specifically, I am trying to use the openai.Completion object, but I keep getting a Cannot find module '@openai/api' error.

I have already tried installing the @openai/api package using npm install @openai/api, but I get a 404 error indicating that the package could not be found. I have also removed it and reinstalled but no luck.

I also tried upgrading to the latest version of Node.js, which is currently 19.1.0, but the issue is stuborn. I created a test script (test.js) with the following code:

const openai = require('openai');

openai.apiKey = 'sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

async function runTest() {
    try {
        const gpt3Response = await openai.Completion.create({
            engine: 'davinci-codex',
            prompt: `Create a simple conversational response for beginners, with an easy question at the end, based on the input: "Hello, how are you?"`,
            max_tokens: 50,
            n: 1,
            stop: null,
            temperature: 0.5,
        });
        console.log(gpt3Response.choices[0].text.trim());
    } catch (error) {
        console.error(error);
    }
}

runTest();

When I run this script with node test.js, I get the following error:

Error: Cannot find module '@openai/api'
Require stack:
- C:\Users\User\Documents\Coding\folders\test.js

I have also tested the OpenAI API using VSC Thunder Client, and it seems to work. Here is the POST request I used:

POST https://api.openai.com/v1/engines/davinci/completions
{
    "prompt": "do you like soccer",
    "max_tokens": 50,
    "n": 1,
    "stop": null,
    "temperature": 0.5,
    "top_p": 1,
    "echo": false
}

I received the following response:

{
    "id": "cmpl-75BDDTIZ2Q1yodctHcEohCIsA1f46",
    "object": "text_completion",
    "created": 1681469095,
    "model": "davinci",
    "choices": [{
        "text": "?”\n\n“I’m not sure. I’ve never been to a game.”\n\n“I’m going to the game on Saturday. Would you like to go with me?",
        "index": 0,
        "logprobs": null,
        "finish_reason": "length"
    }],
    "usage": {
        "prompt_tokens": 4,
        "completion_tokens": 49,
        "total_tokens": 53
    }
}

Could you please help me understand what could be causing the Cannot find module '@openai/api' error?

To provide me with next steps to try figure out why this API is not working. Either solutions or further tests I can try.

Thank you!

Upvotes: 1

Views: 1801

Answers (2)

Rok Benko
Rok Benko

Reputation: 22920

You probably installed openai-api library. This is not the correct library. You can uninstall it with the following command:

npm uninstall openai-api

This ↓ is the correct library. Install it with the following command:

npm install openai

You will now get additional errors because you have quite a few mistakes in your code. To help you out, this is the correct code:

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

const configuration = new Configuration({
    apiKey: 'sk-xxxxxxxxxxxxxxxxxxxx',
});

const openai = new OpenAIApi(configuration);

async function runTest() {
    try {
        const gpt3Response = await openai.createCompletion({
            model: 'text-davinci-003',
            prompt: `Create a simple conversational response for beginners, with an easy question at the end, based on the input: "Hello, how are you?"`,
            max_tokens: 50,
            n: 1,
            stop: null,
            temperature: 0.5,
        });
        console.log(gpt3Response.data.choices[0].text.trim());
    } catch (error) {
        console.error(error);
    }
}

runTest();

Upvotes: 2

Trriger
Trriger

Reputation: 48

You have to install it locally with

npm i openai

because Node.js doesn't search in the global folder for dependencies for the local project. Global installation is for global executables and its dependencies.

Upvotes: 1

Related Questions