Add support for registering object instances instead of only through the @OllamaToolService annotation

This commit is contained in:
Sebastiaan de Schaetzen 2025-01-24 13:38:47 +01:00
parent f27bea11d5
commit b2b3febdaa
3 changed files with 93 additions and 75 deletions

View File

@ -521,6 +521,23 @@ public class MyOllamaService{
} }
``` ```
Or, if one needs to provide an object instance directly:
```java
public class MyOllamaService{
public void chatWithAnnotatedTool(){
ollamaAPI.registerAnnotatedTools(new BackendService());
OllamaChatRequest requestModel = builder
.withMessage(OllamaChatMessageRole.USER,
"Compute the most important constant in the world using 5 digits")
.build();
OllamaChatResult chatResult = ollamaAPI.chat(requestModel);
}
}
```
The request should be the following: The request should be the following:
```json ```json

View File

@ -826,7 +826,9 @@ public class OllamaAPI {
toolRegistry.addTool(toolSpecification.getFunctionName(), toolSpecification); toolRegistry.addTool(toolSpecification.getFunctionName(), toolSpecification);
} }
public void registerAnnotatedTools() { public void registerAnnotatedTools() {
try {
Class<?> callerClass = null; Class<?> callerClass = null;
try { try {
callerClass = Class.forName(Thread.currentThread().getStackTrace()[2].getClassName()); callerClass = Class.forName(Thread.currentThread().getStackTrace()[2].getClassName());
@ -835,14 +837,22 @@ public class OllamaAPI {
} }
OllamaToolService ollamaToolServiceAnnotation = callerClass.getDeclaredAnnotation(OllamaToolService.class); OllamaToolService ollamaToolServiceAnnotation = callerClass.getDeclaredAnnotation(OllamaToolService.class);
if(ollamaToolServiceAnnotation == null) { if (ollamaToolServiceAnnotation == null) {
throw new IllegalStateException(callerClass + " is not annotated as " + OllamaToolService.class); throw new IllegalStateException(callerClass + " is not annotated as " + OllamaToolService.class);
} }
Class<?>[] providers = ollamaToolServiceAnnotation.providers(); Class<?>[] providers = ollamaToolServiceAnnotation.providers();
for (Class<?> provider : providers) {
registerAnnotatedTools(provider.getDeclaredConstructor().newInstance());
}
} catch (InstantiationException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
for(Class<?> provider : providers){ public void registerAnnotatedTools(Object object) {
Method[] methods = provider.getMethods(); Class<?> objectClass = object.getClass();
Method[] methods = objectClass.getMethods();
for(Method m : methods) { for(Method m : methods) {
ToolSpec toolSpec = m.getDeclaredAnnotation(ToolSpec.class); ToolSpec toolSpec = m.getDeclaredAnnotation(ToolSpec.class);
if(toolSpec == null){ if(toolSpec == null){
@ -895,19 +905,10 @@ public class OllamaAPI {
) )
.build(); .build();
try {
ReflectionalToolFunction reflectionalToolFunction = ReflectionalToolFunction reflectionalToolFunction =
new ReflectionalToolFunction(provider.getDeclaredConstructor().newInstance() new ReflectionalToolFunction(object, m, methodParams);
,m
,methodParams);
toolSpecification.setToolFunction(reflectionalToolFunction); toolSpecification.setToolFunction(reflectionalToolFunction);
toolRegistry.addTool(toolSpecification.getFunctionName(),toolSpecification); toolRegistry.addTool(toolSpecification.getFunctionName(),toolSpecification);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
} }
} }

View File

@ -338,7 +338,7 @@ class TestRealAPIs {
ollamaAPI.setVerbose(true); ollamaAPI.setVerbose(true);
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(config.getModel()); OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(config.getModel());
ollamaAPI.registerAnnotatedTools(); ollamaAPI.registerAnnotatedTools(new AnnotatedTool());
OllamaChatRequest requestModel = builder OllamaChatRequest requestModel = builder
.withMessage(OllamaChatMessageRole.USER, .withMessage(OllamaChatMessageRole.USER,