開始使用免費開始

深入解析回應內容

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

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

本練習屬於課程

Working with the OpenAI API

檢視課程

練習說明

  • 擷取回應中 message 屬性內巢狀的 content

動手互動練習

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

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].____.____)
編輯並執行程式碼