開始使用免費開始

擷取輸出項目與其內容

使用 OpenAI 的 Responses API 時,不同的項目型別會包含不同的資訊。系統已建立一個回應物件並存入變數 response。你的任務是迴圈走訪輸出項目,並依項目型別擷取特定資訊。

本練習屬於課程

使用 OpenAI Responses API

檢視課程

練習說明

  • 迴圈走訪 response.output 裡的項目。
  • 檢查項目的 .type 屬性是否等於 'reasoning',若是,印出 'Found reasoning item'
  • 檢查項目的 .type 屬性是否等於 'message',若是,擷取並印出文字內容。

動手互動練習

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

# Loop through the output items
for item in ____:
    # Check for reasoning items
    if ____ == 'reasoning':
        print('Found reasoning item')
    
    # Check for message items and extract text
    if item.type == 'message':
        message_text = item.____[0].____
        print(message_text)
編輯並執行程式碼