Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ ResponseEntity<ResponseBodyEmitter> streamChatCompletion() {
final Runnable consumeStream =
() -> {
try (stream) {
stream.forEach(
deltaMessage -> {
log.info("Service: {}", deltaMessage);
send(emitter, deltaMessage);
});
stream.forEach(deltaMessage -> send(emitter, deltaMessage));
} finally {
emitter.complete();
}
Expand Down Expand Up @@ -191,18 +187,19 @@ Object inputFiltering(
try {
response = service.inputFiltering(policy);
} catch (OrchestrationFilterException.Input e) {
final var msg =
final var errorMessage =
new StringBuilder(
"[Http %d] Failed to obtain a response as the content was flagged by input filter. "
.formatted(e.getStatusCode()));

Optional.ofNullable(e.getAzureContentSafetyInput())
.map(AzureContentSafetyInput::getViolence)
.filter(rating -> rating.compareTo(policy.getAzureThreshold()) > 0)
.ifPresent(rating -> msg.append("Violence score %d".formatted(rating.getValue())));
.ifPresent(
rating -> errorMessage.append("Violence score %d".formatted(rating.getValue())));

log.debug(msg.toString(), e);
return ResponseEntity.internalServerError().body(msg.toString());
log.error(errorMessage.toString(), e);
return ResponseEntity.internalServerError().body(errorMessage.toString());
}

if ("json".equals(format)) {
Expand All @@ -224,17 +221,18 @@ Object outputFiltering(
try {
content = response.getContent();
} catch (OrchestrationFilterException.Output e) {
final var msg =
final var errorMessage =
new StringBuilder(
"Failed to obtain a response as the content was flagged by output filter. ");

Optional.ofNullable(e.getAzureContentSafetyOutput())
.map(AzureContentSafetyOutput::getViolence)
.filter(rating -> rating.compareTo(policy.getAzureThreshold()) > 0)
.ifPresent(rating -> msg.append("Violence score %d ".formatted(rating.getValue())));
.ifPresent(
rating -> errorMessage.append("Violence score %d ".formatted(rating.getValue())));

log.debug(msg.toString(), e);
return ResponseEntity.internalServerError().body(msg.toString());
log.error(errorMessage.toString(), e);
return ResponseEntity.internalServerError().body(errorMessage.toString());
}

if ("json".equals(format)) {
Expand All @@ -253,14 +251,14 @@ Object llamaGuardInputFiltering(
try {
response = service.llamaGuardInputFilter(enabled);
} catch (OrchestrationFilterException.Input e) {
var msg =
var errorMessage =
"[Http %d] Failed to obtain a response as the content was flagged by input filter. "
.formatted(e.getStatusCode());
if (e.getLlamaGuard38b() != null) {
msg += " Violent crimes are %s".formatted(e.getLlamaGuard38b().isViolentCrimes());
errorMessage += " Violent crimes are %s".formatted(e.getLlamaGuard38b().isViolentCrimes());
}
log.debug(msg, e);
return ResponseEntity.internalServerError().body(msg);
log.error(errorMessage, e);
return ResponseEntity.internalServerError().body(errorMessage);
}

if ("json".equals(format)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ Object inputFiltering(
try {
response = service.inputFiltering(policy);
} catch (OrchestrationClientException e) {
final var msg = "Failed to obtain a response as the content was flagged by input filter.";
log.debug(msg, e);
return ResponseEntity.internalServerError().body(msg);
final var errorMessage =
"Failed to obtain a response as the content was flagged by input filter.";
log.error(errorMessage, e);
return ResponseEntity.internalServerError().body(errorMessage);
}

if ("json".equals(format)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ void testPromptCaching() {
assertThat(second).isNotNull();
assertThat(second.promptCacheKey()).contains(cacheKey);
assertThat(second.usage().isPresent()).isTrue();

assertThat(second.usage().get().inputTokensDetails().cachedTokens()).isGreaterThanOrEqualTo(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatCompletionParameters;
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatMessage.OpenAiChatUserMessage;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

@Slf4j
class OpenAiDeprecatedTest {
OpenAiServiceDeprecated service;

Expand Down Expand Up @@ -57,7 +55,6 @@ void streamChatCompletion() {
.forEach(
delta -> {
final String deltaContent = delta.getDeltaContent();
log.info("delta: {}", delta);
if (!deltaContent.isEmpty()) {
filledDeltaCount.incrementAndGet();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

@Slf4j
class OpenAiTest {
OpenAiService service;

Expand Down Expand Up @@ -63,7 +61,6 @@ void streamChatCompletion() {
delta -> {
usageRef.compareAndExchange(null, delta.getCompletionUsage());
final String deltaContent = delta.getDeltaContent();
log.info("delta: {}", delta);
if (!deltaContent.isEmpty()) {
filledDeltaCount.incrementAndGet();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ void testStreamChatCompletion() {
// foreach consumes all elements, closing the stream at the end
.forEach(
delta -> {
log.info("delta: {}", delta);
if (!delta.isEmpty()) {
filledDeltaCount.incrementAndGet();
}
Expand Down Expand Up @@ -625,7 +624,6 @@ void testCompletionWithFallbackStreaming() {
val filledDeltaCount = new AtomicInteger(0);
stream.forEach(
delta -> {
log.info("delta: {}", delta);
if (!delta.isEmpty()) {
filledDeltaCount.incrementAndGet();
}
Expand All @@ -639,7 +637,6 @@ void testStreamDeltasWithInlineConfig() {
val filledDeltaCount = new AtomicInteger(0);
stream.forEach(
delta -> {
log.info("delta: {}", delta);
if (!delta.getDeltaContent().isEmpty()) {
filledDeltaCount.incrementAndGet();
}
Expand All @@ -653,7 +650,6 @@ void testStreamDeltasWithReferenceById() {
val filledDeltaCount = new AtomicInteger(0);
stream.forEach(
delta -> {
log.info("delta: {}", delta);
if (!delta.getDeltaContent().isEmpty()) {
filledDeltaCount.incrementAndGet();
}
Expand All @@ -667,7 +663,6 @@ void testStreamDeltasWithReferenceByScenario() {
val filledDeltaCount = new AtomicInteger(0);
stream.forEach(
delta -> {
log.info("delta: {}", delta);
if (!delta.getDeltaContent().isEmpty()) {
filledDeltaCount.incrementAndGet();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
class SpringAiOpenAiTest {

private final SpringAiOpenAiService service = new SpringAiOpenAiService();
private static final org.slf4j.Logger log =
org.slf4j.LoggerFactory.getLogger(SpringAiOrchestrationTest.class);

@Test
void testEmbedStrings() {
Expand Down Expand Up @@ -46,7 +44,6 @@ void testStreamChatCompletion() {
// foreach consumes all elements, closing the stream at the end
.forEach(
delta -> {
log.info("delta: {}", delta);
String text = delta.getResult().getOutput().getText();
if (text != null && !text.isEmpty()) {
filledDeltaCount.incrementAndGet();
Expand Down Expand Up @@ -86,7 +83,6 @@ void testChatMemory() {
ChatResponse response = service.chatMemory();
assertThat(response).isNotNull();
String text = response.getResult().getOutput().getText();
log.info(text);
assertThat(text)
.containsAnyOf(
"French", "onion", "pastries", "cheese", "baguette", "coq au vin", "foie gras");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ void testStreamChatCompletion() {
// foreach consumes all elements, closing the stream at the end
.forEach(
delta -> {
log.info("delta: {}", delta);
if (!delta.getResult().getOutput().getText().isEmpty()) {
final var text = delta.getResult().getOutput().getText();
if (text != null && !text.isEmpty()) {
filledDeltaCount.incrementAndGet();
}
});
Expand Down Expand Up @@ -153,7 +153,6 @@ void testChatMemory() {
ChatResponse response = service.chatMemory();
assertThat(response).isNotNull();
String text = response.getResult().getOutput().getText();
log.info(text);
assertThat(text)
.containsAnyOf(
"French", "onion", "pastries", "cheese", "baguette", "coq au vin", "foie gras");
Expand Down