1616
1717import json
1818from os import environ
19- from typing import Callable , Dict , Union
19+ from typing import Any , Callable , Dict , Union
2020
2121from botocore .eventstream import EventStream , EventStreamError
2222from wrapt import ObjectProxy
@@ -297,7 +297,7 @@ def genai_capture_message_content() -> bool:
297297 return capture_content .lower () == "true"
298298
299299
300- def message_to_event (message , capture_content ) :
300+ def message_to_event (message : dict [ str , Any ], capture_content : bool ) -> Event :
301301 attributes = {GEN_AI_SYSTEM : GenAiSystemValues .AWS_BEDROCK .value }
302302 role = message .get ("role" )
303303 content = message .get ("content" )
@@ -314,13 +314,17 @@ def message_to_event(message, capture_content):
314314
315315
316316class _Choice :
317- def __init__ (self , message , finish_reason , index ):
317+ def __init__ (
318+ self , message : dict [str , Any ], finish_reason : str , index : int
319+ ):
318320 self .message = message
319321 self .finish_reason = finish_reason
320322 self .index = index
321323
322324 @classmethod
323- def from_converse (cls , response , capture_content ):
325+ def from_converse (
326+ cls , response : dict [str , Any ], capture_content : bool
327+ ) -> _Choice :
324328 orig_message = response ["output" ]["message" ]
325329 if role := orig_message .get ("role" ):
326330 message = {"role" : role }
@@ -332,7 +336,9 @@ def from_converse(cls, response, capture_content):
332336 return cls (message , response ["stopReason" ], index = 0 )
333337
334338 @classmethod
335- def from_invoke_amazon_titan (cls , response , capture_content ):
339+ def from_invoke_amazon_titan (
340+ cls , response : dict [str , Any ], capture_content : bool
341+ ) -> _Choice :
336342 result = response ["results" ][0 ]
337343 if capture_content :
338344 message = {"content" : result ["outputText" ]}
@@ -341,7 +347,9 @@ def from_invoke_amazon_titan(cls, response, capture_content):
341347 return cls (message , result ["completionReason" ], index = 0 )
342348
343349 @classmethod
344- def from_invoke_anthropic_claude (cls , response , capture_content ):
350+ def from_invoke_anthropic_claude (
351+ cls , response : dict [str , Any ], capture_content : bool
352+ ) -> _Choice :
345353 if capture_content :
346354 message = {
347355 "content" : response ["content" ],
@@ -352,19 +360,18 @@ def from_invoke_anthropic_claude(cls, response, capture_content):
352360
353361 return cls (message , response ["stop_reason" ], index = 0 )
354362
355- def to_body_dict (self ):
363+ def _to_body_dict (self ) -> dict [ str , Any ] :
356364 return {
357365 "finish_reason" : self .finish_reason ,
358366 "index" : self .index ,
359367 "message" : self .message ,
360368 }
361369
362-
363- def to_choice_event (choice : _Choice , ** event_kwargs ):
364- attributes = {GEN_AI_SYSTEM : GenAiSystemValues .AWS_BEDROCK .value }
365- return Event (
366- name = "gen_ai.choice" ,
367- attributes = attributes ,
368- body = choice .to_body_dict (),
369- ** event_kwargs ,
370- )
370+ def to_choice_event (self , ** event_kwargs ) -> Event :
371+ attributes = {GEN_AI_SYSTEM : GenAiSystemValues .AWS_BEDROCK .value }
372+ return Event (
373+ name = "gen_ai.choice" ,
374+ attributes = attributes ,
375+ body = self ._to_body_dict (),
376+ ** event_kwargs ,
377+ )
0 commit comments