

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

# 測試註釋前與註釋後 Lambda 函式
<a name="sms-custom-templates-step3-lambda-test"></a>

您可在 Lambda 主控台測試註釋前與註釋後 Lambda 函式。如您是 Lambda 的新使用者，可透過 AWS Lambda 開發人員指南的主控台利用[建立 Lambda 函式](https://docs.aws.amazon.com/lambda/latest/dg/getting-started-create-function.html#gettingstarted-zip-function)教學課程來了解如何在主控台測試或*調用* Lambda 函式。您可以使用此頁面上的區段，了解如何測試透過 AWS Serverless Application Repository (SAR) 提供的 Ground Truth 註釋前和註釋後範本。

**Topics**
+ [先決條件](#sms-custom-templates-step3-lambda-test-pre)
+ [測試註釋前 Lambda 函式](#sms-custom-templates-step3-lambda-test-pre-annotation)
+ [測試註釋後 Lambda 函式](#sms-custom-templates-step3-lambda-test-post-annotation)

## 先決條件
<a name="sms-custom-templates-step3-lambda-test-pre"></a>

您必須執行下列動作，才能採用本頁所述的測試。
+ 您需要 Lambda 主控台的存取權，且您需要權限以便建立及調用 Lambda 函式。若要了解如何設定這些權限，請參閱[授予建立和選取 AWS Lambda 函數的許可](sms-custom-templates-step3-lambda-permissions.md#sms-custom-templates-step3-postlambda-create-perms)。
+ 如您尚未部署 Ground Truth SAR 配方，請利用[使用 Ground Truth 範本建立 Lambda 函式](sms-custom-templates-step3-lambda-create.md)的程序來執行此操作。
+ 若要測試註釋後 Lambda 函式，您必須在 Amazon S3 擁有包含範例註釋資料的資料檔案。對於簡單測試，您可將下列程式碼複製並貼到檔案，然後另存新檔為 `sample-annotations.json` 並[將其上傳到 Amazon S3](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html)。請注意此檔案的 S3 URI，您需要此資訊才能設定註釋後 Lambda 測試。

  ```
  [{"datasetObjectId":"0","dataObject":{"content":"To train a machine learning model, you need a large, high-quality, labeled dataset. Ground Truth helps you build high-quality training datasets for your machine learning models."},"annotations":[{"workerId":"private.us-west-2.0123456789","annotationData":{"content":"{\"crowd-entity-annotation\":{\"entities\":[{\"endOffset\":8,\"label\":\"verb\",\"startOffset\":3},{\"endOffset\":27,\"label\":\"adjective\",\"startOffset\":11},{\"endOffset\":33,\"label\":\"object\",\"startOffset\":28},{\"endOffset\":51,\"label\":\"adjective\",\"startOffset\":46},{\"endOffset\":65,\"label\":\"adjective\",\"startOffset\":53},{\"endOffset\":74,\"label\":\"adjective\",\"startOffset\":67},{\"endOffset\":82,\"label\":\"adjective\",\"startOffset\":75},{\"endOffset\":102,\"label\":\"verb\",\"startOffset\":97},{\"endOffset\":112,\"label\":\"verb\",\"startOffset\":107},{\"endOffset\":125,\"label\":\"adjective\",\"startOffset\":113},{\"endOffset\":134,\"label\":\"adjective\",\"startOffset\":126},{\"endOffset\":143,\"label\":\"object\",\"startOffset\":135},{\"endOffset\":169,\"label\":\"adjective\",\"startOffset\":153},{\"endOffset\":176,\"label\":\"object\",\"startOffset\":170}]}}"}}]},{"datasetObjectId":"1","dataObject":{"content":"Sift 3 cups of flour into the bowl."},"annotations":[{"workerId":"private.us-west-2.0123456789","annotationData":{"content":"{\"crowd-entity-annotation\":{\"entities\":[{\"endOffset\":4,\"label\":\"verb\",\"startOffset\":0},{\"endOffset\":6,\"label\":\"number\",\"startOffset\":5},{\"endOffset\":20,\"label\":\"object\",\"startOffset\":15},{\"endOffset\":34,\"label\":\"object\",\"startOffset\":30}]}}"}}]},{"datasetObjectId":"2","dataObject":{"content":"Jen purchased 10 shares of the stock on Janurary 1st, 2020."},"annotations":[{"workerId":"private.us-west-2.0123456789","annotationData":{"content":"{\"crowd-entity-annotation\":{\"entities\":[{\"endOffset\":3,\"label\":\"person\",\"startOffset\":0},{\"endOffset\":13,\"label\":\"verb\",\"startOffset\":4},{\"endOffset\":16,\"label\":\"number\",\"startOffset\":14},{\"endOffset\":58,\"label\":\"date\",\"startOffset\":40}]}}"}}]},{"datasetObjectId":"3","dataObject":{"content":"The narrative was interesting, however the character development was weak."},"annotations":[{"workerId":"private.us-west-2.0123456789","annotationData":{"content":"{\"crowd-entity-annotation\":{\"entities\":[{\"endOffset\":29,\"label\":\"adjective\",\"startOffset\":18},{\"endOffset\":73,\"label\":\"adjective\",\"startOffset\":69}]}}"}}]}]
  ```
+ 您必須遵循[授予註釋後 Lambda 權限以便存取註釋](sms-custom-templates-step3-lambda-permissions.md#sms-custom-templates-step3-postlambda-perms)的指示，授予註釋後 Lambda 函式的執行角色權限，以便擔任您用來建立標籤工作的 SageMaker AI 執行角色。註釋後 Lambda 函式使用 SageMaker AI 執行角色來存取 S3 的註釋資料檔案 `sample-annotations.json`。



## 測試註釋前 Lambda 函式
<a name="sms-custom-templates-step3-lambda-test-pre-annotation"></a>

使用下列程序來測試部署 Ground Truth AWS Serverless Application Repository (SAR) 配方時建立的註釋前 Lambda 函數。

**測試 Ground Truth SAR 配方註釋前 Lambda 函式**

1. 開啟 Lambda 主控台的 [**Functions** (函式) 頁面](https://console.aws.amazon.com/lambda/home#/functions)。

1. 從 Ground Truth SAR 配方選取部署的註釋前函式。此函式名稱類似 `serverlessrepo-aws-sagema-GtRecipePreHumanTaskFunc-<id>`。

1. 在 **Code source** (程式碼來源) 區段，選取 **Test** (測試) 旁邊的箭頭。

1. 選取 **Configure test event** (設定測試事件)。

1. 保持選取 **Create new test event** (建立新測試事件) 選項。

1. 從 **Event template** (事件範本)，選取 **SageMaker Ground Truth PreHumanTask**。

1. 為測試指定 **Event name** (事件名稱)。

1. 選取**建立**。

1. 再次選取 **Test** (測試) 旁邊的箭頭，您應會看到已選取您建立的測試 (由事件名稱以點表示)。如未選取，請選取。

1. 選取 **Test** (測試)，即可執行測試。

在執行測試之後，您可看到 **Execution results** (執行結果)。您應會在 **Function logs** (函式日誌) 看到類似下列回應：

```
START RequestId: cd117d38-8365-4e1a-bffb-0dcd631a878f Version: $LATEST
Received event: {
  "version": "2018-10-16",
  "labelingJobArn": "arn:aws:sagemaker:us-east-2:123456789012:labeling-job/example-job",
  "dataObject": {
    "source-ref": "s3://sagemakerexample/object_to_annotate.jpg"
  }
}
{'taskInput': {'taskObject': 's3://sagemakerexample/object_to_annotate.jpg'}, 'isHumanAnnotationRequired': 'true'}
END RequestId: cd117d38-8365-4e1a-bffb-0dcd631a878f
REPORT RequestId: cd117d38-8365-4e1a-bffb-0dcd631a878f	Duration: 0.42 ms	Billed Duration: 1 ms	Memory Size: 128 MB	Max Memory Used: 43 MB
```

我們可在此回應看到 Lambda 函式的輸出符合所需的註釋前回應語法：

```
{'taskInput': {'taskObject': 's3://sagemakerexample/object_to_annotate.jpg'}, 'isHumanAnnotationRequired': 'true'}
```

## 測試註釋後 Lambda 函式
<a name="sms-custom-templates-step3-lambda-test-post-annotation"></a>

使用下列程序來測試部署 Ground Truth AWS Serverless Application Repository (SAR) 配方時建立的註釋後 Lambda 函數。

**測試 Ground Truth SAR 配方註釋後 Lambda**

1. 開啟 Lambda 主控台的 [**Functions** (函式) 頁面](https://console.aws.amazon.com/lambda/home#/functions)。

1. 從 Ground Truth SAR 配方選取部署的註釋後函式。此函式名稱類似 `serverlessrepo-aws-sagema-GtRecipeAnnotationConsol-<id>`。

1. 在 **Code source** (程式碼來源) 區段，選取 **Test** (測試) 旁邊的箭頭。

1. 選取 **Configure test event** (設定測試事件)。

1. 保持選取 **Create new test event** (建立新測試事件) 選項。

1. 在 **Event template** (事件範本)，選取 **SageMaker Ground Truth AnnotationConsolidation**。

1. 為測試指定 **Event name** (事件名稱)。

1. 修改提供的範本程式碼，如下所示：
   + 將 `roleArn` 的 Amazon Resource Name (ARN) 取代為用於建立標籤工作的 SageMaker AI 執行角色 ARN。
   + 將 `s3Uri` 的 S3 URI 取代為您新增至 Amazon S3 `sample-annotations.json` 檔案的 URI。

   在進行這些修改之後，測試會看起來類似以下內容：

   ```
   {
     "version": "2018-10-16",
     "labelingJobArn": "arn:aws:sagemaker:us-east-2:123456789012:labeling-job/example-job",
     "labelAttributeName": "example-attribute",
     "roleArn": "arn:aws:iam::222222222222:role/sm-execution-role",
     "payload": {
       "s3Uri": "s3://your-bucket/sample-annotations.json"
     }
   }
   ```

1. 選取**建立**。

1. 再次選取 **Test** (測試) 旁邊的箭頭，您應會看到已選取您建立的測試 (由事件名稱以點表示)。如未選取，請選取。

1. 選擇 **Test** (測試)，即可執行測試。

在執行測試之後，您應會在 **Function Logs** (函式日誌) 看見 `-- Consolidated Output --` 區段，其中包含 `sample-annotations.json` 的所有註釋清單。