Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example of how to upload image for GPT4 vision in README.md #249

Open
jkyngan opened this issue Nov 10, 2023 · 9 comments
Open

add example of how to upload image for GPT4 vision in README.md #249

jkyngan opened this issue Nov 10, 2023 · 9 comments

Comments

@jkyngan
Copy link

jkyngan commented Nov 10, 2023

First ,thanks for this amazing project.
As GPT-4 vision chat completion endpoint was introduced in v0.7.8,
an update of example in README.md would be great.

@ThibautPV
Copy link

Here is an example :

$result = OpenAI::chat()->create([
        'model' => 'gpt-4-vision-preview',
        'messages' => [
            [
                'role' => 'user',
                'content' => [
                    ['type' => 'text', 'text' => "What’s in this image?"],
                    ['type' => 'image_url', 'image_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' => 900,
    ]);

return $result->choices[0]->message->content;

@MohammadaliMirhamed
Copy link

is it possibe to upload video for it ?

@vesper8
Copy link

vesper8 commented Nov 12, 2023

@MohammadaliMirhamed Vision does not support video, but what people are doing is splitting up their videos into still frames and uploading that.

@aconital
Copy link

Is there a way to upload the images? A lot of times it cannot read an image from a URL and image has to be uploaded.

@ThibautPV
Copy link

ThibautPV commented Nov 12, 2023

If I understood correctly, you need to upload an image to your server.

Then, you can analyze it with GPT Vision

@MohammadaliMirhamed
Copy link

MohammadaliMirhamed commented Nov 20, 2023

I have sent https://m.media-amazon.com/images/I/81nUFx9sXoL._AC_UF894,1000_QL80_.jpg to GPT-4-VISION-PREVIEW
and asked for what time is it . and it responded The time on the clock is 10:10.

@aconital
Copy link

aconital commented Nov 25, 2023

It seems we can directly pass the base64 data to the api instead of url according to the doc here https://platform.openai.com/docs/guides/vision/quick-start. Here is an example:

import base64
import requests

# OpenAI API Key
api_key = "YOUR_OPENAI_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_to_your_image.jpg"

# 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
}

@GilbertrdzDev
Copy link

GilbertrdzDev commented Nov 28, 2023

Here I share the PHP version of @aconital:

function encodeImage($imagePath): string {
    $imageContent = file_get_contents($imagePath);
    return base64_encode($imageContent);
}

$imagePath = '/path/to/image.jpg';
$base64Image = encodeImage($imagePath);

$payload = [
    'model'      => 'gpt-4-vision-preview',
    'messages'   => [
        [
            'role'    => 'user',
            'content' => [
                [
                    'type' => 'text',
                    'text' => "What’s in this image?"
                ],
                [
                    'type' => 'image_url',
                    'image_url' => "data:image/jpeg;base64,$base64Image"
                ],
            ],
        ]
    ],
    'max_tokens' => 200,
];

$result  = OpenAI::chat()->create($payload);

echo "<figure style='font-family:sans-serif;width: 500px'>
         <img style='width: 100%' src='$imagePath' alt=''>
         <figcaption>{$result->choices[0]->message->content}</figcaption>
      </figure>";

Result:

28-11-2023_02-18-07

@LintonAchmad
Copy link

updated payload for php now expecting an object for the image_url - gpt-4o:

$payload = [
            'model'      => 'gpt-4o',
            'messages'   => [
                [
                    'role'    => 'user',
                    'content' => [
                        [
                            'type' => 'text',
                            'text' => "What’s in this image?"
                        ],
                        [
                            'type' => 'image_url',
                            'image_url' => [
                                'url' => "data:image/png;base64,$base64Image"
                            ]
                        ],
                    ],
                ]
            ],
            'max_tokens' => 200,
 ];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants
@vesper8 @aconital @LintonAchmad @MohammadaliMirhamed @jkyngan @GilbertrdzDev @ThibautPV and others