|
| 1 | +# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""This module contains code related to the DataCaptureConfig class, which is used |
| 14 | +for configuring capture, collection, and storage, for prediction requests and responses |
| 15 | +for models hosted on SageMaker Endpoints. |
| 16 | +""" |
| 17 | +from __future__ import print_function, absolute_import |
| 18 | + |
| 19 | +import os |
| 20 | + |
| 21 | +from sagemaker.session import Session |
| 22 | + |
| 23 | +_MODEL_MONITOR_S3_PATH = "model-monitor" |
| 24 | +_DATA_CAPTURE_S3_PATH = "data-capture" |
| 25 | + |
| 26 | + |
| 27 | +class DataCaptureConfig(object): |
| 28 | + """Configuration object passed in when deploying models to Amazon SageMaker Endpoints. |
| 29 | + This object specifies configuration related to endpoint data capture for use with |
| 30 | + Amazon SageMaker Model Monitoring. |
| 31 | + """ |
| 32 | + |
| 33 | + API_MAPPING = {"REQUEST": "Input", "RESPONSE": "Output"} |
| 34 | + |
| 35 | + def __init__( |
| 36 | + self, |
| 37 | + enable_capture, |
| 38 | + sampling_percentage=20, |
| 39 | + destination_s3_uri=None, |
| 40 | + kms_key_id=None, |
| 41 | + capture_options=None, |
| 42 | + csv_content_types=None, |
| 43 | + json_content_types=None, |
| 44 | + ): |
| 45 | + """Initialize a DataCaptureConfig object for capturing data from Amazon SageMaker Endpoints. |
| 46 | +
|
| 47 | + Args: |
| 48 | + enable_capture (bool): Required. Whether data capture should be enabled or not. |
| 49 | + sampling_percentage (int): Optional. Default=20. The percentage of data to sample. |
| 50 | + Must be between 0 and 100. |
| 51 | + destination_s3_uri (str): Optional. Defaults to "s3://<default-session-bucket>/ |
| 52 | + <model-monitor>/data-capture |
| 53 | + kms_key_id (str): Optional. Default=None. The kms key to use when writing to S3. |
| 54 | + capture_options ([str]): Optional. Must be a list containing any combination of the |
| 55 | + following values: "REQUEST", "RESPONSE". Default=["REQUEST", "RESPONSE"]. Denotes |
| 56 | + which data to capture between request and response. |
| 57 | + csv_content_types ([str]): Optional. Default=["text/csv"]. |
| 58 | + json_content_types([str]): Optional. Default=["application/json"]. |
| 59 | +
|
| 60 | + """ |
| 61 | + self.enable_capture = enable_capture |
| 62 | + self.sampling_percentage = sampling_percentage |
| 63 | + self.destination_s3_uri = destination_s3_uri |
| 64 | + if self.destination_s3_uri is None: |
| 65 | + self.destination_s3_uri = os.path.join( |
| 66 | + "s3://", Session().default_bucket(), _MODEL_MONITOR_S3_PATH, _DATA_CAPTURE_S3_PATH |
| 67 | + ) |
| 68 | + |
| 69 | + self.kms_key_id = kms_key_id |
| 70 | + self.capture_options = capture_options or ["REQUEST", "RESPONSE"] |
| 71 | + self.csv_content_types = csv_content_types or ["text/csv"] |
| 72 | + self.json_content_types = json_content_types or ["application/json"] |
| 73 | + |
| 74 | + def to_request_dict(self): |
| 75 | + """Generates a request dictionary using the parameters provided to the class.""" |
| 76 | + request_dict = { |
| 77 | + "EnableCapture": self.enable_capture, |
| 78 | + "InitialSamplingPercentage": self.sampling_percentage, |
| 79 | + "DestinationS3Uri": self.destination_s3_uri, |
| 80 | + "CaptureOptions": [ |
| 81 | + {"CaptureMode": dict(self.API_MAPPING).get(capture_option.upper(), capture_option)} |
| 82 | + for capture_option in list(self.capture_options) |
| 83 | + ], |
| 84 | + } |
| 85 | + |
| 86 | + if self.kms_key_id is not None: |
| 87 | + request_dict["KmsKeyId"] = self.kms_key_id |
| 88 | + |
| 89 | + if self.csv_content_types is not None or self.json_content_types is not None: |
| 90 | + request_dict["CaptureContentTypeHeader"] = {} |
| 91 | + |
| 92 | + if self.csv_content_types is not None: |
| 93 | + request_dict["CaptureContentTypeHeader"]["CsvContentTypes"] = self.csv_content_types |
| 94 | + |
| 95 | + if self.json_content_types is not None: |
| 96 | + request_dict["CaptureContentTypeHeader"]["JsonContentTypes"] = self.json_content_types |
| 97 | + |
| 98 | + return request_dict |
0 commit comments