以本機影像檔進行提示
你在倫敦交通部門擔任資料分析師。你的團隊製作了一張視覺化圖表,呈現不同運輸方式在不同時間的道路車輛數量,你想用 AI 模型從中擷取重點洞見。
影像已本機儲存為 "LDN_2024_traffic.png"。
圖片與資料來源:City of London 的 City Streets 2025 Summary Report(https://www.cityoflondon.gov.uk/assets/Services-Environment/City-Streets-2025-Summary-Report.pdf)。
本練習屬於課程
使用 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)