Understanding OpenAI and ChatGPT with Node.js
In July 2020, OpenAI introduced GPT-3 in the paper "Language Models are Few-Shot Learners", a model that Stanford University's article "How Large Language Models Will Transform Science, Society, and AI" noted was the largest known language model at the time. Base GPT-3 predicts the next token in a document, which works for raw text completion, summarization, translation, classification, and sentiment analysis, but often wanders when a user expects a back-and-forth conversation.
When OpenAI launched ChatGPT in November 2022, it took the GPT-3.5 model family and fine-tuned it with supervised dialogue and reinforcement learning. That training lets ChatGPT track follow-up questions across turns, challenge faulty premises, admit mistakes, and refuse unsafe requests.
Where ChatGPT fits in OpenAI's model lineup
Alongside ChatGPT and earlier reinforcement-learning environments like OpenAI Gym and OpenAI Universe, OpenAI ships specialized models across code, speech, and vision. Codex translates English prompts into working source code. Whisper is an automatic speech recognition network trained on 680,000 hours of multilingual web audio, powering tools like generating YouTube subtitles for any video. DALL-E generates images from natural language captions, while CLIP learns visual concepts from text supervision so it can classify images zero-shot just by reading the category names.
Even with dialogue tuning, ChatGPT has sharp failure modes you need to account for when building on top of OpenAI's models. Its training corpus stops in 2021, it states fabricated facts with complete confidence, it leans on repetitive stock phrasing, and its refusal boundary is brittle enough that rewording a rejected question slightly will often make the model answer it.
Create an OpenAI account and API key
To call OpenAI's models from Node.js, create an account on the OpenAI signup page.
Once signed in, generate a secret API key on the OpenAI API keys page.
Install the Node.js library and choose a GPT-3 model
In your Node.js project directory, install the official openai package:
npm install openai
The GPT-3 model documentation exposes four model tiers that trade capability against speed and cost, with Davinci as the most capable model and Ada as the fastest:
Requests to the OpenAI completions endpoint take your model, prompt, temperature, and max_tokens parameters in the request body:
Run the Node.js pet name generator
Clone OpenAI's Node.js quickstart repository to run a working server-side app:
git clone https://github.com/openai/openai-quickstart-node.git
Move into the project directory and copy .env.example to .env:
cd openai-quickstart-node
cp .env.example .env
Paste the secret key you created earlier as OPENAI_API_KEY inside .env. Keep every OpenAI request on the server side; calling the API directly from browser JavaScript exposes your secret key to anyone who inspects network traffic.
Install the dependencies and start the development server:
npm install
npm run dev
Open http://localhost:3000 in your browser to test the pet name generator:
If you run into questions while wiring up the API, reach out on Twitter.