การ Prompt ด้วยไฟล์รูปภาพในเครื่อง
คุณทำงานเป็นนักวิเคราะห์ข้อมูลให้กับหน่วยงานขนส่งของกรุงลอนดอน ทีมของคุณได้สร้างกราฟแสดงจำนวนยานพาหนะบนถนนในช่วงเวลาต่าง ๆ แยกตามประเภทของการเดินทาง และต้องการใช้โมเดล AI เพื่อดึงข้อมูลเชิงลึกสำคัญจากกราฟนี้
รูปภาพถูกจัดเก็บไว้ในเครื่องในชื่อ "LDN_2024_traffic.png"
Image and Data Credit: City Streets 2025 Summary Report by the City of London.
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การทำงานกับ OpenAI Responses API
คำแนะนำการฝึกหัด
- Import โมดูล
base64เพื่อเข้ารหัสไฟล์รูปภาพ - เข้ารหัสไฟล์รูปภาพเป็น base64 โดยใช้ฟังก์ชัน
b64encode()จากbase64แล้วเก็บผลลัพธ์ไว้ในตัวแปรimage_base64 - กำหนดข้อความ input รูปภาพในคำขอให้ครบถ้วน โดยระบุการใช้ 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)