Enhance CI workflow and refactor integration tests

- Added setup step for Ollama installation in the GitHub Actions workflow.
- Updated environment variables for integration tests to utilize an external Ollama host.
- Refactored test method names in OllamaAPIIntegrationTest for clarity and consistency, changing 'testAskModel' to 'testGenerate'.
- Introduced new tests for image processing from URLs and files, while removing outdated tests for improved test suite relevance.
This commit is contained in:
amithkoujalgi 2025-08-30 18:00:04 +05:30
parent c705432510
commit 0ec20d14b0
No known key found for this signature in database
GPG Key ID: E29A37746AF94B70
3 changed files with 81 additions and 67 deletions

View File

@ -20,6 +20,10 @@ jobs:
with: with:
ref: ${{ github.event.inputs.branch }} ref: ${{ github.event.inputs.branch }}
- name: Set up Ollama
run: |
curl -fsSL https://ollama.com/install.sh | sh
- name: Set up JDK 17 - name: Set up JDK 17
uses: actions/setup-java@v3 uses: actions/setup-java@v3
with: with:
@ -33,3 +37,6 @@ jobs:
- name: Run integration tests - name: Run integration tests
run: mvn clean verify -Pintegration-tests run: mvn clean verify -Pintegration-tests
env:
USE_EXTERNAL_OLLAMA_HOST: "true"
OLLAMA_HOST: "http://localhost:11434"

View File

@ -1,11 +1,13 @@
package io.github.ollama4j.utils; package io.github.ollama4j.utils;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.util.Objects;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
@ -15,7 +17,7 @@ public class Utils {
private static ObjectMapper objectMapper; private static ObjectMapper objectMapper;
public static ObjectMapper getObjectMapper() { public static ObjectMapper getObjectMapper() {
if(objectMapper == null) { if (objectMapper == null) {
objectMapper = new ObjectMapper(); objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule()); objectMapper.registerModule(new JavaTimeModule());
} }
@ -35,4 +37,9 @@ public class Utils {
return out.toByteArray(); return out.toByteArray();
} }
} }
public static File getFileFromClasspath(String fileName) {
ClassLoader classLoader = Utils.class.getClassLoader();
return new File(Objects.requireNonNull(classLoader.getResource(fileName)).getFile());
}
} }

View File

@ -154,7 +154,7 @@ class OllamaAPIIntegrationTest {
@Test @Test
@Order(6) @Order(6)
void testAskModelWithStructuredOutput() void testGenerateWithStructuredOutput()
throws OllamaBaseException, IOException, InterruptedException, URISyntaxException { throws OllamaBaseException, IOException, InterruptedException, URISyntaxException {
api.pullModel(GENERAL_PURPOSE_MODEL); api.pullModel(GENERAL_PURPOSE_MODEL);
@ -184,7 +184,7 @@ class OllamaAPIIntegrationTest {
@Test @Test
@Order(6) @Order(6)
void testAskModelWithDefaultOptions() void testGennerateModelWithDefaultOptions()
throws OllamaBaseException, IOException, InterruptedException, URISyntaxException { throws OllamaBaseException, IOException, InterruptedException, URISyntaxException {
api.pullModel(GENERAL_PURPOSE_MODEL); api.pullModel(GENERAL_PURPOSE_MODEL);
boolean raw = false; boolean raw = false;
@ -199,7 +199,7 @@ class OllamaAPIIntegrationTest {
@Test @Test
@Order(7) @Order(7)
void testAskModelWithDefaultOptionsStreamed() void testGenerateWithDefaultOptionsStreamed()
throws OllamaBaseException, IOException, URISyntaxException, InterruptedException { throws OllamaBaseException, IOException, URISyntaxException, InterruptedException {
api.pullModel(GENERAL_PURPOSE_MODEL); api.pullModel(GENERAL_PURPOSE_MODEL);
boolean raw = false; boolean raw = false;
@ -222,7 +222,7 @@ class OllamaAPIIntegrationTest {
@Test @Test
@Order(8) @Order(8)
void testAskModelWithOptions() throws OllamaBaseException, IOException, URISyntaxException, void testGenerateWithOptions() throws OllamaBaseException, IOException, URISyntaxException,
InterruptedException, ToolInvocationException { InterruptedException, ToolInvocationException {
api.pullModel(GENERAL_PURPOSE_MODEL); api.pullModel(GENERAL_PURPOSE_MODEL);
@ -305,46 +305,6 @@ class OllamaAPIIntegrationTest {
.contains("6"), "Response should contain '6'"); .contains("6"), "Response should contain '6'");
} }
@Test
@Order(10)
void testChatWithImageFromURL() throws OllamaBaseException, IOException, InterruptedException,
URISyntaxException, ToolInvocationException {
api.pullModel(VISION_MODEL);
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(VISION_MODEL);
OllamaChatRequest requestModel = builder.withMessage(OllamaChatMessageRole.USER,
"What's in the picture?", Collections.emptyList(),
"https://t3.ftcdn.net/jpg/02/96/63/80/360_F_296638053_0gUVA4WVBKceGsIr7LNqRWSnkusi07dq.jpg")
.build();
api.registerAnnotatedTools(new OllamaAPIIntegrationTest());
OllamaChatResult chatResult = api.chat(requestModel);
assertNotNull(chatResult);
}
@Test
@Order(10)
void testChatWithImageFromFileWithHistoryRecognition() throws OllamaBaseException, IOException,
URISyntaxException, InterruptedException, ToolInvocationException {
api.pullModel(VISION_MODEL);
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(VISION_MODEL);
OllamaChatRequest requestModel = builder.withMessage(OllamaChatMessageRole.USER,
"What's in the picture?", Collections.emptyList(),
List.of(getImageFileFromClasspath("emoji-smile.jpeg"))).build();
OllamaChatResult chatResult = api.chat(requestModel);
assertNotNull(chatResult);
assertNotNull(chatResult.getResponseModel());
builder.reset();
requestModel = builder.withMessages(chatResult.getChatHistory())
.withMessage(OllamaChatMessageRole.USER, "What's the color?").build();
chatResult = api.chat(requestModel);
assertNotNull(chatResult);
assertNotNull(chatResult.getResponseModel());
}
@Test @Test
@Order(11) @Order(11)
void testChatWithExplicitToolDefinition() throws OllamaBaseException, IOException, URISyntaxException, void testChatWithExplicitToolDefinition() throws OllamaBaseException, IOException, URISyntaxException,
@ -617,9 +577,49 @@ class OllamaAPIIntegrationTest {
+ chatResult.getResponseModel().getMessage().getContent()); + chatResult.getResponseModel().getMessage().getContent());
} }
@Test
@Order(10)
void testChatWithImageFromURL() throws OllamaBaseException, IOException, InterruptedException,
URISyntaxException, ToolInvocationException {
api.pullModel(VISION_MODEL);
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(VISION_MODEL);
OllamaChatRequest requestModel = builder.withMessage(OllamaChatMessageRole.USER,
"What's in the picture?", Collections.emptyList(),
"https://t3.ftcdn.net/jpg/02/96/63/80/360_F_296638053_0gUVA4WVBKceGsIr7LNqRWSnkusi07dq.jpg")
.build();
api.registerAnnotatedTools(new OllamaAPIIntegrationTest());
OllamaChatResult chatResult = api.chat(requestModel);
assertNotNull(chatResult);
}
@Test
@Order(10)
void testChatWithImageFromFileWithHistoryRecognition() throws OllamaBaseException, IOException,
URISyntaxException, InterruptedException, ToolInvocationException {
api.pullModel(VISION_MODEL);
OllamaChatRequestBuilder builder = OllamaChatRequestBuilder.getInstance(VISION_MODEL);
OllamaChatRequest requestModel = builder.withMessage(OllamaChatMessageRole.USER,
"What's in the picture?", Collections.emptyList(),
List.of(getImageFileFromClasspath("emoji-smile.jpeg"))).build();
OllamaChatResult chatResult = api.chat(requestModel);
assertNotNull(chatResult);
assertNotNull(chatResult.getResponseModel());
builder.reset();
requestModel = builder.withMessages(chatResult.getChatHistory())
.withMessage(OllamaChatMessageRole.USER, "What's the color?").build();
chatResult = api.chat(requestModel);
assertNotNull(chatResult);
assertNotNull(chatResult.getResponseModel());
}
@Test @Test
@Order(17) @Order(17)
void testAskModelWithOptionsAndImageURLs() void testGenerateWithOptionsAndImageURLs()
throws OllamaBaseException, IOException, URISyntaxException, InterruptedException { throws OllamaBaseException, IOException, URISyntaxException, InterruptedException {
api.pullModel(VISION_MODEL); api.pullModel(VISION_MODEL);
@ -633,7 +633,7 @@ class OllamaAPIIntegrationTest {
@Test @Test
@Order(18) @Order(18)
void testAskModelWithOptionsAndImageFiles() void testGenerateWithOptionsAndImageFiles()
throws OllamaBaseException, IOException, URISyntaxException, InterruptedException { throws OllamaBaseException, IOException, URISyntaxException, InterruptedException {
api.pullModel(VISION_MODEL); api.pullModel(VISION_MODEL);
File imageFile = getImageFileFromClasspath("roses.jpg"); File imageFile = getImageFileFromClasspath("roses.jpg");
@ -650,7 +650,7 @@ class OllamaAPIIntegrationTest {
@Test @Test
@Order(20) @Order(20)
void testAskModelWithOptionsAndImageFilesStreamed() void testGenerateWithOptionsAndImageFilesStreamed()
throws OllamaBaseException, IOException, URISyntaxException, InterruptedException { throws OllamaBaseException, IOException, URISyntaxException, InterruptedException {
api.pullModel(VISION_MODEL); api.pullModel(VISION_MODEL);