ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

ChatGPT & LLM AI Engineering Guide• 6 min read

Calling LLM APIs in JavaScript & Python

Learn how to integrate OpenAI GPT-4 and Google Gemini APIs into real web applications with streaming responses and JSON schema.

Calling the Chat Completion API

Modern LLM endpoints accept an array of message objects: system (rules & persona), user (the query), and assistant (prior conversation history).

Calling OpenAI API in Node.js / TypeScript
import OpenAI from 'openai';

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

async function generateSummary(text) {
  const completion = await openai.chat.completions.create({
    model: 'gpt-4o-mini',
    messages: [
      { role: 'system', content: 'You are a concise technical editor.' },
      { role: 'user', content: `Summarize this article in 3 bullet points:\n${text}` }
    ],
    temperature: 0.2, // Low temperature for deterministic output
  });

  return completion.choices[0].message.content;
}
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Which API parameter controls randomness, where 0.0 is deterministic and 1.0+ is highly creative?