Gpt-4 multiple image analyser using php

Hello,

                    I would like to have a php script that can upload multiple images and ask questions based on that. Any code reference available? Just need the php code or curl code to upload multiple images and ask questions based on that. Thanks in advance!

Hi @NTUSER

There is not an official PHP library for the API as of now.

Here’s a list of PHP community libraries for OpenAI API that you could utilise to build it.

Here’s the cURL from docs for multiple image inputs:

curl https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-4-vision-preview",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "What are in these images? Is there any difference between them?"
          },
          {
            "type": "image_url",
            "image_url": {
              "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
            }
          },
          {
            "type": "image_url",
            "image_url": {
              "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
            }
          }
        ]
      }
    ],
    "max_tokens": 300
  }'

Thanks a lot @sps , i have checked but i am facing one issue,


{ "error": { "message": "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please contact us through our help center at help.openai.com.)", "type": "invalid_request_error", "param": null, "code": null } }

$imgpath='doc5_1.jpg';
$bencode=base64_encode($imgpath);
//echo $bencode;exit;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Authorization: Bearer xxxxxxxxxxxxx',
]);
$data='{
    "model": "gpt-4-vision-preview",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "What are in these image?"
          },
          {
            "type": "image_url",
            "image_url": {
              "url": "data:image/jpg;base64,ZG9jNV8xLmpwZw==",
            }
          }
          
        ]
      }
    ],
    "max_tokens": 3000
  }';
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$response = curl_exec($ch);
print_r($response);
curl_close($ch);

This is my current code.

Does the encode method you use to base64_encode images directly read and encode images from file path or do you have to read the image first?

$imgpath='doc5_1.jpg';
$bencode=base64_encode($imgpath);

This is the code i used to encode, i have given the path directly but still not working. Very wierd. Any idea by looking to the error and code i have sent?

"url": "data:image/jpg;base64,ZG9jNV8xLmpwZw==",

the extra , at the end was the issue, the error gone when i removed.