

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

# SageMaker 推論
<a name="nova-model-sagemaker-inference"></a>

自訂 Amazon Nova 模型現在可在 SageMaker 推論上使用。透過 SageMaker 上的 Amazon Nova，您可以從訓練過的自訂 Amazon Nova 模型開始取得預測或推論。SageMaker 提供廣泛的機器學習 (ML) 基礎架構和模型部署選項，有助於滿足您所有的機器學習推論需求。透過 SageMaker 推論，您可以擴展模型部署、更有效地在生產環境中管理模型，並減少營運負擔。

SageMaker 為您提供各種推論選項，例如取得低延遲推論的即時端點，以及批次請求的非同步端點。透過為您的使用案例利用適當的推論選項，您可以確保高效的模型部署和推論。如需 SageMaker 推論的詳細資訊，請參閱[部署模型以進行推論](https://docs.aws.amazon.com//sagemaker/latest/dg/deploy-model.html)。

**重要**  
SageMaker 推論僅支援全階自訂模型和 LoRA 合併模型。對於未合併的 LoRA 模型和基礎模型，請使用 Amazon Bedrock。

## 功能
<a name="nova-sagemaker-inference-features"></a>

下列功能適用於 SageMaker 推論上的 Amazon Nova 模型：

**模型功能**
+ 產生文字

**部署和擴展**
+ 具有自訂執行個體選擇的即時端點
+ Auto Scaling – 根據流量模式自動調整容量，以最佳化成本和 GPU 使用率。如需詳細資訊，請參閱[自動擴展 Amazon SageMaker 模型](https://docs.aws.amazon.com//sagemaker/latest/dg/endpoint-auto-scaling.html)。
+ 串流 API 支援即時產生字符

**監控和最佳化**
+ 用於監控和警示的 Amazon CloudWatch 整合
+ 透過 VPC 組態的可用區域感知延遲最佳化

**開發工具**
+ AWS CLI 支援 – 如需詳細資訊，請參閱 [AWS SageMaker 的 CLI 命令參考](https://docs.aws.amazon.com/cli/latest/reference/sagemaker/)。
+  透過 SDK 支援進行筆記本整合

## 支援的模型和執行個體
<a name="nova-sagemaker-inference-supported"></a>

建立 SageMaker 推論端點時，您可以設定兩個環境變數來設定部署： `CONTEXT_LENGTH`和 `MAX_CONCURRENCY`。
+ `CONTEXT_LENGTH` – 每個請求的總字符長度上限 （輸入 \$1 輸出）
+ `MAX_CONCURRENCY` – 端點將服務的並行請求數目上限

下表列出支援的 Amazon Nova 模型、執行個體類型和支援的組態。MAX\$1CONCURRENCY 值代表每個 CONTEXT\$1LENGTH 設定支援的並行上限：


****  
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/nova/latest/nova2-userguide/nova-model-sagemaker-inference.html)

**注意**  
顯示的 MAX\$1CONCURRENCY 值是每個 CONTEXT\$1LENGTH 設定的上限。您可以使用相同並行的較低內容長度，但超過這些值會導致 SageMaker 端點建立失敗。  
例如，在具有 ml.g5.12xlarge 的 Amazon Nova Micro 上：  
`CONTEXT_LENGTH=2000`、→ `MAX_CONCURRENCY=32` 有效
`CONTEXT_LENGTH=8000`、`MAX_CONCURRENCY=32`→ 已拒絕 （內容長度為 8000 的並行限制為 16)
`CONTEXT_LENGTH=8000`、→ `MAX_CONCURRENCY=4` 有效
`CONTEXT_LENGTH=8000`、→ `MAX_CONCURRENCY=16` 有效
`CONTEXT_LENGTH=10000` → 拒絕 （此執行個體的最大內容長度為 8000)

## 支援 AWS 的區域
<a name="nova-sagemaker-inference-regions"></a>

下表列出可在 SageMaker 推論上使用 Amazon Nova 模型 AWS 的區域：


****  

| 區域名稱 | 區域代碼 | 可用性 | 
| --- | --- | --- | 
| 美國東部 (維吉尼亞北部) | us-east-1 | Available | 
| 美國西部 (奧勒岡) | us-west-2 | Available | 

## 支援的容器映像
<a name="nova-sagemaker-inference-container-images"></a>

下表依區域列出 SageMaker 推論上 Amazon Nova 模型的容器映像 URIs。每個區域可使用兩個映像標籤：版本化標籤 (`v1.0.0`) 和最新標籤 (`SM-Inference-latest`)。對於生產部署，我們建議您使用版本控制的標籤。


****  

| 區域 | 容器映像 URIs | 
| --- | --- | 
| us-east-1 | 708977205387.dkr.ecr.us-east-1.amazonaws.com/nova-inference-repo:SM-Inference-latest | 
| us-west-2 | 176779409107.dkr.ecr.us-west-2.amazonaws.com/nova-inference-repo:SM-Inference-latest | 

## 最佳實務
<a name="nova-sagemaker-inference-best-practices"></a>

如需在 SageMaker 上部署和管理模型的最佳實務，請參閱 [ SageMaker 的最佳實務](https://docs.aws.amazon.com//sagemaker/latest/dg/best-practices.html)。

## 支援
<a name="nova-sagemaker-inference-support"></a>

如需 SageMaker 推論上 Amazon Nova 模型的問題和支援，請透過主控台或您的 AWS 客戶經理聯絡 AWS Support。

**Topics**
+ [功能](#nova-sagemaker-inference-features)
+ [支援的模型和執行個體](#nova-sagemaker-inference-supported)
+ [支援 AWS 的區域](#nova-sagemaker-inference-regions)
+ [支援的容器映像](#nova-sagemaker-inference-container-images)
+ [最佳實務](#nova-sagemaker-inference-best-practices)
+ [支援](#nova-sagemaker-inference-support)
+ [開始使用](nova-sagemaker-inference-getting-started.md)
+ [API 參考](nova-sagemaker-inference-api-reference.md)
+ [評估託管在 SageMaker 推論上的模型](nova-eval-on-sagemaker-inference.md)
+ [在 Amazon SageMaker 推論濫用偵測中部署 Amazon Nova Forge 模型](nova-sagemaker-inference-abuse-detection.md)

# 開始使用
<a name="nova-sagemaker-inference-getting-started"></a>

本指南說明如何在 SageMaker 即時端點上部署自訂的 Amazon Nova 模型、設定推論參數，以及叫用模型進行測試。

## 先決條件
<a name="nova-sagemaker-inference-prerequisites"></a>

以下是在 SageMaker 推論上部署 Amazon Nova 模型的先決條件：
+ 建立 AWS 帳戶 - 如果您還沒有 ，請參閱[建立 AWS 帳戶](https://docs.aws.amazon.com//sagemaker/latest/dg/gs-set-up.html#sign-up-for-aws)。
+ 必要的 IAM 許可 - 確保您的 IAM 使用者或角色已連接下列受管政策：
  + `AmazonSageMakerFullAccess`
  + `AmazonS3FullAccess`
+ 必要的 SDKs/CLI 版本 - 下列 SDK 版本已在 SageMaker 推論上使用 Amazon Nova 模型進行測試和驗證：
  + 適用於資源型 API 方法的 SageMaker Python SDK v3.0.0\$1 (`sagemaker>=3.0.0`)
  + Boto3 1.35.0\$1 版 (`boto3>=1.35.0`) 用於直接 API 呼叫。本指南中的範例使用此方法。
+ 增加服務配額 - 針對您計劃用於 Amazon SageMaker SageMaker 服務配額 （例如 `ml.p5.48xlarge for endpoint usage`)。如需支援的執行個體類型清單，請參閱 [支援的模型和執行個體](nova-model-sagemaker-inference.md#nova-sagemaker-inference-supported)。若要請求提高配額，請參閱[請求提高配額](https://docs.aws.amazon.com//servicequotas/latest/userguide/request-quota-increase.html)。如需 SageMaker 執行個體配額的相關資訊，請參閱 [SageMaker 端點和配額](https://docs.aws.amazon.com//general/latest/gr/sagemaker.html)。

## 步驟 1：設定 AWS 登入資料
<a name="nova-sagemaker-inference-step1"></a>

使用下列其中一種方法設定您的 AWS 登入資料：

**選項 1： AWS CLI （建議）**

```
aws configure
```

出現提示時，輸入您的 AWS 存取金鑰、私密金鑰和預設區域。

**選項 2： AWS credentials 檔案**

建立或編輯 `~/.aws/credentials`：

```
[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY
```

**選項 3：環境變數**

```
export AWS_ACCESS_KEY_ID=your_access_key
export AWS_SECRET_ACCESS_KEY=your_secret_key
```

**注意**  
如需 AWS 登入資料的詳細資訊，請參閱[組態和登入資料檔案設定](https://docs.aws.amazon.com//cli/latest/userguide/cli-configure-files.html)。

**初始化 AWS 用戶端**

使用下列程式碼建立 Python 指令碼或筆記本，以初始化 AWS SDK 並驗證您的登入資料：

```
import boto3

# AWS Configuration - Update these for your environment
REGION = "us-east-1"  # Supported regions: us-east-1, us-west-2
AWS_ACCOUNT_ID = "YOUR_ACCOUNT_ID"  # Replace with your AWS account ID

# Initialize AWS clients using default credential chain
sagemaker = boto3.client('sagemaker', region_name=REGION)
sts = boto3.client('sts')

# Verify credentials
try:
    identity = sts.get_caller_identity()
    print(f"Successfully authenticated to AWS Account: {identity['Account']}")
    
    if identity['Account'] != AWS_ACCOUNT_ID:
        print(f"Warning: Connected to account {identity['Account']}, expected {AWS_ACCOUNT_ID}")

except Exception as e:
    print(f"Failed to authenticate: {e}")
    print("Please verify your credentials are configured correctly.")
```

如果身分驗證成功，您應該會看到確認 AWS 帳戶 ID 的輸出。

## 步驟 2：建立 SageMaker 執行角色
<a name="nova-sagemaker-inference-step2"></a>

SageMaker 執行角色是一種 IAM 角色，可授予 SageMaker 代表您存取 AWS 資源的許可，例如用於模型成品的 Amazon S3 儲存貯體和用於記錄的 CloudWatch。

**建立執行角色**

**注意**  
建立 IAM 角色需要 `iam:CreateRole`和 `iam:AttachRolePolicy`許可。在繼續之前，請確定您的 IAM 使用者或角色具有這些許可。

下列程式碼會建立具有部署 Amazon Nova 自訂模型必要許可的 IAM 角色：

```
import json

# Create SageMaker Execution Role
role_name = f"SageMakerInference-ExecutionRole-{AWS_ACCOUNT_ID}"

trust_policy = {
    "Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {"Service": "sagemaker.amazonaws.com"},
            "Action": "sts:AssumeRole"
        }
    ]
}

iam = boto3.client('iam', region_name=REGION)

# Create the role
role_response = iam.create_role(
    RoleName=role_name,
    AssumeRolePolicyDocument=json.dumps(trust_policy),
    Description='SageMaker execution role with S3 and SageMaker access'
)

# Attach required policies
iam.attach_role_policy(
    RoleName=role_name,
    PolicyArn='arn:aws:iam::aws:policy/AmazonSageMakerFullAccess'
)

iam.attach_role_policy(
    RoleName=role_name,
    PolicyArn='arn:aws:iam::aws:policy/AmazonS3FullAccess'
)

SAGEMAKER_EXECUTION_ROLE_ARN = role_response['Role']['Arn']
print(f"Created SageMaker execution role: {SAGEMAKER_EXECUTION_ROLE_ARN}")
```

**使用現有的執行角色 （選用）**

如果您已有 SageMaker 執行角色，您可以改用它：

```
# Replace with your existing role ARN
SAGEMAKER_EXECUTION_ROLE_ARN = "arn:aws:iam::YOUR_ACCOUNT_ID:role/YOUR_EXISTING_ROLE_NAME"
```

若要尋找帳戶中現有的 SageMaker 角色：

```
iam = boto3.client('iam', region_name=REGION)
response = iam.list_roles()
sagemaker_roles = [role for role in response['Roles'] if 'SageMaker' in role['RoleName']]
for role in sagemaker_roles:
    print(f"{role['RoleName']}: {role['Arn']}")
```

**重要**  
執行角色必須與 `sagemaker.amazonaws.com`和 具有信任關係，才能存取 Amazon S3 和 SageMaker 資源。

如需 SageMaker 執行角色的詳細資訊，請參閱 [SageMaker 角色](https://docs.aws.amazon.com//sagemaker/latest/dg/sagemaker-roles.html)。

## 步驟 3：設定模型參數
<a name="nova-sagemaker-inference-step3"></a>

設定 Amazon Nova 模型的部署參數。這些設定控制模型行為、資源配置和推論特性。如需每個支援的執行個體類型和支援的 CONTEXT\$1LENGTH 和 MAX\$1CONCURRENCY 值清單，請參閱 [支援的模型和執行個體](nova-model-sagemaker-inference.md#nova-sagemaker-inference-supported)。

**必要參數**
+ `IMAGE`：Amazon Nova 推論容器的 Docker 容器映像 URI。這將由 提供 AWS。
+ `CONTEXT_LENGTH`：模型內容長度。
+ `MAX_CONCURRENCY`：每次反覆運算的序列數目上限；設定在 GPU 上的單一批次內可同時處理個別使用者請求 （提示） 的數量限制。範圍：大於 0 的整數。

**選用的產生參數**
+ `DEFAULT_TEMPERATURE`：控制產生時的隨機性。範圍：0.0 到 2.0 (0.0 = 確定性，較高 = 更多隨機）。
+ `DEFAULT_TOP_P`：Hucleus 取樣閾值用於權杖選取。範圍：1e-10 到 1.0。
+ `DEFAULT_TOP_K`：將權杖選擇限制為最熱門的 K 權杖。範圍：整數 -1 或更高 (-1 = 無限制）。
+ `DEFAULT_MAX_NEW_TOKENS`：回應中要產生的字符數量上限 （即最大輸出字符）。範圍：整數 1 或更高。
+ `DEFAULT_LOGPROBS`：每個字符傳回的日誌機率。範圍：整數 1 到 20。

**設定您的部署**

```
# AWS Configuration
REGION = "us-east-1"  # Must match region from Step 1

# ECR Account mapping by region
ECR_ACCOUNT_MAP = {
    "us-east-1": "708977205387",
    "us-west-2": "176779409107"
}

# Container Image
IMAGE = f"{ECR_ACCOUNT_MAP[REGION]}.dkr.ecr.{REGION}.amazonaws.com/nova-inference-repo:SM-Inference-latest"
print(f"IMAGE = {IMAGE}")

# Model Parameters
CONTEXT_LENGTH = "16000"       # Maximum total context length
MAX_CONCURRENCY = "2"          # Maximum concurrent sequences

# Optional: Default generation parameters (uncomment to use)
DEFAULT_TEMPERATURE = "0.0"   # Deterministic output
DEFAULT_TOP_P = "1.0"         # Consider all tokens
# DEFAULT_TOP_K = "50"        # Uncomment to limit to top 50 tokens
# DEFAULT_MAX_NEW_TOKENS = "2048"  # Uncomment to set max output tokens
# DEFAULT_LOGPROBS = "1"      # Uncomment to enable log probabilities

# Build environment variables for the container
environment = {
    'CONTEXT_LENGTH': CONTEXT_LENGTH,
    'MAX_CONCURRENCY': MAX_CONCURRENCY,
}

# Add optional parameters if defined
if 'DEFAULT_TEMPERATURE' in globals():
    environment['DEFAULT_TEMPERATURE'] = DEFAULT_TEMPERATURE
if 'DEFAULT_TOP_P' in globals():
    environment['DEFAULT_TOP_P'] = DEFAULT_TOP_P
if 'DEFAULT_TOP_K' in globals():
    environment['DEFAULT_TOP_K'] = DEFAULT_TOP_K
if 'DEFAULT_MAX_NEW_TOKENS' in globals():
    environment['DEFAULT_MAX_NEW_TOKENS'] = DEFAULT_MAX_NEW_TOKENS
if 'DEFAULT_LOGPROBS' in globals():
    environment['DEFAULT_LOGPROBS'] = DEFAULT_LOGPROBS

print("Environment configuration:")
for key, value in environment.items():
    print(f"  {key}: {value}")
```

**設定部署特定的參數**

現在為您的 Amazon Nova 模型部署設定特定參數，包括模型成品位置和執行個體類型選擇。

**設定部署識別符**

```
# Deployment identifier - use a descriptive name for your use case
JOB_NAME = "my-nova-deployment"
```

**指定模型成品位置**

提供存放訓練過之 Amazon Nova 模型成品的 Amazon S3 URI。這應該是模型訓練或微調任務的輸出位置。

```
# S3 location of your trained Nova model artifacts
# Replace with your model's S3 URI - must end with /
MODEL_S3_LOCATION = "s3://your-bucket-name/path/to/model/artifacts/"
```

**選取模型變體和執行個體類型**

```
# Configure model variant and instance type
TESTCASE = {
    "model": "lite2",              # Options: micro, lite, lite2
    "instance": "ml.p5.48xlarge"   # Refer to "Supported models and instances" section
}

# Generate resource names
INSTANCE_TYPE = TESTCASE["instance"]
MODEL_NAME = JOB_NAME + "-" + TESTCASE["model"] + "-" + INSTANCE_TYPE.replace(".", "-")
ENDPOINT_CONFIG_NAME = MODEL_NAME + "-Config"
ENDPOINT_NAME = MODEL_NAME + "-Endpoint"

print(f"Model Name: {MODEL_NAME}")
print(f"Endpoint Config: {ENDPOINT_CONFIG_NAME}")
print(f"Endpoint Name: {ENDPOINT_NAME}")
```

**命名慣例**

程式碼會自動為 AWS 資源產生一致的名稱：
+ 模型名稱：`{JOB_NAME}-{model}-{instance-type}`
+ 端點組態： `{MODEL_NAME}-Config`
+ 端點名稱： `{MODEL_NAME}-Endpoint`

## 步驟 4：建立 SageMaker 模型和端點組態
<a name="nova-sagemaker-inference-step4"></a>

在此步驟中，您將建立兩個基本資源：參考 Amazon Nova 模型成品的 SageMaker 模型物件，以及定義模型部署方式的端點組態。

**SageMaker 模型**：封裝推論容器映像、模型成品位置和環境組態的模型物件。這是可重複使用的資源，可部署到多個端點。

**端點組態**：定義部署的基礎設施設定，包括執行個體類型、執行個體計數和模型變體。這可讓您與模型本身分開管理部署設定。

**建立 SageMaker 模型**

下列程式碼會建立參考 Amazon Nova 模型成品的 SageMaker 模型：

```
try:
    model_response = sagemaker.create_model(
        ModelName=MODEL_NAME,
        PrimaryContainer={
            'Image': IMAGE,
            'ModelDataSource': {
                'S3DataSource': {
                    'S3Uri': MODEL_S3_LOCATION,
                    'S3DataType': 'S3Prefix',
                    'CompressionType': 'None'
                }
            },
            'Environment': environment
        },
        ExecutionRoleArn=SAGEMAKER_EXECUTION_ROLE_ARN,
        EnableNetworkIsolation=True
    )
    print("Model created successfully!")
    print(f"Model ARN: {model_response['ModelArn']}")
    
except sagemaker.exceptions.ClientError as e:
    print(f"Error creating model: {e}")
```

重要參數：
+ `ModelName`：模型的唯一識別符
+ `Image`：Amazon Nova 推論的 Docker 容器映像 URI
+ `ModelDataSource`：模型成品的 Amazon S3 位置
+ `Environment`：在步驟 3 中設定的環境變數
+ `ExecutionRoleArn`：步驟 2 中的 IAM 角色
+ `EnableNetworkIsolation`：將 設定為 True 以增強安全性 （防止容器進行傳出網路呼叫）

**建立端點組態**

接著，建立定義部署基礎設施的端點組態：

```
# Create Endpoint Configuration
try:
    production_variant = {
        'VariantName': 'primary',
        'ModelName': MODEL_NAME,
        'InitialInstanceCount': 1,
        'InstanceType': INSTANCE_TYPE,
    }
    
    config_response = sagemaker.create_endpoint_config(
        EndpointConfigName=ENDPOINT_CONFIG_NAME,
        ProductionVariants=[production_variant]
    )
    print("Endpoint configuration created successfully!")
    print(f"Config ARN: {config_response['EndpointConfigArn']}")
    
except sagemaker.exceptions.ClientError as e:
    print(f"Error creating endpoint configuration: {e}")
```

重要參數：
+ `VariantName`：此模型變體的識別符 （單一模型部署使用「主要」)
+ `ModelName`：參考上面建立的模型
+ `InitialInstanceCount`：要部署的執行個體數目 （從 1 開始，視需要稍後擴展）
+ `InstanceType`：在步驟 3 中選取 ML 執行個體類型

**驗證資源建立**

您可以驗證您的資源是否已成功建立：

```
# Describe the model
model_info = sagemaker.describe_model(ModelName=MODEL_NAME)
print(f"Model Status: {model_info['ModelName']} created")

# Describe the endpoint configuration
config_info = sagemaker.describe_endpoint_config(EndpointConfigName=ENDPOINT_CONFIG_NAME)
print(f"Endpoint Config Status: {config_info['EndpointConfigName']} created")
```

## 步驟 5：部署端點
<a name="nova-sagemaker-inference-step5"></a>

下一步是透過建立 SageMaker 即時端點來部署 Amazon Nova 模型。此端點會託管您的模型，並提供安全的 HTTPS 端點來提出推論請求。

端點建立通常需要 15-30 分鐘，因為 會 AWS 佈建基礎設施、下載模型成品，以及初始化推論容器。

**建立端點**

```
import time

try:
    endpoint_response = sagemaker.create_endpoint(
        EndpointName=ENDPOINT_NAME,
        EndpointConfigName=ENDPOINT_CONFIG_NAME
    )
    print("Endpoint creation initiated successfully!")
    print(f"Endpoint ARN: {endpoint_response['EndpointArn']}")
except Exception as e:
    print(f"Error creating endpoint: {e}")
```

**監控端點建立**

下列程式碼會輪詢端點狀態，直到部署完成：

```
# Monitor endpoint creation progress
print("Waiting for endpoint creation to complete...")
print("This typically takes 15-30 minutes...\n")

while True:
    try:
        response = sagemaker.describe_endpoint(EndpointName=ENDPOINT_NAME)
        status = response['EndpointStatus']
        
        if status == 'Creating':
            print(f"⏳ Status: {status} - Provisioning infrastructure and loading model...")
        elif status == 'InService':
            print(f"✅ Status: {status}")
            print("\nEndpoint creation completed successfully!")
            print(f"Endpoint Name: {ENDPOINT_NAME}")
            print(f"Endpoint ARN: {response['EndpointArn']}")
            break
        elif status == 'Failed':
            print(f"❌ Status: {status}")
            print(f"Failure Reason: {response.get('FailureReason', 'Unknown')}")
            print("\nFull response:")
            print(response)
            break
        else:
            print(f"Status: {status}")
        
    except Exception as e:
        print(f"Error checking endpoint status: {e}")
        break
    
    time.sleep(30)  # Check every 30 seconds
```

**確認端點已就緒**

一旦端點成為 InService，您就可以驗證其組態：

```
# Get detailed endpoint information
endpoint_info = sagemaker.describe_endpoint(EndpointName=ENDPOINT_NAME)

print("\n=== Endpoint Details ===")
print(f"Endpoint Name: {endpoint_info['EndpointName']}")
print(f"Endpoint ARN: {endpoint_info['EndpointArn']}")
print(f"Status: {endpoint_info['EndpointStatus']}")
print(f"Creation Time: {endpoint_info['CreationTime']}")
print(f"Last Modified: {endpoint_info['LastModifiedTime']}")

# Get endpoint config for instance type details
endpoint_config_name = endpoint_info['EndpointConfigName']
endpoint_config = sagemaker.describe_endpoint_config(EndpointConfigName=endpoint_config_name)

# Display production variant details
for variant in endpoint_info['ProductionVariants']:
    print(f"\nProduction Variant: {variant['VariantName']}")
    print(f"  Current Instance Count: {variant['CurrentInstanceCount']}")
    print(f"  Desired Instance Count: {variant['DesiredInstanceCount']}")
    # Get instance type from endpoint config
    for config_variant in endpoint_config['ProductionVariants']:
        if config_variant['VariantName'] == variant['VariantName']:
            print(f"  Instance Type: {config_variant['InstanceType']}")
            break
```

**對端點建立失敗進行故障診斷**

常見的失敗原因：
+ **容量不足**：請求的執行個體類型在您的區域無法使用
  + 解決方案：嘗試不同的執行個體類型或請求提高配額
+ **IAM 許可**：執行角色缺少必要的許可
  + 解決方案：確認角色可存取 Amazon S3 模型成品和必要的 SageMaker 許可
+ **找不到模型成品**：Amazon S3 URI 不正確或無法存取
  + 解決方案：驗證 Amazon S3 URI 並檢查儲存貯體許可，確保您位於正確的區域
+ **資源限制**：端點或執行個體超過帳戶限制
  + 解決方案：透過 Service Quotas 或 Support 請求提高服務配額 AWS 

**注意**  
如果您需要刪除失敗的端點並重新開始：  

```
sagemaker.delete_endpoint(EndpointName=ENDPOINT_NAME)
```

## 步驟 6：叫用端點
<a name="nova-sagemaker-inference-step6"></a>

一旦您的端點為 InService，您就可以傳送推論請求，以從 Amazon Nova 模型產生預測。SageMaker 支援同步端點 （即時串流/非串流模式） 和非同步端點 （以 Amazon S3 為基礎進行批次處理）。

**設定執行時間用戶端**

使用適當的逾時設定建立 SageMaker 執行期用戶端：

```
import json
import boto3
import botocore
from botocore.exceptions import ClientError

# Configure client with appropriate timeouts
config = botocore.config.Config(
    read_timeout=120,      # Maximum time to wait for response
    connect_timeout=10,    # Maximum time to establish connection
    retries={'max_attempts': 3}  # Number of retry attempts
)

# Create SageMaker Runtime client
runtime_client = boto3.client('sagemaker-runtime', config=config, region_name=REGION)
```

**建立通用推論函數**

下列 函數會同時處理串流和非串流請求：

```
def invoke_nova_endpoint(request_body):
    """
    Invoke Nova endpoint with automatic streaming detection.
    
    Args:
        request_body (dict): Request payload containing prompt and parameters
    
    Returns:
        dict: Response from the model (for non-streaming requests)
        None: For streaming requests (prints output directly)
    """
    body = json.dumps(request_body)
    is_streaming = request_body.get("stream", False)
    
    try:
        print(f"Invoking endpoint ({'streaming' if is_streaming else 'non-streaming'})...")
        
        if is_streaming:
            response = runtime_client.invoke_endpoint_with_response_stream(
                EndpointName=ENDPOINT_NAME,
                ContentType='application/json',
                Body=body
            )
            
            event_stream = response['Body']
            for event in event_stream:
                if 'PayloadPart' in event:
                    chunk = event['PayloadPart']
                    if 'Bytes' in chunk:
                        data = chunk['Bytes'].decode()
                        print("Chunk:", data)
        else:
            # Non-streaming inference
            response = runtime_client.invoke_endpoint(
                EndpointName=ENDPOINT_NAME,
                ContentType='application/json',
                Accept='application/json',
                Body=body
            )
            
            response_body = response['Body'].read().decode('utf-8')
            result = json.loads(response_body)
            print("✅ Response received successfully")
            return result
    
    except ClientError as e:
        error_code = e.response['Error']['Code']
        error_message = e.response['Error']['Message']
        print(f"❌ AWS Error: {error_code} - {error_message}")
    except Exception as e:
        print(f"❌ Unexpected error: {str(e)}")
```

**範例 1：非串流聊天完成**

使用聊天格式進行對話互動：

```
# Non-streaming chat request
chat_request = {
    "messages": [
        {"role": "user", "content": "Hello! How are you?"}
    ],
    "max_tokens": 100,
    "max_completion_tokens": 100,  # Alternative to max_tokens
    "stream": False,
    "temperature": 0.7,
    "top_p": 0.9,
    "top_k": 50,
    "logprobs": True,
    "top_logprobs": 3,
    "reasoning_effort": "low",  # Options: "low", "high"
    "allowed_token_ids": None,  # List of allowed token IDs
    "truncate_prompt_tokens": None,  # Truncate prompt to this many tokens
    "stream_options": None
}

response = invoke_nova_endpoint(chat_request)
```

**回應範例：**

```
{
    "id": "chatcmpl-123456",
    "object": "chat.completion",
    "created": 1234567890,
    "model": "default",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "Hello! I'm doing well, thank you for asking. I'm here and ready to help you with any questions or tasks you might have. How can I assist you today?"
            },
            "logprobs": {
                "content": [
                    {
                        "token": "Hello",
                        "logprob": -0.123,
                        "top_logprobs": [
                            {"token": "Hello", "logprob": -0.123},
                            {"token": "Hi", "logprob": -2.456},
                            {"token": "Hey", "logprob": -3.789}
                        ]
                    }
                    # Additional tokens...
                ]
            },
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 12,
        "completion_tokens": 28,
        "total_tokens": 40
    }
}
```

**範例 2：簡單文字完成**

使用完成格式產生簡單的文字：

```
# Simple completion request
completion_request = {
    "prompt": "The capital of France is",
    "max_tokens": 50,
    "stream": False,
    "temperature": 0.0,
    "top_p": 1.0,
    "top_k": -1,  # -1 means no limit
    "logprobs": 3,  # Number of log probabilities to return
    "allowed_token_ids": None,  # List of allowed token IDs
    "truncate_prompt_tokens": None,  # Truncate prompt to this many tokens
    "stream_options": None
}

response = invoke_nova_endpoint(completion_request)
```

**回應範例：**

```
{
    "id": "cmpl-789012",
    "object": "text_completion",
    "created": 1234567890,
    "model": "default",
    "choices": [
        {
            "text": " Paris.",
            "index": 0,
            "logprobs": {
                "tokens": [" Paris", "."],
                "token_logprobs": [-0.001, -0.002],
                "top_logprobs": [
                    {" Paris": -0.001, " London": -5.234, " Rome": -6.789},
                    {".": -0.002, ",": -4.567, "!": -7.890}
                ]
            },
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 6,
        "completion_tokens": 2,
        "total_tokens": 8
    }
}
```

**範例 3：串流聊天完成**

```
# Streaming chat request
streaming_request = {
    "messages": [
        {"role": "user", "content": "Tell me a short story about a robot"}
    ],
    "max_tokens": 200,
    "stream": True,
    "temperature": 0.7,
    "top_p": 0.95,
    "top_k": 40,
    "logprobs": True,
    "top_logprobs": 2,
    "reasoning_effort": "high",  # For more detailed reasoning
    "stream_options": {"include_usage": True}
}

invoke_nova_endpoint(streaming_request)
```

**串流輸出範例：**

```
Chunk: data: {"id":"chatcmpl-029ca032-fa01-4868-80b7-c4cb1af90fb9","object":"chat.completion.chunk","created":1772060532,"model":"default","choices":[{"index":0,"delta":{"role":"assistant","content":"","reasoning_content":null},"logprobs":null,"finish_reason":null}],"prompt_token_ids":null}
Chunk: data: {"id":"chatcmpl-029ca032-fa01-4868-80b7-c4cb1af90fb9","object":"chat.completion.chunk","created":1772060532,"model":"default","choices":[{"index":0,"delta":{"content":" Once","reasoning_content":null},"logprobs":{"content":[{"token":"\u2581Once","logprob":-0.6078429222106934,"bytes":[226,150,129,79,110,99,101],"top_logprobs":[{"token":"\u2581Once","logprob":-0.6078429222106934,"bytes":[226,150,129,79,110,99,101]},{"token":"\u2581In","logprob":-0.7864127159118652,"bytes":[226,150,129,73,110]}]}]},"finish_reason":null,"token_ids":null}]}
Chunk: data: {"id":"chatcmpl-029ca032-fa01-4868-80b7-c4cb1af90fb9","object":"chat.completion.chunk","created":1772060532,"model":"default","choices":[{"index":0,"delta":{"content":" upon","reasoning_content":null},"logprobs":{"content":[{"token":"\u2581upon","logprob":-0.0012345,"bytes":[226,150,129,117,112,111,110],"top_logprobs":[{"token":"\u2581upon","logprob":-0.0012345,"bytes":[226,150,129,117,112,111,110]},{"token":"\u2581a","logprob":-6.789,"bytes":[226,150,129,97]}]}]},"finish_reason":null,"token_ids":null}]}
Chunk: data: {"id":"chatcmpl-029ca032-fa01-4868-80b7-c4cb1af90fb9","object":"chat.completion.chunk","created":1772060532,"model":"default","choices":[{"index":0,"delta":{"content":" a","reasoning_content":null},"logprobs":{"content":[{"token":"\u2581a","logprob":-0.0001234,"bytes":[226,150,129,97],"top_logprobs":[{"token":"\u2581a","logprob":-0.0001234,"bytes":[226,150,129,97]},{"token":"\u2581time","logprob":-9.123,"bytes":[226,150,129,116,105,109,101]}]}]},"finish_reason":null,"token_ids":null}]}
Chunk: data: {"id":"chatcmpl-029ca032-fa01-4868-80b7-c4cb1af90fb9","object":"chat.completion.chunk","created":1772060532,"model":"default","choices":[{"index":0,"delta":{"content":" time","reasoning_content":null},"logprobs":{"content":[{"token":"\u2581time","logprob":-0.0023456,"bytes":[226,150,129,116,105,109,101],"top_logprobs":[{"token":"\u2581time","logprob":-0.0023456,"bytes":[226,150,129,116,105,109,101]},{"token":",","logprob":-6.012,"bytes":[44]}]}]},"finish_reason":null,"token_ids":null}]}

# Additional chunks...

Chunk: data: {"id":"chatcmpl-029ca032-fa01-4868-80b7-c4cb1af90fb9","object":"chat.completion.chunk","created":1772060532,"model":"default","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":15,"completion_tokens":87,"total_tokens":102}}
Chunk: data: [DONE]
```

**範例 4：多模式聊天完成**

針對影像和文字輸入使用多模式格式：

```
# Multimodal chat request (if supported by your model)
multimodal_request = {
    "messages": [
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "What's in this image?"},
                {"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,..."}}
            ]
        }
    ],
    "max_tokens": 150,
    "temperature": 0.3,
    "top_p": 0.8,
    "stream": False
}

response = invoke_nova_endpoint(multimodal_request)
```

**回應範例：**

```
{
    "id": "chatcmpl-345678",
    "object": "chat.completion",
    "created": 1234567890,
    "model": "default",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": "The image shows..."
            },
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 1250,
        "completion_tokens": 45,
        "total_tokens": 1295
    }
}
```

## 步驟 7：清除資源 （選用）
<a name="nova-sagemaker-inference-step7"></a>

為了避免產生不必要的費用，請刪除您在本教學課程中建立 AWS 的資源。SageMaker 端點會在執行時產生費用，即使您沒有主動提出推論請求。

**重要**  
刪除資源是永久的，無法復原。在繼續之前，請確定您不再需要這些資源。

**刪除端點**

```
import boto3

# Initialize SageMaker client
sagemaker = boto3.client('sagemaker', region_name=REGION)

try:
    print("Deleting endpoint...")
    sagemaker.delete_endpoint(EndpointName=ENDPOINT_NAME)
    print(f"✅ Endpoint '{ENDPOINT_NAME}' deletion initiated")
    print("Charges will stop once deletion completes (typically 2-5 minutes)")
except Exception as e:
    print(f"❌ Error deleting endpoint: {e}")
```

**注意**  
端點刪除是非同步的。您可以監控刪除狀態：  

```
import time

print("Monitoring endpoint deletion...")
while True:
    try:
        response = sagemaker.describe_endpoint(EndpointName=ENDPOINT_NAME)
        status = response['EndpointStatus']
        print(f"Status: {status}")
        time.sleep(10)
    except sagemaker.exceptions.ClientError as e:
        if e.response['Error']['Code'] == 'ValidationException':
            print("✅ Endpoint successfully deleted")
            break
        else:
            print(f"Error: {e}")
            break
```

**刪除端點組態**

刪除端點後，移除端點組態：

```
try:
    print("Deleting endpoint configuration...")
    sagemaker.delete_endpoint_config(EndpointConfigName=ENDPOINT_CONFIG_NAME)
    print(f"✅ Endpoint configuration '{ENDPOINT_CONFIG_NAME}' deleted")
except Exception as e:
    print(f"❌ Error deleting endpoint configuration: {e}")
```

**刪除模型**

移除 SageMaker 模型物件：

```
try:
    print("Deleting model...")
    sagemaker.delete_model(ModelName=MODEL_NAME)
    print(f"✅ Model '{MODEL_NAME}' deleted")
except Exception as e:
    print(f"❌ Error deleting model: {e}")
```

# API 參考
<a name="nova-sagemaker-inference-api-reference"></a>

SageMaker 上的 Amazon Nova 模型使用標準 SageMaker 執行期 API 進行推論。如需完整的 API 文件，請參閱[測試部署的模型](https://docs.aws.amazon.com//sagemaker/latest/dg/realtime-endpoints-test-endpoints.html)。

## 端點調用
<a name="nova-sagemaker-inference-api-invocation"></a>

SageMaker 上的 Amazon Nova 模型支援兩種叫用方法：
+ **同步調用**：將 [InvokeEndpoint](https://docs.aws.amazon.com//sagemaker/latest/APIReference/API_runtime_InvokeEndpoint.html) API 用於即時、非串流推論請求。
+ **串流調用**：使用 [InvokeEndpointWithResponseStream](https://docs.aws.amazon.com//sagemaker/latest/APIReference/API_runtime_InvokeEndpointWithResponseStream.html) API 進行即時串流推論請求。

## 要求格式
<a name="nova-sagemaker-inference-api-request"></a>

Amazon Nova 模型支援兩種請求格式：

**聊天完成格式**

使用此格式進行對話互動：

```
{
  "messages": [
    {"role": "user", "content": "string"}
  ],
  "max_tokens": integer,
  "max_completion_tokens": integer,
  "stream": boolean,
  "temperature": float,
  "top_p": float,
  "top_k": integer,
  "logprobs": boolean,
  "top_logprobs": integer,
  "reasoning_effort": "low" | "high",
  "allowed_token_ids": [integer],
  "truncate_prompt_tokens": integer,
  "stream_options": {
    "include_usage": boolean
  }
}
```

**文字完成格式**

使用此格式產生簡單的文字：

```
{
  "prompt": "string",
  "max_tokens": integer,
  "stream": boolean,
  "temperature": float,
  "top_p": float,
  "top_k": integer,
  "logprobs": integer,
  "allowed_token_ids": [integer],
  "truncate_prompt_tokens": integer,
  "stream_options": {
    "include_usage": boolean
  }
}
```

**多模式聊天完成格式**

針對影像和文字輸入使用此格式：

```
{
  "messages": [
    {
      "role": "user",
      "content": [
        {"type": "text", "text": "What's in this image?"},
        {"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,..."}}
      ]
    }
  ],
  "max_tokens": integer,
  "temperature": float,
  "top_p": float,
  "stream": boolean
}
```

**請求參數**
+ `messages` （陣列）：適用於聊天完成格式。具有 `role`和 `content` 欄位的訊息物件陣列。內容可以是純文字的字串，也可以是多模式輸入的陣列。
+ `prompt` （字串）：適用於文字完成格式。要從中產生的輸入文字。
+ `max_tokens` （整數）：回應中要產生的字符數目上限。範圍：1 或更高。
+ `max_completion_tokens` （整數）：完成聊天時 max\$1tokens 的替代方案。要產生的完成權杖數目上限。
+ `temperature` （浮點數）：控制產生時的隨機性。範圍：0.0 到 2.0 (0.0 = 確定性，2.0 = 最大隨機性）。
+ `top_p` （浮點數）：Nucleus 取樣閾值。範圍：1e-10 到 1.0。
+ `top_k` （整數）：將權杖選擇限制為 K 最有可能的權杖。範圍：-1 或更高 (-1 = 無限制）。
+ `stream` （布林值）：是否串流回應。將 設為 `true` 以進行串流，將 設為 `false`以進行非串流。
+ `logprobs` （布林值/整數）：對於聊天完成，請使用布林值。對於文字完成，請針對要傳回的日誌機率使用整數。範圍：1 到 20。
+ `top_logprobs` （整數）：最有可能傳回日誌機率的字符數量 （僅限聊天完成）。
+ `reasoning_effort` （字串）：推理工作的程度。選項：「低」、「高」（僅限 Nova 2 Lite 自訂模型的聊天完成）。
+ `allowed_token_ids` （陣列）：允許產生的字符 IDs清單。將輸出限制為指定的字符。
+ `truncate_prompt_tokens` （整數）：如果超過限制，請將提示截斷為許多字符。
+ `stream_options` （物件）：串流回應的選項。包含`include_usage`布林值，以在串流回應中包含字符用量。

## 回應格式
<a name="nova-sagemaker-inference-api-response"></a>

回應格式取決於呼叫方法和請求類型：

**聊天完成回應 （非串流）**

對於同步聊天完成請求：

```
{
  "id": "chatcmpl-123e4567-e89b-12d3-a456-426614174000",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "nova-micro-custom",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hello! I'm doing well, thank you for asking. How can I help you today?",
        "refusal": null,
        "reasoning": null,
        "reasoning_content": null
      },
      "logprobs": {
        "content": [
          {
            "token": "Hello",
            "logprob": -0.31725305,
            "bytes": [72, 101, 108, 108, 111],
            "top_logprobs": [
              {
                "token": "Hello",
                "logprob": -0.31725305,
                "bytes": [72, 101, 108, 108, 111]
              },
              {
                "token": "Hi",
                "logprob": -1.3190403,
                "bytes": [72, 105]
              }
            ]
          }
        ]
      },
      "finish_reason": "stop",
      "stop_reason": null,
      "token_ids": [9906, 0, 358, 2157, 1049, 11, 1309, 345, 369, 6464, 13]
    }
  ],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21,
    "prompt_tokens_details": {
      "cached_tokens": 0
    }
  },
  "prompt_token_ids": [9906, 0, 358]
}
```

**文字完成回應 （非串流）**

對於同步文字完成請求：

```
{
  "id": "cmpl-123e4567-e89b-12d3-a456-426614174000",
  "object": "text_completion",
  "created": 1677652288,
  "model": "nova-micro-custom",
  "choices": [
    {
      "index": 0,
      "text": "Paris, the capital and most populous city of France.",
      "logprobs": {
        "tokens": ["Paris", ",", " the", " capital"],
        "token_logprobs": [-0.31725305, -0.07918124, -0.12345678, -0.23456789],
        "top_logprobs": [
          {
            "Paris": -0.31725305,
            "London": -1.3190403,
            "Rome": -2.1234567
          },
          {
            ",": -0.07918124,
            " is": -1.2345678
          }
        ]
      },
      "finish_reason": "stop",
      "stop_reason": null,
      "prompt_token_ids": [464, 6864, 315, 4881, 374],
      "token_ids": [3915, 11, 279, 6864, 323, 1455, 95551, 3363, 315, 4881, 13]
    }
  ],
  "usage": {
    "prompt_tokens": 5,
    "completion_tokens": 11,
    "total_tokens": 16,
    "prompt_tokens_details": {
      "cached_tokens": 0
    }
  }
}
```

**聊天完成串流回應**

對於串流聊天完成請求，回應會以伺服器傳送事件 (SSE) 的形式傳送：

```
data: {
  "id": "chatcmpl-123e4567-e89b-12d3-a456-426614174000",
  "object": "chat.completion.chunk",
  "created": 1677652288,
  "model": "nova-micro-custom",
  "choices": [
    {
      "index": 0,
      "delta": {
        "role": "assistant",
        "content": "Hello",
        "refusal": null,
        "reasoning": null,
        "reasoning_content": null
      },
      "logprobs": {
        "content": [
          {
            "token": "Hello",
            "logprob": -0.31725305,
            "bytes": [72, 101, 108, 108, 111],
            "top_logprobs": [
              {
                "token": "Hello",
                "logprob": -0.31725305,
                "bytes": [72, 101, 108, 108, 111]
              }
            ]
          }
        ]
      },
      "finish_reason": null,
      "stop_reason": null
    }
  ],
  "usage": null,
  "prompt_token_ids": null
}

data: {
  "id": "chatcmpl-123e4567-e89b-12d3-a456-426614174000",
  "object": "chat.completion.chunk",
  "created": 1677652288,
  "model": "nova-micro-custom",
  "choices": [
    {
      "index": 0,
      "delta": {
        "content": "! I'm"
      },
      "logprobs": null,
      "finish_reason": null,
      "stop_reason": null
    }
  ],
  "usage": null
}

data: {
  "id": "chatcmpl-123e4567-e89b-12d3-a456-426614174000",
  "object": "chat.completion.chunk",
  "created": 1677652288,
  "model": "nova-micro-custom",
  "choices": [
    {
      "index": 0,
      "delta": {},
      "finish_reason": "stop",
      "stop_reason": null
    }
  ],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21,
    "prompt_tokens_details": {
      "cached_tokens": 0
    }
  }
}

data: [DONE]
```

**文字完成串流回應**

對於串流文字完成請求：

```
data: {
  "id": "cmpl-123e4567-e89b-12d3-a456-426614174000",
  "object": "text_completion",
  "created": 1677652288,
  "model": "nova-micro-custom",
  "choices": [
    {
      "index": 0,
      "text": "Paris",
      "logprobs": {
        "tokens": ["Paris"],
        "token_logprobs": [-0.31725305],
        "top_logprobs": [
          {
            "Paris": -0.31725305,
            "London": -1.3190403
          }
        ]
      },
      "finish_reason": null,
      "stop_reason": null
    }
  ],
  "usage": null
}

data: {
  "id": "cmpl-123e4567-e89b-12d3-a456-426614174000",
  "object": "text_completion",
  "created": 1677652288,
  "model": "nova-micro-custom",
  "choices": [
    {
      "index": 0,
      "text": ", the capital",
      "logprobs": null,
      "finish_reason": null,
      "stop_reason": null
    }
  ],
  "usage": null
}

data: {
  "id": "cmpl-123e4567-e89b-12d3-a456-426614174000",
  "object": "text_completion",
  "created": 1677652288,
  "model": "nova-micro-custom",
  "choices": [
    {
      "index": 0,
      "text": "",
      "finish_reason": "stop",
      "stop_reason": null
    }
  ],
  "usage": {
    "prompt_tokens": 5,
    "completion_tokens": 11,
    "total_tokens": 16
  }
}

data: [DONE]
```

**回應欄位說明**
+ `id`：完成的唯一識別符
+ `object`：傳回的物件類型 ("chat.completion"、"text\$1completion"、"chat.completion.chunk")
+ `created`：建立完成時的 Unix 時間戳記
+ `model`：用於完成的模型
+ `choices`：完成選項陣列
+ `usage`：字符用量資訊，包括提示、完成和字符總數
+ `logprobs`：字符的日誌機率資訊 （請求時）
+ `finish_reason`：模型停止產生的原因 ("stop"、"length"、"content\$1filter")
+ `delta`：串流回應中的增量內容
+ `reasoning`：使用 reasoning\$1effort 時合理的內容
+ `token_ids`：產生文字的字符 IDs 陣列

如需完整的 API 文件，請參閱 [InvokeEndpoint API 參考](https://docs.aws.amazon.com//sagemaker/latest/APIReference/API_runtime_InvokeEndpoint.html)和 [InvokeEndpointWithResponseStream API 參考](https://docs.aws.amazon.com//sagemaker/latest/APIReference/API_runtime_InvokeEndpointWithResponseStream.html)。

# 評估託管在 SageMaker 推論上的模型
<a name="nova-eval-on-sagemaker-inference"></a>

本指南說明如何使用 [Inspect AI](https://github.com/UKGovernmentBEIS/inspect_ai) 開放原始碼評估架構，評估部署在 SageMaker 推論端點上的自訂 Amazon Nova 模型。

**注意**  
如需實作演練，請參閱 [SageMaker Inspect AI 快速入門筆記本](https://github.com/aws-samples/amazon-nova-samples/tree/main/customization/sagemaker-inference/sagemaker_inspect_quickstart.ipynb)。

## 概觀
<a name="nova-eval-sagemaker-overview"></a>

您可以使用 AI 研究社群的標準化基準，評估部署在 SageMaker 端點上的自訂 Amazon Nova 模型。此方法可讓您：
+ 大規模評估自訂的 Amazon Nova 模型 （微調、分割或以其他方式調整）
+ 跨多個端點執行個體以平行推論執行評估
+ 使用 MMLU、TruthfulQA 和 HumanEval 等基準比較模型效能
+ 與您現有的 SageMaker 基礎設施整合

## 支援的模型
<a name="nova-eval-sagemaker-supported-models"></a>

SageMaker 推論提供者適用於：
+ Amazon Nova 模型 (Nova Micro、Nova Lite、Nova Lite 2)
+ 透過 vLLM 或 OpenAI 相容推論伺服器部署的模型
+ 支援 OpenAI 聊天完成 API 格式的任何端點

## 先決條件
<a name="nova-eval-sagemaker-prerequisites"></a>

開始前，請確保您具備以下條件：
+  AWS 帳戶 具有建立和叫用 SageMaker 端點許可的
+ AWS 透過 AWS CLI、環境變數或 IAM 角色設定的登入資料
+ Python 3.9 或更新版本

**必要的 IAM 許可**

您的 IAM 使用者或角色需要下列許可：

```
{
  "Version": "2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "sagemaker:InvokeEndpoint",
        "sagemaker:DescribeEndpoint"
      ],
      "Resource": "arn:aws:sagemaker:*:*:endpoint/*"
    }
  ]
}
```

## 步驟 1：部署 SageMaker 端點
<a name="nova-eval-sagemaker-step1"></a>

在執行評估之前，您需要執行模型的 SageMaker 推論端點。

如需使用 Amazon Nova 模型建立 SageMaker 推論端點的說明，請參閱 [開始使用](nova-sagemaker-inference-getting-started.md)。

一旦您的端點處於 `InService` 狀態，請記下要用於評估命令的端點名稱。

## 步驟 2：安裝評估相依性
<a name="nova-eval-sagemaker-step2"></a>

建立 Python 虛擬環境並安裝必要的套件。

```
# Create virtual environment
python3.12 -m venv venv
source venv/bin/activate

# Install uv for faster package installation
pip install uv

# Install Inspect AI and evaluation benchmarks
uv pip install inspect-ai inspect-evals

# Install AWS dependencies
uv pip install aioboto3 boto3 botocore openai
```

## 步驟 3：設定 AWS 登入資料
<a name="nova-eval-sagemaker-step3"></a>

選擇下列其中一種身分驗證方法：

**選項 1： AWS CLI （建議）**

```
aws configure
```

出現提示時，輸入您的 AWS 存取金鑰 ID、私密存取金鑰和預設區域。

**選項 2：環境變數**

```
export AWS_ACCESS_KEY_ID=your_access_key
export AWS_SECRET_ACCESS_KEY=your_secret_key
export AWS_DEFAULT_REGION=us-west-2
```

**選項 3：IAM 角色**

如果在 Amazon EC2 或 SageMaker 筆記本上執行，會自動使用執行個體的 IAM 角色。

**驗證登入資料**

```
import boto3

sts = boto3.client('sts')
identity = sts.get_caller_identity()
print(f"Account: {identity['Account']}")
print(f"User/Role: {identity['Arn']}")
```

## 步驟 4：安裝 SageMaker 提供者
<a name="nova-eval-sagemaker-step4"></a>

SageMaker 提供者可讓 Inspect AI 與您的 SageMaker 端點通訊。在[快速入門筆記本](https://github.com/aws-samples/amazon-nova-samples/tree/main/customization/sagemaker-inference/sagemaker_inspect_quickstart.ipynb)中簡化了提供者安裝程序。

## 步驟 5：下載評估基準
<a name="nova-eval-sagemaker-step5"></a>

複製檢查 Evals 儲存庫以存取標準基準：

```
git clone https://github.com/UKGovernmentBEIS/inspect_evals.git
```

此儲存庫包含基準，例如：
+ MMLU 和 MMLU-Pro （知識和推理）
+ TruthfulQA （真實性）
+ HumanEval （程式碼產生）
+ GSM8K （數學推理）

## 步驟 6：執行評估
<a name="nova-eval-sagemaker-step6"></a>

使用您的 SageMaker 端點執行評估：

```
cd inspect_evals/src/inspect_evals/

inspect eval mmlu_pro/mmlu_pro.py \
  --model sagemaker/my-nova-endpoint \
  -M region_name=us-west-2 \
  --max-connections 256 \
  --max-retries 100 \
  --display plain
```

**金鑰參數**


| 參數 | 預設 | Description | 
| --- | --- | --- | 
| --max-connections | 10 | 對端點的平行請求數量。使用執行個體計數擴展 （例如，10 個執行個體 × 25 = 250)。 | 
| --max-retries | 3 | 重試失敗請求的嘗試。使用 50-100 進行大型評估。 | 
| -M region\$1name | us-east-1 | AWS 部署端點的區域。 | 
| -M read\$1timeout | 600 | 請求以秒為單位的逾時。 | 
| -M connect\$1timeout | 60 | 連線逾時，以秒為單位。 | 

**調校建議**

對於多執行個體端點：

```
# 10-instance endpoint example
--max-connections 250   # ~25 connections per instance
--max-retries 100       # Handle transient errors
```

設定`--max-connections`過高可能會壓倒端點並導致限流。設定太低，未充分利用容量。

## 步驟 7：檢視結果
<a name="nova-eval-sagemaker-step7"></a>

啟動檢查 AI 檢視器以分析評估結果：

```
inspect view
```

檢視器會顯示：
+ 整體分數和指標
+ 每個範例結果與模型回應
+ 錯誤分析和失敗模式

## 管理端點
<a name="nova-eval-sagemaker-managing-endpoints"></a>

**更新端點**

若要使用新模型或組態更新現有端點：

```
import boto3

sagemaker = boto3.client('sagemaker', region_name=REGION)

# Create new model and endpoint configuration
# Then update the endpoint
sagemaker.update_endpoint(
    EndpointName=EXISTING_ENDPOINT_NAME,
    EndpointConfigName=NEW_ENDPOINT_CONFIG_NAME
)
```

**刪除端點**

```
sagemaker.delete_endpoint(EndpointName=ENDPOINT_NAME)
```

## 加入自訂基準
<a name="nova-eval-sagemaker-custom-benchmarks"></a>

您可以使用下列工作流程，將新基準新增至檢查 AI：

1. 研究基準的資料集格式和評估指標

1. 在 中檢閱類似的實作 `inspect_evals/`

1. 建立任務檔案，將資料集記錄轉換為檢查 AI 範例

1. 實作適當的求解器和評分器

1. 使用小型測試執行進行驗證

範例任務結構：

```
from inspect_ai import Task, task
from inspect_ai.dataset import hf_dataset
from inspect_ai.scorer import choice
from inspect_ai.solver import multiple_choice

@task
def my_benchmark():
    return Task(
        dataset=hf_dataset("dataset_name", split="test"),
        solver=multiple_choice(),
        scorer=choice()
    )
```

## 疑難排解
<a name="nova-eval-sagemaker-troubleshooting"></a>

**常見問題**

**端點限流或逾時**
+ 減少 `--max-connections`
+ 增加 `--max-retries`
+ 檢查端點 CloudWatch 指標是否有容量問題

**身分驗證錯誤**
+ 確認已正確設定 AWS 登入資料
+ 檢查 IAM 許可包括 `sagemaker:InvokeEndpoint`

**模型錯誤**
+ 確認端點處於 `InService` 狀態
+ 檢查模型是否支援 OpenAI 聊天完成 API 格式

## 相關資源
<a name="nova-eval-sagemaker-related-resources"></a>
+ [檢查 AI 文件](https://inspect.ai-safety-institute.org.uk/)
+ [檢查 Evals 儲存庫](https://github.com/UKGovernmentBEIS/inspect_evals)
+ [SageMaker 開發人員指南](https://docs.aws.amazon.com//sagemaker/latest/dg/whatis.html)
+ [部署模型以進行推論](https://docs.aws.amazon.com//sagemaker/latest/dg/deploy-model.html)
+ [設定 AWS CLI](https://docs.aws.amazon.com//cli/latest/userguide/cli-chap-configure.html)

# 在 Amazon SageMaker 推論濫用偵測中部署 Amazon Nova Forge 模型
<a name="nova-sagemaker-inference-abuse-detection"></a>

AWS 致力於負責任地使用 AI。為了協助防止潛在的濫用，當您在 Amazon SageMaker 推論中部署 Amazon Nova Forge 模型時，SageMaker 推論會實作自動濫用偵測機制，以識別可能違反 AWS[可接受的使用政策和](https://aws.amazon.com/aup/)服務條款，包括[負責任的 AI 政策](https://aws.amazon.com/ai/responsible-ai/policy/)。

我們的濫用偵測機制是完全自動化的，因此沒有人工審查或存取使用者輸入或模型輸出。

自動濫用偵測包括：
+ **分類內容** – 我們使用分類器來偵測使用者輸入和模型輸出中的有害內容 （例如煽動暴力的內容）。分類器是一種演算法，用於處理模型輸入和輸出，並指派傷害類型和可信度。我們可能會在 Amazon Nova Forge Model 用量上執行這些分類器。分類程序會自動執行，不涉及對使用者輸入或模型輸出進行人工審查。
+ **識別模式** – 我們使用分類器指標來識別潛在的違規和重複行為。我們可能會編譯和共用匿名分類器指標。Amazon SageMaker Inference 不會儲存使用者輸入或模型輸出。
+ **偵測和封鎖兒童性濫用資料 (CSAM)** – 您對您 （和最終使用者） 上傳至 Amazon SageMaker Inference 的內容負責，並且必須確保此內容沒有非法影像。為了協助停止 CSAM 的傳播，在 Amazon SageMaker 推論中部署 Amazon Nova Forge 模型時，SageMaker 推論可能會使用自動濫用偵測機制 （例如雜湊比對技術或分類器） 來偵測明顯的 CSAM。如果 Amazon SageMaker Inference 在您的映像輸入中偵測到明顯的 CSAM，Amazon SageMaker Inference 會封鎖請求，而且您會收到自動錯誤訊息。Amazon SageMaker Inference 也可以向國家缺少和入侵兒童中心 (NCMEC) 或相關當局提交報告。我們非常重視 CSAM，並將繼續更新我們的偵測、封鎖和報告機制。相關法律可能會要求您採取其他動作，而且您必須對這些動作負責。

一旦我們的自動濫用偵測機制發現潛在違規，我們可能會請求您使用 Amazon SageMaker 推論以及遵守服務條款的相關資訊。如果您沒有回應、不願意或無法遵守這些條款或政策， AWS 可能會暫停您對 Amazon SageMaker 推論的存取。如果我們的自動化測試偵測到模型回應與我們的條款和政策不一致，則可能會向您收取失敗的推論任務的費用。

如果您有其他問題，請聯絡 AWS Support。如需詳細資訊，請參閱 [Amazon SageMaker FAQs](https://aws.amazon.com/sagemaker/ai/faqs/)。