简体   繁体   English

使用 java sdk v2 调用 sagemaker 推理端点

[英]Call a sagemaker inference endpoint using the java sdk v2

I am trying to call a sagemaker inference endpoint from Java. I can do it without an issue from Python, running this after installing the sagemaker package:我正在尝试从 Java 调用一个 sagemaker 推理端点。在安装sagemaker package 后运行它,我可以从 Python 毫无问题地做到这一点:

predictor = TensorFlowPredictor('my-endpoint')
data = {'foo': 'bar'}
result = predictor.predict(data)

How can I do this using the latest official library?我怎样才能使用最新的官方库来做到这一点? I'm assuming it is software.amazon.awssdk:sagemaker:2.17.167我假设它是software.amazon.awssdk:sagemaker:2.17.167

There are similar questions on this site, but they seem to use an older version of the client.这个网站上有类似的问题,但他们似乎使用的是旧版本的客户端。 I also found a github repository with examples, but it does not show how to call an inference endpoint.我还找到了一个带有示例的 github 存储库,但它没有显示如何调用推理端点。

Answering my own question.回答我自己的问题。 I was using the wrong library.我使用了错误的库。 For inference there is a separate "runtime" dependency: software.amazon.awssdk:sagemakerruntime:2.17.167对于推理,有一个单独的“运行时”依赖项: software.amazon.awssdk:sagemakerruntime:2.17.167

Something like this then works, this is Kotlin code:这样就可以工作了,这是 Kotlin 代码:

val runtime = SageMakerRuntimeClient.builder()
    .region(Region.EU_CENTRAL_1)
    .build()

val requestString = """
    {"foo": "bar"}
"""

val request = InvokeEndpointRequest.builder()
    .endpointName("my-endpoint")
    .contentType("application/json")
    .body(SdkBytes.fromString(requestString, Charset.defaultCharset()))
    .build()

val response = runtime.invokeEndpoint(request)

println(response.body().asString(Charset.defaultCharset()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 AWS SDK V2 为 Amazon S3 配置终端节点? - How do you configure the endpoint for Amazon S3 by using the AWS SDK V2? 关闭 AWS 中的 IdleConnectionReaper Java SDK v2 - Shutdown IdleConnectionReaper in AWS Java SDK v2 尝试使用 AWS Java v2 SDK 完成分段上传时出现 S3Exception - S3Exception when trying to complete multi-part upload using the AWS Java v2 SDK 在使用 aws sdk 为 java v2 构建 SsmClient 时是否绝对有必要明确提及区域? - Is it absolutely necessary to explicitely mention region while building SsmClient using aws sdk for java v2? 检查 EC2 实例是否完成初始化。 使用 AWS Java SDK v2 - Check if EC2 instance finished initializing. Using the AWS Java SDK v2 多模型端点的无服务推理 - Amazon Sagemaker - Serveless inference over multi-model endpoint - Amazon Sagemaker 调试和部署 sagemaker 端点的 featurizer(用于 imodel 推理的数据处理器) - debug and deploy featurizer (data processor for imodel inference) of sagemaker endpoint Amazon Sagemaker:推理端点中的用户输入数据验证 - Amazon Sagemaker: User Input data validation in Inference Endpoint 如何在 AWS sagemaker 中为 yolov5 推理创建端点 - How can I create an endpoint for yolov5 inference in AWS sagemaker 如何从另一台服务器调用 sagemaker 端点 - how to call sagemaker endpoint from another server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM