From 58ae1aeca83e8279009cace1f039bc3cf635241d Mon Sep 17 00:00:00 2001 From: Joseph Daigle Date: Sat, 11 Oct 2025 20:13:52 -0400 Subject: [PATCH 1/2] Add support for prompt caching directives in BedrockChatClient for MEAI Fixes #3930 --- extensions/AWSSDK.Extensions.sln | 7 +++ .../BedrockChatClient.cs | 50 +++++++++++++++++-- .../BedrockMEAITests.NetStandard.csproj | 36 +++++++++++++ .../23b92a6e-3ec2-4193-9ae1-89c242b1e8f6.json | 11 ++++ 4 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 extensions/test/BedrockMEAITests/BedrockMEAITests.NetStandard.csproj create mode 100644 generator/.DevConfigs/23b92a6e-3ec2-4193-9ae1-89c242b1e8f6.json diff --git a/extensions/AWSSDK.Extensions.sln b/extensions/AWSSDK.Extensions.sln index c2d06ce0e53b..847ba401da04 100644 --- a/extensions/AWSSDK.Extensions.sln +++ b/extensions/AWSSDK.Extensions.sln @@ -80,6 +80,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AWSSDK.Extensions.CborProto EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CborProtocol.Tests.NetFramework", "test\CborProtocol.Tests\CborProtocol.Tests.NetFramework.csproj", "{9FFDECAB-94D6-7EB1-1A5B-487492600755}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BedrockMEAITests.NetStandard", "test\BedrockMEAITests\BedrockMEAITests.NetStandard.csproj", "{B4A8EC7D-5EEC-48DE-AB46-A3F9BA5D0340}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -222,6 +224,10 @@ Global {9FFDECAB-94D6-7EB1-1A5B-487492600755}.Debug|Any CPU.Build.0 = Debug|Any CPU {9FFDECAB-94D6-7EB1-1A5B-487492600755}.Release|Any CPU.ActiveCfg = Release|Any CPU {9FFDECAB-94D6-7EB1-1A5B-487492600755}.Release|Any CPU.Build.0 = Release|Any CPU + {B4A8EC7D-5EEC-48DE-AB46-A3F9BA5D0340}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B4A8EC7D-5EEC-48DE-AB46-A3F9BA5D0340}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B4A8EC7D-5EEC-48DE-AB46-A3F9BA5D0340}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B4A8EC7D-5EEC-48DE-AB46-A3F9BA5D0340}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -261,6 +267,7 @@ Global {4FCCBD72-8E9B-D74B-6538-D8684D181230} = {3D822DC2-ED2E-4434-BC4F-CE7FCD846B02} {24CBBC97-409E-BAC8-0553-D260AB0B8C6A} = {3D822DC2-ED2E-4434-BC4F-CE7FCD846B02} {9FFDECAB-94D6-7EB1-1A5B-487492600755} = {A960D001-40B3-4B1A-A890-D1049FB7586E} + {B4A8EC7D-5EEC-48DE-AB46-A3F9BA5D0340} = {A960D001-40B3-4B1A-A890-D1049FB7586E} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {949367A4-5683-4FD3-93F4-A2CEA6EECB21} diff --git a/extensions/src/AWSSDK.Extensions.Bedrock.MEAI/BedrockChatClient.cs b/extensions/src/AWSSDK.Extensions.Bedrock.MEAI/BedrockChatClient.cs index c2f5c6e38ba2..0479bc878232 100644 --- a/extensions/src/AWSSDK.Extensions.Bedrock.MEAI/BedrockChatClient.cs +++ b/extensions/src/AWSSDK.Extensions.Bedrock.MEAI/BedrockChatClient.cs @@ -392,12 +392,31 @@ private static List CreateSystem(List? r if (options?.Instructions is { } instructions) { - system.Add(new SystemContentBlock() { Text = instructions }); + system.Add(new SystemContentBlock() + { + Text = instructions, + }); } - system.AddRange(messages - .Where(m => m.Role == ChatRole.System && m.Contents.Any(c => c is TextContent)) - .Select(m => new SystemContentBlock() { Text = string.Concat(m.Contents.OfType()) })); + foreach (var message in messages + .Where(m => m.Role == ChatRole.System && m.Contents.Any(c => c is TextContent))) + { + system.Add(new SystemContentBlock() + { + Text = string.Concat(message.Contents.OfType()), + }); + + if (message.AdditionalProperties?.TryGetValue(nameof(ContentBlock.CachePoint), out var maybeCachePoint) == true) + { + if (maybeCachePoint is CachePointBlock cachePointBlock) + { + system.Add(new SystemContentBlock() + { + CachePoint = cachePointBlock, + }); + } + } + } return system; } @@ -440,6 +459,17 @@ private static List CreateMessages(List? rawMessages, IEnumera } } + if (chatMessage.AdditionalProperties?.TryGetValue(nameof(ContentBlock.CachePoint), out var maybeCachePoint) == true) + { + if (maybeCachePoint is CachePointBlock cachePointBlock) + { + contents.Add(new() + { + CachePoint = cachePointBlock + }); + } + } + messages.Add(new() { Role = chatMessage.Role == ChatRole.Assistant ? ConversationRole.Assistant : ConversationRole.User, @@ -573,6 +603,18 @@ private static List CreateContents(ChatMessage message) }); break; } + + + if (content.AdditionalProperties?.TryGetValue(nameof(ContentBlock.CachePoint), out var maybeCachePoint) == true) + { + if (maybeCachePoint is CachePointBlock cachePointBlock) + { + contents.Add(new() + { + CachePoint = cachePointBlock + }); + } + } } return contents; diff --git a/extensions/test/BedrockMEAITests/BedrockMEAITests.NetStandard.csproj b/extensions/test/BedrockMEAITests/BedrockMEAITests.NetStandard.csproj new file mode 100644 index 000000000000..cdb86c7b051b --- /dev/null +++ b/extensions/test/BedrockMEAITests/BedrockMEAITests.NetStandard.csproj @@ -0,0 +1,36 @@ + + + netcoreapp3.1;net8.0 + BedrockMEAITests + BedrockMEAITests + + false + false + false + false + false + false + false + false + + true + Latest + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/generator/.DevConfigs/23b92a6e-3ec2-4193-9ae1-89c242b1e8f6.json b/generator/.DevConfigs/23b92a6e-3ec2-4193-9ae1-89c242b1e8f6.json new file mode 100644 index 000000000000..6604ad990b12 --- /dev/null +++ b/generator/.DevConfigs/23b92a6e-3ec2-4193-9ae1-89c242b1e8f6.json @@ -0,0 +1,11 @@ +{ + "extensions": [ + { + "extensionName": "Extensions.Bedrock.MEAI", + "type": "minor", + "changeLogMessages": [ + "Add support for prompt caching in BedrockChatClient for MEAI" + ] + } + ] +} \ No newline at end of file From 58e0d8973e804dc48d01462ec7025397aa6df68b Mon Sep 17 00:00:00 2001 From: Joseph Daigle Date: Mon, 13 Oct 2025 16:23:27 -0400 Subject: [PATCH 2/2] Reverting code changes that require updates to build system. --- extensions/AWSSDK.Extensions.sln | 7 ---- .../BedrockMEAITests.NetStandard.csproj | 36 ------------------- 2 files changed, 43 deletions(-) delete mode 100644 extensions/test/BedrockMEAITests/BedrockMEAITests.NetStandard.csproj diff --git a/extensions/AWSSDK.Extensions.sln b/extensions/AWSSDK.Extensions.sln index 847ba401da04..c2d06ce0e53b 100644 --- a/extensions/AWSSDK.Extensions.sln +++ b/extensions/AWSSDK.Extensions.sln @@ -80,8 +80,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AWSSDK.Extensions.CborProto EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CborProtocol.Tests.NetFramework", "test\CborProtocol.Tests\CborProtocol.Tests.NetFramework.csproj", "{9FFDECAB-94D6-7EB1-1A5B-487492600755}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BedrockMEAITests.NetStandard", "test\BedrockMEAITests\BedrockMEAITests.NetStandard.csproj", "{B4A8EC7D-5EEC-48DE-AB46-A3F9BA5D0340}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -224,10 +222,6 @@ Global {9FFDECAB-94D6-7EB1-1A5B-487492600755}.Debug|Any CPU.Build.0 = Debug|Any CPU {9FFDECAB-94D6-7EB1-1A5B-487492600755}.Release|Any CPU.ActiveCfg = Release|Any CPU {9FFDECAB-94D6-7EB1-1A5B-487492600755}.Release|Any CPU.Build.0 = Release|Any CPU - {B4A8EC7D-5EEC-48DE-AB46-A3F9BA5D0340}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B4A8EC7D-5EEC-48DE-AB46-A3F9BA5D0340}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B4A8EC7D-5EEC-48DE-AB46-A3F9BA5D0340}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B4A8EC7D-5EEC-48DE-AB46-A3F9BA5D0340}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -267,7 +261,6 @@ Global {4FCCBD72-8E9B-D74B-6538-D8684D181230} = {3D822DC2-ED2E-4434-BC4F-CE7FCD846B02} {24CBBC97-409E-BAC8-0553-D260AB0B8C6A} = {3D822DC2-ED2E-4434-BC4F-CE7FCD846B02} {9FFDECAB-94D6-7EB1-1A5B-487492600755} = {A960D001-40B3-4B1A-A890-D1049FB7586E} - {B4A8EC7D-5EEC-48DE-AB46-A3F9BA5D0340} = {A960D001-40B3-4B1A-A890-D1049FB7586E} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {949367A4-5683-4FD3-93F4-A2CEA6EECB21} diff --git a/extensions/test/BedrockMEAITests/BedrockMEAITests.NetStandard.csproj b/extensions/test/BedrockMEAITests/BedrockMEAITests.NetStandard.csproj deleted file mode 100644 index cdb86c7b051b..000000000000 --- a/extensions/test/BedrockMEAITests/BedrockMEAITests.NetStandard.csproj +++ /dev/null @@ -1,36 +0,0 @@ - - - netcoreapp3.1;net8.0 - BedrockMEAITests - BedrockMEAITests - - false - false - false - false - false - false - false - false - - true - Latest - - - - - - - - - - - - - - - - - - - \ No newline at end of file