refactor: rename generateAsync method to generate and update image handling in OllamaAPI

- Renamed `generateAsync` to `generate` for clarity.
- Consolidated image handling in `generateWithImages` to accept multiple image types (File, byte[], String).
- Updated request format handling in `OllamaCommonRequest` and related classes to use a more flexible format property.
- Adjusted integration and unit tests to reflect changes in method signatures and functionality.
This commit is contained in:
Amith Koujalgi
2025-09-15 23:35:53 +05:30
parent 51501cf5e1
commit 44c6236243
9 changed files with 102 additions and 188 deletions

View File

@@ -602,9 +602,9 @@ class OllamaAPIIntegrationTest {
throws OllamaBaseException, IOException, URISyntaxException, InterruptedException {
api.pullModel(VISION_MODEL);
OllamaResult result = api.generateWithImageURLs(VISION_MODEL, "What is in this image?",
OllamaResult result = api.generateWithImages(VISION_MODEL, "What is in this image?",
List.of("https://i.pinimg.com/736x/f9/4e/cb/f94ecba040696a3a20b484d2e15159ec.jpg"),
new OptionsBuilder().build());
new OptionsBuilder().build(), null, null);
assertNotNull(result);
assertNotNull(result.getResponse());
assertFalse(result.getResponse().isEmpty());
@@ -617,8 +617,8 @@ class OllamaAPIIntegrationTest {
api.pullModel(VISION_MODEL);
File imageFile = getImageFileFromClasspath("roses.jpg");
try {
OllamaResult result = api.generateWithImageFiles(VISION_MODEL, "What is in this image?",
List.of(imageFile), new OptionsBuilder().build());
OllamaResult result = api.generateWithImages(VISION_MODEL, "What is in this image?",
List.of(imageFile), new OptionsBuilder().build(), null, null);
assertNotNull(result);
assertNotNull(result.getResponse());
assertFalse(result.getResponse().isEmpty());
@@ -637,11 +637,17 @@ class OllamaAPIIntegrationTest {
StringBuffer sb = new StringBuffer();
OllamaResult result = api.generateWithImageFiles(VISION_MODEL, "What is in this image?",
List.of(imageFile), new OptionsBuilder().build(), (s) -> {
OllamaResult result = api.generateWithImages(
VISION_MODEL,
"What is in this image?",
List.of(imageFile),
new OptionsBuilder().build(),
null,
(s) -> {
LOG.info(s);
sb.append(s);
});
}
);
assertNotNull(result);
assertNotNull(result.getResponse());
assertFalse(result.getResponse().isEmpty());

View File

@@ -154,15 +154,15 @@ class TestMockedAPIs {
String model = OllamaModelType.LLAMA2;
String prompt = "some prompt text";
try {
when(ollamaAPI.generateWithImageFiles(
model, prompt, Collections.emptyList(), new OptionsBuilder().build()))
when(ollamaAPI.generateWithImages(
model, prompt, Collections.emptyList(), new OptionsBuilder().build(), null, null))
.thenReturn(new OllamaResult("", "", 0, 200));
ollamaAPI.generateWithImageFiles(
model, prompt, Collections.emptyList(), new OptionsBuilder().build());
ollamaAPI.generateWithImages(
model, prompt, Collections.emptyList(), new OptionsBuilder().build(), null, null);
verify(ollamaAPI, times(1))
.generateWithImageFiles(
model, prompt, Collections.emptyList(), new OptionsBuilder().build());
} catch (IOException | OllamaBaseException | InterruptedException e) {
.generateWithImages(
model, prompt, Collections.emptyList(), new OptionsBuilder().build(), null, null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@@ -173,14 +173,14 @@ class TestMockedAPIs {
String model = OllamaModelType.LLAMA2;
String prompt = "some prompt text";
try {
when(ollamaAPI.generateWithImageURLs(
model, prompt, Collections.emptyList(), new OptionsBuilder().build()))
when(ollamaAPI.generateWithImages(
model, prompt, Collections.emptyList(), new OptionsBuilder().build(), null, null))
.thenReturn(new OllamaResult("", "", 0, 200));
ollamaAPI.generateWithImageURLs(
model, prompt, Collections.emptyList(), new OptionsBuilder().build());
ollamaAPI.generateWithImages(
model, prompt, Collections.emptyList(), new OptionsBuilder().build(), null, null);
verify(ollamaAPI, times(1))
.generateWithImageURLs(
model, prompt, Collections.emptyList(), new OptionsBuilder().build());
.generateWithImages(
model, prompt, Collections.emptyList(), new OptionsBuilder().build(), null, null);
} catch (IOException | OllamaBaseException | InterruptedException | URISyntaxException e) {
throw new RuntimeException(e);
}
@@ -191,10 +191,10 @@ class TestMockedAPIs {
OllamaAPI ollamaAPI = Mockito.mock(OllamaAPI.class);
String model = OllamaModelType.LLAMA2;
String prompt = "some prompt text";
when(ollamaAPI.generateAsync(model, prompt, false, false))
when(ollamaAPI.generate(model, prompt, false, false))
.thenReturn(new OllamaAsyncResultStreamer(null, null, 3));
ollamaAPI.generateAsync(model, prompt, false, false);
verify(ollamaAPI, times(1)).generateAsync(model, prompt, false, false);
ollamaAPI.generate(model, prompt, false, false);
verify(ollamaAPI, times(1)).generate(model, prompt, false, false);
}
@Test

View File

@@ -44,11 +44,11 @@ public class TestGenerateRequestSerialization extends AbstractSerializationTest<
builder.withPrompt("Some prompt").withGetJsonResponse().build();
String jsonRequest = serialize(req);
System.out.printf(jsonRequest);
// no jackson deserialization as format property is not boolean ==> omit as deserialization
// of request is never used in real code anyways
JSONObject jsonObject = new JSONObject(jsonRequest);
String requestFormatProperty = jsonObject.getString("format");
assertEquals("json", requestFormatProperty);
}
}