24
24
class BedrockResponse :
25
25
def __init__ (
26
26
self ,
27
- body : Any ,
27
+ body : Any = None ,
28
28
status_code : int = 200 ,
29
29
content_type : str = "application/json" ,
30
30
session_attributes : dict [str , Any ] | None = None ,
@@ -36,61 +36,38 @@ def __init__(
36
36
self .content_type = content_type
37
37
self .session_attributes = session_attributes
38
38
self .prompt_session_attributes = prompt_session_attributes
39
-
40
- if knowledge_bases_configuration is not None :
41
- if not isinstance (knowledge_bases_configuration , list ) or not all (
42
- isinstance (item , dict ) for item in knowledge_bases_configuration
43
- ):
44
- raise ValueError ("knowledge_bases_configuration must be a list of dictionaries" )
45
-
46
39
self .knowledge_bases_configuration = knowledge_bases_configuration
47
40
48
- def to_dict (self , event : BedrockAgentEvent ) -> dict [str , Any ]:
49
- result : dict [str , Any ] = {
50
- "messageVersion" : "1.0" ,
51
- "response" : {
52
- "apiPath" : event .api_path ,
53
- "actionGroup" : event .action_group ,
54
- "httpMethod" : event .http_method ,
55
- "httpStatusCode" : self .status_code ,
56
- "responseBody" : {
57
- self .content_type : {"body" : json .dumps (self .body ) if isinstance (self .body , dict ) else self .body },
58
- },
59
- },
41
+ def is_json (self ) -> bool :
42
+ return self .content_type == "application/json"
43
+
44
+ def to_dict (self ) -> dict :
45
+ return {
46
+ "body" : self .body ,
47
+ "status_code" : self .status_code ,
48
+ "content_type" : self .content_type ,
49
+ "session_attributes" : self .session_attributes ,
50
+ "prompt_session_attributes" : self .prompt_session_attributes ,
51
+ "knowledge_bases_configuration" : self .knowledge_bases_configuration ,
60
52
}
61
53
62
- # Add optional attributes if they exist
63
- if self .session_attributes is not None :
64
- result ["sessionAttributes" ] = self .session_attributes
65
-
66
- if self .prompt_session_attributes is not None :
67
- result ["promptSessionAttributes" ] = self .prompt_session_attributes
68
-
69
- if self .knowledge_bases_configuration is not None :
70
- result ["knowledgeBasesConfiguration" ] = self .knowledge_bases_configuration
71
-
72
- return result
73
-
74
54
75
55
class BedrockResponseBuilder (ResponseBuilder ):
76
- """
77
- Bedrock Response Builder. This builds the response dict to be returned by Lambda when using Bedrock Agents.
78
-
79
- Since the payload format is different from the standard API Gateway Proxy event, we override the build method.
80
- """
81
-
82
56
@override
83
57
def build (self , event : BedrockAgentEvent , * args ) -> dict [str , Any ]:
84
- """
85
- Build the response dictionary to be returned by the Lambda function.
86
- """
87
58
self ._route (event , None )
88
59
89
- body = self .response .body
90
- if self .response .is_json () and not isinstance (self .response .body , str ):
91
- body = self .serializer (self .response .body )
60
+ bedrock_response = None
61
+ if isinstance (self .response .body , dict ) and "body" in self .response .body :
62
+ bedrock_response = BedrockResponse (** self .response .body )
63
+ body = bedrock_response .body
64
+ else :
65
+ body = self .response .body
66
+
67
+ if self .response .is_json () and not isinstance (body , str ):
68
+ body = self .serializer (body )
92
69
93
- base_response = {
70
+ response = {
94
71
"messageVersion" : "1.0" ,
95
72
"response" : {
96
73
"actionGroup" : event .action_group ,
@@ -105,22 +82,16 @@ def build(self, event: BedrockAgentEvent, *args) -> dict[str, Any]:
105
82
},
106
83
}
107
84
108
- if isinstance (self .response , BedrockResponse ):
109
- self ._add_bedrock_specific_configs (base_response )
110
-
111
- return base_response
112
-
113
- def _add_bedrock_specific_configs (self , response : dict [str , Any ]) -> None :
114
- if not isinstance (self .response , BedrockResponse ):
115
- return
116
-
117
- optional_configs = {
118
- "sessionAttributes" : self .response .session_attributes ,
119
- "promptSessionAttributes" : self .response .prompt_session_attributes ,
120
- "knowledgeBasesConfiguration" : self .response .knowledge_bases_configuration ,
121
- }
85
+ # Add Bedrock-specific attributes
86
+ if bedrock_response :
87
+ if bedrock_response .session_attributes :
88
+ response ["sessionAttributes" ] = bedrock_response .session_attributes
89
+ if bedrock_response .prompt_session_attributes :
90
+ response ["promptSessionAttributes" ] = bedrock_response .prompt_session_attributes
91
+ if bedrock_response .knowledge_bases_configuration :
92
+ response ["knowledgeBasesConfiguration" ] = bedrock_response .knowledge_bases_configuration
122
93
123
- response . update ({ k : v for k , v in optional_configs . items () if v is not None })
94
+ return response
124
95
125
96
126
97
class BedrockAgentResolver (ApiGatewayResolver ):
0 commit comments