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/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