AI Model Wiki

    Skip to main content
    50 min
    Lesson 2 of 6

    Chat Completions & Models

    From the course OpenAI API Masterclass

    Mastering Chat Completions

    The Chat Completions API is the core endpoint for interacting with GPT models. Understanding its parameters is essential for building great AI applications.

    The Messages Array

    Every request includes a messages array with roles:

    const response = await openai.chat.completions.create({
      model: "gpt-4o",
      messages: [
        { role: "system", content: "You are a helpful assistant." },
        { role: "user", content: "Explain quantum computing." },
        { role: "assistant", content: "Quantum computing uses..." },
        { role: "user", content: "How does entanglement work?" }
      ],
    });
    

    Model Selection

    Choose the right model for your use case:

    ModelBest ForContextCost
    gpt-4oComplex reasoning, multimodal128K$$
    gpt-4o-miniFast, cost-effective tasks128K$
    o1 / o3Deep reasoning, math, code128K-200K$$$

    Key Parameters

    • temperature (0-2): Controls randomness. 0 = deterministic, 1 = creative
    • max_tokens: Limits response length. Always set this in production
    • top_p: Alternative to temperature for controlling diversity
    • response_format: Request JSON output with { type: "json_object" }
    • seed: Enable reproducible outputs for testing

    Structured Outputs

    Force the model to return valid JSON matching a schema:

    const response = await openai.chat.completions.create({
      model: "gpt-4o",
      messages: [{ role: "user", content: "List 3 planets" }],
      response_format: {
        type: "json_schema",
        json_schema: {
          name: "planets",
          schema: {
            type: "object",
            properties: {
              planets: { type: "array", items: { type: "string" } }
            }
          }
        }
      }
    });
    

    Temperature Guidance by Use Case

    A common mistake is leaving temperature at the default (1.0) for tasks that require deterministic outputs. Use temperature 0 for: classification, data extraction, code generation, and any task with a single correct answer. Use temperature 0.7-1.0 for: brainstorming, creative writing, and persona-driven chat. Use temperature 1.2-2.0 rarely and only for highly experimental creative tasks — outputs become erratic at extreme values. Always pair low temperature with seed for reproducible testing.

    Seed and Reproducibility

    const response = await openai.chat.completions.create({
      model: "gpt-4o",
      messages: [{ role: "user", content: "Classify this email as spam or not: ..." }],
      temperature: 0,
      seed: 42,
    });
    // Check response.system_fingerprint — if it changes between calls,
    // OpenAI updated the model and outputs may differ despite same seed.
    

    Key Takeaways

    • •Use the messages array with system, user, and assistant roles
    • •Select models based on task complexity and cost requirements
    • •Control output with temperature, max_tokens, and response_format
    • •Use structured outputs for reliable JSON responses
    0 of 6 lessons marked complete

    Progress is kept in this browser only.