OpenAI API — Chat, Completion, Embedding

লেসন ২৭ / ৪৬

🔑 API Setup

// Install
npm install openai
pip install openai

// Auth (env variable)
OPENAI_API_KEY=sk-...

// Never hardcode key in code!

💬 Chat Completion

import OpenAI from "openai";
const openai = new OpenAI();

const completion = await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [
    { role: "system", content: "You are helpful." },
    { role: "user", content: "Explain recursion in 2 lines." }
  ],
  temperature: 0.3,
  max_tokens: 200
});

console.log(completion.choices[0].message.content);
console.log("Tokens:", completion.usage);

📡 Streaming

const stream = await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [...],
  stream: true
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content || "");
}

🔢 Embedding API

const emb = await openai.embeddings.create({
  model: "text-embedding-3-small",
  input: "Hello world"
});
// emb.data[0].embedding → 1536-dim vector

🛠️ Function/Tool Calling

const response = await openai.chat.completions.create({
  model: "gpt-4o",
  messages: [...],
  tools: [{
    type: "function",
    function: {
      name: "get_weather",
      description: "Get weather for city",
      parameters: {
        type: "object",
        properties: { city: { type: "string" } },
        required: ["city"]
      }
    }
  }]
});

if (response.choices[0].message.tool_calls) {
  // Execute function, send result back
}

📊 Structured Output

const r = await openai.chat.completions.create({
  model: "gpt-4o-2024-08-06",
  messages: [...],
  response_format: {
    type: "json_schema",
    json_schema: {
      name: "extract",
      strict: true,
      schema: { /* JSON schema */ }
    }
  }
});

🖼️ Vision API

{
  role: "user",
  content: [
    { type: "text", text: "Describe this image" },
    { type: "image_url", image_url: { url: "https://..." }}
  ]
}

🎙️ Whisper (transcription)

const transcript = await openai.audio.transcriptions.create({
  file: fs.createReadStream("audio.mp3"),
  model: "whisper-1",
  language: "bn"
});

🎨 DALL-E

const image = await openai.images.generate({
  model: "dall-e-3",
  prompt: "Sunset over Padma river",
  size: "1024x1024",
  quality: "standard",
  n: 1
});

💰 Cost Tracking

  • Response-এ usage field — token count
  • Calculate cost: tokens × price/1M
  • Log to DB for monitoring
  • Set spending limit in dashboard
  • Alert webhook on threshold

⚠️ Best Practice

  • Retry with exponential backoff (429, 500)
  • Timeout set (30-60s)
  • Error handling explicit
  • Log request_id for debugging
  • Rate limit per user implement
  • Cache repeat queries
  • Test environment-এ separate key

🔄 Batch API (50% off)

// JSONL file with requests, async result in 24h
{
  "custom_id": "1",
  "method": "POST",
  "url": "/v1/chat/completions",
  "body": { "model": "gpt-4o-mini", "messages": [...] }
}
// Upload → batch.create → poll → retrieve