TensorFlow Serving
Since Camel 4.10
Only producer is supported
The TensorFlow Serving component provides support for invoking the TensorFlow Serving Client API (gRPC). It enables Camel to access TensorFlow Serving model servers to run inference with TensorFlow saved models remotely.
To use the TensorFlow Serving component, Maven users will need to add the following dependency to their pom.xml
:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-tensorflow-serving</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
URI format
tensorflow-serving:api[?options]
Where api
represents one of the TensorFlow Serving Client API (gPRC). While its RESTful Client API is not directly supported by the component, you can refer to the doc to get an idea of each API that TensorFlow Serving provides.
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties
,*.yaml
files, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The TensorFlow Serving component supports 9 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
The configuration. | TensorFlowServingConfiguration | ||
Required servable name. | String | ||
Optional choice of which version of the model to use. Use this specific version number. | Long | ||
Optional choice of which version of the model to use. Use the version associated with the given label. | String | ||
A named signature to evaluate. If unspecified, the default signature will be used. | String | ||
The target URI of the client. See: https://grpc.github.io/grpc-java/javadoc/io/grpc/Grpc.html#newChannelBuilder(java.lang.String,io.grpc.ChannelCredentials). | localhost:8500 | String | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean | |
The credentials of the client. | ChannelCredentials |
Endpoint Options
The TensorFlow Serving endpoint is configured using URI syntax:
tensorflow-serving:api
With the following path and query parameters:
Query Parameters (7 parameters)
Name | Description | Default | Type |
---|---|---|---|
Required servable name. | String | ||
Optional choice of which version of the model to use. Use this specific version number. | Long | ||
Optional choice of which version of the model to use. Use the version associated with the given label. | String | ||
A named signature to evaluate. If unspecified, the default signature will be used. | String | ||
The target URI of the client. See: https://grpc.github.io/grpc-java/javadoc/io/grpc/Grpc.html#newChannelBuilder(java.lang.String,io.grpc.ChannelCredentials). | localhost:8500 | String | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The credentials of the client. | ChannelCredentials |
Message Headers
The TensorFlow Serving component supports 6 message header(s), which is/are listed below:
Name | Description | Default | Type |
---|---|---|---|
CamelTensorFlowServingTarget (producer) Constant: | The target of the client. See: https://grpc.github.io/grpc-java/javadoc/io/grpc/Grpc.html#newChannelBuilder(java.lang.String,io.grpc.ChannelCredentials). | String | |
CamelTensorFlowServingCredentials (producer) Constant: | The credentials of the client. | ChannelCredentials | |
CamelTensorFlowServingModelName (producer) Constant: | Required servable name. | String | |
CamelTensorFlowServingModelVersion (producer) Constant: | Optional choice of which version of the model to use. Use this specific version number. | long | |
CamelTensorFlowServingModelVersionLabel (producer) Constant: | Optional choice of which version of the model to use. Use the version associated with the given label. | String | |
CamelTensorFlowServingSignatureName (producer) Constant: | A named signature to evaluate. If unspecified, the default signature will be used. | String |
Usage
The component supports the following APIs.
tensorflow-serving:<api>[?options]
API | Description | Options | Input (Message Body) | Result (Message Body) |
---|---|---|---|---|
| Return the status of a model in the Model server. |
|
|
|
| Return the metadata of a model in the Model server. |
|
|
|
| Run a classification with a model in the Model server. |
|
| |
| Run a regression with a model in the Model server. |
|
| |
| Provide generic access to a model in the Model server. |
|
|
|
Examples
Model status API
from("direct:model-status")
.to("tensorflow-serving:model-status?modelName=half_plus_two&modelVersion=123")
.log("Status: ${body.getModelVersionStatus(0).state}");
from("direct:model-status-with-headers")
.setHeader(TensorFlowServingConstants.MODEL_NAME, constant("half_plus_two"))
.setHeader(TensorFlowServingConstants.MODEL_VERSION, constant(123))
.to("tensorflow-serving:model-status")
.log("Status: ${body.getModelVersionStatus(0).state}");
Model Metadata API
Currently, TensorFlow Serving only supports signature_def
as the metadata field. See: get_model_metadata.proto
from("direct:model-metadata")
.to("tensorflow-serving:model-metadata?modelName=half_plus_two&modelVersion=123")
.log("Metadata: ${body.getMetadataOrThrow('signature_def')}");
from("direct:model-metadata-with-headers")
.setHeader(TensorFlowServingConstants.MODEL_NAME, constant("half_plus_two"))
.setHeader(TensorFlowServingConstants.MODEL_VERSION, constant(123))
.to("tensorflow-serving:model-metadata")
.log("Metadata: ${body.getMetadataOrThrow('signature_def')}");
Classify API
The signature name should be resolved by looking up the model metadata.
from("direct:classify")
.setBody(constant(InputOuterClass.Input.newBuilder()
.setExampleList(InputOuterClass.ExampleList.newBuilder()
.addExamples(Example.newBuilder()
.setFeatures(Features.newBuilder()
.putFeature("x", Feature.newBuilder()
.setFloatList(FloatList.newBuilder().addValue(1.0f))
.build()))))
.build()))
.to("tensorflow-serving:classify?modelName=half_plus_two&modelVersion=123&signatureName=classify_x_to_y")
.log("Result: ${body.result.getClassifications(0).getClasses(0).score}");
from("direct:classify-with-headers")
.setBody(constant(InputOuterClass.Input.newBuilder()
.setExampleList(InputOuterClass.ExampleList.newBuilder()
.addExamples(Example.newBuilder()
.setFeatures(Features.newBuilder()
.putFeature("x", Feature.newBuilder()
.setFloatList(FloatList.newBuilder().addValue(1.0f))
.build()))))
.build()))
.setHeader(TensorFlowServingConstants.MODEL_NAME, constant("half_plus_two"))
.setHeader(TensorFlowServingConstants.MODEL_VERSION, constant(123))
.setHeader(TensorFlowServingConstants.SIGNATURE_NAME, constant("classify_x_to_y"))
.to("tensorflow-serving:classify")
.log("Result: ${body.result.getClassifications(0).getClasses(0).score}");
Regress API
The signature name should be resolved by looking up the model metadata.
from("direct:regress")
.setBody(constant(InputOuterClass.Input.newBuilder()
.setExampleList(InputOuterClass.ExampleList.newBuilder()
.addExamples(Example.newBuilder()
.setFeatures(Features.newBuilder()
.putFeature("x", Feature.newBuilder()
.setFloatList(FloatList.newBuilder().addValue(1.0f))
.build()))))
.build()))
.to("tensorflow-serving:regress?modelName=half_plus_two&modelVersion=123&signatureName=regress_x_to_y")
.log("Result: ${body.result.getRegressions(0).value}");
from("direct:regress-with-headers")
.setBody(constant(InputOuterClass.Input.newBuilder()
.setExampleList(InputOuterClass.ExampleList.newBuilder()
.addExamples(Example.newBuilder()
.setFeatures(Features.newBuilder()
.putFeature("x", Feature.newBuilder()
.setFloatList(FloatList.newBuilder().addValue(1.0f))
.build()))))
.build()))
.setHeader(TensorFlowServingConstants.MODEL_NAME, constant("half_plus_two"))
.setHeader(TensorFlowServingConstants.MODEL_VERSION, constant(123))
.setHeader(TensorFlowServingConstants.SIGNATURE_NAME, constant("regress_x_to_y"))
.to("tensorflow-serving:regress")
.log("Result: ${body.result.getRegressions(0).value}");
Predict API
The labels of inputs (x
) and outputs (y
) should be resolved by looking up the model metadata.
from("direct:predict")
.setBody(constant(Predict.PredictRequest.newBuilder()
.putInputs("x", TensorProto.newBuilder()
.setDtype(DataType.DT_FLOAT)
.setTensorShape(TensorShapeProto.newBuilder()
.addDim(TensorShapeProto.Dim.newBuilder().setSize(3)))
.addFloatVal(1.0f)
.addFloatVal(2.0f)
.addFloatVal(5.0f)
.build())
.build()))
.to("tensorflow-serving:predict?modelName=half_plus_two&modelVersion=123")
.log("Result1: ${body.getOutputsOrThrow('y').getFloatVal(0)}")
.log("Result2: ${body.getOutputsOrThrow('y').getFloatVal(1)}")
.log("Result3: ${body.getOutputsOrThrow('y').getFloatVal(2)}");
from("direct:predict-with-headers")
.setBody(constant(Predict.PredictRequest.newBuilder()
.putInputs("x", TensorProto.newBuilder()
.setDtype(DataType.DT_FLOAT)
.setTensorShape(TensorShapeProto.newBuilder()
.addDim(TensorShapeProto.Dim.newBuilder().setSize(3)))
.addFloatVal(1.0f)
.addFloatVal(2.0f)
.addFloatVal(5.0f)
.build())
.build()))
.setHeader(TensorFlowServingConstants.MODEL_NAME, constant("half_plus_two"))
.setHeader(TensorFlowServingConstants.MODEL_VERSION, constant(123))
.to("tensorflow-serving:predict")
.log("Result1: ${body.getOutputsOrThrow('y').getFloatVal(0)}")
.log("Result2: ${body.getOutputsOrThrow('y').getFloatVal(1)}")
.log("Result3: ${body.getOutputsOrThrow('y').getFloatVal(2)}");
Spring Boot Auto-Configuration
When using tensorflow-serving with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-tensorflow-serving-starter</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
The component supports 10 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | Boolean | |
The configuration. The option is a org.apache.camel.component.tensorflow.serving.TensorFlowServingConfiguration type. | TensorFlowServingConfiguration | ||
The credentials of the client. The option is a io.grpc.ChannelCredentials type. | ChannelCredentials | ||
Whether to enable auto configuration of the tensorflow-serving component. This is enabled by default. | Boolean | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | Boolean | |
Required servable name. | String | ||
Optional choice of which version of the model to use. Use this specific version number. | Long | ||
Optional choice of which version of the model to use. Use the version associated with the given label. | String | ||
A named signature to evaluate. If unspecified, the default signature will be used. | String | ||
The target URI of the client. See: https://grpc.github.io/grpc-java/javadoc/io/grpc/Grpc.html#newChannelBuilder(java.lang.String,io.grpc.ChannelCredentials). | localhost:8500 | String |
tensorflow.serving.GetModelStatus.GetModelStatusRequest
tensorflow.serving.GetModelStatus.GetModelStatusResponse
tensorflow.serving.GetModelMetadata.GetModelMetadataRequest
tensorflow.serving.GetModelMetadata.GetModelMetadataResponse
tensorflow.serving.Classification.ClassificationRequest
tensorflow.serving.InputOuterClass.Input
tensorflow.serving.Classification.ClassificationResponse
tensorflow.serving.RegressionOuterClass.RegressionRequest
tensorflow.serving.InputOuterClass.Input
tensorflow.serving.RegressionOuterClass.RegressionResponse
tensorflow.serving.Predict.PredictRequest
tensorflow.serving.Predict.PredictResponse