How to use chatgpt-4 in node.js?

I’ve tried to use chatgpt 4.0 with my api key in Node.js.
But I get 404 error like this.

Request failed with status code 404

And This is my code.

import { Configuration, OpenAIApi } from 'openai';

const configuration = new Configuration({ apiKey: process.env.CHATGPTKEY });
const openai = new OpenAIApi(configuration);

const props = {
      model: 'gpt-4-0314',
      prompt: prompt,
      temperature: 0.7,
      top_p: 1,
      frequency_penalty: 0,
      presence_penalty: 0,
      max_tokens: 200,
      stream: false,
      n: 1
};

let data = await openai.createCompletion(props);

How can I fix this issue and use chatgpt-4?
I wanna you can solve this for me.
Thank you. Cheers

To use GPT4 you need to use the Chat Completion endpoint (and have been granted access to GPT4 model

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

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

const completion = await openai.createChatCompletion({
  model: "gpt-4",
  messages: [{role: "user", content: "Hello world"}],
});
console.log(completion.data.choices[0].message);

Example from API Reference page

2 Likes

Have you completed the package installations? For example, using ‘npm install openai’ and the gpt-4 model name might fix the error

Thank you for your reply.
Yes I’ve install package
image

2 Likes

Thank you for your help.

It is working now.

1 Like