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

Remove ImageContent and AudioContent #5814

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

MackinnonBuck
Copy link
Member

@MackinnonBuck MackinnonBuck commented Jan 24, 2025

Removes the ImageContent and AudioContent types.

The recommendation will be that chat client implementors and app developers use DataContent.MediaType to distinguish between the various content types.

Fixes #5719

Microsoft Reviewers: Open in CodeFlow

@dotnet-comment-bot
Copy link
Collaborator

‼️ Found issues ‼️

Project Coverage Type Expected Actual
Microsoft.Extensions.Caching.Hybrid Line 86 84.76 🔻

🎉 Good job! The coverage increased 🎉
Update MinCodeCoverage in the project files.

Project Expected Actual
Microsoft.Extensions.AI 88 89

Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=928961&view=codecoverage-tab

parts.Add(new ChatMessageImageContentItem(BinaryData.FromBytes(data), imageContent.MediaType));
break;
case DataContent dataContent when dataContent.HasImageMediaType():
if (dataContent.Data is { IsEmpty: false } data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine, but fwiw I find the IsEmpty: false double-negative a little confusing. This could also be:

if (dataContent.ContainsData)
{
    ... dataContent.Data ...
}

/// <param name="content">The <see cref="DataContent"/>.</param>
/// <returns><see langword="true"/> if the content represents an image; otherwise, <see langword="false"/>.</returns>
public static bool HasImageMediaType(this DataContent content)
=> content.MediaType is { } mediaType && mediaType.StartsWith("image/", StringComparison.OrdinalIgnoreCase);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
=> content.MediaType is { } mediaType && mediaType.StartsWith("image/", StringComparison.OrdinalIgnoreCase);
=> content.MediaType?.StartsWith("image/", StringComparison.OrdinalIgnoreCase) is true;

?

If this is something we expect every consumer to need to do, should we bake it in with a helper instance method on DataContent, e.g.

public bool HasMediaTypePrefix(string prefix)

? ("Prefix" isn't ideal naming, but HasMediaTypeType doesn't sound right, either :))

@@ -375,7 +375,7 @@ private IEnumerable<OllamaChatRequestMessage> ToOllamaChatRequestMessages(ChatMe
OllamaChatRequestMessage? currentTextMessage = null;
foreach (var item in content.Contents)
{
if (currentTextMessage is not null && item is not ImageContent)
if (currentTextMessage is not null && item is not DataContent)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What ends up happening to the logic if it's a DataContent but HasImageMediaType below is false? Might we end up coalescing incorrectly / yielding items out of order?

new AudioContent("http://localhost/audio"),
new ImageContent("http://localhost/image"),
new DataContent("http://localhost/audio", mediaType: "audio/mpeg"),
new DataContent("http://localhost/image", mediaType: "image/png"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did these tests actually require providing a media type now when wasn't provided before?

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

Successfully merging this pull request may close these issues.

Revisit DataContent and friends
3 participants