mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-05-15 11:57:12 +02:00
Merge pull request #87 from seeseemelk/feature/annotated-objects
Add support for registering object instances
This commit is contained in:
commit
a06a4025fa
@ -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
|
||||||
|
@ -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());
|
||||||
@ -840,9 +842,17 @@ public class OllamaAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Class<?>[] providers = ollamaToolServiceAnnotation.providers();
|
Class<?>[] providers = ollamaToolServiceAnnotation.providers();
|
||||||
|
|
||||||
for (Class<?> provider : 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) {
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user