使用 boto3 调用 AWS SageMaker 端点时出错:"Unable to parse data as JSON. Make sure the Content-Type header is set to "application/json”
Error when invoking AWS SageMaker endpoint using boto3 : "Unable to parse data as JSON. Make sure the Content-Type header is set to "application/json"
我正在尝试使用 boto3 通过以下简单代码调用 AWS SageMaker 端点
import boto3
session = boto3.Session(profile_name='mlacc',
region_name='us-west-2')
sagemaker_client = session.client('sagemaker-runtime')
request_body = "{\n \"requestSource\": \"unittest\",\n \"clusters\": [{\n \"clusterMetadata\": {\n "
"\"clusterId\": \"id1\",\n \"topic\": [\"corona virus\", \"Donald Trump\"],\n "
"\"clusterSize\": 2\n },\n \"documents\": [{\n \"uid\": \"1\",\n "
"\"content\": \"content2\",\n \"domain\": \"CNN.com\",\n \"title\": \"This is a "
"title\",\n \"similarityScore\": 1.3,\n \"published_at\": 1566264017,\n "
"\"domain_rank\": 1,\n \"trust_domain_score\": 1\n }, {\n \"uid\": \"2\","
"\n \"content\": \"content2\",\n \"domain\": \"CNN.com\",\n \"title\": "
"\"This is a title\",\n \"similarityScore\": 1.3,\n \"published_at\": 1566264017,"
"\n \"domain_rank\": 1,\n \"trust_domain_score\": 1\n }, {\n \"uid\": "
"\"2\",\n \"content\": \"content3\",\n \"domain\": \"CNN.com\",\n \"title\": "
"\"This is a title\",\n \"similarityScore\": 1.3,\n \"published_at\": 1566264017,"
"\n \"domain_rank\": 1,\n \"trust_domain_score\": 1\n }]\n }]\n}"
response = sagemaker_client.invoke_endpoint(
EndpointName='myEndpoint22',
Body=request_body,
ContentType='application/json',
)
response_json = response['Body'].read().decode('utf-8')
print(response_json)
当我 运行 这段代码
时出现以下错误
Traceback (most recent call last):
File "/Users/rppatwa/Desktop/WorkDocs/CodePlayground/SimplePythonProject/src/PrototypeTesting/SummarizationLocal.py", line 205, in <module>
main()
File "/Users/rppatwa/Desktop/WorkDocs/CodePlayground/SimplePythonProject/src/PrototypeTesting/SummarizationLocal.py", line 186, in main
ContentType='application/json',
File "/Users/rppatwa/anaconda3/lib/python3.7/site-packages/botocore/client.py", line 316, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/Users/rppatwa/anaconda3/lib/python3.7/site-packages/botocore/client.py", line 635, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.errorfactory.ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (400) from model with message "Unable to parse data as JSON. Make sure the Content-Type header is set to "application/json"". See https://us-west-2.console.aws.amazon.com/cloudwatch/home?region=us-west-2#logEventViewer:group=/aws/sagemaker/Endpoints/KeyurshaASMLModel in account 753843489946 for more information.
如果我内联正文(不使用 request_json),则此调用会成功。请让我知道我缺少什么。
谢谢
您需要删除 ContentType='application/json',
后的尾随逗号,并尝试使用下面的代码段将 JSON 传递到正文字段。
import json
json.dumps(request_body)
test=json.dumps(request_body).encode()
这也将验证 JSON 您正在 passing.Now 通过调用端点的主体测试。
我正在尝试使用 boto3 通过以下简单代码调用 AWS SageMaker 端点
import boto3
session = boto3.Session(profile_name='mlacc',
region_name='us-west-2')
sagemaker_client = session.client('sagemaker-runtime')
request_body = "{\n \"requestSource\": \"unittest\",\n \"clusters\": [{\n \"clusterMetadata\": {\n "
"\"clusterId\": \"id1\",\n \"topic\": [\"corona virus\", \"Donald Trump\"],\n "
"\"clusterSize\": 2\n },\n \"documents\": [{\n \"uid\": \"1\",\n "
"\"content\": \"content2\",\n \"domain\": \"CNN.com\",\n \"title\": \"This is a "
"title\",\n \"similarityScore\": 1.3,\n \"published_at\": 1566264017,\n "
"\"domain_rank\": 1,\n \"trust_domain_score\": 1\n }, {\n \"uid\": \"2\","
"\n \"content\": \"content2\",\n \"domain\": \"CNN.com\",\n \"title\": "
"\"This is a title\",\n \"similarityScore\": 1.3,\n \"published_at\": 1566264017,"
"\n \"domain_rank\": 1,\n \"trust_domain_score\": 1\n }, {\n \"uid\": "
"\"2\",\n \"content\": \"content3\",\n \"domain\": \"CNN.com\",\n \"title\": "
"\"This is a title\",\n \"similarityScore\": 1.3,\n \"published_at\": 1566264017,"
"\n \"domain_rank\": 1,\n \"trust_domain_score\": 1\n }]\n }]\n}"
response = sagemaker_client.invoke_endpoint(
EndpointName='myEndpoint22',
Body=request_body,
ContentType='application/json',
)
response_json = response['Body'].read().decode('utf-8')
print(response_json)
当我 运行 这段代码
时出现以下错误Traceback (most recent call last):
File "/Users/rppatwa/Desktop/WorkDocs/CodePlayground/SimplePythonProject/src/PrototypeTesting/SummarizationLocal.py", line 205, in <module>
main()
File "/Users/rppatwa/Desktop/WorkDocs/CodePlayground/SimplePythonProject/src/PrototypeTesting/SummarizationLocal.py", line 186, in main
ContentType='application/json',
File "/Users/rppatwa/anaconda3/lib/python3.7/site-packages/botocore/client.py", line 316, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/Users/rppatwa/anaconda3/lib/python3.7/site-packages/botocore/client.py", line 635, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.errorfactory.ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (400) from model with message "Unable to parse data as JSON. Make sure the Content-Type header is set to "application/json"". See https://us-west-2.console.aws.amazon.com/cloudwatch/home?region=us-west-2#logEventViewer:group=/aws/sagemaker/Endpoints/KeyurshaASMLModel in account 753843489946 for more information.
如果我内联正文(不使用 request_json),则此调用会成功。请让我知道我缺少什么。
谢谢
您需要删除 ContentType='application/json',
后的尾随逗号,并尝试使用下面的代码段将 JSON 传递到正文字段。
import json
json.dumps(request_body)
test=json.dumps(request_body).encode()
这也将验证 JSON 您正在 passing.Now 通过调用端点的主体测试。