#Can GPT-4-0125-Preview API Analyze Images?

1 messages · Page 1 of 1 (latest)

verbal galleon
#

Can the chat completions gpt-4-0125-preview endpoint API analyze images? If so what does the code look like in javascript for this?

queen fieldBOT
#

Hi @verbal galleon. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

No, the gpt-4-0125-preview model cannot analyze images. However, OpenAI has introduced a new model called gpt-4-vision-preview that can analyze images. This model is part of the GPT-4 series and is available via the Chat Completions API.

Here is an example of how you can use this model to analyze images in JavaScript:

const axios = require('axios');

async function analyzeImage(base64Image, mimeType, OPENAI_API_KEY) {
    const payload = {
        model: "gpt-4-vision-preview",
        messages: [
            {
                role: "system",
                content: "Analyze the image and provide a description."
            },
            {
                role: "user",
                content: `data:${mimeType};base64,${base64Image}`
            }
        ],
        max_tokens: 3500
    };

    const headers = {
        Authorization: `Bearer ${OPENAI_API_KEY}`,
        "Content-Type": "application/json"
    };

    const response = await axios.post("<https://api.openai.com/v1/chat/completions",> payload, { headers });

    if (response.data.choices && response.data.choices.length > 0) {
        const content = response.data.choices[0].message.content;
        console.log("Response received:", content);
        return content;
    } else {
        throw new Error('No response from OpenAI API');
    }
}

In this code, base64Image is the base64 encoded string of the image, mimeType is the MIME type of the image (like 'image/jpeg' or 'image/png'), and OPENAI_API_KEY is your OpenAI API key. The function analyzeImage sends a POST request to the OpenAI API with the image and returns the description of the image provided by the model.

Please note that you need to install the axios library to use this code. You can install it using npm:

npm install axios