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
gpt-4-vision-preview…
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(image_file.read()).decode(‘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 = requests.post(“completions”, headers=headers, json=payload)
print(response.json())
{‘error’: {‘message’: 'The model gpt-4-vision-preview
does not exist or you do not have access to it.}}