OpenAI vision with local images in Python

Learn how to use local images in OpenAI's Chat Completions API. This notebook covers encoding images, creating a payload with text and image content, making the API request, and printing the response content.

This notebook was created with MLJAR Studio

MLJAR Studio is Python code editior with interactive code recipes and local AI assistant.
You have code recipes UI displayed at the top of code cells.

Documentation

Don't worry about imports. MLJAR Studio do the job for you :)

# import packages
import os
from dotenv import load_dotenv
from openai import OpenAI, AuthenticationError
import base64
import requests

OpenAI client connection:

# load .env file
load_dotenv()

# get api key from environment
api_key = os.environ["OPENAI_KEY"]

# create OpenAI client
def create_client(api_key):
    try:
        client = OpenAI(api_key=api_key)
        client.models.list()
        return client
    except AuthenticationError:
        print("Incorrect API")
    return None

client = create_client(api_key)

In this example we will use this image from our computer:

Image of flower

This recipe allows to create chat completion with text and image (vision):

# set image path
image_path = r"../../../../demo/flower.jpeg"

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

base64_image = encode_image(image_path)

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

# create payload
payload = {
  "model": "gpt-4o",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "What is this flower and how can i care about it?"
        },
        {
          "type": "image_url",
          "image_url": { "url": f"data:image/jpeg;base64,{base64_image}"}
        }
      ]
    }
  ],
  "max_tokens": 600
}

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

# print response
print(response.json()["choices"][0]["message"]["content"])

Conclusions

Now you know that you can use both URL and local images to create a chat completion. Stay tuned for more!

Recipes used in the openai-vision-local-image.ipynb

All code recipes used in this notebook are listed below. You can click them to check their documentation.

Packages used in the openai-vision-local-image.ipynb

List of packages that need to be installed in your Python environment to run this notebook. Please note that MLJAR Studio automatically installs and imports required modules for you.

openai>=1.35.14

python-dotenv>=1.0.1

requests>=2.31.0