-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ai): Support tools from non-public classes/methods
- Loading branch information
1 parent
a24f152
commit 0c308dc
Showing
3 changed files
with
219 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
...rconia-ai-core/src/test/java/io/arconia/ai/core/tools/method/MethodToolCallbackTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
package io.arconia.ai.core.tools.method; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
import io.arconia.ai.core.tools.annotation.Tool; | ||
import io.arconia.ai.core.tools.json.JsonParser; | ||
import io.arconia.ai.core.tools.metadata.ToolMetadata; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Unit tests for {@link MethodToolCallback}. | ||
*/ | ||
class MethodToolCallbackTests { | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { | ||
"publicStaticMethod", | ||
"privateStaticMethod", | ||
"packageStaticMethod", | ||
"publicMethod", | ||
"privateMethod", | ||
"packageMethod" | ||
}) | ||
void shouldCallToolFromPublicClass(String methodName) { | ||
validateAssertions(methodName, new PublicTools()); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { | ||
"publicStaticMethod", | ||
"privateStaticMethod", | ||
"packageStaticMethod", | ||
"publicMethod", | ||
"privateMethod", | ||
"packageMethod" | ||
}) | ||
void shouldCallToolFromPrivateClass(String methodName) { | ||
validateAssertions(methodName, new PrivateTools()); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { | ||
"publicStaticMethod", | ||
"privateStaticMethod", | ||
"packageStaticMethod", | ||
"publicMethod", | ||
"privateMethod", | ||
"packageMethod" | ||
}) | ||
void shouldCallToolFromPackageClass(String methodName) { | ||
validateAssertions(methodName, new PackageTools()); | ||
} | ||
|
||
private static void validateAssertions(String methodName, Object toolObject) { | ||
Method toolMethod = getMethod(methodName, toolObject.getClass()); | ||
assertThat(toolMethod).isNotNull(); | ||
MethodToolCallback callback = MethodToolCallback.builder() | ||
.toolMetadata(ToolMetadata.from(toolMethod)) | ||
.toolMethod(toolMethod) | ||
.toolObject(toolObject) | ||
.build(); | ||
|
||
String result = callback.call(""" | ||
{ | ||
"input": "Wingardium Leviosa" | ||
} | ||
"""); | ||
|
||
assertThat(JsonParser.fromJson(result, new TypeReference<List<String>>() {})) | ||
.contains("Wingardium Leviosa"); | ||
} | ||
|
||
private static Method getMethod(String name, Class<?> toolsClass) { | ||
return Arrays.stream(ReflectionUtils.getDeclaredMethods(toolsClass)) | ||
.filter(m -> m.getName().equals(name)) | ||
.findFirst() | ||
.orElseThrow(); | ||
} | ||
|
||
static public class PublicTools { | ||
|
||
@Tool("Test description") | ||
public static List<String> publicStaticMethod(String input) { | ||
return List.of(input); | ||
} | ||
|
||
@Tool("Test description") | ||
private static List<String> privateStaticMethod(String input) { | ||
return List.of(input); | ||
} | ||
|
||
@Tool("Test description") | ||
static List<String> packageStaticMethod(String input) { | ||
return List.of(input); | ||
} | ||
|
||
@Tool("Test description") | ||
public List<String> publicMethod(String input) { | ||
return List.of(input); | ||
} | ||
|
||
@Tool("Test description") | ||
private List<String> privateMethod(String input) { | ||
return List.of(input); | ||
} | ||
|
||
@Tool("Test description") | ||
List<String> packageMethod(String input) { | ||
return List.of(input); | ||
} | ||
|
||
} | ||
|
||
static private class PrivateTools { | ||
|
||
@Tool("Test description") | ||
public static List<String> publicStaticMethod(String input) { | ||
return List.of(input); | ||
} | ||
|
||
@Tool("Test description") | ||
private static List<String> privateStaticMethod(String input) { | ||
return List.of(input); | ||
} | ||
|
||
@Tool("Test description") | ||
static List<String> packageStaticMethod(String input) { | ||
return List.of(input); | ||
} | ||
|
||
@Tool("Test description") | ||
public List<String> publicMethod(String input) { | ||
return List.of(input); | ||
} | ||
|
||
@Tool("Test description") | ||
private List<String> privateMethod(String input) { | ||
return List.of(input); | ||
} | ||
|
||
@Tool("Test description") | ||
List<String> packageMethod(String input) { | ||
return List.of(input); | ||
} | ||
|
||
} | ||
|
||
static class PackageTools { | ||
|
||
@Tool("Test description") | ||
public static List<String> publicStaticMethod(String input) { | ||
return List.of(input); | ||
} | ||
|
||
@Tool("Test description") | ||
private static List<String> privateStaticMethod(String input) { | ||
return List.of(input); | ||
} | ||
|
||
@Tool("Test description") | ||
static List<String> packageStaticMethod(String input) { | ||
return List.of(input); | ||
} | ||
|
||
@Tool("Test description") | ||
public List<String> publicMethod(String input) { | ||
return List.of(input); | ||
} | ||
|
||
@Tool("Test description") | ||
private List<String> privateMethod(String input) { | ||
return List.of(input); | ||
} | ||
|
||
@Tool("Test description") | ||
List<String> packageMethod(String input) { | ||
return List.of(input); | ||
} | ||
|
||
} | ||
|
||
} |