

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# エンドポイントを呼び出す
<a name="canvas-deploy-model-invoke"></a>

**注記**  
SageMaker AI エンドポイントをプログラムで呼び出す前に、[Amazon SageMaker Canvas でモデルのデプロイをテスト](canvas-deploy-model-test.md)することをお勧めします。

SageMaker AI エンドポイントにデプロイした Amazon SageMaker Canvas モデルは、本番環境でアプリケーションとともに使用できます。他の [SageMaker AI リアルタイムエンドポイント](https://docs.aws.amazon.com/sagemaker/latest/dg/realtime-endpoints.html)を呼び出すのと同じ方法で、エンドポイントをプログラムで呼び出します。エンドポイントをプログラムで呼び出すと、「[デプロイをテストする](canvas-deploy-model-test.md)」で説明されているのと同じフィールドを含むレスポンスオブジェクトが返されます。

エンドポイントをプログラムで呼び出す方法の詳細については、「[リアルタイム推論用のモデルを呼び出す](realtime-endpoints-test-endpoints.md)」を参照してください。

次の Python の例は、モデルタイプに基づいてエンドポイントを呼び出す方法を示します。

## JumpStart 基盤モデル
<a name="canvas-invoke-js-example"></a>

次の例は、エンドポイントにデプロイした JumpStart 基盤モデルを呼び出す方法を示します。

```
import boto3
import pandas as pd

client = boto3.client("runtime.sagemaker")
body = pd.DataFrame(
    [['feature_column1', 'feature_column2'], 
    ['feature_column1', 'feature_column2']]
).to_csv(header=False, index=False).encode("utf-8")
    
response = client.invoke_endpoint(
    EndpointName="endpoint_name",
    ContentType="text/csv",
    Body=body,
    Accept="application/json"
)
```

## 数値予測モデルとカテゴリ予測モデル
<a name="canvas-invoke-tabular-example"></a>

次の例は、数値予測モデルまたはカテゴリ予測モデルを呼び出す方法を示します。

```
import boto3
import pandas as pd

client = boto3.client("runtime.sagemaker")
body = pd.DataFrame(['feature_column1', 'feature_column2'], ['feature_column1', 'feature_column2']).to_csv(header=False, index=False).encode("utf-8")
    
response = client.invoke_endpoint(
    EndpointName="endpoint_name",
    ContentType="text/csv",
    Body=body,
    Accept="application/json"
)
```

## 時系列予測モデル
<a name="canvas-invoke-forecast-example"></a>

次の例は、時系列予測モデルを呼び出す方法を示します。時系列予測モデルの呼び出しをテストする方法の完全な例については、「[Time-Series Forecasting with Amazon SageMaker Autopilot](https://github.com/aws/amazon-sagemaker-examples/blob/eef13dae197a6e588a8bc111aba3244f99ee0fbb/autopilot/autopilot_time_series.ipynb)」を参照してください。

```
import boto3
import pandas as pd

csv_path = './real-time-payload.csv'
data = pd.read_csv(csv_path)

client = boto3.client("runtime.sagemaker")

body = data.to_csv(index=False).encode("utf-8")
    
response = client.invoke_endpoint(
    EndpointName="endpoint_name",
    ContentType="text/csv",
    Body=body,
    Accept="application/json"
)
```

## 画像予測モデル
<a name="canvas-invoke-cv-example"></a>

次の例は、画像予測モデルを呼び出す方法を示します。

```
import boto3
client = boto3.client("runtime.sagemaker")
with open("example_image.jpg", "rb") as file:
    body = file.read()
    response = client.invoke_endpoint(
        EndpointName="endpoint_name",
        ContentType="application/x-image",
        Body=body,
        Accept="application/json"
    )
```

## テキスト予測モデル
<a name="canvas-invoke-nlp-example"></a>

次の例は、テキスト予測モデルを呼び出す方法を示します。

```
import boto3
import pandas as pd

client = boto3.client("runtime.sagemaker")
body = pd.DataFrame([["Example text 1"], ["Example text 2"]]).to_csv(header=False, index=False).encode("utf-8")
    
response = client.invoke_endpoint(
    EndpointName="endpoint_name",
    ContentType="text/csv",
    Body=body,
    Accept="application/json"
)
```