From 5e6971cc4a6272a509a9ffdef00713d5a35df44b Mon Sep 17 00:00:00 2001 From: Markus Klenke Date: Fri, 27 Dec 2024 22:20:34 +0100 Subject: [PATCH] Adds first approach to annotation based tool callings using basic java reflection --- .../java/io/github/ollama4j/OllamaAPI.java | 103 ++++++++++++++++++ .../tools/ReflectionalToolFunction.java | 49 +++++++++ .../tools/annotations/OllamaToolService.java | 13 +++ .../tools/annotations/ToolProperty.java | 17 +++ .../ollama4j/tools/annotations/ToolSpec.java | 15 +++ .../integrationtests/TestRealAPIs.java | 90 ++++++++++++++- .../ollama4j/samples/AnnotatedTool.java | 21 ++++ 7 files changed, 304 insertions(+), 4 deletions(-) create mode 100644 src/main/java/io/github/ollama4j/tools/ReflectionalToolFunction.java create mode 100644 src/main/java/io/github/ollama4j/tools/annotations/OllamaToolService.java create mode 100644 src/main/java/io/github/ollama4j/tools/annotations/ToolProperty.java create mode 100644 src/main/java/io/github/ollama4j/tools/annotations/ToolSpec.java create mode 100644 src/test/java/io/github/ollama4j/samples/AnnotatedTool.java diff --git a/src/main/java/io/github/ollama4j/OllamaAPI.java b/src/main/java/io/github/ollama4j/OllamaAPI.java index 90dcd35..ddd7783 100644 --- a/src/main/java/io/github/ollama4j/OllamaAPI.java +++ b/src/main/java/io/github/ollama4j/OllamaAPI.java @@ -15,11 +15,17 @@ import io.github.ollama4j.models.ps.ModelsProcessResponse; import io.github.ollama4j.models.request.*; import io.github.ollama4j.models.response.*; import io.github.ollama4j.tools.*; +import io.github.ollama4j.tools.annotations.OllamaToolService; +import io.github.ollama4j.tools.annotations.ToolProperty; +import io.github.ollama4j.tools.annotations.ToolSpec; import io.github.ollama4j.utils.Options; import io.github.ollama4j.utils.Utils; import lombok.Setter; import java.io.*; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; import java.net.URI; import java.net.URISyntaxException; import java.net.http.HttpClient; @@ -603,6 +609,15 @@ public class OllamaAPI { OllamaToolsResult toolResult = new OllamaToolsResult(); Map toolResults = new HashMap<>(); + if(!prompt.startsWith("[AVAILABLE_TOOLS]")){ + final Tools.PromptBuilder promptBuilder = new Tools.PromptBuilder(); + for(Tools.ToolSpecification spec : toolRegistry.getRegisteredSpecs()) { + promptBuilder.withToolSpecification(spec); + } + promptBuilder.withPrompt(prompt); + prompt = promptBuilder.build(); + } + OllamaResult result = generate(model, prompt, raw, options, null); toolResult.setModelResult(result); @@ -811,6 +826,94 @@ public class OllamaAPI { toolRegistry.addTool(toolSpecification.getFunctionName(), toolSpecification); } + public void registerAnnotatedTools() { + Class callerClass = null; + try { + callerClass = Class.forName(Thread.currentThread().getStackTrace()[2].getClassName()); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + + OllamaToolService ollamaToolServiceAnnotation = callerClass.getDeclaredAnnotation(OllamaToolService.class); + if(ollamaToolServiceAnnotation == null) { + throw new IllegalStateException(callerClass + " is not annotated as " + OllamaToolService.class); + } + + Class[] providers = ollamaToolServiceAnnotation.providers(); + + for(Class provider : providers){ + System.err.println("Provider: " + provider.getName()); + Method[] methods = provider.getMethods(); + for(Method m : methods) { + ToolSpec toolSpec = m.getDeclaredAnnotation(ToolSpec.class); + if(toolSpec == null){ + continue; + } + String operationName = !toolSpec.name().isBlank() ? toolSpec.name() : m.getName(); + String operationDesc = !toolSpec.desc().isBlank() ? toolSpec.desc() : operationName; + System.err.println("Method: " + operationName); + + final Tools.PropsBuilder propsBuilder = new Tools.PropsBuilder(); + LinkedHashMap methodParams = new LinkedHashMap<>(); + for (Parameter parameter : m.getParameters()) { + final ToolProperty toolPropertyAnn = parameter.getDeclaredAnnotation(ToolProperty.class); + String propType = parameter.getType().getTypeName(); + if(toolPropertyAnn == null) { + methodParams.put(parameter.getName(),null); + continue; + } + String propName = !toolPropertyAnn.name().isBlank() ? toolPropertyAnn.name() : parameter.getName(); + methodParams.put(propName,propType); + propsBuilder.withProperty(propName,Tools.PromptFuncDefinition.Property.builder() + .type(propType) + .description(toolPropertyAnn.desc()) + .required(toolPropertyAnn.required()) + .build()); + } + final Map params = propsBuilder.build(); + List reqProps = params.entrySet().stream() + .filter(e -> e.getValue().isRequired()) + .map(Map.Entry::getKey) + .collect(Collectors.toList()); + + Tools.ToolSpecification toolSpecification = Tools.ToolSpecification.builder() + .functionName(operationName) + .functionDescription(operationDesc) + .toolPrompt( + Tools.PromptFuncDefinition.builder().type("function").function( + Tools.PromptFuncDefinition.PromptFuncSpec.builder() + .name(operationName) + .description(operationDesc) + .parameters( + Tools.PromptFuncDefinition.Parameters.builder() + .type("object") + .properties( + params + ) + .required(reqProps) + .build() + ).build() + ).build() + ) + .build(); + + try { + ReflectionalToolFunction reflectionalToolFunction = + new ReflectionalToolFunction(provider.getDeclaredConstructor().newInstance() + ,m + ,methodParams); + + toolSpecification.setToolFunction(reflectionalToolFunction); + toolRegistry.addTool(toolSpecification.getFunctionName(),toolSpecification); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | + NoSuchMethodException e) { + throw new RuntimeException(e); + } + } + } + + } + /** * Adds a custom role. * diff --git a/src/main/java/io/github/ollama4j/tools/ReflectionalToolFunction.java b/src/main/java/io/github/ollama4j/tools/ReflectionalToolFunction.java new file mode 100644 index 0000000..397b2f6 --- /dev/null +++ b/src/main/java/io/github/ollama4j/tools/ReflectionalToolFunction.java @@ -0,0 +1,49 @@ +package io.github.ollama4j.tools; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +import java.lang.reflect.Method; +import java.util.LinkedHashMap; +import java.util.Map; + +@Setter +@Getter +@AllArgsConstructor +public class ReflectionalToolFunction implements ToolFunction{ + + private Object functionHolder; + private Method function; + private LinkedHashMap propertyDefinition; + + @Override + public Object apply(Map arguments) { + LinkedHashMap argumentsCopy = new LinkedHashMap<>(this.propertyDefinition); + for (Map.Entry param : this.propertyDefinition.entrySet()){ + argumentsCopy.replace(param.getKey(),typeCast(arguments.get(param.getKey()),param.getValue())); + } + try { + return function.invoke(functionHolder, argumentsCopy.values().toArray()); + } catch (Exception e) { + throw new RuntimeException("Failed to invoke tool: " + function.getName(), e); + } + } + + private Object typeCast(Object inputValue, String className) { + if(className == null || inputValue == null) { + return null; + } + String inputValueString = inputValue.toString(); + if("java.lang.Integer".equals(className)){ + return Integer.parseInt(inputValueString); + } + if("java.lang.Boolean".equals(className)){ + return Boolean.valueOf(inputValueString); + } + else { + return inputValueString; + } + } + +} diff --git a/src/main/java/io/github/ollama4j/tools/annotations/OllamaToolService.java b/src/main/java/io/github/ollama4j/tools/annotations/OllamaToolService.java new file mode 100644 index 0000000..8140f92 --- /dev/null +++ b/src/main/java/io/github/ollama4j/tools/annotations/OllamaToolService.java @@ -0,0 +1,13 @@ +package io.github.ollama4j.tools.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface OllamaToolService { + + Class[] providers(); +} diff --git a/src/main/java/io/github/ollama4j/tools/annotations/ToolProperty.java b/src/main/java/io/github/ollama4j/tools/annotations/ToolProperty.java new file mode 100644 index 0000000..bd1bf07 --- /dev/null +++ b/src/main/java/io/github/ollama4j/tools/annotations/ToolProperty.java @@ -0,0 +1,17 @@ +package io.github.ollama4j.tools.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.PARAMETER) +public @interface ToolProperty { + + String name(); + + String desc(); + + boolean required() default true; +} diff --git a/src/main/java/io/github/ollama4j/tools/annotations/ToolSpec.java b/src/main/java/io/github/ollama4j/tools/annotations/ToolSpec.java new file mode 100644 index 0000000..c8725a4 --- /dev/null +++ b/src/main/java/io/github/ollama4j/tools/annotations/ToolSpec.java @@ -0,0 +1,15 @@ +package io.github.ollama4j.tools.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface ToolSpec { + + String name() default ""; + + String desc(); +} diff --git a/src/test/java/io/github/ollama4j/integrationtests/TestRealAPIs.java b/src/test/java/io/github/ollama4j/integrationtests/TestRealAPIs.java index 668a5dc..d0b4aeb 100644 --- a/src/test/java/io/github/ollama4j/integrationtests/TestRealAPIs.java +++ b/src/test/java/io/github/ollama4j/integrationtests/TestRealAPIs.java @@ -7,8 +7,11 @@ import io.github.ollama4j.models.response.ModelDetail; import io.github.ollama4j.models.response.OllamaResult; import io.github.ollama4j.models.embeddings.OllamaEmbeddingsRequestBuilder; import io.github.ollama4j.models.embeddings.OllamaEmbeddingsRequestModel; +import io.github.ollama4j.samples.AnnotatedTool; +import io.github.ollama4j.tools.OllamaToolCallsFunction; import io.github.ollama4j.tools.ToolFunction; import io.github.ollama4j.tools.Tools; +import io.github.ollama4j.tools.annotations.OllamaToolService; import io.github.ollama4j.utils.OptionsBuilder; import lombok.Data; import org.junit.jupiter.api.BeforeEach; @@ -27,6 +30,8 @@ import java.util.*; import static org.junit.jupiter.api.Assertions.*; +@OllamaToolService(providers = {AnnotatedTool.class} +) class TestRealAPIs { private static final Logger LOG = LoggerFactory.getLogger(TestRealAPIs.class); @@ -229,7 +234,7 @@ class TestRealAPIs { @Test @Order(3) - void testChatWithTools() { + void testChatWithExplicitToolDefinition() { testEndpointReachability(); try { ollamaAPI.setVerbose(true); @@ -275,9 +280,10 @@ class TestRealAPIs { assertEquals(OllamaChatMessageRole.ASSISTANT.getRoleName(),chatResult.getResponseModel().getMessage().getRole().getRoleName()); List toolCalls = chatResult.getChatHistory().get(1).getToolCalls(); assertEquals(1, toolCalls.size()); - assertEquals("get-employee-details",toolCalls.get(0).getFunction().getName()); - assertEquals(1, toolCalls.get(0).getFunction().getArguments().size()); - Object employeeName = toolCalls.get(0).getFunction().getArguments().get("employee-name"); + OllamaToolCallsFunction function = toolCalls.get(0).getFunction(); + assertEquals("get-employee-details", function.getName()); + assertEquals(1, function.getArguments().size()); + Object employeeName = function.getArguments().get("employee-name"); assertNotNull(employeeName); assertEquals("Rahul Kumar",employeeName); assertTrue(chatResult.getChatHistory().size()>2); @@ -288,6 +294,82 @@ class TestRealAPIs { } } + @Test + @Order(3) + void testChatWithAnnotatedToolsAndSingleParam() { + testEndpointReachability(); + try { + ollamaAPI.setVerbose(true); + OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(config.getModel()); + + ollamaAPI.registerAnnotatedTools(); + + OllamaChatRequest requestModel = builder + .withMessage(OllamaChatMessageRole.USER, + "Compute the most important constant in the world using 5 digits") + .build(); + + OllamaChatResult chatResult = ollamaAPI.chat(requestModel); + assertNotNull(chatResult); + assertNotNull(chatResult.getResponseModel()); + assertNotNull(chatResult.getResponseModel().getMessage()); + assertEquals(OllamaChatMessageRole.ASSISTANT.getRoleName(),chatResult.getResponseModel().getMessage().getRole().getRoleName()); + List toolCalls = chatResult.getChatHistory().get(1).getToolCalls(); + assertEquals(1, toolCalls.size()); + OllamaToolCallsFunction function = toolCalls.get(0).getFunction(); + assertEquals("computeMkeConstant", function.getName()); + assertEquals(1, function.getArguments().size()); + Object noOfDigits = function.getArguments().get("noOfDigits"); + assertNotNull(noOfDigits); + assertEquals("5",noOfDigits); + assertTrue(chatResult.getChatHistory().size()>2); + List finalToolCalls = chatResult.getResponseModel().getMessage().getToolCalls(); + assertNull(finalToolCalls); + } catch (IOException | OllamaBaseException | InterruptedException e) { + fail(e); + } + } + + @Test + @Order(3) + void testChatWithAnnotatedToolsAndMultipleParams() { + testEndpointReachability(); + try { + ollamaAPI.setVerbose(true); + OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(config.getModel()); + + ollamaAPI.registerAnnotatedTools(); + + OllamaChatRequest requestModel = builder + .withMessage(OllamaChatMessageRole.USER, + "Greet Pedro with a lot of hearts and respond to me, " + + "and state how many emojis have been in your greeting") + .build(); + + OllamaChatResult chatResult = ollamaAPI.chat(requestModel); + assertNotNull(chatResult); + assertNotNull(chatResult.getResponseModel()); + assertNotNull(chatResult.getResponseModel().getMessage()); + assertEquals(OllamaChatMessageRole.ASSISTANT.getRoleName(),chatResult.getResponseModel().getMessage().getRole().getRoleName()); + List toolCalls = chatResult.getChatHistory().get(1).getToolCalls(); + assertEquals(1, toolCalls.size()); + OllamaToolCallsFunction function = toolCalls.get(0).getFunction(); + assertEquals("sayHello", function.getName()); + assertEquals(2, function.getArguments().size()); + Object name = function.getArguments().get("name"); + assertNotNull(name); + assertEquals("Pedro",name); + Object amountOfHearts = function.getArguments().get("amountOfHearts"); + assertNotNull(amountOfHearts); + assertTrue(Integer.parseInt(amountOfHearts.toString()) > 1); + assertTrue(chatResult.getChatHistory().size()>2); + List finalToolCalls = chatResult.getResponseModel().getMessage().getToolCalls(); + assertNull(finalToolCalls); + } catch (IOException | OllamaBaseException | InterruptedException e) { + fail(e); + } + } + @Test @Order(3) void testChatWithToolsAndStream() { diff --git a/src/test/java/io/github/ollama4j/samples/AnnotatedTool.java b/src/test/java/io/github/ollama4j/samples/AnnotatedTool.java new file mode 100644 index 0000000..8d75be1 --- /dev/null +++ b/src/test/java/io/github/ollama4j/samples/AnnotatedTool.java @@ -0,0 +1,21 @@ +package io.github.ollama4j.samples; + +import io.github.ollama4j.tools.annotations.ToolProperty; +import io.github.ollama4j.tools.annotations.ToolSpec; + +import java.math.BigDecimal; + +public class AnnotatedTool { + + @ToolSpec(desc = "Computes the most important constant all around the globe!") + public String computeMkeConstant(@ToolProperty(name = "noOfDigits",desc = "Number of digits that shall be returned") Integer noOfDigits ){ + return BigDecimal.valueOf((long)(Math.random()*1000000L),noOfDigits).toString(); + } + + @ToolSpec(desc = "Says hello to a friend!") + public String sayHello(@ToolProperty(name = "name",desc = "Name of the friend") String name, Integer someRandomProperty, @ToolProperty(name="amountOfHearts",desc = "amount of heart emojis that should be used", required = false) Integer amountOfHearts) { + String hearts = amountOfHearts!=null ? "♡".repeat(amountOfHearts) : ""; + return "Hello " + name +" ("+someRandomProperty+") " + hearts; + } + +}