Command Palette

Search for a command to run...

Hectal
Phase 2: Calling a Model
LLM Engineering · Exercise 1 of 6Next
Beginner

Build the request, read the reply

Write build_body(model, system, user, max_tokens) that returns the chat-completions request as a dictionary with keys model, messages (a system message first, then the user message) and max_tokens.

Write read_reply(status, text). For status 200 return choices[0].message.content from the JSON in text. For any other status raise ValueError with the message "HTTP <status>: <error message>", taking the message from error.message in the JSON body.

What Check tests

  1. build_body has the model, a system message then a user message, and max_tokens
  2. read_reply returns the content on status 200
  3. A 401 raises ValueError("HTTP 401: Invalid API key")
  4. A 429 uses its own message

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.