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
build_bodyhas the model, a system message then a user message, andmax_tokensread_replyreturns the content on status 200- A 401 raises
ValueError("HTTP 401: Invalid API key") - 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.