Skip to content

Commit 1ac6f7e

Browse files
authored
Update Microsoft.Extensions.AI to 9.1.0-preview.1.25064.3 and incorporate video blocks (#3607)
1 parent be07f59 commit 1ac6f7e

File tree

5 files changed

+87
-9
lines changed

5 files changed

+87
-9
lines changed

extensions/src/AWSSDK.Extensions.Bedrock.MEAI/AWSSDK.Extensions.Bedrock.MEAI.NetFramework.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
</Choose>
3838

3939
<ItemGroup>
40-
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.0.1-preview.1.24570.5" />
40+
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.1.0-preview.1.25064.3" />
4141
</ItemGroup>
4242

4343
<ItemGroup>

extensions/src/AWSSDK.Extensions.Bedrock.MEAI/AWSSDK.Extensions.Bedrock.MEAI.NetStandard.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
</Choose>
4242

4343
<ItemGroup>
44-
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.0.1-preview.1.24570.5" />
44+
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.1.0-preview.1.25064.3" />
4545
</ItemGroup>
4646

4747
<ItemGroup>

extensions/src/AWSSDK.Extensions.Bedrock.MEAI/AWSSDK.Extensions.Bedrock.MEAI.nuspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
<group targetFramework="net472">
1616
<dependency id="AWSSDK.Core" version="4.0.0.0-preview.5" />
1717
<dependency id="AWSSDK.BedrockRuntime" version="4.0.0.0-preview.5" />
18-
<dependency id="Microsoft.Extensions.AI.Abstractions" version="9.0.1-preview.1.24570.5" />
18+
<dependency id="Microsoft.Extensions.AI.Abstractions" version="9.1.0-preview.1.25064.3" />
1919
</group>
2020
<group targetFramework="netstandard2.0">
2121
<dependency id="AWSSDK.Core" version="4.0.0.0-preview.5" />
2222
<dependency id="AWSSDK.BedrockRuntime" version="4.0.0.0-preview.5" />
23-
<dependency id="Microsoft.Extensions.AI.Abstractions" version="9.0.1-preview.1.24570.5" />
23+
<dependency id="Microsoft.Extensions.AI.Abstractions" version="9.1.0-preview.1.25064.3" />
2424
</group>
2525
<group targetFramework="net8.0">
2626
<dependency id="AWSSDK.Core" version="4.0.0.0-preview.5" />
2727
<dependency id="AWSSDK.BedrockRuntime" version="4.0.0.0-preview.5" />
28-
<dependency id="Microsoft.Extensions.AI.Abstractions" version="9.0.1-preview.1.24570.5" />
28+
<dependency id="Microsoft.Extensions.AI.Abstractions" version="9.1.0-preview.1.25064.3" />
2929
</group>
3030
</dependencies>
3131
</metadata>

extensions/src/AWSSDK.Extensions.Bedrock.MEAI/BedrockChatClient.cs

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,19 @@ public async Task<ChatCompletion> CompleteAsync(
9898
result.Contents.Add(new TextContent(text));
9999
}
100100

101-
if (content.Image is { Source.Bytes: { } bytes, Format.Value: { } formatValue })
101+
if (content.Image is { Source.Bytes: { } imageBytes, Format: { } imageFormat })
102102
{
103-
result.Contents.Add(new ImageContent(bytes.ToArray(), $"image/{formatValue}"));
103+
result.Contents.Add(new ImageContent(imageBytes.ToArray(), GetMimeType(imageFormat)));
104+
}
105+
106+
if (content.Video is { Source.Bytes: { } videoBytes, Format: { } videoFormat })
107+
{
108+
result.Contents.Add(new DataContent(videoBytes.ToArray(), GetMimeType(videoFormat)));
109+
}
110+
111+
if (content.Document is { Source.Bytes: { } documentBytes, Format: { } documentFormat })
112+
{
113+
result.Contents.Add(new DataContent(documentBytes.ToArray(), GetMimeType(documentFormat)));
104114
}
105115

106116
if (content.ToolUse is { } toolUse)
@@ -344,11 +354,22 @@ private static List<ContentBlock> CreateContents(ChatMessage message)
344354
}
345355
});
346356
}
357+
else if (GetVideoFormat(dc.MediaType) is VideoFormat videoFormat)
358+
{
359+
contents.Add(new()
360+
{
361+
Video = new()
362+
{
363+
Source = new() { Bytes = new(dc.Data!.Value.ToArray()) },
364+
Format = videoFormat,
365+
}
366+
});
367+
}
347368
else if (GetDocumentFormat(dc.MediaType) is DocumentFormat docFormat)
348369
{
349370
contents.Add(new()
350371
{
351-
Document = new DocumentBlock()
372+
Document = new()
352373
{
353374
Source = new() { Bytes = new(dc.Data!.Value.ToArray()) },
354375
Format = docFormat,
@@ -414,6 +435,22 @@ private static List<ContentBlock> CreateContents(ChatMessage message)
414435
_ => null,
415436
};
416437

438+
/// <summary>Gets the MIME type for a <see cref="DocumentFormat"/>.</summary>
439+
private static string? GetMimeType(DocumentFormat? format) =>
440+
format?.Value switch
441+
{
442+
"csv" => "text/csv",
443+
"html" => "text/html",
444+
"md" => "text/markdown",
445+
"txt" => "text/plain",
446+
"pdf" => "application/pdf",
447+
"doc" => "application/msword",
448+
"docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
449+
"xls" => "application/vnd.ms-excel",
450+
"xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
451+
_ => null,
452+
};
453+
417454
/// <summary>Gets the <see cref="ImageFormat"/> for the specified MIME type.</summary>
418455
private static ImageFormat? GetImageFormat(string? mediaType) =>
419456
mediaType switch
@@ -425,6 +462,47 @@ private static List<ContentBlock> CreateContents(ChatMessage message)
425462
_ => null,
426463
};
427464

465+
/// <summary>Gets the MIME type for a <see cref="ImageFormat"/>.</summary>
466+
private static string? GetMimeType(ImageFormat? format) =>
467+
format?.Value switch
468+
{
469+
"jpeg" => "image/jpeg",
470+
"png" => "image/png",
471+
"gif" => "image/gif",
472+
"webp" => "image/webp",
473+
_ => null,
474+
};
475+
476+
/// <summary>Gets the <see cref="VideoFormat"/> for the specified MIME type.</summary>
477+
private static VideoFormat? GetVideoFormat(string? mediaType) =>
478+
mediaType switch
479+
{
480+
"video/x-flv" => VideoFormat.Flv,
481+
"video/x-matroska" => VideoFormat.Mkv,
482+
"video/quicktime" => VideoFormat.Mov,
483+
"video/mp4" => VideoFormat.Mp4,
484+
"video/mpeg" => VideoFormat.Mpeg,
485+
"video/3gpp" => VideoFormat.Three_gp,
486+
"video/webm" => VideoFormat.Webm,
487+
"video/x-ms-wmv" => VideoFormat.Wmv,
488+
_ => null,
489+
};
490+
491+
/// <summary>Gets the MIME type for a <see cref="VideoFormat"/>.</summary>
492+
private static string? GetMimeType(VideoFormat? format) =>
493+
format?.Value switch
494+
{
495+
"flv" => "video/x-flv",
496+
"mkv" => "video/x-matroska",
497+
"mov" => "video/quicktime",
498+
"mp4" => "video/mp4",
499+
"mpeg" or "mpg" => "video/mpeg",
500+
"three_gp" => "video/3gpp",
501+
"webm" => "video/webm",
502+
"wmv" => "video/x-ms-wmv",
503+
_ => null,
504+
};
505+
428506
/// <summary>Converts a <see cref="Dictionary{String, Object}"/> to a <see cref="Document"/>.</summary>
429507
private static Document DictionaryToDocument(IDictionary<string, object?>? arguments)
430508
{

extensions/test/BedrockMEAITests/BedrockMEAITests.NetFramework.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</PropertyGroup>
1919

2020
<ItemGroup>
21-
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.0.1-preview.1.24570.5" />
21+
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.1.0-preview.1.25064.3" />
2222
<PackageReference Include="xunit" Version="2.9.2" />
2323
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
2424
</ItemGroup>

0 commit comments

Comments
 (0)