#How do I use chatgpt API?
1 messages · Page 1 of 1 (latest)
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).