Skip to content

Commit

Permalink
Merge pull request #1086 from jmartisk/guardrails-test
Browse files Browse the repository at this point in the history
Move JsonGuardrailsUtilsTest to the deployment module
  • Loading branch information
geoand authored Nov 19, 2024
2 parents c74b715 + 7a9ae5f commit f1e7487
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package io.quarkiverse.langchain4j.guardrails;
package io.quarkiverse.langchain4j.test.guardrails;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;

import jakarta.inject.Inject;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.fasterxml.jackson.core.type.TypeReference;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkiverse.langchain4j.guardrails.JsonGuardrailsUtils;
import io.quarkus.test.QuarkusUnitTest;

@QuarkusTest
class JsonGuardrailsUtilsTest {

@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));

@Inject
JsonGuardrailsUtils jsonGuardrailsUtils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import com.fasterxml.jackson.databind.ObjectMapper;

@ApplicationScoped
class JsonGuardrailsUtils {
public class JsonGuardrailsUtils {

@Inject
ObjectMapper objectMapper;

private JsonGuardrailsUtils() {
}

String trimNonJson(String llmResponse) {
public String trimNonJson(String llmResponse) {
int jsonMapStart = llmResponse.indexOf('{');
int jsonListStart = llmResponse.indexOf('[');
if (jsonMapStart < 0 && jsonListStart < 0) {
Expand All @@ -29,15 +29,15 @@ String trimNonJson(String llmResponse) {
return jsonEnd >= 0 && jsonStart < jsonEnd ? llmResponse.substring(jsonStart, jsonEnd + 1) : null;
}

<T> T deserialize(String json, Class<T> expectedOutputClass) {
public <T> T deserialize(String json, Class<T> expectedOutputClass) {
try {
return objectMapper.readValue(json, expectedOutputClass);
} catch (JsonProcessingException e) {
return null;
}
}

<T> T deserialize(String json, TypeReference<T> expectedOutputType) {
public <T> T deserialize(String json, TypeReference<T> expectedOutputType) {
try {
return objectMapper.readValue(json, expectedOutputType);
} catch (JsonProcessingException e) {
Expand Down

0 comments on commit f1e7487

Please sign in to comment.