Giter Site home page Giter Site logo

Comments (8)

lgrammel avatar lgrammel commented on June 1, 2024

The error comes from the OpenAI API. Can you test whether you also receive it when using the OpenAI fetch API directly?

from ai.

lucasishuman avatar lucasishuman commented on June 1, 2024

When I use the OpenAI SDK directly (as shown below) - I get a valid response from OpenAI.

Note - I need to change the message > content > image props to match OpenAI's message schema - maybe there is an issue mapping AI SDK image props to OpenAI image props in the AI SDK?

Using
"openai": "4.44.0"

Sent message (using OpenAI message schema)

    const messages = [
    {
      role: 'user',
      content: [
        {
          type: 'text',
          text: 'What is in this image',
        },
        {
          type: 'image_url',
          image_url: {
            url: 'https://static01.nyt.com/images/2024/05/12/.....',
          },
        },
      ],
    }
];

Route handler

import OpenAI from "openai";

export async function POST(req: Request) {
  const { messages } = await req.json();

  const openai = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY,
  });

  const response = await openai.chat.completions.create({
    model = 'gpt-4-turbo',
    messages,
  });

  console.log(response.choices[0]);
}

I feel like I must be missing something very obvious if no one else sees this behavior - but I also don't see an example in the AI SDK for messages with image content.

My OPENAI_API_KEY is in my env file and is valid.

from ai.

lucasishuman avatar lucasishuman commented on June 1, 2024

Maybe worth also noting is that client-side I am using the useChat hook, manually constructing the user message with image data/URL shown in original post, and then using append to send it to my app API.

from ai.

lucasishuman avatar lucasishuman commented on June 1, 2024

This is related to #1555 because when I remove the data:image/jpeg;base64, from the beginning of my base64 encoded image data string, I get a valid response.

However, when I pass a URL I still get an error.

So part of the fix may be just a matter of providing better documentation for messages with image content, but there's still something funky when sending an image URL.

from ai.

lgrammel avatar lgrammel commented on June 1, 2024

@lucasishuman can you share the img?

from ai.

lucasishuman avatar lucasishuman commented on June 1, 2024

You can use any valid image URL to test, such as...

  const result = await streamText({
    messages: [
      {
        role: 'user',
        content: [
          {
            type: 'text',
            text: 'What’s in this image?',
          },
          {
            type: 'image',
            image:
              'https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg',
          },
        ],
      },
    ],
    model: openai('gpt-4-turbo'),
  });

from ai.

lgrammel avatar lgrammel commented on June 1, 2024

@lucasishuman try using a URL

image: new 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')

See examples/ai-core/src/generate-text/openai-multimodal-url.ts for a full example

from ai.

lucasishuman avatar lucasishuman commented on June 1, 2024

@lgrammel - Great, thanks. That was the missing info I needed.

I feel like adding the image data and image URL examples to the AI SDK docs would be helpful.

You can close this issue.

from ai.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.