the-algorithm/product-mixer/component-library/src/main/scala/com/twitter/product_mixer/component_library/scorer/common/ManagedModelClient.scala
twitter-team ef4c5eb65e Twitter Recommendation Algorithm
Please note we have force-pushed a new initial commit in order to remove some publicly-available Twitter user information. Note that this process may be required in the future.
2023-03-31 17:36:31 -05:00

34 lines
1.2 KiB
Scala

package com.twitter.product_mixer.component_library.scorer.common
import com.twitter.finagle.Http
import com.twitter.finagle.grpc.FinagleChannelBuilder
import com.twitter.finagle.grpc.FutureConverters
import com.twitter.stitch.Stitch
import inference.GRPCInferenceServiceGrpc
import inference.GrpcService.ModelInferRequest
import inference.GrpcService.ModelInferResponse
import io.grpc.ManagedChannel
/**
* Client wrapper for calling a Cortex Managed Inference Service (go/cmis) ML Model using GRPC.
* @param httpClient Finagle HTTP Client to use for connection.
* @param modelPath Wily path to the ML Model service (e.g. /cluster/local/role/service/instance).
*/
case class ManagedModelClient(
httpClient: Http.Client,
modelPath: String)
extends MLModelInferenceClient {
private val channel: ManagedChannel =
FinagleChannelBuilder.forTarget(modelPath).httpClient(httpClient).build()
private val inferenceServiceStub = GRPCInferenceServiceGrpc.newFutureStub(channel)
def score(request: ModelInferRequest): Stitch[ModelInferResponse] = {
Stitch
.callFuture(
FutureConverters
.RichListenableFuture(inferenceServiceStub.modelInfer(request)).toTwitter)
}
}