HANDS-ON TASK · 1 OF 5

The OpenAI SDK in Python

Task: Same API, real code

Goal

Rewrite the curl call as a small Python CLI using the preinstalled openai SDK, and save one answer to /root/sdk-reply.txt.

The point of this task: the SDK is not magic. Every line maps straight onto a piece of the curl command you already sent.

Practice

This VM ships Python with the SDK installed (plain python, no venv needed). Create /root/ask.py with exactly this (for example with nano /root/ask.py):

python
import sys
import openai

client = openai.OpenAI()

reply = client.chat.completions.create(
    model="deepseek-v4-flash",
    max_tokens=300,
    messages=[{"role": "user", "content": sys.argv[1]}],
)
print(reply.choices[0].message.content)

Line by line:

  • import sys gives you sys.argv, the list of words you typed on the command line. sys.argv[0] is the script's own name, sys.argv[1] is the first thing after it - your question.
  • import openai is the official OpenAI SDK.
  • client = openai.OpenAI() - the empty parentheses are the good part: the SDK reads OPENAI_API_KEY and OPENAI_BASE_URL from the environment by itself. No key in your code, ever.
  • client.chat.completions.create(...) is the POST /v1/chat/completions from the curl task, dressed as a function call. Compare the arguments to the JSON you sent by hand: model is "model", max_tokens is "max_tokens", messages is "messages" - same names, same meaning.
  • print(reply.choices[0].message.content) is the same path you plucked with jq (.choices[0].message.content), written as Python attributes.

Run it and save the answer:

bash
python /root/ask.py "Give me three uses for an old Raspberry Pi" > /root/sdk-reply.txt
cat /root/sdk-reply.txt

The quotes around the question keep it a single sys.argv[1]. Without them the shell would split it into separate words and your script would only see the first one.

Expected Result

Your one-file CLI answers any question you pass it - that is the entire core of every AI-powered tool you have seen.

Ready To Continue

Press Submit once ask.py exists and /root/sdk-reply.txt has an answer.


Your lab setup

This VM comes preconfigured with your AI keys - just type claude to get started. Your key and URL are already set as $OPENAI_BASE_URL and $OPENAI_API_KEY.

Start the lab on the right to run checks.

Pass the checks to continue
Spin up a fresh environment and practice live.
linux-basic-ai · fresh machine · ready in under a minute