OpenAI vision with URL images in Python

Discover how to use OpenAI API to create chat completions with text and images given from URL in Python. This notebook covers creating API request with user message and URL image and printing the response.

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

All of the required packages are imported automatically by MLJAR Studio.

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

Create the 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 send the below image to the OpenAI model and ask a question about it.

Banner from article

The image is from one of our articles. If you are interested, here is a link: How to create Dashboard in Python from PostgreSQL

# create api request
response = client.chat.completions.create(
  model="gpt-4o",
  messages=[
    {
      "role": "user",
      "content": [
        {"type": "text", "text": "What is in the image?"},
        {
          "type": "image_url",
          "image_url": {"url": "https://mljar.com/blog/postgresql-dashboard-python/banner.jpg"}
        }
      ]
    }
  ],
  max_tokens=300
)

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

Conclusions

As you can see, chatting with the OpenAI model has much more options than you think. Follow us to see more of them.

Recipes used in the openai-vision-url-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-url-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