Skip to content

Commit 3975b0b

Browse files
authored
Add support for prompt caching directives in BedrockChatClient for MEAI
1 parent e69299b commit 3975b0b

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

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

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,31 @@ private static List<SystemContentBlock> CreateSystem(List<SystemContentBlock>? r
392392

393393
if (options?.Instructions is { } instructions)
394394
{
395-
system.Add(new SystemContentBlock() { Text = instructions });
395+
system.Add(new SystemContentBlock()
396+
{
397+
Text = instructions,
398+
});
396399
}
397400

398-
system.AddRange(messages
399-
.Where(m => m.Role == ChatRole.System && m.Contents.Any(c => c is TextContent))
400-
.Select(m => new SystemContentBlock() { Text = string.Concat(m.Contents.OfType<TextContent>()) }));
401+
foreach (var message in messages
402+
.Where(m => m.Role == ChatRole.System && m.Contents.Any(c => c is TextContent)))
403+
{
404+
system.Add(new SystemContentBlock()
405+
{
406+
Text = string.Concat(message.Contents.OfType<TextContent>()),
407+
});
408+
409+
if (message.AdditionalProperties?.TryGetValue(nameof(ContentBlock.CachePoint), out var maybeCachePoint) == true)
410+
{
411+
if (maybeCachePoint is CachePointBlock cachePointBlock)
412+
{
413+
system.Add(new SystemContentBlock()
414+
{
415+
CachePoint = cachePointBlock,
416+
});
417+
}
418+
}
419+
}
401420

402421
return system;
403422
}
@@ -440,6 +459,17 @@ private static List<Message> CreateMessages(List<Message>? rawMessages, IEnumera
440459
}
441460
}
442461

462+
if (chatMessage.AdditionalProperties?.TryGetValue(nameof(ContentBlock.CachePoint), out var maybeCachePoint) == true)
463+
{
464+
if (maybeCachePoint is CachePointBlock cachePointBlock)
465+
{
466+
contents.Add(new()
467+
{
468+
CachePoint = cachePointBlock
469+
});
470+
}
471+
}
472+
443473
messages.Add(new()
444474
{
445475
Role = chatMessage.Role == ChatRole.Assistant ? ConversationRole.Assistant : ConversationRole.User,
@@ -573,6 +603,18 @@ private static List<ContentBlock> CreateContents(ChatMessage message)
573603
});
574604
break;
575605
}
606+
607+
608+
if (content.AdditionalProperties?.TryGetValue(nameof(ContentBlock.CachePoint), out var maybeCachePoint) == true)
609+
{
610+
if (maybeCachePoint is CachePointBlock cachePointBlock)
611+
{
612+
contents.Add(new()
613+
{
614+
CachePoint = cachePointBlock
615+
});
616+
}
617+
}
576618
}
577619

578620
return contents;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extensions": [
3+
{
4+
"extensionName": "Extensions.Bedrock.MEAI",
5+
"type": "minor",
6+
"changeLogMessages": [
7+
"Add support for prompt caching in BedrockChatClient for MEAI"
8+
]
9+
}
10+
]
11+
}

0 commit comments

Comments
 (0)