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

OpenAIChatCompletionChoiceMessageContentItemModel.imageUrl Not working properly #172

Open
gianlucasama opened this issue Apr 29, 2024 · 2 comments

Comments

@gianlucasama
Copy link

gianlucasama commented Apr 29, 2024

Following code:

String encodedImage = await loadImage(imageFile);
final message = OpenAIChatCompletionChoiceMessageModel(
  content: [
    OpenAIChatCompletionChoiceMessageContentItemModel.imageUrl(
      'data:image/jpeg;base64,$encodedImage',
    ),
  ],
  role: OpenAIChatMessageRole.user,
);
final chatCompletion = await OpenAI.instance.chat.create(
  model: 'gpt-4-turbo',
  seed: 0,
  messages: [message], 
  temperature: 1,
  maxTokens: 512,
);

Throws:

RequestFailedException (RequestFailedException(message: Invalid type for 'messages[0].content[0].image_url': expected an object, but got a string instead., statusCode: 400))

Solved by changing OpenAIChatCompletionChoiceMessageContentItemModel method:

/// This method used to convert the [OpenAIChatCompletionChoiceMessageContentItemModel] to a [Map<String, dynamic>] object.
  Map<String, dynamic> toMap() {
    return {
      "type": type,
      if (text != null) "text": text,
      if (imageUrl != null) "image_url": imageUrl,
    };
  }

Into:

/// This method used to convert the [OpenAIChatCompletionChoiceMessageContentItemModel] to a [Map<String, dynamic>] object.
  Map<String, dynamic> toMap() {
    return {
      "type": type,
      if (text != null) "text": text,
      if (imageUrl != null) "image_url": {"url": imageUrl},
    };
  }

Following https://platform.openai.com/docs/guides/vision

If you need a pull request for this let me know :)
(Just noticed there is already a pull request for this)

@anasfik
Copy link
Owner

anasfik commented May 8, 2024

I believe the issue is resolved in #166, will update the package also today, can you check?

@gianlucasama
Copy link
Author

gianlucasama commented May 15, 2024

Yes, I can check it. I was also looking into adding the "detail" parameter so that people can manually switch between the "low" and "high" image processing schemes provided by OpenAI. From what they say the "auto" value is default: it decides automatically based on input image size. I still think we need manual control though. Being sure about input image pricing can be important.

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

2 participants