MulaiMulai sekarang secara gratis

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.

Latihan ini adalah bagian dari kursus

Working with the OpenAI API

Lihat Kursus

Petunjuk latihan

  • Extract the content from the response, which is nested inside the message attribute.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

client = OpenAI(api_key="")

response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "user", "content": "Quick productivity tip."}]
)

# Extract the content from the response
print(response.choices[0].____.____)
Edit dan Jalankan Kode