这是indexloc提供的服务,不要输入任何密码
Skip to content

OpenAIChatCompletionChoiceMessageContentItemModel.imageUrl Not working properly #172

@gianlucasama

Description

@gianlucasama

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions