added ai-moderation image and text
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package com.krylov.refound.ai.client;
|
||||
|
||||
import com.krylov.refound.ai.dto.ModerationResponse;
|
||||
import com.krylov.refound.ai.dto.TextModerationRequest;
|
||||
import com.krylov.refound.ai.exception.AiServerException;
|
||||
import com.krylov.refound.ai.exception.AiUnavailableException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.retry.annotation.Backoff;
|
||||
import org.springframework.retry.annotation.Recover;
|
||||
import org.springframework.retry.annotation.Retryable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class TextModerationClient {
|
||||
|
||||
private final AiHttpClient client;
|
||||
|
||||
@Retryable(
|
||||
retryFor = {
|
||||
AiUnavailableException.class,
|
||||
AiServerException.class
|
||||
},
|
||||
maxAttempts = 3,
|
||||
backoff = @Backoff(
|
||||
delay = 500,
|
||||
multiplier = 2
|
||||
)
|
||||
)
|
||||
public ModerationResponse moderate(String text) {
|
||||
log.info("Calling AI service");
|
||||
return client.post(
|
||||
"/api/v1/moderation/text",
|
||||
new TextModerationRequest(text),
|
||||
ModerationResponse.class
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@Recover
|
||||
public ModerationResponse recover(AiUnavailableException e, String text) {
|
||||
log.error("AI unavailable after retries", e);
|
||||
|
||||
return ModerationResponse.builder()
|
||||
.approved(false)
|
||||
.reason("AI_UNAVAILABLE")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user