Upload an image to chat gpt using the API?

11.8k views Asked by At

How do you upload an image to chat gpt using the API? Can you give an example of code that can do that?

I've tried looking at the documentation, but they don't have a good way to upload a jpg as context.

1

There are 1 answers

0
Muhammad Mubashirullah Durrani On

The API as of right now mentions two approaches to feed it images:

  1. Give it the URL of the image
  2. Give it the base64 encoded format

Both examples are given in code in their documentation here: https://platform.openai.com/docs/guides/vision

Code from there for the encoded option (best to visit link to stay updated)

import base64
import requests

# OpenAI API Key
api_key = "YOUR_OPENAI_API_KEY"

# Function to encode the image
def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

# Path to your image
image_path = "path_to_your_image.jpg"

# Getting the base64 string
base64_image = encode_image(image_path)

headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {api_key}"
}

payload = {
    "model": "gpt-4-vision-preview",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "What’s in this image?"
          },
          {
            "type": "image_url",
            "image_url": {
              "url": f"data:image/jpeg;base64,{base64_image}"
            }
          }
        ]
      }
    ],
    "max_tokens": 300
}

response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)

print(response.json())

Does this answer your question?