@@ -120,7 +120,8 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
120120 requestBody . textGenerationConfig . temperature ;
121121 }
122122 if ( requestBody . textGenerationConfig ?. topP !== undefined ) {
123- spanAttributes [ ATTR_GEN_AI_REQUEST_TOP_P ] = requestBody . textGenerationConfig . topP ;
123+ spanAttributes [ ATTR_GEN_AI_REQUEST_TOP_P ] =
124+ requestBody . textGenerationConfig . topP ;
124125 }
125126 if ( requestBody . textGenerationConfig ?. maxTokenCount !== undefined ) {
126127 spanAttributes [ ATTR_GEN_AI_REQUEST_MAX_TOKENS ] =
@@ -132,47 +133,58 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
132133 }
133134 } else if ( modelId . includes ( 'amazon.nova' ) ) {
134135 if ( requestBody . inferenceConfig ?. temperature !== undefined ) {
135- spanAttributes [ ATTR_GEN_AI_REQUEST_TEMPERATURE ] = requestBody . inferenceConfig . temperature ;
136+ spanAttributes [ ATTR_GEN_AI_REQUEST_TEMPERATURE ] =
137+ requestBody . inferenceConfig . temperature ;
136138 }
137139 if ( requestBody . inferenceConfig ?. top_p !== undefined ) {
138- spanAttributes [ ATTR_GEN_AI_REQUEST_TOP_P ] = requestBody . inferenceConfig . top_p ;
140+ spanAttributes [ ATTR_GEN_AI_REQUEST_TOP_P ] =
141+ requestBody . inferenceConfig . top_p ;
139142 }
140143 if ( requestBody . inferenceConfig ?. max_new_tokens !== undefined ) {
141- spanAttributes [ ATTR_GEN_AI_REQUEST_MAX_TOKENS ] = requestBody . inferenceConfig . max_new_tokens ;
144+ spanAttributes [ ATTR_GEN_AI_REQUEST_MAX_TOKENS ] =
145+ requestBody . inferenceConfig . max_new_tokens ;
142146 }
143147 if ( requestBody . inferenceConfig ?. stopSequences !== undefined ) {
144- spanAttributes [ ATTR_GEN_AI_REQUEST_STOP_SEQUENCES ] = requestBody . inferenceConfig . stopSequences ;
148+ spanAttributes [ ATTR_GEN_AI_REQUEST_STOP_SEQUENCES ] =
149+ requestBody . inferenceConfig . stopSequences ;
145150 }
146151 } else if ( modelId . includes ( 'anthropic.claude' ) ) {
147152 if ( requestBody . max_tokens !== undefined ) {
148- spanAttributes [ ATTR_GEN_AI_REQUEST_MAX_TOKENS ] = requestBody . max_tokens ;
153+ spanAttributes [ ATTR_GEN_AI_REQUEST_MAX_TOKENS ] =
154+ requestBody . max_tokens ;
149155 }
150156 if ( requestBody . temperature !== undefined ) {
151- spanAttributes [ ATTR_GEN_AI_REQUEST_TEMPERATURE ] = requestBody . temperature ;
157+ spanAttributes [ ATTR_GEN_AI_REQUEST_TEMPERATURE ] =
158+ requestBody . temperature ;
152159 }
153160 if ( requestBody . top_p !== undefined ) {
154161 spanAttributes [ ATTR_GEN_AI_REQUEST_TOP_P ] = requestBody . top_p ;
155162 }
156163 if ( requestBody . stop_sequences !== undefined ) {
157- spanAttributes [ ATTR_GEN_AI_REQUEST_STOP_SEQUENCES ] = requestBody . stop_sequences ;
164+ spanAttributes [ ATTR_GEN_AI_REQUEST_STOP_SEQUENCES ] =
165+ requestBody . stop_sequences ;
158166 }
159167 } else if ( modelId . includes ( 'meta.llama' ) ) {
160168 if ( requestBody . max_gen_len !== undefined ) {
161- spanAttributes [ ATTR_GEN_AI_REQUEST_MAX_TOKENS ] = requestBody . max_gen_len ;
169+ spanAttributes [ ATTR_GEN_AI_REQUEST_MAX_TOKENS ] =
170+ requestBody . max_gen_len ;
162171 }
163172 if ( requestBody . temperature !== undefined ) {
164- spanAttributes [ ATTR_GEN_AI_REQUEST_TEMPERATURE ] = requestBody . temperature ;
173+ spanAttributes [ ATTR_GEN_AI_REQUEST_TEMPERATURE ] =
174+ requestBody . temperature ;
165175 }
166176 if ( requestBody . top_p !== undefined ) {
167177 spanAttributes [ ATTR_GEN_AI_REQUEST_TOP_P ] = requestBody . top_p ;
168178 }
169179 // request for meta llama models does not contain stop_sequences field
170180 } else if ( modelId . includes ( 'cohere.command-r' ) ) {
171181 if ( requestBody . max_tokens !== undefined ) {
172- spanAttributes [ ATTR_GEN_AI_REQUEST_MAX_TOKENS ] = requestBody . max_tokens ;
182+ spanAttributes [ ATTR_GEN_AI_REQUEST_MAX_TOKENS ] =
183+ requestBody . max_tokens ;
173184 }
174185 if ( requestBody . temperature !== undefined ) {
175- spanAttributes [ ATTR_GEN_AI_REQUEST_TEMPERATURE ] = requestBody . temperature ;
186+ spanAttributes [ ATTR_GEN_AI_REQUEST_TEMPERATURE ] =
187+ requestBody . temperature ;
176188 }
177189 if ( requestBody . p !== undefined ) {
178190 spanAttributes [ ATTR_GEN_AI_REQUEST_TOP_P ] = requestBody . p ;
@@ -181,17 +193,22 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
181193 // NOTE: We approximate the token count since this value is not directly available in the body
182194 // According to Bedrock docs they use (total_chars / 6) to approximate token count for pricing.
183195 // https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-prepare.html
184- spanAttributes [ ATTR_GEN_AI_USAGE_INPUT_TOKENS ] = Math . ceil ( requestBody . message . length / 6 ) ;
196+ spanAttributes [ ATTR_GEN_AI_USAGE_INPUT_TOKENS ] = Math . ceil (
197+ requestBody . message . length / 6
198+ ) ;
185199 }
186200 if ( requestBody . stop_sequences !== undefined ) {
187- spanAttributes [ ATTR_GEN_AI_REQUEST_STOP_SEQUENCES ] = requestBody . stop_sequences ;
201+ spanAttributes [ ATTR_GEN_AI_REQUEST_STOP_SEQUENCES ] =
202+ requestBody . stop_sequences ;
188203 }
189204 } else if ( modelId . includes ( 'cohere.command' ) ) {
190205 if ( requestBody . max_tokens !== undefined ) {
191- spanAttributes [ ATTR_GEN_AI_REQUEST_MAX_TOKENS ] = requestBody . max_tokens ;
206+ spanAttributes [ ATTR_GEN_AI_REQUEST_MAX_TOKENS ] =
207+ requestBody . max_tokens ;
192208 }
193209 if ( requestBody . temperature !== undefined ) {
194- spanAttributes [ ATTR_GEN_AI_REQUEST_TEMPERATURE ] = requestBody . temperature ;
210+ spanAttributes [ ATTR_GEN_AI_REQUEST_TEMPERATURE ] =
211+ requestBody . temperature ;
195212 }
196213 if ( requestBody . p !== undefined ) {
197214 spanAttributes [ ATTR_GEN_AI_REQUEST_TOP_P ] = requestBody . p ;
@@ -200,23 +217,30 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
200217 // NOTE: We approximate the token count since this value is not directly available in the body
201218 // According to Bedrock docs they use (total_chars / 6) to approximate token count for pricing.
202219 // https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-prepare.html
203- spanAttributes [ ATTR_GEN_AI_USAGE_INPUT_TOKENS ] = Math . ceil ( requestBody . prompt . length / 6 ) ;
220+ spanAttributes [ ATTR_GEN_AI_USAGE_INPUT_TOKENS ] = Math . ceil (
221+ requestBody . prompt . length / 6
222+ ) ;
204223 }
205224 if ( requestBody . stop_sequences !== undefined ) {
206- spanAttributes [ ATTR_GEN_AI_REQUEST_STOP_SEQUENCES ] = requestBody . stop_sequences ;
225+ spanAttributes [ ATTR_GEN_AI_REQUEST_STOP_SEQUENCES ] =
226+ requestBody . stop_sequences ;
207227 }
208228 } else if ( modelId . includes ( 'mistral' ) ) {
209229 if ( requestBody . prompt !== undefined ) {
210230 // NOTE: We approximate the token count since this value is not directly available in the body
211231 // According to Bedrock docs they use (total_chars / 6) to approximate token count for pricing.
212232 // https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-prepare.html
213- spanAttributes [ ATTR_GEN_AI_USAGE_INPUT_TOKENS ] = Math . ceil ( requestBody . prompt . length / 6 ) ;
233+ spanAttributes [ ATTR_GEN_AI_USAGE_INPUT_TOKENS ] = Math . ceil (
234+ requestBody . prompt . length / 6
235+ ) ;
214236 }
215237 if ( requestBody . max_tokens !== undefined ) {
216- spanAttributes [ ATTR_GEN_AI_REQUEST_MAX_TOKENS ] = requestBody . max_tokens ;
238+ spanAttributes [ ATTR_GEN_AI_REQUEST_MAX_TOKENS ] =
239+ requestBody . max_tokens ;
217240 }
218241 if ( requestBody . temperature !== undefined ) {
219- spanAttributes [ ATTR_GEN_AI_REQUEST_TEMPERATURE ] = requestBody . temperature ;
242+ spanAttributes [ ATTR_GEN_AI_REQUEST_TEMPERATURE ] =
243+ requestBody . temperature ;
220244 }
221245 if ( requestBody . top_p !== undefined ) {
222246 spanAttributes [ ATTR_GEN_AI_REQUEST_TOP_P ] = requestBody . top_p ;
@@ -273,7 +297,7 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
273297 span . setAttribute ( ATTR_GEN_AI_RESPONSE_FINISH_REASONS , [ stopReason ] ) ;
274298 }
275299 }
276-
300+
277301 private responseHookInvokeModel (
278302 response : NormalizedResponse ,
279303 span : Span ,
@@ -286,10 +310,16 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
286310 const responseBody = JSON . parse ( decodedResponseBody ) ;
287311 if ( currentModelId . includes ( 'amazon.titan' ) ) {
288312 if ( responseBody . inputTextTokenCount !== undefined ) {
289- span . setAttribute ( ATTR_GEN_AI_USAGE_INPUT_TOKENS , responseBody . inputTextTokenCount ) ;
313+ span . setAttribute (
314+ ATTR_GEN_AI_USAGE_INPUT_TOKENS ,
315+ responseBody . inputTextTokenCount
316+ ) ;
290317 }
291318 if ( responseBody . results ?. [ 0 ] ?. tokenCount !== undefined ) {
292- span . setAttribute ( ATTR_GEN_AI_USAGE_OUTPUT_TOKENS , responseBody . results [ 0 ] . tokenCount ) ;
319+ span . setAttribute (
320+ ATTR_GEN_AI_USAGE_OUTPUT_TOKENS ,
321+ responseBody . results [ 0 ] . tokenCount
322+ ) ;
293323 }
294324 if ( responseBody . results ?. [ 0 ] ?. completionReason !== undefined ) {
295325 span . setAttribute ( ATTR_GEN_AI_RESPONSE_FINISH_REASONS , [
@@ -299,44 +329,73 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
299329 } else if ( currentModelId . includes ( 'amazon.nova' ) ) {
300330 if ( responseBody . usage !== undefined ) {
301331 if ( responseBody . usage . inputTokens !== undefined ) {
302- span . setAttribute ( ATTR_GEN_AI_USAGE_INPUT_TOKENS , responseBody . usage . inputTokens ) ;
332+ span . setAttribute (
333+ ATTR_GEN_AI_USAGE_INPUT_TOKENS ,
334+ responseBody . usage . inputTokens
335+ ) ;
303336 }
304337 if ( responseBody . usage . outputTokens !== undefined ) {
305- span . setAttribute ( ATTR_GEN_AI_USAGE_OUTPUT_TOKENS , responseBody . usage . outputTokens ) ;
338+ span . setAttribute (
339+ ATTR_GEN_AI_USAGE_OUTPUT_TOKENS ,
340+ responseBody . usage . outputTokens
341+ ) ;
306342 }
307343 }
308344 if ( responseBody . stopReason !== undefined ) {
309- span . setAttribute ( ATTR_GEN_AI_RESPONSE_FINISH_REASONS , [ responseBody . stopReason ] ) ;
345+ span . setAttribute ( ATTR_GEN_AI_RESPONSE_FINISH_REASONS , [
346+ responseBody . stopReason ,
347+ ] ) ;
310348 }
311349 } else if ( currentModelId . includes ( 'anthropic.claude' ) ) {
312350 if ( responseBody . usage ?. input_tokens !== undefined ) {
313- span . setAttribute ( ATTR_GEN_AI_USAGE_INPUT_TOKENS , responseBody . usage . input_tokens ) ;
351+ span . setAttribute (
352+ ATTR_GEN_AI_USAGE_INPUT_TOKENS ,
353+ responseBody . usage . input_tokens
354+ ) ;
314355 }
315356 if ( responseBody . usage ?. output_tokens !== undefined ) {
316- span . setAttribute ( ATTR_GEN_AI_USAGE_OUTPUT_TOKENS , responseBody . usage . output_tokens ) ;
357+ span . setAttribute (
358+ ATTR_GEN_AI_USAGE_OUTPUT_TOKENS ,
359+ responseBody . usage . output_tokens
360+ ) ;
317361 }
318362 if ( responseBody . stop_reason !== undefined ) {
319- span . setAttribute ( ATTR_GEN_AI_RESPONSE_FINISH_REASONS , [ responseBody . stop_reason ] ) ;
363+ span . setAttribute ( ATTR_GEN_AI_RESPONSE_FINISH_REASONS , [
364+ responseBody . stop_reason ,
365+ ] ) ;
320366 }
321367 } else if ( currentModelId . includes ( 'meta.llama' ) ) {
322368 if ( responseBody . prompt_token_count !== undefined ) {
323- span . setAttribute ( ATTR_GEN_AI_USAGE_INPUT_TOKENS , responseBody . prompt_token_count ) ;
369+ span . setAttribute (
370+ ATTR_GEN_AI_USAGE_INPUT_TOKENS ,
371+ responseBody . prompt_token_count
372+ ) ;
324373 }
325374 if ( responseBody . generation_token_count !== undefined ) {
326- span . setAttribute ( ATTR_GEN_AI_USAGE_OUTPUT_TOKENS , responseBody . generation_token_count ) ;
375+ span . setAttribute (
376+ ATTR_GEN_AI_USAGE_OUTPUT_TOKENS ,
377+ responseBody . generation_token_count
378+ ) ;
327379 }
328380 if ( responseBody . stop_reason !== undefined ) {
329- span . setAttribute ( ATTR_GEN_AI_RESPONSE_FINISH_REASONS , [ responseBody . stop_reason ] ) ;
381+ span . setAttribute ( ATTR_GEN_AI_RESPONSE_FINISH_REASONS , [
382+ responseBody . stop_reason ,
383+ ] ) ;
330384 }
331385 } else if ( currentModelId . includes ( 'cohere.command-r' ) ) {
332386 if ( responseBody . text !== undefined ) {
333387 // NOTE: We approximate the token count since this value is not directly available in the body
334388 // According to Bedrock docs they use (total_chars / 6) to approximate token count for pricing.
335389 // https://docs.aws.amazon.com/bedrock/latest/userguide/model-customization-prepare.html
336- span . setAttribute ( ATTR_GEN_AI_USAGE_OUTPUT_TOKENS , Math . ceil ( responseBody . text . length / 6 ) ) ;
390+ span . setAttribute (
391+ ATTR_GEN_AI_USAGE_OUTPUT_TOKENS ,
392+ Math . ceil ( responseBody . text . length / 6 )
393+ ) ;
337394 }
338395 if ( responseBody . finish_reason !== undefined ) {
339- span . setAttribute ( ATTR_GEN_AI_RESPONSE_FINISH_REASONS , [ responseBody . finish_reason ] ) ;
396+ span . setAttribute ( ATTR_GEN_AI_RESPONSE_FINISH_REASONS , [
397+ responseBody . finish_reason ,
398+ ] ) ;
340399 }
341400 } else if ( currentModelId . includes ( 'cohere.command' ) ) {
342401 if ( responseBody . generations ?. [ 0 ] ?. text !== undefined ) {
0 commit comments