Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions extensions/src/AWSSDK.Extensions.Bedrock.MEAI/BedrockChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,31 @@ private static List<SystemContentBlock> CreateSystem(List<SystemContentBlock>? 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<TextContent>()) }));
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<TextContent>()),
});

if (message.AdditionalProperties?.TryGetValue(nameof(ContentBlock.CachePoint), out var maybeCachePoint) == true)
{
if (maybeCachePoint is CachePointBlock cachePointBlock)
{
system.Add(new SystemContentBlock()
{
CachePoint = cachePointBlock,
});
}
}
}

return system;
}
Expand Down Expand Up @@ -440,6 +459,17 @@ private static List<Message> CreateMessages(List<Message>? 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,
Expand Down Expand Up @@ -573,6 +603,18 @@ private static List<ContentBlock> 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;
Expand Down
11 changes: 11 additions & 0 deletions generator/.DevConfigs/23b92a6e-3ec2-4193-9ae1-89c242b1e8f6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extensions": [
{
"extensionName": "Extensions.Bedrock.MEAI",
"type": "minor",
"changeLogMessages": [
"Add support for prompt caching in BedrockChatClient for MEAI"
]
}
]
}