KServe
Since Camel 4.10
Only producer is supported
The KServe component provides the ability to access various AI model servers using the KServe Open Inference Protocl V2. This allows Camel to remotely perform inference with AI models on various model servers that support the KServe V2 protocol.
Currently, this component only supports GRPC API. |
To use the KServe component, Maven users will need to add the following dependency to their pom.xml
:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-kserve</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
URI format
kserve:api[?options]
Where api
represents one of the KServe Open Inference Protocol GRPC API.
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 KServe component supports 9 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
The configuration. | KServeConfiguration | ||
The name of the model used for inference. | String | ||
The version of the model used for inference. | String | ||
The target URI of the client. See: https://grpc.github.io/grpc-java/javadoc/io/grpc/Grpc.html#newChannelBuilder%28java.lang.String,io.grpc.ChannelCredentials%29. | localhost:8001 | 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 | |
Used for enabling or disabling all consumer based health checks from this component. | true | boolean | |
Used for enabling or disabling all producer based health checks from this component. Notice: Camel has by default disabled all producer based health-checks. You can turn on producer checks globally by setting camel.health.producersEnabled=true. | true | boolean | |
The credentials of the client. | ChannelCredentials |
Endpoint Options
The KServe endpoint is configured using URI syntax:
kserve:api
With the following path and query parameters:
Query Parameters (5 parameters)
Name | Description | Default | Type |
---|---|---|---|
The name of the model used for inference. | String | ||
The version of the model used for inference. | String | ||
The target URI of the client. See: https://grpc.github.io/grpc-java/javadoc/io/grpc/Grpc.html#newChannelBuilder%28java.lang.String,io.grpc.ChannelCredentials%29. | localhost:8001 | 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 KServe component supports 2 message header(s), which is/are listed below:
Name | Description | Default | Type |
---|---|---|---|
CamelKServeModelName (producer) Constant: | The name of the model used for inference. | String | |
CamelKServeModelVersion (producer) Constant: | The version of the model used for inference. | String |
Usage
The component supports the following APIs.
kserve:<api>[?options]
API | Description | Options | Input (Message Body) | Result (Message Body) |
---|---|---|---|---|
| Performs inference using the specified model. |
|
|
|
| Indicates if a specific model is ready for inferencing. |
|
|
|
| Provides information about a model. |
|
|
|
| Indicates if the server is ready for inferencing. |
| ||
| Indicates if the inference server is able to receive and respond to metadata and inference requests. |
| ||
| Provides information about the server. |
|
Examples
Infer (ModelInfer) API
from("direct:infer")
.setBody(constant(createRequest()))
.to("kserve:infer?modelName=simple&modelVersion=1")
.process(this::postprocess)
.log("Result: ${body}");
// Helper methods
ModelInferRequest createRequest() {
// How to create a request differs depending on the input types of the model.
var ints0 = IntStream.range(1, 17).boxed().collect(Collectors.toList());
var content0 = InferTensorContents.newBuilder().addAllIntContents(ints0);
var input0 = ModelInferRequest.InferInputTensor.newBuilder()
.setName("INPUT0").setDatatype("INT32").addShape(1).addShape(16)
.setContents(content0);
var ints1 = IntStream.range(0, 16).boxed().collect(Collectors.toList());
var content1 = InferTensorContents.newBuilder().addAllIntContents(ints1);
var input1 = ModelInferRequest.InferInputTensor.newBuilder()
.setName("INPUT1").setDatatype("INT32").addShape(1).addShape(16)
.setContents(content1);
return ModelInferRequest.newBuilder()
.addInputs(0, input0).addInputs(1, input1)
.build();
}
void postprocess(Exchange exchange) {
// How to post-process the response differs depending on the output types
// of the model.
var response = exchange.getMessage().getBody(ModelInferResponse.class);
var content = response.getRawOutputContents(0);
var buffer = content.asReadOnlyByteBuffer().order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
var ints = new ArrayList<Integer>(buffer.remaining());
while (buffer.hasRemaining()) {
ints.add(buffer.get());
}
exchange.getMessage().setBody(ints);
}
from("direct:infer-with-headers")
.setBody(constant(createRequest()))
.setHeader(KServeConstants.MODEL_NAME, constant("simple"))
.setHeader(KServeConstants.MODEL_VERSION, constant("1"))
.to("kserve:infer")
.process(this::postprocess)
.log("Result: ${body}");
// ... Same as the previous example
ModelReady API
from("direct:model-ready")
.to("kserve:model/ready?modelName=simple&modelVersion=1")
.log("Status: ${body.ready}");
from("direct:model-ready-with-headers")
.setHeader(KServeConstants.MODEL_NAME, constant("simple"))
.setHeader(KServeConstants.MODEL_VERSION, constant("1"))
.to("kserve:model/ready")
.log("Status: ${body.ready}");
ModelMetadata API
from("direct:model-metadata")
.to("kserve:model/metadata?modelName=simple&modelVersion=1")
.log("Metadata: ${body}");
from("direct:model-metadata-with-headers")
.setHeader(KServeConstants.MODEL_NAME, constant("simple"))
.setHeader(KServeConstants.MODEL_VERSION, constant("1"))
.to("kserve:model/metadata")
.log("Metadata: ${body}");
ServerReady API
from("direct:server-ready")
.to("kserve:server/ready")
.log("Status: ${body.ready}");
Spring Boot Auto-Configuration
When using kserve 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-kserve-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.kserve.KServeConfiguration type. | KServeConfiguration | ||
The credentials of the client. The option is a io.grpc.ChannelCredentials type. | ChannelCredentials | ||
Whether to enable auto configuration of the kserve component. This is enabled by default. | Boolean | ||
Used for enabling or disabling all consumer based health checks from this component. | true | Boolean | |
Used for enabling or disabling all producer based health checks from this component. Notice: Camel has by default disabled all producer based health-checks. You can turn on producer checks globally by setting camel.health.producersEnabled=true. | true | 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 | |
The name of the model used for inference. | String | ||
The version of the model used for inference. | String | ||
The target URI of the client. See: https://grpc.github.io/grpc-java/javadoc/io/grpc/Grpc.html#newChannelBuilder%28java.lang.String,io.grpc.ChannelCredentials%29. | localhost:8001 | String |
inference.GrpcPredictV2.ModelInferRequest
inference.GrpcPredictV2.ModelInferResponse
inference.GrpcPredictV2.ModelReadyRequest
inference.GrpcPredictV2.ModelReadyResponse
inference.GrpcPredictV2.ModelMetadataRequest
inference.GrpcPredictV2.ModelMetadataResponse
inference.GrpcPredictV2.ServerReadyResponse
inference.GrpcPredictV2.ServerLiveResponse
inference.GrpcPredictV2.ServerMetadataResponse