Digging into the response
One key skill in working with APIs is extracting the right data from a structured response. Now, you'll practice retrieving the necessary text from an OpenAI API response.
The OpenAI class has already been imported for you from the openai library.
Deze oefening maakt deel uit van de cursus
Working with DeepSeek in Python
Oefeninstructies
- Extract the
contentfrom the response, which is nested inside themessageattribute.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
client = OpenAI(api_key="", base_url="https://api.together.xyz/v1")
response = client.chat.completions.create(
model="deepseek-ai/DeepSeek-V3",
messages=[{"role": "user", "content": "Quick, one-sentence productivity tip."}]
)
# Extract the content from the response
print(response.choices[0].____.____)