CommencerCommencer gratuitement

Prompting with Local Image Files

You're working as a data analyst for London's transportation department. Your team has created a visualization showing the number of vehicles on the roads at various times across different modes of transportation, and you want to use an AI model to extract key insights from it.

The image is stored locally as "LDN_2024_traffic.png".


Image and Data Credit: City Streets 2025 Summary Report by the City of London.

Cet exercice fait partie du cours

Working with the OpenAI Responses API

Afficher le cours

Instructions

  • Import the base64 module to encode the image file.
  • Encode the image file as base64 using the b64encode() function from base64, storing the result in image_base64.
  • Complete the image input message in the request to indicate the use of base64 and using the base64 encodings.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# 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-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)
Modifier et exécuter le code