The /v1/chat/completions endpoint is the workhorse of the OpenAI API. It accepts an array of messages and returns the model's response.
import OpenAI from 'openai';
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
async function main() {
const completion = await openai.chat.completions.create({
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Explain quantum computing in one sentence." }
],
model: "gpt-4o",
temperature: 0.7,
});
console.log(completion.choices[0].message.content);
}
0.0 is deterministic and focused (good for coding/JSON). 1.0 or higher is creative and diverse (good for brainstorming/writing).