Skip to content

Commit

Permalink
Added output parsers for numeric types (langchain4j#27)
Browse files Browse the repository at this point in the history
Added output parsers for numeric types: byte, short, int, long,
BigInteger, float, double and BigDecimal
  • Loading branch information
langchain4j authored Jul 15, 2023
1 parent 907c1eb commit ce598df
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.langchain4j.model.output;

import java.math.BigDecimal;

public class BigDecimalOutputParser implements OutputParser<BigDecimal> {

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

@Override
public String formatInstructions() {
return "floating point number";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.langchain4j.model.output;

import java.math.BigInteger;

public class BigIntegerOutputParser implements OutputParser<BigInteger> {

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

@Override
public String formatInstructions() {
return "integer number";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.langchain4j.model.output;

public class ByteOutputParser implements OutputParser<Byte> {

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

@Override
public String formatInstructions() {
return "integer number in range [-128, 127]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.langchain4j.model.output;

public class DoubleOutputParser implements OutputParser<Double> {

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

@Override
public String formatInstructions() {
return "floating point number";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.langchain4j.model.output;

public class FloatOutputParser implements OutputParser<Float> {

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

@Override
public String formatInstructions() {
return "floating point number";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.langchain4j.model.output;

public class IntOutputParser implements OutputParser<Integer> {

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

@Override
public String formatInstructions() {
return "integer number";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.langchain4j.model.output;

public class LongOutputParser implements OutputParser<Long> {

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

@Override
public String formatInstructions() {
return "integer number";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.langchain4j.model.output;

public class ShortOutputParser implements OutputParser<Short> {

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

@Override
public String formatInstructions() {
return "integer number in range [-32768, 32767]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
Expand All @@ -24,6 +26,29 @@ public class ServiceOutputParser {
static {
OUTPUT_PARSERS.put(boolean.class, new BooleanOutputParser());
OUTPUT_PARSERS.put(Boolean.class, new BooleanOutputParser());

OUTPUT_PARSERS.put(byte.class, new ByteOutputParser());
OUTPUT_PARSERS.put(Byte.class, new ByteOutputParser());

OUTPUT_PARSERS.put(short.class, new ShortOutputParser());
OUTPUT_PARSERS.put(Short.class, new ShortOutputParser());

OUTPUT_PARSERS.put(int.class, new IntOutputParser());
OUTPUT_PARSERS.put(Integer.class, new IntOutputParser());

OUTPUT_PARSERS.put(long.class, new LongOutputParser());
OUTPUT_PARSERS.put(Long.class, new LongOutputParser());

OUTPUT_PARSERS.put(BigInteger.class, new BigIntegerOutputParser());

OUTPUT_PARSERS.put(float.class, new FloatOutputParser());
OUTPUT_PARSERS.put(Float.class, new FloatOutputParser());

OUTPUT_PARSERS.put(double.class, new DoubleOutputParser());
OUTPUT_PARSERS.put(Double.class, new DoubleOutputParser());

OUTPUT_PARSERS.put(BigDecimal.class, new BigDecimalOutputParser());

OUTPUT_PARSERS.put(Date.class, new DateOutputParser());
OUTPUT_PARSERS.put(LocalDate.class, new LocalDateOutputParser());
OUTPUT_PARSERS.put(LocalTime.class, new LocalTimeOutputParser());
Expand Down

0 comments on commit ce598df

Please sign in to comment.