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

chat with image_url bug #176

Open
kobotis opened this issue May 22, 2024 · 12 comments
Open

chat with image_url bug #176

kobotis opened this issue May 22, 2024 · 12 comments

Comments

@kobotis
Copy link

kobotis commented May 22, 2024

this is sample format from Openai Docs
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What’s in this image?"},
{
"type": "image_url",
"image_url": {
"url": "https://placehold.co/600x400",
},
},
],
}
],
is sample must be
{"type": "imageurl" , "image_url" : { "url": " "https://placehold.co/600x400"}

but dart_openai generates
{"type":"image_url","image_url":"https://placehold.co/600x400"}

so OpenAI api says error
Unhandled Exception: RequestFailedException(message: Invalid type for 'messages[2].content[1].image_url': expected an object, but got a string instead., statusCode: 400

dart_openai version is 5.1.0

@kobotis
Copy link
Author

kobotis commented May 22, 2024

// ignore_for_file: public_member_api_docs, sort_constructors_first
/// {@template openai_chat_completion_choice_message_content_item_model}
/// This represents the content item of the [OpenAIChatCompletionChoiceMessageModel] model of the OpenAI API, which is used in the [OpenAIChat] methods.
/// {@endtemplate}
class OpenAIChatCompletionChoiceMessageContentItemModel {
  /// The type of the content item.
  final String type;

  /// The text content of the item.
  final String? text;

  /// The image url content of the item.
  final Map<String, String>? imageUrl;

  @override
  int get hashCode => type.hashCode ^ text.hashCode ^ imageUrl.hashCode;

  /// {@macro openai_chat_completion_choice_message_content_item_model}
  OpenAIChatCompletionChoiceMessageContentItemModel._({
    required this.type,
    this.text,
    this.imageUrl,
  });

  /// This is used to convert a [Map<String, dynamic>] object to a [OpenAIChatCompletionChoiceMessageContentItemModel] object.
  factory OpenAIChatCompletionChoiceMessageContentItemModel.fromMap(
    Map<String, dynamic> asMap,
  ) {
    return OpenAIChatCompletionChoiceMessageContentItemModel._(
      type: asMap['type'],
      text: asMap['text'],
      imageUrl: asMap['image_url'] != null ? {"url": asMap['image_url']} : null,
    );
  }

  /// Represents a text content item factory, which is used to create a text [OpenAIChatCompletionChoiceMessageContentItemModel].
  factory OpenAIChatCompletionChoiceMessageContentItemModel.text(String text) {
    return OpenAIChatCompletionChoiceMessageContentItemModel._(
      type: 'text',
      text: text,
    );
  }

  /// Represents a image content item factory, which is used to create a image [OpenAIChatCompletionChoiceMessageContentItemModel].
  factory OpenAIChatCompletionChoiceMessageContentItemModel.imageUrl(
    String imageUrl,
  ) {
    return OpenAIChatCompletionChoiceMessageContentItemModel._(
      type: 'image_url',
      imageUrl: {"url": imageUrl},
    );
  }

  /// 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,
    };
  }

  @override
  bool operator ==(
    covariant OpenAIChatCompletionChoiceMessageContentItemModel other,
  ) {
    if (identical(this, other)) return true;

    return other.type == type &&
        other.text == text &&
        other.imageUrl == imageUrl;
  }

  @override
  String toString() => switch (type) {
        'text' =>
          'OpenAIChatCompletionChoiceMessageContentItemModel(type: $type, text: $text)',
        'image_url' =>
          'OpenAIChatCompletionChoiceMessageContentItemModel(type: $type, imageUrl: $imageUrl)',
        _ => 'OpenAIChatCompletionChoiceMessageContentItemModel(type: $type)',
      };
}

i changed \lib\src\core\models\chat\sub_models\choices\sub_models\sub_models\content.dart
and it works

@hfranco346
Copy link

Any update under this issue?

@reedmi
Copy link

reedmi commented Jun 7, 2024

The latest version 5.1.0 still has this issue. When is the next version expected to be updated?

https://pub.dev/packages/dart_openai/versions

@hnvmeta
Copy link

hnvmeta commented Jun 7, 2024

I have met this issue when I use model 'gpt-4o'.

@razyalov
Copy link

razyalov commented Jun 9, 2024

any update on this issue? Seems like the changes from pr 166 didn't make it to the released version

@untitledapps
Copy link

please @anasfik release this to pub.dev

@untitledapps
Copy link

untitledapps commented Jun 11, 2024

For anyone needing encountering this problem, the temporary fix is to fork the package and then apply this fix Link. Has to be done sadly.

@missionnowin
Copy link

For anyone needing encountering this problem, the temporary fix is to use to "gpt-4-vision-preview". Using that model it works for me.

This temprorary fix now not working for anyone who intersted in it. It return error 404 and error message "The model gpt-4-vision-preview has been deprecated, learn more here: https://platform.openai.com/docs/deprecations"

@untitledapps
Copy link

For anyone needing encountering this problem, the temporary fix is to use to "gpt-4-vision-preview". Using that model it works for me.

This temprorary fix now not working for anyone who intersted in it. It return error 404 and error message "The model gpt-4-vision-preview has been deprecated, learn more here: https://platform.openai.com/docs/deprecations"

Updated my reply

@bhavika-mamtora
Copy link

Is there any solution for this issue ?

@franboladoruiz
Copy link

I migrated to https://pub.dev/packages/openai_dart

@novalain
Copy link

How is this not fixed yet, the fix is simple? Also they deprecated the working model gpt-4-vision-preview so now there is no workaround. Will migrate to the package mentioned above

burce-coder added a commit to burce-coder/openai that referenced this issue Oct 23, 2024
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

10 participants