@@ -69,15 +69,30 @@ export interface BedrockConverseAI21ChatCompletionsParams
69
69
countPenalty ?: number ;
70
70
}
71
71
72
- const getMessageTextContentArray = ( message : Message ) : { text : string } [ ] => {
72
+ const getMessageTextContentArray = (
73
+ message : Message
74
+ ) : Array < { text : string } | { cachePoint : { type : string } } > => {
73
75
if ( message . content && typeof message . content === 'object' ) {
74
- return message . content
75
- . filter ( ( item ) => item . type === 'text' )
76
- . map ( ( item ) => {
77
- return {
78
- text : item . text || '' ,
79
- } ;
76
+ const filteredContentMessages = message . content . filter (
77
+ ( item ) => item . type === 'text'
78
+ ) ;
79
+ const finalContent : Array <
80
+ { text : string } | { cachePoint : { type : string } }
81
+ > = [ ] ;
82
+ filteredContentMessages . forEach ( ( item ) => {
83
+ finalContent . push ( {
84
+ text : item . text || '' ,
80
85
} ) ;
86
+ // push a cache point.
87
+ if ( item . cache_control ) {
88
+ finalContent . push ( {
89
+ cachePoint : {
90
+ type : 'default' ,
91
+ } ,
92
+ } ) ;
93
+ }
94
+ } ) ;
95
+ return finalContent ;
81
96
}
82
97
return [
83
98
{
@@ -162,6 +177,15 @@ const getMessageContent = (message: Message) => {
162
177
} ) ;
163
178
}
164
179
}
180
+
181
+ if ( item . cache_control ) {
182
+ // if content item has `cache_control`, push the cache point to the out array
183
+ out . push ( {
184
+ cachePoint : {
185
+ type : 'default' ,
186
+ } ,
187
+ } ) ;
188
+ }
165
189
} ) ;
166
190
}
167
191
@@ -219,7 +243,10 @@ export const BedrockConverseChatCompleteConfig: ProviderConfig = {
219
243
transform : ( params : BedrockChatCompletionsParams ) => {
220
244
if ( ! params . messages ) return ;
221
245
const systemMessages = params . messages . reduce (
222
- ( acc : { text : string } [ ] , msg ) => {
246
+ (
247
+ acc : Array < { text : string } | { cachePoint : { type : string } } > ,
248
+ msg
249
+ ) => {
223
250
if ( SYSTEM_MESSAGE_ROLES . includes ( msg . role ) )
224
251
return acc . concat ( ...getMessageTextContentArray ( msg ) ) ;
225
252
return acc ;
@@ -234,17 +261,29 @@ export const BedrockConverseChatCompleteConfig: ProviderConfig = {
234
261
tools : {
235
262
param : 'toolConfig' ,
236
263
transform : ( params : BedrockChatCompletionsParams ) => {
237
- const toolConfig = {
238
- tools : params . tools ?. map ( ( tool ) => {
239
- if ( ! tool . function ) return ;
240
- return {
241
- toolSpec : {
242
- name : tool . function . name ,
243
- description : tool . function . description ,
244
- inputSchema : { json : tool . function . parameters } ,
264
+ const canBeAmazonModel = params . model ?. includes ( 'amazon' ) ;
265
+ const tools : Array <
266
+ | { toolSpec : { name : string ; description ?: string ; inputSchema : any } }
267
+ | { cachePoint : { type : string } }
268
+ > = [ ] ;
269
+ params . tools ?. forEach ( ( tool ) => {
270
+ tools . push ( {
271
+ toolSpec : {
272
+ name : tool . function . name ,
273
+ description : tool . function . description ,
274
+ inputSchema : { json : tool . function . parameters } ,
275
+ } ,
276
+ } ) ;
277
+ if ( tool . cache_control && ! canBeAmazonModel ) {
278
+ tools . push ( {
279
+ cachePoint : {
280
+ type : 'default' ,
245
281
} ,
246
- } ;
247
- } ) ,
282
+ } ) ;
283
+ }
284
+ } ) ;
285
+ const toolConfig = {
286
+ tools : tools ,
248
287
} ;
249
288
let toolChoice = undefined ;
250
289
if ( params . tool_choice ) {
@@ -341,6 +380,9 @@ type BedrockContentItem = {
341
380
bytes : string ;
342
381
} ;
343
382
} ;
383
+ cachePoint ?: {
384
+ type : string ;
385
+ } ;
344
386
} ;
345
387
346
388
interface BedrockChatCompletionResponse {
@@ -358,6 +400,10 @@ interface BedrockChatCompletionResponse {
358
400
inputTokens : number ;
359
401
outputTokens : number ;
360
402
totalTokens : number ;
403
+ cacheReadInputTokenCount ?: number ;
404
+ cacheReadInputTokens ?: number ;
405
+ cacheWriteInputTokenCount ?: number ;
406
+ cacheWriteInputTokens ?: number ;
361
407
} ;
362
408
}
363
409
@@ -421,6 +467,10 @@ export const BedrockChatCompleteResponseTransform: (
421
467
}
422
468
423
469
if ( 'output' in response ) {
470
+ const shouldSendCacheUsage =
471
+ response . usage . cacheWriteInputTokens ||
472
+ response . usage . cacheReadInputTokens ;
473
+
424
474
let content : string = '' ;
425
475
content = response . output . message . content
426
476
. filter ( ( item ) => item . text )
@@ -453,6 +503,10 @@ export const BedrockChatCompleteResponseTransform: (
453
503
prompt_tokens : response . usage . inputTokens ,
454
504
completion_tokens : response . usage . outputTokens ,
455
505
total_tokens : response . usage . totalTokens ,
506
+ ...( shouldSendCacheUsage && {
507
+ cache_read_input_tokens : response . usage . cacheReadInputTokens ,
508
+ cache_creation_input_tokens : response . usage . cacheWriteInputTokens ,
509
+ } ) ,
456
510
} ,
457
511
} ;
458
512
const toolCalls = response . output . message . content
@@ -503,6 +557,10 @@ export interface BedrockChatCompleteStreamChunk {
503
557
inputTokens : number ;
504
558
outputTokens : number ;
505
559
totalTokens : number ;
560
+ cacheReadInputTokenCount ?: number ;
561
+ cacheReadInputTokens ?: number ;
562
+ cacheWriteInputTokenCount ?: number ;
563
+ cacheWriteInputTokens ?: number ;
506
564
} ;
507
565
}
508
566
@@ -534,6 +592,9 @@ export const BedrockChatCompleteStreamChunkTransform: (
534
592
}
535
593
536
594
if ( parsedChunk . usage ) {
595
+ const shouldSendCacheUsage =
596
+ parsedChunk . usage . cacheWriteInputTokens ||
597
+ parsedChunk . usage . cacheReadInputTokens ;
537
598
return [
538
599
`data: ${ JSON . stringify ( {
539
600
id : fallbackId ,
@@ -552,6 +613,11 @@ export const BedrockChatCompleteStreamChunkTransform: (
552
613
prompt_tokens : parsedChunk . usage . inputTokens ,
553
614
completion_tokens : parsedChunk . usage . outputTokens ,
554
615
total_tokens : parsedChunk . usage . totalTokens ,
616
+ ...( shouldSendCacheUsage && {
617
+ cache_read_input_tokens : parsedChunk . usage . cacheReadInputTokens ,
618
+ cache_creation_input_tokens :
619
+ parsedChunk . usage . cacheWriteInputTokens ,
620
+ } ) ,
555
621
} ,
556
622
} ) } \n\n`,
557
623
`data: [DONE]\n\n` ,
0 commit comments