[ ABORT TO HUD ]
SEQ. 1
SEQ. 2

Chat Completions API

⚙️ OpenAI API & Code Integration 20 min 300 BASE XP

The Core API Endpoint

The /v1/chat/completions endpoint is the workhorse of the OpenAI API. It accepts an array of messages and returns the model's response.

Message Roles

  • System: Sets the behavior and rules (the persona).
  • User: The prompt or input from the human.
  • Assistant: The model's previous responses (used to maintain conversation history).

Basic Node.js Example

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);
}
Temperature parameter: Controls randomness. 0.0 is deterministic and focused (good for coding/JSON). 1.0 or higher is creative and diverse (good for brainstorming/writing).
SYNAPSE VERIFICATION
QUERY 1 // 2
Which message role is used to set the overall behavior and rules for the model?
User
Assistant
System
Function
Watch: 139x Rust Speedup
Chat Completions API | OpenAI API & Code Integration — OpenAI Academy