anasfik / openai

Dart/Flutter SDK for ChatGPT and all OpenAI APIs (GPT, Dall-e..)
https://pub.dev/packages/dart_openai
MIT License
539 stars 151 forks source link

chat with image_url bug #176

Open kobotis opened 1 month ago

kobotis commented 1 month ago

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 commented 1 month ago
// 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 commented 1 month ago

Any update under this issue?

reedmi commented 3 weeks ago

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 commented 3 weeks ago

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

razyalov commented 3 weeks ago

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

UntitledApps commented 2 weeks ago

please @anasfik release this to pub.dev

UntitledApps commented 2 weeks ago

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 commented 1 week ago

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 commented 1 week ago

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