AWS sagemaker invoke_endpoint 错误:“输入中的未知参数:\”InferenceComponentName\“”

问题描述 投票:0回答:1

我正在尝试创建一个 API 来调用附加了 llama-python-7b 推理模型的 sagemaker 端点。

当使用

invoke_endpoint
参数调用
InferenceComponentName
时,我得到
Parameter validation failed:\nUnknown parameter in input: \"InferenceComponentName\", must be one of: EndpointName, Body, ContentType, Accept, CustomAttributes, TargetModel, TargetVariant, TargetContainerHostname, InferenceId, EnableExplanations"

该参数直接来自 sagemaker 推理模型给出的 Python SDK 示例。我的 lambda 代码本质上是这个示例的副本:

import json
import boto3

endpoint_name = '<endpoint>'

def lambda_handler(event, context):
    client = boto3.client('runtime.sagemaker')
    response = client.invoke_endpoint(
        EndpointName=endpoint_name, InferenceComponentName='<inference component name>',
        ContentType='application/json',
        Body=json.dumps(event).encode('utf-8'),
    )
    response = response["Body"].read().decode("utf8")
    response = json.loads(response)
    return response

如果我不传入这个参数,我会得到:

An error occurred (ValidationError) when calling the InvokeEndpoint operation: Inference Component Name header is required for endpoints to which you plan to deploy inference components. Please include Inference Component Name header or consider using SageMaker models.

所以它缺少一些东西,但我不确定什么或如何格式化它。

我还向与 lambda 函数相关的角色授予了

AmazonSageMakerFullAccess
权限,以尝试消除权限问题。

python aws-lambda amazon-sagemaker llama
1个回答
0
投票

问题出在 AWS lambda 加载的 boto3 版本上。

我使用的是python3.11,它附带了boto3-1.26.90,但是直到更高版本才添加InferenceComponentName。请参阅此页面,了解有关哪个版本附带哪个解释器的更多信息:https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html

按照本页中的步骤,我能够通过创建包含正确的 python 包和 lambda 脚本的部署.zip 文件来加载正确的版本:https://docs.aws.amazon.com/lambda/latest /dg/python-package.html#python-package-dependencies

© www.soinside.com 2019 - 2024. All rights reserved.