Using gpt-4-vision-preview with the API and how to upload images to chat GPT using Python Code

I am running this code in Visual Studio Code, but having the error below that my API is not supported for gpt-4-vision-preview. I am not sure if the code is correct and how to link my API with

import openai
import base64
import requests

OpenAI API Key

api_key = “API_KEY”

Function to encode the image

def encode_image(image_path):
with open(image_path, “rb”) as image_file:
return base64.b64encode(‘utf-8’)

Path to your image

image_path = “Path_of_the_image.jpeg”

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 =“completions”, headers=headers, json=payload)


{‘error’: {‘message’: 'The model gpt-4-vision-preview does not exist or you do not have access to it.}}

Have you put at least $5 into the API for credits?

Rate limits - OpenAI API

You need to be in at least tier 1 to use the vision API, or any other GPT-4 models.

I am able to link it with Python and get the reply, thank you so much.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.