Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门。本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
教程:开始使用 Amazon A2I API
本教程介绍了您可以用来开始使用 Amazon A2I 的 API 操作。
要使用 Jupyter 笔记本运行这些操作,请从使用 Amazon A2I 使用案例和示例使用将 SageMaker 笔记本实例与 Amazon A2I Jupyter 笔记本电脑一起使用了解如何在 SageMaker 笔记本实例中使用它。
要了解有关可用于 Amazon A2I 的 API 操作的更多信息,请参阅在 Amazon Augmented AI 中使用 API.
创建私有人工作团队
您可以创建一个私人工作团队并将自己添加为员工,以便您可以预览 Amazon A2I。
如果您不熟悉 Amazon Cognito,我们建议您使用 SageMaker 控制台创建私人劳动力并将自己添加为私人员工。有关说明,请参阅第 1 步:创建工作团队。
如果您熟悉 Amazon Cognito,可以使用以下说明使用 SageMaker API 创建私有工作团队。创建工作团队后,请注意工作团队 ARN (WorkteamArn
)。
要了解有关私有人力和其他可用配置的更多信息,请参阅使用私有人力.
创建私有人力
如果您尚未创建私有人力,则可以使用Amazon Cognito 用户池. 确保您已将自己添加到此用户池。您可以使用Amazon SDK for Python (Boto3) create_workforce
function. 有关其他特定于语言的 SDK,请参阅中的列表创建劳动力.
response = client.create_workforce(
CognitoConfig={
"UserPool": "Pool_ID
",
"ClientId": "app-client-id
"
},
WorkforceName="workforce-name
"
)
创建私人工作团队
在中创建私人劳动力之后Amazon用于配置和启动人工循环的区域,您可以使用Amazon SDK for Python (Boto3) create_workteam
function. 有关其他特定于语言的 SDK,请参阅中的列表CreateWorkteam
.
response = client.create_workteam(
WorkteamName="work-team-name
",
WorkforceName= "workforce-name
",
MemberDefinitions=[
{
"CognitoMemberDefinition": {
"UserPool": "<aws-region>_ID
",
"UserGroup": "user-group
",
"ClientId": "app-client-id
"
},
}
]
)
按如下方式访问您的工作团队 ARN:
workteamArn = response["WorkteamArn"]
列出账户中的私人工作团队
如果您已创建私有工作组,则可以列出指定的所有工作团队。Amazon使用账户中的区域Amazon SDK for Python (Boto3) list_workteams
function. 有关其他特定于语言的 SDK,请参阅中的列表ListWorkteams
.
response = client.list_workteams()
如果您的帐户中有许多工作团队,则可能需要使用MaxResults
、SortBy
, 和NameContains
来筛选结果。
创建人工审核工作流程
您可以使用 Amazon A2I 创建人工审核工作流。CreateFlowDefinition
operation. 在创建人工审核工作流程之前,您需要创建人工任务 UI。您可使用CreateHumanTaskUi
operation.
如果您将亚马逊 A2I 与 Amazon Textract 或 Amazon Rekognition 集成一起使用,则可以使用 JSON 指定激活条件。
创建人工任务 UI
如果您正在创建人工审核工作流程以与 Amazon Textract 或 Amazon Rekognition 集成一起使用,则需要使用和修改预制的工作人员任务模板。对于所有自定义集成,您可以使用自己的自定义工作人员任务模板。使用下表了解如何使用两个内置集成的工作人员任务模板创建人工任务 UI。将模板替换为您自己的模板,以自定义此请求。
- Amazon Textract – Key-value pair extraction
-
要详细了解相关此模板,请参阅Amazon Textract 的自定义模板示例.
- Amazon Rekognition – Image moderation
-
要详细了解相关此模板,请参阅Amazon Rekognition 的自定义模板示例.
- Custom Integration
-
以下是可在自定义集成中使用的示例模板。在此模板中使用了此模板。笔记本,展示了与 Amazon Comprehend 的自定义集成。
使用上面指定的模板,您可以使用Amazon SDK for Python (Boto3) create_human_task_ui
function. 有关其他特定于语言的 SDK,请参阅中的列表CreateHumanTaskUi
.
response = client.create_human_task_ui(
HumanTaskUiName="human-task-ui-name
",
UiTemplate={
"Content": template
}
)
此响应元素包含人工任务 UI ARN。按如下方式保存此内容:
humanTaskUiArn = response["HumanTaskUiArn"]
创建 JSON 以指定激活条件
对于 Amazon Textract 和 Amazon Rekognition 内置集成,您可以将激活条件保存在 JSON 对象中,然后在CreateFlowDefinition
请求.
接下来,选择一个选项卡以查看可用于这些内置集成的示例激活条件。有关激活条件选项的其他信息,请参阅亚马逊中用于人 Augmented AI 循环激活条件的 JSON 架构.
- Amazon Textract – Key-value pair extraction
-
此示例指定了特定密钥的条件(例如Mail address
) (在文档中)。如果 Amazon Textract 的信心超出此处设定的阈值,则会将文档发送给人员进行审核,启动人工循环的特定密钥将提示给工作人员。
import json
humanLoopActivationConditions = json.dumps(
{
"Conditions": [
{
"Or": [
{
"ConditionType": "ImportantFormKeyConfidenceCheck",
"ConditionParameters": {
"ImportantFormKey": "Mail address",
"ImportantFormKeyAliases": ["Mail Address:","Mail address:", "Mailing Add:","Mailing Addresses"],
"KeyValueBlockConfidenceLessThan": 100,
"WordBlockConfidenceLessThan": 100
}
},
{
"ConditionType": "MissingImportantFormKey",
"ConditionParameters": {
"ImportantFormKey": "Mail address",
"ImportantFormKeyAliases": ["Mail Address:","Mail address:","Mailing Add:","Mailing Addresses"]
}
},
{
"ConditionType": "ImportantFormKeyConfidenceCheck",
"ConditionParameters": {
"ImportantFormKey": "Phone Number",
"ImportantFormKeyAliases": ["Phone number:", "Phone No.:", "Number:"],
"KeyValueBlockConfidenceLessThan": 100,
"WordBlockConfidenceLessThan": 100
}
},
{
"ConditionType": "ImportantFormKeyConfidenceCheck",
"ConditionParameters": {
"ImportantFormKey": "*",
"KeyValueBlockConfidenceLessThan": 100,
"WordBlockConfidenceLessThan": 100
}
},
{
"ConditionType": "ImportantFormKeyConfidenceCheck",
"ConditionParameters": {
"ImportantFormKey": "*",
"KeyValueBlockConfidenceGreaterThan": 0,
"WordBlockConfidenceGreaterThan": 0
}
}
]
}
]
}
)
- Amazon Rekognition – Image moderation
-
此处使用的人类循环激活条件是针对 Amazon Rekognition 内容审核量身定制的;它们基于Suggestive
和Female Swimwear
Or Underwear
审核标签。
import json
humanLoopActivationConditions = json.dumps(
{
"Conditions": [
{
"Or": [
{
"ConditionType": "ModerationLabelConfidenceCheck",
"ConditionParameters": {
"ModerationLabelName": "Suggestive",
"ConfidenceLessThan": 98
}
},
{
"ConditionType": "ModerationLabelConfidenceCheck",
"ConditionParameters": {
"ModerationLabelName": "Female Swimwear Or Underwear",
"ConfidenceGreaterThan": 98
}
}
]
}
]
}
)
创建人工审核工作流程
本节提供了一个示例,说明CreateFlowDefinition
Amazon SDK for Python (Boto3)请求使用前面章节中创建的资源。有关其他特定于语言的 SDK,请参阅中的列表CreateFlowDefinition. 使用下表中的选项卡查看为 Amazon Textract 和 Amazon Rekognition 内置集成创建人工审核工作流程的请求。
- Amazon Textract – Key-value pair extraction
-
如果您使用与 Amazon Textract 的内置集成,则必须指定"AWS/Textract/AnalyzeDocument/Forms/V1"
为了"AwsManagedHumanLoopRequestSource"
在HumanLoopRequestSource
.
response = client.create_flow_definition(
FlowDefinitionName="human-review-workflow-name
",
HumanLoopRequestSource={
"AwsManagedHumanLoopRequestSource": "AWS/Textract/AnalyzeDocument/Forms/V1"
},
HumanLoopActivationConfig={
"HumanLoopActivationConditionsConfig": {
"HumanLoopActivationConditions": humanLoopActivationConditions
}
},
HumanLoopConfig={
"WorkteamArn": workteamArn
,
"HumanTaskUiArn": humanTaskUiArn
,
"TaskTitle": "Document entry review
",
"TaskDescription": "Review the document and instructions. Complete the task
",
"TaskCount": 1
,
"TaskAvailabilityLifetimeInSeconds": 43200
,
"TaskTimeLimitInSeconds": 3600
,
"TaskKeywords": [
"document review
",
],
},
OutputConfig={
"S3OutputPath": "s3://DOC-EXAMPLE-BUCKET
/prefix/
",
},
RoleArn="arn:aws:iam::<account-number>:role/<role-name>
",
Tags=[
{
"Key": "string
",
"Value": "string
"
},
]
)
- Amazon Rekognition – Image moderation
-
如果您使用与 Amazon Rekognition 的内置集成,则必须指定"AWS/Rekognition/DetectModerationLabels/Image/V3"
为了"AwsManagedHumanLoopRequestSource"
在HumanLoopRequestSource
.
response = client.create_flow_definition(
FlowDefinitionName="human-review-workflow-name
",
HumanLoopRequestSource={
"AwsManagedHumanLoopRequestSource": "AWS/Rekognition/DetectModerationLabels/Image/V3"
},
HumanLoopActivationConfig={
"HumanLoopActivationConditionsConfig": {
"HumanLoopActivationConditions": humanLoopActivationConditions
}
},
HumanLoopConfig={
"WorkteamArn": workteamArn
,
"HumanTaskUiArn": humanTaskUiArn
,
"TaskTitle": "Image content moderation
",
"TaskDescription": "Review the image and instructions. Complete the task
",
"TaskCount": 1
,
"TaskAvailabilityLifetimeInSeconds": 43200
,
"TaskTimeLimitInSeconds": 3600
,
"TaskKeywords": [
"content moderation
",
],
},
OutputConfig={
"S3OutputPath": "s3://DOC-EXAMPLE-BUCKET
/prefix/
",
},
RoleArn="arn:aws:iam::<account-number>:role/<role-name>
",
Tags=[
{
"Key": "string
",
"Value": "string
"
},
]
)
- Custom Integration
-
如果使用自定义集成,请排除以下参数:HumanLoopRequestSource
、HumanLoopActivationConfig
.
response = client.create_flow_definition(
FlowDefinitionName="human-review-workflow-name
",
HumanLoopConfig={
"WorkteamArn": workteamArn
,
"HumanTaskUiArn": humanTaskUiArn
,
"TaskTitle": "Image content moderation
",
"TaskDescription": "Review the image and instructions. Complete the task
",
"TaskCount": 1
,
"TaskAvailabilityLifetimeInSeconds": 43200
,
"TaskTimeLimitInSeconds": 3600
,
"TaskKeywords": [
"content moderation
",
],
},
OutputConfig={
"S3OutputPath": "s3://DOC-EXAMPLE-BUCKET
/prefix/
",
},
RoleArn="arn:aws:iam::<account-number>:role/<role-name>
",
Tags=[
{
"Key": "string
",
"Value": "string
"
},
]
)
在创建人工审核工作流程后,您可以从响应中检索流定义 ARN:
humanReviewWorkflowArn
= response["FlowDefinitionArn"]
创建人工循环
您用来启动人工循环的 API 操作取决于您使用的 Amazon A2I 集成。
在下表中选择您的任务类型,以查看 Amazon Textract 和 Amazon Rekognition 的示例请求Amazon SDK for Python (Boto3).
- Amazon Textract – Key-value pair extraction
-
以下示例使用Amazon SDK for Python (Boto3)调用analyze_document
(在 us-west-2 中)。将斜体红色文本替换为您的资源。加入DataAttributes
参数如果您使用的是 Amazon Mechanical Turk 员队伍。有关更多信息,请参阅 。分析 _文档(在文档中)Amazon SDK for Python (Boto)API 参考.
response = client.analyze_document(
Document={"S3Object": {"Bucket": "AWSDOC-EXAMPLE-BUCKET
", "Name": "document-name.pdf
"},
HumanLoopConfig={
"FlowDefinitionArn":"arn:aws:sagemaker:us-west-2:111122223333:flow-definition/flow-definition-name
",
"HumanLoopName":"human-loop-name
",
"DataAttributes" : {ContentClassifiers:["FreeOfPersonallyIdentifiableInformation"
|"FreeOfAdultContent"
]}
}
FeatureTypes=["FORMS"]
)
只有当 Amazon Textract 对文档分析任务的信心满足您在人工审核工作流程中指定的激活条件时,才会创建人类循环。你可以查看response
元素来确定是否创建了人类循环。要查看此响应中包含的所有内容,请参阅HumanLoopActivationOutput
.
if "HumanLoopArn" in analyzeDocumentResponse["HumanLoopActivationOutput"]:
# A human loop has been started!
print(f"A human loop has been started with ARN: {analyzeDocumentResponse["HumanLoopActivationOutput"]["HumanLoopArn"]}"
- Amazon Rekognition – Image moderation
-
以下示例使用Amazon SDK for Python (Boto3)调用detect_moderation_labels
(在 us-west-2 中)。将斜体红色文本替换为您的资源。加入DataAttributes
参数如果您使用的是 Amazon Mechanical Turk 员队伍。有关更多信息,请参阅 。detect_moderation_label(在文档中)Amazon SDK for Python (Boto)API 参考.
response = client.detect_moderation_labels(
Image={"S3Object":{"Bucket": "AWSDOC-EXAMPLE-BUCKET
", "Name": "image-name.png
"}},
HumanLoopConfig={
"FlowDefinitionArn":"arn:aws:sagemaker:us-west-2:111122223333:flow-definition/flow-definition-name
",
"HumanLoopName":"human-loop-name
",
"DataAttributes":{ContentClassifiers:["FreeOfPersonallyIdentifiableInformation"
|"FreeOfAdultContent"
]}
}
)
只有当 Amazon Rekognition 对图像审核任务的信心满足您在人工审核工作流程中指定的激活条件时,才会创建人类循环。你可以查看response
元素来确定是否创建了人类循环。要查看此响应中包含的所有内容,请参阅HumanLoopActivationOutput
.
if "HumanLoopArn" in response["HumanLoopActivationOutput"]:
# A human loop has been started!
print(f"A human loop has been started with ARN: {response["HumanLoopActivationOutput"]["HumanLoopArn"]}")
- Custom Integration
-
以下示例使用Amazon SDK for Python (Boto3)调用start_human_loop
(在 us-west-2 中)。将斜体红色文本替换为您的资源。加入DataAttributes
参数如果您使用的是 Amazon Mechanical Turk 员队伍。有关更多信息,请参阅 。start_human_LOOP(在文档中)Amazon SDK for Python (Boto)API 参考.
response = client.start_human_loop(
HumanLoopName= "human-loop-name
",
FlowDefinitionArn= "arn:aws:sagemaker:us-west-2:111122223333:flow-definition/flow-definition-name
",
HumanLoopInput={"InputContent": inputContentJson
},
DataAttributes={"ContentClassifiers":["FreeOfPersonallyIdentifiableInformation"|"FreeOfAdultContent"
]}
)
此示例将输入内容存储在变量中inputContentJson
. 假设输入内容包含两个元素:文本模板和情绪(例如Positive
、Negative
,或者Neutral
),其格式如下所示:
inputContent = {
"initialValue": sentiment,
"taskObject": blurb
}
钥匙initialValue
和taskObject
必须与工作人员任务模板的液体元素中使用的密钥相对应。请参阅中的自定义模板创建人工任务 UI请参阅一个示例。
创建inputContentJson
中,执行以下操作:
import json
inputContentJson = json.dumps(inputContent)
每次打电话时都会开始人类循环start_human_loop
. 要检查人类循环的状态,请使用描述了 _human_LOOP:
human_loop_info = a2i.describe_human_loop(HumanLoopName="human_loop_name
")
print(f"HumanLoop Status: {resp["HumanLoopStatus"]}")
print(f"HumanLoop Output Destination: {resp["HumanLoopOutput"]}")