How to use chatgpt-4 in node.js?

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