amazon-web-services - 使用 boto3 调用 AWS SageMaker 终端节点时出错:“无法将数据解析为 JSON。确保 Content-Type 标头设置为“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。现在将测试传递给 body 以调用端点。
推荐阅读
- python - Python点击选项 - 如果参数multiple没有传递,避免多次使用相同的选项
- android - 矩形中的 Android 屏幕边界
- php - Laravel > 5.5 会话和 API
- bash - 如何检查命令的输出是否包含字符串,如果字符串存在则运行命令
- ffmpeg - ffmpeg mp4 到 hls 限制输出块大小而不是持续时间
- c# - 选择 DropDownList1 值时,DropDownList2 中的重复值存储
- php - 如何在 PHP 中使用 mysql 表中的变量
- javascript - PHP中带有提交按钮的未定义索引
- python - 如何从元组中排除字符串以仅显示整数值?
- metal - 颜色的金属 Alpha 值不变