開始使用免費開始

深入剖析回應

使用 API 的關鍵能力之一,是能從結構化回應中擷取正確的資料。現在,你要練習從 OpenAI 的 API 回應中取出需要的文字。

OpenAI 類別已經從 openai 函式庫為你匯入。

本練習屬於課程

使用 Python 操作 DeepSeek

檢視課程

練習說明

  • 從回應中擷取 content,它巢狀在 message 屬性裡。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

client = OpenAI(api_key="", base_url="https://api.together.xyz/v1")

response = client.chat.completions.create(
  model="deepseek-ai/DeepSeek-V4.1-Flash",
  messages=[{"role": "user", "content": "Quick, one-sentence productivity tip."}]
)

# Extract the content from the response
print(response.choices[0].____.____)
編輯並執行程式碼