

自 2025 年 11 月 7 日起，Amazon Fraud Detector 不再向新客戶開放。對於類似 Amazon Fraud Detector 的功能，請探索 Amazon SageMaker、AutoGluon 和 AWS WAF。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 匯入 SageMaker AI 模型
<a name="import-an-amazon-sagemaker-model"></a>

您可以選擇將 SageMaker AI 託管模型匯入 Amazon Fraud Detector。與模型類似，SageMaker AI 模型可以新增到偵測器，並使用 `GetEventPrediction` API 產生詐騙預測。作為`GetEventPrediction`請求的一部分，Amazon Fraud Detector 將調用您的 SageMaker AI 端點並將結果傳遞到您的規則。

您可以設定 Amazon Fraud Detector 使用作為`GetEventPrediction`請求一部分傳送的事件變數。如果您選擇使用事件變數，則必須提供輸入範本。Amazon Fraud Detector 將使用此範本將您的事件變數轉換為必要的輸入承載，以叫用 SageMaker AI 端點。或者，您可以將 SageMaker AI 模型設定為使用作為`GetEventPrediction`請求一部分傳送的 byteBuffer。

Amazon Fraud Detector 支援匯入使用 JSON 或 CSV 輸入格式和 JSON 或 CSV 輸出格式的 SageMaker AI 演算法。支援的 SageMaker AI 演算法範例包括 XGBoost、線性學習程式和隨機剪切森林。

## 使用 匯入 SageMaker AI 模型 適用於 Python (Boto3) 的 AWS SDK
<a name="import-an-amazon-sagemaker-model-using-the-aws-python-sdk"></a>

若要匯入 SageMaker AI 模型，請使用 `PutExternalModel` API。下列範例假設 SageMaker AI 端點`sagemaker-transaction-model`已部署、為 `InService` 狀態，並使用 XGBoost 演算法。

輸入組態指定 將使用事件變數來建構模型輸入 (`useEventVariables` 設定為 `TRUE`)。輸入格式為 TEXT\_CSV，因為 XGBoost 需要 CSV 輸入。csvInputTemplate 指定如何從作為`GetEventPrediction`請求一部分傳送的變數建構 CSV 輸入。此範例假設您已建立變數 `order_amt`、 `prev_amt``hist_amt`和 `payment_type`。

輸出組態會指定 SageMaker AI 模型的回應格式，並將適當的 CSV 索引映射至 Amazon Fraud Detector 變數 `sagemaker_output_score`。設定完成後，您可以在 規則中使用輸出變數。

**注意**  
SageMaker AI 模型的輸出必須映射至具有來源 的變數`EXTERNAL_MODEL_SCORE`。您無法使用變數在主控台中建立這些**變數**。您必須在設定模型匯入時改為建立它們。

```
import boto3
fraudDetector = boto3.client('frauddetector')

fraudDetector.put_external_model (
modelSource = 'SAGEMAKER',
modelEndpoint = 'sagemaker-transaction-model',
invokeModelEndpointRoleArn = 'your_SagemakerExecutionRole_arn',
inputConfiguration = {
    'useEventVariables' : True,
    'eventTypeName' : 'sample_transaction',
    'format' : 'TEXT_CSV',
    'csvInputTemplate' : '{{order_amt}}, {{prev_amt}}, {{hist_amt}}, {{payment_type}}'
},

outputConfiguration = {
    'format' : 'TEXT_CSV',
    'csvIndexToVariableMap' : {
        '0' : 'sagemaker_output_score'
    }
},
    
modelEndpointStatus = 'ASSOCIATED'
)
```