使用 URL 中的图像进行提示
您正在为一家在线市场构建自动化商品目录系统。系统需要分析商品图片,并为目录生成准确的描述。您将使用基于角色的消息,将图像 URL 发送给模型,并请求同时给出分类和描述。
一张冬季夹克照片的 URL 字符串已存为 image_url,并且已为您定义了 visualize_image() 函数,用于将图像与生成的描述进行对照。
本练习是课程的一部分
使用 OpenAI Responses API
练习说明
- 添加一条用户消息,其中同时包含请求对商品进行分类和描述的文本提示,以及使用合适内容类型传入的图像 URL(
image_url)。 - 使用您的
messages列表向"gpt-5.4-mini"模型创建请求,并提取输出文本。
交互式实操练习
通过完成这段示例代码来试试这个练习。
messages = [{"role": "system", "content": "You are a product cataloging expert who provides concise classifications and descriptions."}]
# Add user message with text and image
messages.append({
"role": "____",
"content": [
{"type": "____", "____": "Classify this product and write a brief but punchy description for our catalog."},
{"type": "____", "image_url": "____"}
]
})
# Create the response
response = ____
print(response.output_text)
visualize_image(image_url)