Sagemaker allows users to host any ML model on their own AWS infrastructure.

With portkey you can manage/restrict access, log requests, and more.

Provider Slug. sagemaker

Portkey SDK Integration with AWS Sagemaker

1. Install the Portkey SDK

Add the Portkey SDK to your application to interact with Sagemaker’s API through Portkey’s gateway.

npm install --save portkey-ai

2. Initialize Portkey with a Virtual Key

There are multiple ways to integrate Sagemaker with Portkey. You can use your AWS credentials, or use an assumed role.

In this example we will create a virtual key and use it to interact with Sagemaker. This helps you restrict access (specific models, few endpoints, etc).

Create a virtual key in the Portkey dashboard in the virtual keys section. You can select sagemaker as the provider, and fill in deployment details.

Initialize the Portkey SDK with the virtual key. (If you are using the REST API, skip to next step)

import Portkey from 'portkey-ai'

const portkey = new Portkey({
    apiKey: "PORTKEY_API_KEY", // Replace with your Portkey API key
    virtualKey: "VIRTUAL_KEY" // Replace with your Sagemaker Virtual Key
})

3. Invoke the Sagemaker model

response = portkey.post(
    url="endpoints/{endpoint_name}/invocations",
    # You can pass any key value pair required by the model, apart from `url`, they are passed as kwargs to the Sagemaker endpoint
    inputs="my_custom_value",
    my_custom_key="my_custom_value",
)

print(response)

Making Requests without Virtual Keys

If you do not want to add your AWS details to Portkey vault, you can also directly pass them while instantiating the Portkey client.

These are the supported headers/parameters for Sagemaker (Not required if you’re using a virtual key):

Node SDKPython SDKREST Headers
awsAccessKeyIdaws_access_key_idx-portkey-aws-access-key-id
awsSecretAccessKeyaws_secret_access_keyx-portkey-aws-secret-access-key
awsRegionaws_regionx-portkey-aws-region
awsSessionTokenaws_session_tokenx-portkey-aws-session-token
sagemakerCustomAttributessagemaker_custom_attributesx-portkey-amzn-sagemaker-custom-attributes
sagemakerTargetModelsagemaker_target_modelx-portkey-amzn-sagemaker-target-model
sagemakerTargetVariantsagemaker_target_variantx-portkey-amzn-sagemaker-target-variant
sagemakerTargetContainerHostnamesagemaker_target_container_hostnamex-portkey-amzn-sagemaker-target-container-hostname
sagemakerInferenceIdsagemaker_inference_idx-portkey-amzn-sagemaker-inference-id
sagemakerEnableExplanationssagemaker_enable_explanationsx-portkey-amzn-sagemaker-enable-explanations
sagemakerInferenceComponentsagemaker_inference_componentx-portkey-amzn-sagemaker-inference-component
sagemakerSessionIdsagemaker_session_idx-portkey-amzn-sagemaker-session-id
sagemakerModelNamesagemaker_model_namex-portkey-amzn-sagemaker-model-name

Example

from portkey_ai import Portkey

portkey = Portkey(
    api_key="PORTKEY_API_KEY",  # Replace with your Portkey API key
    provider="sagemaker",
    aws_region="us-east-1", # Replace with your AWS region  
    aws_access_key_id="AWS_ACCESS_KEY_ID", # Replace with your AWS access key id
    aws_secret_access_key="AWS_SECRET_ACCESS_KEY", # Replace with your AWS secret access key
    amzn_sagemaker_inference_component="SAGEMAKER_INFERENCE_COMPONENT" # Replace with your Sagemaker inference component
)

response = portkey.post(
    url="endpoints/{endpoint_name}/invocations",
    # You can pass any key value pair required by the model, apart from `url`, they are passed as kwargs to the Sagemaker endpoint
    inputs="my_custom_value",
    my_custom_key="my_custom_value"
)

print(response)

Next Steps

The complete list of features supported in the SDK are available on the link below.

SDK

You’ll find more information in the relevant sections:

  1. Add metadata to your requests
  2. Add gateway configs to your Sagemaker requests
  3. Tracing Sagemaker requests