使用本地图像文件进行提示
您在伦敦交通部门担任数据分析师。您的团队制作了一张可视化图,展示了不同交通方式在不同时间段的道路车辆数量。您希望使用一个 AI 模型从中提取关键信息。
该图像本地存储为 "LDN_2024_traffic.png"。
图像与数据来源:City of London 发布的 City Streets 2025 Summary Report。
本练习是课程的一部分
使用 OpenAI Responses API
练习说明
- 导入
base64模块以对图像文件进行编码。 - 使用
base64中的b64encode()将图像文件编码为 base64,并将结果存入image_base64。 - 在请求中补全图像输入消息,指明使用 base64,并使用该 base64 编码。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import base64 module
____
# Encode the image file as base64
with open(image_path, "rb") as f:
image_base64 = base64.____(f.read()).decode("utf-8")
# Create a response with text and image input
response = client.responses.create(
model="gpt-5.4-mini",
input=[
{"role": "user", "content": [
{"type": "input_text", "text": "What mode of transport contributed the highest number of vehicles during business hours? Answer very concisely."},
{"type": "input_image", "image_url": f"data:image/png;____,{____}"}
]}
]
)
print(response.output_text)
visualize_image(image_url)