开始使用免费开始使用

提取输出条目及其内容

使用 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)
编辑并运行代码