Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Trim LLM response string before parsing into Java types
  • Loading branch information
langchain4j committed Jun 13, 2024
1 parent 2b2c09f commit 35e76cc
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class BigDecimalOutputParser implements OutputParser<BigDecimal> {

@Override
public BigDecimal parse(String string) {
return new BigDecimal(string);
return new BigDecimal(string.trim());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class BigIntegerOutputParser implements OutputParser<BigInteger> {

@Override
public BigInteger parse(String string) {
return new BigInteger(string);
return new BigInteger(string.trim());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class BooleanOutputParser implements OutputParser<Boolean> {

@Override
public Boolean parse(String string) {
return Boolean.parseBoolean(string);
return Boolean.parseBoolean(string.trim());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class ByteOutputParser implements OutputParser<Byte> {

@Override
public Byte parse(String string) {
return Byte.parseByte(string);
return Byte.parseByte(string.trim());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class DoubleOutputParser implements OutputParser<Double> {

@Override
public Double parse(String string) {
return Double.parseDouble(string);
return Double.parseDouble(string.trim());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public EnumOutputParser(Class<? extends Enum> enumClass) {

@Override
public Enum parse(String string) {
string = string.trim();
for (Enum enumConstant : enumClass.getEnumConstants()) {
if (enumConstant.name().equalsIgnoreCase(string)) {
return enumConstant;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class FloatOutputParser implements OutputParser<Float> {

@Override
public Float parse(String string) {
return Float.parseFloat(string);
return Float.parseFloat(string.trim());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class IntOutputParser implements OutputParser<Integer> {

@Override
public Integer parse(String string) {
return Integer.parseInt(string);
return Integer.parseInt(string.trim());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class LocalDateOutputParser implements OutputParser<LocalDate> {

@Override
public LocalDate parse(String string) {
return LocalDate.parse(string, ISO_LOCAL_DATE);
return LocalDate.parse(string.trim(), ISO_LOCAL_DATE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class LocalDateTimeOutputParser implements OutputParser<LocalDateTime> {

@Override
public LocalDateTime parse(String string) {
return LocalDateTime.parse(string, ISO_LOCAL_DATE_TIME);
return LocalDateTime.parse(string.trim(), ISO_LOCAL_DATE_TIME);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class LocalTimeOutputParser implements OutputParser<LocalTime> {

@Override
public LocalTime parse(String string) {
return LocalTime.parse(string, ISO_LOCAL_TIME);
return LocalTime.parse(string.trim(), ISO_LOCAL_TIME);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class LongOutputParser implements OutputParser<Long> {

@Override
public Long parse(String string) {
return Long.parseLong(string);
return Long.parseLong(string.trim());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class ShortOutputParser implements OutputParser<Short> {

@Override
public Short parse(String string) {
return Short.parseShort(string);
return Short.parseShort(string.trim());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void test_BigDecimal() {
.isEqualTo("floating point number");

assertThat(parser.parse("3.14")).isEqualTo(new BigDecimal("3.14"));
assertThat(parser.parse(" 3.14 ")).isEqualTo(new BigDecimal("3.14"));

assertThatExceptionOfType(NumberFormatException.class)
.isThrownBy(() -> parser.parse("3.14.15"));
Expand All @@ -31,6 +32,7 @@ public void test_BigInteger() {
.isEqualTo("integer number");

assertThat(parser.parse("42")).isEqualTo(42);
assertThat(parser.parse(" 42 ")).isEqualTo(42);

assertThatExceptionOfType(NumberFormatException.class)
.isThrownBy(() -> parser.parse("42.0"));
Expand All @@ -43,6 +45,7 @@ public void test_Boolean() {
.isEqualTo("one of [true, false]");

assertThat(parser.parse("true"))
.isEqualTo(parser.parse(" true "))
.isEqualTo(parser.parse("TRUE"))
.isEqualTo(true);
assertThat(parser.parse("false"))
Expand All @@ -59,6 +62,7 @@ public void test_Byte() {
.isEqualTo("integer number in range [-128, 127]");

assertThat(parser.parse("42")).isEqualTo((byte) 42);
assertThat(parser.parse(" 42 ")).isEqualTo((byte) 42);
assertThat(parser.parse("-42")).isEqualTo((byte) -42);

assertThatExceptionOfType(NumberFormatException.class)
Expand All @@ -74,6 +78,7 @@ public void test_Date() {

assertThat(parser.parse("2020-01-12"))
.isEqualTo(parser.parse("2020-01-12"))
.isEqualTo(parser.parse(" 2020-01-12 "))
.isEqualTo(new Date(120, Calendar.JANUARY, 12));

assertThatExceptionOfType(RuntimeException.class)
Expand All @@ -88,6 +93,7 @@ public void test_Double() {
.isEqualTo("floating point number");

assertThat(parser.parse("3.14")).isEqualTo(3.14);
assertThat(parser.parse(" 3.14 ")).isEqualTo(3.14);

assertThatExceptionOfType(NumberFormatException.class)
.isThrownBy(() -> parser.parse("3.14.15"));
Expand All @@ -105,6 +111,7 @@ public void test_Enum() {
.isEqualTo("one of [A, B, C]");

assertThat(parser.parse("A"))
.isEqualTo(parser.parse(" A "))
.isEqualTo(parser.parse("a"))
.isEqualTo(Enum.A);
assertThat(parser.parse("B"))
Expand All @@ -126,6 +133,7 @@ public void test_Float() {
.isEqualTo("floating point number");

assertThat(parser.parse("3.14")).isEqualTo(3.14f);
assertThat(parser.parse(" 3.14 ")).isEqualTo(3.14f);

assertThatExceptionOfType(NumberFormatException.class)
.isThrownBy(() -> parser.parse("3.14.15"));
Expand All @@ -138,6 +146,7 @@ public void test_Integer() {
.isEqualTo("integer number");

assertThat(parser.parse("42")).isEqualTo(42);
assertThat(parser.parse(" 42 ")).isEqualTo(42);

assertThatExceptionOfType(NumberFormatException.class)
.isThrownBy(() -> parser.parse("42.0"));
Expand All @@ -150,7 +159,7 @@ public void test_LocalDate() {
.isEqualTo("yyyy-MM-dd");

assertThat(parser.parse("2020-01-12"))
.isEqualTo(parser.parse("2020-01-12"))
.isEqualTo(parser.parse(" 2020-01-12 "))
.isEqualTo(LocalDate.of(2020, 1, 12));

assertThatExceptionOfType(RuntimeException.class)
Expand All @@ -167,7 +176,7 @@ public void test_LocalDateTime() {
.isEqualTo("yyyy-MM-ddTHH:mm:ss");

assertThat(parser.parse("2020-01-12T12:34:56"))
.isEqualTo(parser.parse("2020-01-12T12:34:56"))
.isEqualTo(parser.parse(" 2020-01-12T12:34:56 "))
.isEqualTo(LocalDateTime.of(2020, 1, 12, 12, 34, 56));

assertThatExceptionOfType(RuntimeException.class)
Expand All @@ -184,7 +193,7 @@ public void test_LocalTime() {
.isEqualTo("HH:mm:ss");

assertThat(parser.parse("12:34:56"))
.isEqualTo(parser.parse("12:34:56"))
.isEqualTo(parser.parse(" 12:34:56 "))
.isEqualTo(LocalTime.of(12, 34, 56));

assertThat(parser.parse("12:34:56.789"))
Expand All @@ -201,6 +210,7 @@ public void test_Long() {
.isEqualTo("integer number");

assertThat(parser.parse("42")).isEqualTo(42L);
assertThat(parser.parse(" 42 ")).isEqualTo(42L);

assertThatExceptionOfType(NumberFormatException.class)
.isThrownBy(() -> parser.parse("42.0"));
Expand All @@ -213,6 +223,7 @@ public void test_Short() {
.isEqualTo("integer number in range [-32768, 32767]");

assertThat(parser.parse("42")).isEqualTo((short) 42);
assertThat(parser.parse(" 42 ")).isEqualTo((short) 42);
assertThat(parser.parse("-42")).isEqualTo((short) -42);

assertThatExceptionOfType(NumberFormatException.class)
Expand Down

0 comments on commit 35e76cc

Please sign in to comment.