Skip to content

Commit 9df5618

Browse files
committed
Adopting suggestions
- streamline error handling at endpoint - extends docs
1 parent c438d9a commit 9df5618

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

docs/guides/ORCHESTRATION_CHAT_COMPLETION.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,16 @@ var result =
164164
```
165165
#### Behavior of Input and Output Filters
166166

167-
- **Input Filter**: Triggers a `400 Bad Request` in response during the `chatCompletion` call if the input message violates the filter policy.
168-
- **Output Filter**: Allows the request to complete but throws an exception when accessing filtered content via `result.getContent()`.
167+
- **Input Filter**:
168+
If the input message violates the filter policy, a `400 (Bad Request)` response will be received during the `chatCompletion` call.
169+
An `OrchestrationClientException` will be thrown.
169170

170-
You will find [some examples](../../sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/OrchestrationController.java) in our Spring Boot application demonstrating response handling with filters.
171+
- **Output Filter**:
172+
If the response message violates the output filter policy, the `chatCompletion` call will complete without exception.
173+
The convenience method `getContent()` on the resulting object will throw an `OrchestrationClientException` upon invocation.
174+
The low level API under `getOriginalResponse()` will not throw an exception.
175+
176+
You will find [some examples](../../sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/OrchestrationController.java) in our Spring Boot application demonstrating response handling with filters.
171177

172178
### Data masking
173179

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import com.fasterxml.jackson.databind.ObjectMapper;
99
import com.sap.ai.sdk.app.services.OrchestrationService;
1010
import com.sap.ai.sdk.orchestration.AzureFilterThreshold;
11+
import com.sap.ai.sdk.orchestration.OrchestrationChatResponse;
1112
import com.sap.ai.sdk.orchestration.OrchestrationClientException;
1213
import com.sap.ai.sdk.orchestration.model.DPIEntities;
1314
import com.sap.cloud.sdk.cloudplatform.thread.ThreadContextExecutors;
14-
import java.util.Map;
1515
import javax.annotation.Nonnull;
1616
import lombok.extern.slf4j.Slf4j;
1717
import org.springframework.beans.factory.annotation.Autowired;
@@ -142,13 +142,22 @@ ResponseEntity<String> inputFiltering(
142142
@RequestHeader(value = "accept", required = false) final String accept,
143143
@Nonnull @PathVariable("policy") final AzureFilterThreshold policy)
144144
throws JsonProcessingException {
145-
final var response = service.inputFiltering(policy);
146-
if ("application/json".equals(accept)) {
145+
146+
final OrchestrationChatResponse response;
147+
try {
148+
response = service.inputFiltering(policy);
149+
} catch (OrchestrationClientException e) {
150+
final var msg = "Failed to obtain a response as the content was flagged by input filter.";
151+
log.debug(msg, e);
152+
return ResponseEntity.internalServerError().body(msg);
153+
}
154+
155+
if (accept.equals("application/json")) {
147156
return ResponseEntity.ok()
148157
.contentType(MediaType.APPLICATION_JSON)
149158
.body(mapper.writeValueAsString(response));
150159
}
151-
return ResponseEntity.ok(response.getContent());
160+
return ResponseEntity.ok().body(response.getContent());
152161
}
153162

154163
/**
@@ -173,21 +182,19 @@ ResponseEntity<String> outputFiltering(
173182
@Nonnull @PathVariable("policy") final AzureFilterThreshold policy)
174183
throws JsonProcessingException, OrchestrationClientException {
175184

176-
String content;
185+
final var response = service.outputFiltering(policy);
177186
try {
178-
content = service.outputFiltering(policy).getContent();
187+
if (accept.equals("application/json")) {
188+
return ResponseEntity.ok()
189+
.contentType(MediaType.APPLICATION_JSON)
190+
.body(mapper.writeValueAsString(response));
191+
}
192+
return ResponseEntity.ok().body(response.getContent());
179193
} catch (OrchestrationClientException e) {
180-
content = "Failed to obtain a response as the content was flagged by output filter.";
181-
log.debug(content, e);
194+
final var msg = "Failed to obtain a response as the content was flagged by output filter.";
195+
log.debug(msg, e);
196+
return ResponseEntity.internalServerError().body(msg);
182197
}
183-
184-
if ("application/json".equals(accept)) {
185-
return ResponseEntity.ok()
186-
.contentType(MediaType.APPLICATION_JSON)
187-
.body(mapper.writeValueAsString(Map.of("content", content)));
188-
}
189-
190-
return ResponseEntity.ok(content);
191198
}
192199

193200
/**

0 commit comments

Comments
 (0)