mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-05-15 03:47:13 +02:00
Add support for registering object instances instead of only through the @OllamaToolService
annotation
This commit is contained in:
parent
f27bea11d5
commit
b2b3febdaa
@ -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:
|
||||
|
||||
```json
|
||||
|
@ -826,7 +826,9 @@ public class OllamaAPI {
|
||||
toolRegistry.addTool(toolSpecification.getFunctionName(), toolSpecification);
|
||||
}
|
||||
|
||||
|
||||
public void registerAnnotatedTools() {
|
||||
try {
|
||||
Class<?> callerClass = null;
|
||||
try {
|
||||
callerClass = Class.forName(Thread.currentThread().getStackTrace()[2].getClassName());
|
||||
@ -840,9 +842,17 @@ public class OllamaAPI {
|
||||
}
|
||||
|
||||
Class<?>[] providers = ollamaToolServiceAnnotation.providers();
|
||||
|
||||
for (Class<?> provider : providers) {
|
||||
Method[] methods = provider.getMethods();
|
||||
registerAnnotatedTools(provider.getDeclaredConstructor().newInstance());
|
||||
}
|
||||
} catch (InstantiationException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void registerAnnotatedTools(Object object) {
|
||||
Class<?> objectClass = object.getClass();
|
||||
Method[] methods = objectClass.getMethods();
|
||||
for(Method m : methods) {
|
||||
ToolSpec toolSpec = m.getDeclaredAnnotation(ToolSpec.class);
|
||||
if(toolSpec == null){
|
||||
@ -895,19 +905,10 @@ public class OllamaAPI {
|
||||
)
|
||||
.build();
|
||||
|
||||
try {
|
||||
ReflectionalToolFunction reflectionalToolFunction =
|
||||
new ReflectionalToolFunction(provider.getDeclaredConstructor().newInstance()
|
||||
,m
|
||||
,methodParams);
|
||||
|
||||
new ReflectionalToolFunction(object, m, methodParams);
|
||||
toolSpecification.setToolFunction(reflectionalToolFunction);
|
||||
toolRegistry.addTool(toolSpecification.getFunctionName(),toolSpecification);
|
||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
|
||||
NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -338,7 +338,7 @@ class TestRealAPIs {
|
||||
ollamaAPI.setVerbose(true);
|
||||
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(config.getModel());
|
||||
|
||||
ollamaAPI.registerAnnotatedTools();
|
||||
ollamaAPI.registerAnnotatedTools(new AnnotatedTool());
|
||||
|
||||
OllamaChatRequest requestModel = builder
|
||||
.withMessage(OllamaChatMessageRole.USER,
|
||||
|
Loading…
x
Reference in New Issue
Block a user