A chat-completions call built and read by hand
The fake server checks the key and builds a reply of the real shape. Java uses the course's Json helper; real Java projects use Jackson.
Calling a model means sending an HTTPS POST with a JSON body (model name, messages, limits) and reading a JSON reply. SDKs wrap this request; knowing the raw shape lets you debug, switch providers and call any model from any language.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write build_request(question) that returns the request dictionary for a one-message chat to model small-model with max_tokens 50, and print it as compact JSON.
Write describe(status, body_text) that returns "ok: <content>" for status 200 (read choices[0].message.content) and "error <status>: <message>" otherwise (read error.message).
Explain it without notes
What is inside a chat-completions request, and what comes back?
Why can the same client code often talk to several providers?
Why should code check the status code before reading choices?
Expected output
POST /v1/chat/completions
request body: {"model":"small-model","messages":[{"role":"user","content":"What is the capital of France?"}],"max_tokens":50}
status: 200
response body: {"id":"chatcmpl-001","object":"chat.completion","model":"small-model","choices":[{"index":0,"message":{"role":"assistant","content":"Paris."},"finish_reason":"stop"}],"usage":{"prompt_tokens":8,"completion_tokens":2,"total_tokens":10}}
answer: Paris.
finish_reason: stop
tokens: 8 in, 2 out
wrong key -> 401: Invalid API key