Skip to content

Commit 5563547

Browse files
committed
Finishing touches
1 parent aadaa09 commit 5563547

File tree

4 files changed

+103
-43
lines changed

4 files changed

+103
-43
lines changed

sample-code/spring-app/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@
112112
<groupId>com.fasterxml.jackson.core</groupId>
113113
<artifactId>jackson-databind</artifactId>
114114
</dependency>
115+
<dependency>
116+
<groupId>com.fasterxml.jackson.datatype</groupId>
117+
<artifactId>jackson-datatype-jsr310</artifactId>
118+
</dependency>
115119
<dependency>
116120
<groupId>com.fasterxml.jackson.core</groupId>
117121
<artifactId>jackson-core</artifactId>

sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/ConfigurationController.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ class ConfigurationController {
2828
/**
2929
* Get the list of configurations.
3030
*
31-
* @return the list of configurations
31+
* @param accept the accept header
32+
* @return a response entity with a string representation of the list of configurations
3233
*/
3334
@GetMapping("/configurations")
3435
ResponseEntity<String> getConfigurations(
3536
@RequestHeader(value = "accept", required = false) final String accept)
3637
throws JsonProcessingException {
37-
var configList = CLIENT.query("default");
38+
final var configList = CLIENT.query("default");
3839
if ("application/json".equals(accept)) {
3940
return ResponseEntity.ok()
4041
.contentType(MediaType.APPLICATION_JSON)
@@ -43,9 +44,15 @@ ResponseEntity<String> getConfigurations(
4344
return ResponseEntity.ok(buildMessage(configList));
4445
}
4546

46-
String buildMessage(AiConfigurationList configList) {
47-
var message = new StringBuilder("The following configurations are available: ");
48-
for (var resource : configList.getResources()) {
47+
/**
48+
* Build a message from the configuration list.
49+
*
50+
* @param configList The configuration list.
51+
* @return the message
52+
*/
53+
private String buildMessage(final AiConfigurationList configList) {
54+
final var message = new StringBuilder("The following configurations are available: ");
55+
for (final var resource : configList.getResources()) {
4956
message.append(resource.getName()).append(", ");
5057
}
5158
message.setCharAt(message.length() - 2, '.');

sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/DeploymentController.java

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import com.sap.ai.sdk.core.model.AiDeploymentDeletionResponse;
1616
import com.sap.ai.sdk.core.model.AiDeploymentList;
1717
import com.sap.ai.sdk.core.model.AiDeploymentModificationRequest;
18-
import com.sap.ai.sdk.core.model.AiDeploymentModificationResponse;
1918
import com.sap.ai.sdk.core.model.AiDeploymentTargetStatus;
2019
import com.sap.ai.sdk.core.model.AiParameterArgumentBinding;
2120
import com.sap.ai.sdk.foundationmodels.openai.OpenAiModel;
@@ -41,7 +40,8 @@ class DeploymentController {
4140
private static final DeploymentApi CLIENT = new DeploymentApi();
4241
private static final String RESOURCE_GROUP = "default";
4342
private final ObjectMapper mapper =
44-
new ObjectMapper().setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
43+
new ObjectMapper()
44+
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
4545
.registerModule(new JavaTimeModule());
4646

4747
/**
@@ -61,6 +61,13 @@ AiDeploymentDeletionResponse createAndDeleteDeploymentByConfigId(final String co
6161
return CLIENT.delete(RESOURCE_GROUP, deployment.getId());
6262
}
6363

64+
/**
65+
* Create and delete a deployment with the Java specific configuration ID
66+
*
67+
* @param configId The configuration id.
68+
* @param accept The accept header.
69+
* @return a response entity with a string representation of the deployment creation response
70+
*/
6471
@GetMapping("/by-config/{id}/createDelete")
6572
ResponseEntity<String> createAndDeleteDeploymentByConfigId(
6673
@Nonnull @PathVariable("id") final String configId,
@@ -72,7 +79,8 @@ ResponseEntity<String> createAndDeleteDeploymentByConfigId(
7279
.contentType(MediaType.APPLICATION_JSON)
7380
.body(mapper.writeValueAsString(response));
7481
}
75-
return ResponseEntity.ok("Deployment under config with id " + configId + " created and deleted.");
82+
return ResponseEntity.ok(
83+
"Deployment under config with id " + configId + " created and deleted.");
7684
}
7785

7886
/**
@@ -81,26 +89,29 @@ ResponseEntity<String> createAndDeleteDeploymentByConfigId(
8189
* <p>Only RUNNING deployments can be STOPPED
8290
*
8391
* @param configId The configuration id.
84-
* @return the deployment modification response
92+
* @param accept The accept header.
93+
* @return a response entity with a string representation of the deployment modification response
8594
*/
8695
@GetMapping("/by-config/{id}/stop")
8796
@Nonnull
8897
ResponseEntity<String> stopByConfigId(
8998
@Nonnull @PathVariable("id") final String configId,
90-
@RequestHeader(value = "accept", required = false) final String accept) throws JsonProcessingException {
99+
@RequestHeader(value = "accept", required = false) final String accept)
100+
throws JsonProcessingException {
91101
final List<AiDeployment> myDeployments = getAllByConfigId(configId);
92102
log.info("Found {} deployments to STOP", myDeployments.size());
93103

94104
// STOP my deployments
95-
var stoppedDeployments = myDeployments.stream()
96-
.map(
97-
deployment ->
98-
CLIENT.modify(
99-
RESOURCE_GROUP,
100-
deployment.getId(),
101-
AiDeploymentModificationRequest.create()
102-
.targetStatus(AiDeploymentTargetStatus.STOPPED)))
103-
.toList();
105+
final var stoppedDeployments =
106+
myDeployments.stream()
107+
.map(
108+
deployment ->
109+
CLIENT.modify(
110+
RESOURCE_GROUP,
111+
deployment.getId(),
112+
AiDeploymentModificationRequest.create()
113+
.targetStatus(AiDeploymentTargetStatus.STOPPED)))
114+
.toList();
104115

105116
if ("application/json".equals(accept)) {
106117
return ResponseEntity.ok()
@@ -116,19 +127,23 @@ ResponseEntity<String> stopByConfigId(
116127
* <p>Only UNKNOWN and STOPPED deployments can be DELETED
117128
*
118129
* @param configId The configuration id.
119-
* @return the deployment deletion response
130+
* @param accept The accept header.
131+
* @return a response entity with a string representation of the deployment deletion response
120132
*/
121133
@GetMapping("/by-config/{id}/delete")
122134
@Nonnull
123135
ResponseEntity<String> deleteByConfigId(
124-
@Nonnull @PathVariable("id") final String configId, @RequestHeader(value = "accept", required = false) final String accept) throws JsonProcessingException {
136+
@Nonnull @PathVariable("id") final String configId,
137+
@RequestHeader(value = "accept", required = false) final String accept)
138+
throws JsonProcessingException {
125139
final List<AiDeployment> myDeployments = getAllByConfigId(configId);
126140
log.info("Found {} deployments to DELETE", myDeployments.size());
127141

128142
// DELETE my deployments
129-
var responseList = myDeployments.stream()
130-
.map(deployment -> CLIENT.delete(RESOURCE_GROUP, deployment.getId()))
131-
.toList();
143+
final var responseList =
144+
myDeployments.stream()
145+
.map(deployment -> CLIENT.delete(RESOURCE_GROUP, deployment.getId()))
146+
.toList();
132147
if ("application/json".equals(accept)) {
133148
return ResponseEntity.ok()
134149
.contentType(MediaType.APPLICATION_JSON)
@@ -141,7 +156,8 @@ ResponseEntity<String> deleteByConfigId(
141156
* Get all deployments with the Java specific configuration ID
142157
*
143158
* @param configId The configuration id.
144-
* @return the Java specific deployments
159+
* @param accept The accept header.
160+
* @return a response entity with a string representation of the Java specific deployments
145161
*/
146162
@GetMapping("/by-config/{id}/getAll")
147163
ResponseEntity<String> getAllByConfigId(
@@ -157,16 +173,27 @@ ResponseEntity<String> getAllByConfigId(
157173
return ResponseEntity.ok(buildMessage(deployments));
158174
}
159175

160-
private String buildMessage(List<AiDeployment> deployments) {
161-
var message = new StringBuilder("The following deployments are available: ");
162-
for (var deployment : deployments) {
176+
/**
177+
* Build a message from the deployment list.
178+
*
179+
* @param deployments The deployment list.
180+
* @return the message
181+
*/
182+
private String buildMessage(final List<AiDeployment> deployments) {
183+
final var message = new StringBuilder("The following deployments are available: ");
184+
for (final var deployment : deployments) {
163185
message.append(deployment.getId()).append(", ");
164186
}
165187
message.setCharAt(message.length() - 2, '.');
166188
return message.toString();
167189
}
168190

169-
191+
/**
192+
* Get all deployments with the Java specific configuration ID
193+
*
194+
* @param configId The configuration id.
195+
* @return the Java specific deployments
196+
*/
170197
@Nonnull
171198
List<AiDeployment> getAllByConfigId(@Nonnull @PathVariable("id") final String configId) {
172199
final AiDeploymentList deploymentList = CLIENT.query(RESOURCE_GROUP);
@@ -179,7 +206,8 @@ List<AiDeployment> getAllByConfigId(@Nonnull @PathVariable("id") final String co
179206
/**
180207
* Get all deployments, including non-Java specific deployments
181208
*
182-
* @return the Java specific deployments
209+
* @param accept The accept header.
210+
* @return a response entity with a string representation of the Java specific deployments
183211
*/
184212
@GetMapping("/getAll")
185213
@Nonnull
@@ -195,6 +223,11 @@ ResponseEntity<String> getAll(
195223
return ResponseEntity.ok(buildMessage(deployments.getResources().stream().toList()));
196224
}
197225

226+
/**
227+
* Get all deployments
228+
*
229+
* @return all deployments
230+
*/
198231
@Nullable
199232
AiDeploymentList getAll() {
200233
return CLIENT.query(RESOURCE_GROUP);

sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/ScenarioController.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.sap.ai.sdk.core.model.AiModelList;
1010
import com.sap.ai.sdk.core.model.AiScenarioList;
1111
import javax.annotation.Nonnull;
12-
1312
import org.springframework.http.MediaType;
1413
import org.springframework.http.ResponseEntity;
1514
import org.springframework.web.bind.annotation.GetMapping;
@@ -21,8 +20,6 @@
2120
@SuppressWarnings("unused") // debug method that doesn't need to be tested
2221
class ScenarioController {
2322

24-
// TODO: Fix javadocs
25-
2623
private static final ScenarioApi CLIENT = new ScenarioApi();
2724
private final ObjectMapper mapper =
2825
new ObjectMapper()
@@ -32,14 +29,15 @@ class ScenarioController {
3229
/**
3330
* Get the list of available scenarios
3431
*
35-
* @return the list of available scenarios
32+
* @param accept the accept header
33+
* @return a response entity with a string representation of the list of available scenarios
3634
*/
3735
@GetMapping("/scenarios")
3836
@Nonnull
3937
ResponseEntity<String> getScenarios(
4038
@RequestHeader(value = "accept", required = false) final String accept)
4139
throws JsonProcessingException {
42-
var scenarioList = CLIENT.query("default");
40+
final var scenarioList = CLIENT.query("default");
4341
if ("application/json".equals(accept)) {
4442
return ResponseEntity.ok()
4543
.contentType(MediaType.APPLICATION_JSON)
@@ -48,6 +46,11 @@ ResponseEntity<String> getScenarios(
4846
return ResponseEntity.ok(buildScenarioMessage(scenarioList));
4947
}
5048

49+
/**
50+
* Get the list of available models
51+
*
52+
* @return the list of available models
53+
*/
5154
@Nonnull
5255
AiModelList getModels() {
5356
return CLIENT.queryModels("foundation-models", "default");
@@ -56,14 +59,15 @@ AiModelList getModels() {
5659
/**
5760
* Get the list of available models
5861
*
59-
* @return the list of available models
62+
* @param accept the accept header
63+
* @return a response entity with a string representation of the list of available models
6064
*/
6165
@GetMapping("/models")
6266
@Nonnull
6367
ResponseEntity<String> getModels(
6468
@RequestHeader(value = "accept", required = false) final String accept)
6569
throws JsonProcessingException {
66-
var modelList = getModels();
70+
final var modelList = getModels();
6771
if ("application/json".equals(accept)) {
6872
return ResponseEntity.ok()
6973
.contentType(MediaType.APPLICATION_JSON)
@@ -72,18 +76,30 @@ ResponseEntity<String> getModels(
7276
return ResponseEntity.ok(buildModelMessage(modelList));
7377
}
7478

75-
String buildScenarioMessage(AiScenarioList scenarioList) {
76-
var message = new StringBuilder("The following scenarios are available: ");
77-
for (var resource : scenarioList.getResources()) {
79+
/**
80+
* Build a message from the scenario list
81+
*
82+
* @param scenarioList the list of scenarios
83+
* @return a string representation of the list of scenarios
84+
*/
85+
private String buildScenarioMessage(final AiScenarioList scenarioList) {
86+
final var message = new StringBuilder("The following scenarios are available: ");
87+
for (final var resource : scenarioList.getResources()) {
7888
message.append(resource.getName()).append(", ");
7989
}
8090
message.setCharAt(message.length() - 2, '.');
8191
return message.toString();
8292
}
8393

84-
String buildModelMessage(AiModelList modelList) {
85-
var message = new StringBuilder("The following models are available: ");
86-
for (var resource : modelList.getResources()) {
94+
/**
95+
* Build a message from the model list
96+
*
97+
* @param modelList the list of models
98+
* @return a string representation of the list of models
99+
*/
100+
private String buildModelMessage(final AiModelList modelList) {
101+
final var message = new StringBuilder("The following models are available: ");
102+
for (final var resource : modelList.getResources()) {
87103
message.append(resource.getModel()).append(", ");
88104
}
89105
message.setCharAt(message.length() - 2, '.');

0 commit comments

Comments
 (0)