@@ -9,6 +9,10 @@ TorchServe Client for Java (TSC4J) is a Java client library for [TorchServe](htt
9
9
- [ Management API] ( https://pytorch.org/serve/management_api.html )
10
10
- [ Metrics API] ( https://pytorch.org/serve/metrics_api.html )
11
11
12
+ ## Requirements
13
+
14
+ - Java 17+
15
+
12
16
## Install
13
17
14
18
1 . Add the [ JitPack] ( https://jitpack.io ) repository to your ` pom.xml ` :
@@ -34,6 +38,191 @@ TorchServe Client for Java (TSC4J) is a Java client library for [TorchServe](htt
34
38
35
39
## Usage
36
40
41
+ ### Inference
42
+
43
+ - Prediction:
44
+
45
+ ```java
46
+ TorchServeClient client = TorchServeClient.newInstance();
47
+
48
+ byte[] image = Files.readAllBytes(Path.of("0.png"));
49
+ Object result = client.inference().predictions("mnist_v2", image);
50
+ System.out.println(result);
51
+ // => 0
52
+ ```
53
+
54
+ - With the inference API endpoint other than < http://localhost:8080 > :
55
+
56
+ ``` java
57
+ TorchServeClient client = TorchServeClient . builder()
58
+ .inferenceAddress(" http://localhost:12345" )
59
+ .build();
60
+ ```
61
+
62
+ - With token authorization:
63
+
64
+ ``` java
65
+ TorchServeClient client = TorchServeClient . builder()
66
+ .inferenceKey(" <inference-key>" )
67
+ .build();
68
+ ```
69
+
70
+ ### Management
71
+
72
+ - Register a model:
73
+
74
+ ``` java
75
+ TorchServeClient client = TorchServeClient . newInstance();
76
+
77
+ Response response = client. management(). registerModel(
78
+ " https://torchserve.pytorch.org/mar_files/mnist_v2.mar" ,
79
+ RegisterModelOptions . empty());
80
+ System . out. println(response. getStatus());
81
+ // => "Model "mnist_v2" Version: 2.0 registered with 0 initial workers. Use scale workers API to add workers for the model."
82
+ ```
83
+
84
+ - Scale workers for a model:
85
+
86
+ ``` java
87
+ TorchServeClient client = TorchServeClient . newInstance();
88
+
89
+ Response response = client. management(). setAutoScale(
90
+ " mnist_v2" ,
91
+ SetAutoScaleOptions . builder()
92
+ .minWorker(1 )
93
+ .maxWorker(2 )
94
+ .build());
95
+ System . out. println(response. getStatus());
96
+ // => "Processing worker updates..."
97
+ ```
98
+
99
+ - Describe a model:
100
+
101
+ ``` java
102
+ TorchServeClient client = TorchServeClient . newInstance();
103
+
104
+ List<ModelDetail > model = client. management(). describeModel(" mnist_v2" );
105
+ System . out. println(model. get(0 ));
106
+ // =>
107
+ // ModelDetail {
108
+ // modelName: mnist_v2
109
+ // modelVersion: 2.0
110
+ // ...
111
+ ```
112
+
113
+ - Unregister a model:
114
+
115
+ ``` java
116
+ TorchServeClient client = TorchServeClient . newInstance();
117
+
118
+ Response response = client. management(). unregisterModel(
119
+ " mnist_v2" ,
120
+ UnregisterModelOptions . empty());
121
+ System . out. println(response. getStatus());
122
+ // => "Model "mnist_v2" unregistered"
123
+ ```
124
+
125
+ - List models:
126
+
127
+ ``` java
128
+ TorchServeClient client = TorchServeClient . newInstance();
129
+
130
+ ModelList models = client. management(). listModels(10 , null );
131
+ System . out. println(models);
132
+ // =>
133
+ // ModelList {
134
+ // nextPageToken: null
135
+ // models: [Model {
136
+ // modelName: mnist_v2
137
+ // modelUrl: https://torchserve.pytorch.org/mar_files/mnist_v2.mar
138
+ // },
139
+ // ...
140
+ ```
141
+
142
+ - Set default version for a model:
143
+
144
+ ``` java
145
+ TorchServeClient client = TorchServeClient . newInstance();
146
+
147
+ Response response = client. management(). setDefault(" mnist_v2" , " 2.0" );
148
+ System . out. println(response. getStatus());
149
+ // => "Default version successfully updated for model "mnist_v2" to "2.0""
150
+ ```
151
+
152
+ - With the management API endpoint other than < http://localhost:8081 > :
153
+
154
+ ``` java
155
+ TorchServeClient client = TorchServeClient . builder()
156
+ .managementAddress(" http://localhost:12345" )
157
+ .build();
158
+ ```
159
+
160
+ - With token authorization:
161
+
162
+ ``` java
163
+ TorchServeClient client = TorchServeClient . builder()
164
+ .managementKey(" <management-key>" )
165
+ .build();
166
+ ```
167
+
168
+ ### Metrics
169
+
170
+ - Get metrics in Prometheus format:
171
+
172
+ ``` java
173
+ TorchServeClient client = TorchServeClient . newInstance();
174
+
175
+ String metrics = client. metrics(). metrics();
176
+ System . out. println(metrics);
177
+ // =>
178
+ // # HELP MemoryUsed Torchserve prometheus gauge metric with unit: Megabytes
179
+ // # TYPE MemoryUsed gauge
180
+ // MemoryUsed{Level="Host",Hostname="3a9b51d41fbf",} 2075.09765625
181
+ // ...
182
+ ```
183
+
184
+ - With the metrics API endpoint other than < http://localhost:8082 > :
185
+
186
+ ``` java
187
+ TorchServeClient client = TorchServeClient . builder()
188
+ .metricsAddress(" http://localhost:12345" )
189
+ .build();
190
+ ```
191
+
192
+ ### Configuration
193
+
194
+ #### tsc4j.properties
195
+
196
+ ``` properties
197
+ inference.key = <inference-key>
198
+ inference.address = http://localhost:8080
199
+ # inference.address takes precedence over inference.port if it's defined
200
+ inference.port = 8080
201
+
202
+ management.key = <management-key>
203
+ management.address = http://localhost:8081
204
+ # management.address takes precedence over management.port if it's defined
205
+ management.port = 8081
206
+
207
+ metrics.address = http://localhost:8082
208
+ # metrics.address takes precedence over metrics.port if it's defined
209
+ metrics.port = 8082
210
+ ```
211
+
212
+ #### System properties
213
+
214
+ You can configure the TSC4J properties via system properties with prefix ` tsc4j. ` .
215
+
216
+ For instance, you can configure ` inference.address ` with the ` tsc4j.inference.address ` system property.
217
+
218
+ #### Environment variables
219
+
220
+ You can also configure the TSC4J properties via environment variables with prefix ` TSC4J_ ` .
221
+
222
+ For instance, you can configure ` inference.address ` with the ` TSC4J_INFERENCE_ADDRESS ` environment variable.
223
+
224
+ ### Examples
225
+
37
226
See [ examples] ( ./examples/ ) .
38
227
39
228
## Build
0 commit comments