forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbedrock_agent.py
More file actions
60 lines (46 loc) · 2.21 KB
/
bedrock_agent.py
File metadata and controls
60 lines (46 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from __future__ import annotations
import logging
from typing import TYPE_CHECKING, Any
from aws_lambda_powertools.utilities.parser.envelopes.base import BaseEnvelope
from aws_lambda_powertools.utilities.parser.models import BedrockAgentEventModel, BedrockAgentFunctionEventModel
if TYPE_CHECKING:
from aws_lambda_powertools.utilities.parser.types import T
logger = logging.getLogger(__name__)
class BedrockAgentEnvelope(BaseEnvelope):
"""Bedrock Agent envelope to extract data within input_text key"""
def parse(self, data: dict[str, Any] | Any | None, model: type[T]) -> T | None:
"""Parses data found with model provided
Parameters
----------
data : dict
Lambda event to be parsed
model : type[T]
Data model provided to parse after extracting data using envelope
Returns
-------
T | None
Parsed detail payload with model provided
"""
logger.debug(f"Parsing incoming data with Bedrock Agent model {BedrockAgentEventModel}")
parsed_envelope: BedrockAgentEventModel = BedrockAgentEventModel.model_validate(data)
logger.debug(f"Parsing event payload in `input_text` with {model}")
return self._parse(data=parsed_envelope.input_text, model=model)
class BedrockAgentFunctionEnvelope(BaseEnvelope):
"""Bedrock Agent Function envelope to extract data within input_text key"""
def parse(self, data: dict[str, Any] | Any | None, model: type[T]) -> T | None:
"""Parses data found with model provided
Parameters
----------
data : dict
Lambda event to be parsed
model : type[T]
Data model provided to parse after extracting data using envelope
Returns
-------
T | None
Parsed detail payload with model provided
"""
logger.debug(f"Parsing incoming data with Bedrock Agent Function model {BedrockAgentFunctionEventModel}")
parsed_envelope: BedrockAgentFunctionEventModel = BedrockAgentFunctionEventModel.model_validate(data)
logger.debug(f"Parsing event payload in `input_text` with {model}")
return self._parse(data=parsed_envelope.input_text, model=model)