使用 Items 進行自訂處理
你正在建立一個需要提供詳細記錄以便偵錯的聊天機器人。這個聊天機器人使用具備 reasoning 能力的模型,你想要建立自訂的輸出訊息,清楚區分推理摘要與助理回覆。你手上有一個先前 API 呼叫得到的 response 物件,其中包含多個輸出項目。
本練習屬於課程
使用 OpenAI Responses API
練習說明
- 針對
response.output中的每個項目進行迴圈。 - 檢查項目型別是否為
'reasoning',若存在摘要則印出格式化訊息顯示推理摘要。 - 檢查項目型別是否為
'message',並印出格式化訊息,包含助理的文字輸出。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Loop through each item in the response output
for item in response.____:
# Check if the item is a reasoning item
if item.____ == '____':
if item.____:
print(f"Reasoning: {item.____[0]}")
else:
print("No reasoning summary found.")
# Check if the item is a message item
if item.____ == 'message':
print(f"Assistant: {item.____[0].text}")