You need to convert the image to a base64 encoded string, this is necessary because the API expects the image data in a text format :
import base64
def image_to_base64(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
Then create the payload for the API request. Include the base64 encoded image in the payload :
import requests
def create_payload(base64_image):
return {
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is in this image?"
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
]
}
Send the request to the Assistants API endpoint.
def send_request(payload, api_key):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
return response.json()
# Example usage
image_path = "path_to_your_image.jpg"
base64_image = image_to_base64(image_path)
payload = create_payload(base64_image)
api_key = "your_api_key_here"
response = send_request(payload, api_key)
print(response)
The response will contain the model's analysis of the image. You can extract and display this information as needed.
def handle_response(response):
if 'choices' in response:
for choice in response['choices']:
print(choice['message']['content'])
else:
print("No response received.")
handle_response(response)
If you are using Azure AI services, the process is similar but involves using Azure-specific endpoints and authentication methods. Ensure you have the correct endpoint and API key from your Azure portal.
def send_request_azure(payload, api_key, endpoint):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
response = requests.post(endpoint, headers=headers, json=payload)
return response.json()
# Example usage for Azure
azure_endpoint = "your_azure_endpoint_here"
response_azure = send_request_azure(payload, api_key, azure_endpoint)
print(response_azure)