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

configuration (producer)

The configuration.

TensorFlowServingConfiguration

modelName (common)

Required servable name.

String

modelVersion (common)

Optional choice of which version of the model to use. Use this specific version number.

Long

modelVersionLabel (common)

Optional choice of which version of the model to use. Use the version associated with the given label.

String

signatureName (common)

A named signature to evaluate. If unspecified, the default signature will be used.

String

target (common)

localhost:8500

String

lazyStartProducer (producer)

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

autowiredEnabled (advanced)

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

credentials (security)

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:

Path Parameters (1 parameters)

Name Description Default Type

api (producer)

Required The TensorFlow Serving API.

Enum values:

  • model-status

  • model-metadata

  • classify

  • regress

  • predict

String

Query Parameters (7 parameters)

Name Description Default Type

modelName (common)

Required servable name.

String

modelVersion (common)

Optional choice of which version of the model to use. Use this specific version number.

Long

modelVersionLabel (common)

Optional choice of which version of the model to use. Use the version associated with the given label.

String

signatureName (common)

A named signature to evaluate. If unspecified, the default signature will be used.

String

target (common)

localhost:8500

String

lazyStartProducer (producer (advanced))

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

credentials (security)

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: TARGET

String

CamelTensorFlowServingCredentials (producer)

Constant: CREDENTIALS

The credentials of the client.

ChannelCredentials

CamelTensorFlowServingModelName (producer)

Constant: MODEL_NAME

Required servable name.

String

CamelTensorFlowServingModelVersion (producer)

Constant: MODEL_VERSION

Optional choice of which version of the model to use. Use this specific version number.

long

CamelTensorFlowServingModelVersionLabel (producer)

Constant: MODEL_VERSION_LABEL

Optional choice of which version of the model to use. Use the version associated with the given label.

String

CamelTensorFlowServingSignatureName (producer)

Constant: SIGNATURE_NAME

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)

model-status

Return the status of a model in the Model server.

modelName
modelVersion
modelVersionLabel

GetModelStatusRequest [1]
(optional)

GetModelStatusResponse [2]

model-metadata

Return the metadata of a model in the Model server.

modelName
modelVersion
modelVersionLabel

GetModelMetadataRequest [3]
(optional)

GetModelMetadataResponse [4]

classify

Run a classification with a model in the Model server.

modelName
modelVersion
modelVersionLabel
signatureName

ClassificationRequest [5]
Input [6]

ClassificationResponse [7]

regress

Run a regression with a model in the Model server.

modelName
modelVersion
modelVersionLabel
signatureName

RegressionRequest [8]
Input [9]

RegressionResponse [10]

predict

Provide generic access to a model in the Model server.

modelName
modelVersion
modelVersionLabel
signatureName

PredictRequest [11]

PredictResponse [12]

Examples

Model status API

Check model status
from("direct:model-status")
    .to("tensorflow-serving:model-status?modelName=half_plus_two&modelVersion=123")
    .log("Status: ${body.getModelVersionStatus(0).state}");
Specify the model name and version with headers
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

Fetch model metadata
from("direct:model-metadata")
    .to("tensorflow-serving:model-metadata?modelName=half_plus_two&modelVersion=123")
    .log("Metadata: ${body.getMetadataOrThrow('signature_def')}");
Specify the model name and version with headers
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.

Classify
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}");
Specify the model and signature name with headers
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.

Regress
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}");
Specify the model and signature name with headers
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.

Predict
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)}");
Specify the model name and version with headers
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

camel.component.tensorflow-serving.autowired-enabled

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

camel.component.tensorflow-serving.configuration

The configuration. The option is a org.apache.camel.component.tensorflow.serving.TensorFlowServingConfiguration type.

TensorFlowServingConfiguration

camel.component.tensorflow-serving.credentials

The credentials of the client. The option is a io.grpc.ChannelCredentials type.

ChannelCredentials

camel.component.tensorflow-serving.enabled

Whether to enable auto configuration of the tensorflow-serving component. This is enabled by default.

Boolean

camel.component.tensorflow-serving.lazy-start-producer

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

camel.component.tensorflow-serving.model-name

Required servable name.

String

camel.component.tensorflow-serving.model-version

Optional choice of which version of the model to use. Use this specific version number.

Long

camel.component.tensorflow-serving.model-version-label

Optional choice of which version of the model to use. Use the version associated with the given label.

String

camel.component.tensorflow-serving.signature-name

A named signature to evaluate. If unspecified, the default signature will be used.

String

camel.component.tensorflow-serving.target

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


1. tensorflow.serving.GetModelStatus.GetModelStatusRequest
2. tensorflow.serving.GetModelStatus.GetModelStatusResponse
3. tensorflow.serving.GetModelMetadata.GetModelMetadataRequest
4. tensorflow.serving.GetModelMetadata.GetModelMetadataResponse
5. tensorflow.serving.Classification.ClassificationRequest
6. tensorflow.serving.InputOuterClass.Input
7. tensorflow.serving.Classification.ClassificationResponse
8. tensorflow.serving.RegressionOuterClass.RegressionRequest
9. tensorflow.serving.InputOuterClass.Input
10. tensorflow.serving.RegressionOuterClass.RegressionResponse
11. tensorflow.serving.Predict.PredictRequest
12. tensorflow.serving.Predict.PredictResponse