[英]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.