refactor: optimize ...

This commit is contained in:
Michael Yang 2024-03-08 12:12:45 +08:00
parent d2ef347d5d
commit 4b4fb420c5
4 changed files with 20 additions and 11 deletions

View File

@ -15,8 +15,12 @@
*/
package com.agentsflex.convert;
import com.agentsflex.util.ArrayUtil;
import com.agentsflex.util.StringUtil;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
@ -30,6 +34,14 @@ public class ConvertService {
register(new LongConverter(), Long.class, long.class);
register(new DoubleConverter(), Double.class, double.class);
register(new FloatConverter(), Float.class, float.class);
register(new ShortConverter(), Short.class, short.class);
register(new BigDecimalConverter(), BigDecimal.class);
register(new BigIntegerConverter(), BigInteger.class);
register(new ByteConverter(), byte.class);
register(new ByteArrayConverter(), byte[].class);
}
private static void register(IConverter<?> converter, Class<?>... classes) {
@ -48,11 +60,11 @@ public class ConvertService {
return value;
}
String valueString = value.toString();
if (valueString == null) {
return null;
if (toType == Serializable.class && ArrayUtil.contains(value.getClass().getInterfaces(), Serializable.class)) {
return value;
}
valueString = valueString.trim();
String valueString = value.toString().trim();
if (valueString.isEmpty()) {
return null;
}
@ -61,6 +73,7 @@ public class ConvertService {
if (converter != null) {
return converter.convert(valueString);
}
return null;
}

View File

@ -28,14 +28,10 @@ public class Functions<T> extends ArrayList<Function<T>> {
public static <R> Functions<R> from(Class<?> clazz, String... methodNames) {
List<Method> methodList = ClassUtil.getAllMethods(clazz, method -> {
if (Modifier.isStatic(method.getModifiers())) {
if (!Modifier.isStatic(method.getModifiers())) {
return false;
}
// if (!resultType.isAssignableFrom(method.getReturnType())) {
// return false;
// }
if (method.getAnnotation(FunctionDef.class) == null) {
return false;
}

View File

@ -27,7 +27,7 @@ public class OpenAiLlmTest {
OpenAiLlm llm = new OpenAiLlm(config);
FunctionPrompt prompt = new FunctionPrompt("今天北京的天气怎么样", WeatherUtil.class);
FunctionPrompt prompt = new FunctionPrompt("今天北京的天气怎么样", WeatherFunctions.class);
FunctionMessageResponse response = llm.chat(prompt);
Object result = response.invoke();

View File

@ -3,7 +3,7 @@ package com.agentsflex.llm.openai;
import com.agentsflex.functions.annotation.FunctionDef;
import com.agentsflex.functions.annotation.FunctionParam;
public class WeatherUtil {
public class WeatherFunctions {
@FunctionDef(name = "get_the_weather_info", description = "get the weather info")
public static String getWeatherInfo(