#How do I use chatgpt API?

1 messages · Page 1 of 1 (latest)

ruby sierra
#

I want to make an app with gpt-4 how to get started in NodeJS?

long mothBOT
#

Hi @ruby sierra. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To use the ChatGPT API with GPT-4 in Node.js, you need to use the Chat Completion endpoint. Here is an example of how you can do this:

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);

In this example, you are using the createChatCompletion method to generate a response from the GPT-4 model. The messages parameter is an array of message objects. Each object has a role that can be 'system', 'user', or 'assistant', and content which is the text of the message from the role.

Please note that you need to have been granted access to the GPT-4 model to use it.

For more information, you can refer to the API Reference page (https://platform.openai.com/docs/api-reference/chat/create) and the OpenAI Node.js Library Github Issue (https://github.com/openai/openai-node/issues/118).

Also, remember that the ChatGPT API is not included in the ChatGPT Plus subscription and are billed separately. The API has its own pricing, which can be found at OpenAI Pricing (https://openai.com/pricing). The ChatGPT Plus subscription covers usage on chat.openai.com only and costs $20/month. You can learn more about this in the Help Article (https://help.openai.com/en/articles/7039783-how-can-i-access-the-chatgpt-api).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: