refactor: clean up and deprecate unused methods in OllamaAPI and related classes

- Removed deprecated methods and unused imports from `OllamaAPI`.
- Updated method signatures to improve clarity and consistency.
- Refactored embedding request handling to utilize `OllamaEmbedRequestModel`.
- Adjusted integration tests to reflect changes in method usage and removed obsolete tests.
- Enhanced code readability by standardizing formatting and comments across various classes.
This commit is contained in:
Amith Koujalgi
2025-09-16 00:27:11 +05:30
parent 44c6236243
commit 656802b343
31 changed files with 558 additions and 959 deletions

View File

@@ -4,8 +4,8 @@ import io.github.ollama4j.OllamaAPI;
import io.github.ollama4j.exceptions.OllamaBaseException;
import io.github.ollama4j.exceptions.ToolInvocationException;
import io.github.ollama4j.models.chat.*;
import io.github.ollama4j.models.embeddings.OllamaEmbedRequestModel;
import io.github.ollama4j.models.embeddings.OllamaEmbedResponseModel;
import io.github.ollama4j.models.response.LibraryModel;
import io.github.ollama4j.models.response.Model;
import io.github.ollama4j.models.response.ModelDetail;
import io.github.ollama4j.models.response.OllamaResult;
@@ -113,15 +113,6 @@ class OllamaAPIIntegrationTest {
assertTrue(models.size() >= 0, "Models list should not be empty");
}
@Test
@Order(2)
void testListModelsFromLibrary()
throws OllamaBaseException, IOException, URISyntaxException, InterruptedException {
List<LibraryModel> models = api.listModelsFromLibrary();
assertNotNull(models);
assertFalse(models.isEmpty());
}
@Test
@Order(3)
void testPullModelAPI() throws URISyntaxException, IOException, OllamaBaseException, InterruptedException {
@@ -144,8 +135,10 @@ class OllamaAPIIntegrationTest {
@Order(5)
void testEmbeddings() throws Exception {
api.pullModel(EMBEDDING_MODEL);
OllamaEmbedResponseModel embeddings = api.embed(EMBEDDING_MODEL,
Arrays.asList("Why is the sky blue?", "Why is the grass green?"));
OllamaEmbedRequestModel m = new OllamaEmbedRequestModel();
m.setModel(EMBEDDING_MODEL);
m.setInput(Arrays.asList("Why is the sky blue?", "Why is the grass green?"));
OllamaEmbedResponseModel embeddings = api.embed(m);
assertNotNull(embeddings, "Embeddings should not be null");
assertFalse(embeddings.getEmbeddings().isEmpty(), "Embeddings should not be empty");
}
@@ -228,7 +221,7 @@ class OllamaAPIIntegrationTest {
requestModel = builder.withMessages(requestModel.getMessages())
.withMessage(OllamaChatMessageRole.USER, "Give me a cool name")
.withOptions(new OptionsBuilder().setTemperature(0.5f).build()).build();
OllamaChatResult chatResult = api.chat(requestModel);
OllamaChatResult chatResult = api.chat(requestModel, null);
assertNotNull(chatResult);
assertNotNull(chatResult.getResponseModel());
@@ -249,7 +242,7 @@ class OllamaAPIIntegrationTest {
expectedResponse)).withMessage(OllamaChatMessageRole.USER, "Who are you?")
.withOptions(new OptionsBuilder().setTemperature(0.0f).build()).build();
OllamaChatResult chatResult = api.chat(requestModel);
OllamaChatResult chatResult = api.chat(requestModel, null);
assertNotNull(chatResult);
assertNotNull(chatResult.getResponseModel());
assertNotNull(chatResult.getResponseModel().getMessage());
@@ -270,7 +263,7 @@ class OllamaAPIIntegrationTest {
.build();
// Start conversation with model
OllamaChatResult chatResult = api.chat(requestModel);
OllamaChatResult chatResult = api.chat(requestModel, null);
assertTrue(chatResult.getChatHistory().stream().anyMatch(chat -> chat.getContent().contains("2")),
"Expected chat history to contain '2'");
@@ -279,7 +272,7 @@ class OllamaAPIIntegrationTest {
.withMessage(OllamaChatMessageRole.USER, "And what is its squared value?").build();
// Continue conversation with model
chatResult = api.chat(requestModel);
chatResult = api.chat(requestModel, null);
assertTrue(chatResult.getChatHistory().stream().anyMatch(chat -> chat.getContent().contains("4")),
"Expected chat history to contain '4'");
@@ -289,7 +282,7 @@ class OllamaAPIIntegrationTest {
"What is the largest value between 2, 4 and 6?").build();
// Continue conversation with the model for the third question
chatResult = api.chat(requestModel);
chatResult = api.chat(requestModel, null);
// verify the result
assertNotNull(chatResult, "Chat result should not be null");
@@ -315,7 +308,7 @@ class OllamaAPIIntegrationTest {
"Give me the ID and address of the employee Rahul Kumar.").build();
requestModel.setOptions(new OptionsBuilder().setTemperature(0.9f).build().getOptionsMap());
OllamaChatResult chatResult = api.chat(requestModel);
OllamaChatResult chatResult = api.chat(requestModel, null);
assertNotNull(chatResult, "chatResult should not be null");
assertNotNull(chatResult.getResponseModel(), "Response model should not be null");
@@ -357,7 +350,7 @@ class OllamaAPIIntegrationTest {
.build();
requestModel.setOptions(new OptionsBuilder().setTemperature(0.9f).build().getOptionsMap());
OllamaChatResult chatResult = api.chat(requestModel);
OllamaChatResult chatResult = api.chat(requestModel, null);
assertNotNull(chatResult, "chatResult should not be null");
assertNotNull(chatResult.getResponseModel(), "Response model should not be null");
@@ -405,11 +398,11 @@ class OllamaAPIIntegrationTest {
.withKeepAlive("0m").withOptions(new OptionsBuilder().setTemperature(0.9f).build())
.build();
OllamaChatResult chatResult = api.chat(requestModel, (s) -> {
OllamaChatResult chatResult = api.chat(requestModel, new OllamaChatStreamObserver((s) -> {
LOG.info(s.toUpperCase());
}, (s) -> {
LOG.info(s.toLowerCase());
});
}));
assertNotNull(chatResult, "chatResult should not be null");
assertNotNull(chatResult.getResponseModel(), "Response model should not be null");
@@ -447,7 +440,7 @@ class OllamaAPIIntegrationTest {
"Compute the most important constant in the world using 5 digits")
.build();
OllamaChatResult chatResult = api.chat(requestModel);
OllamaChatResult chatResult = api.chat(requestModel, null);
assertNotNull(chatResult);
assertNotNull(chatResult.getResponseModel());
assertNotNull(chatResult.getResponseModel().getMessage());
@@ -480,7 +473,7 @@ class OllamaAPIIntegrationTest {
"Greet Rahul with a lot of hearts and respond to me with count of emojis that have been in used in the greeting")
.build();
OllamaChatResult chatResult = api.chat(requestModel);
OllamaChatResult chatResult = api.chat(requestModel, null);
assertNotNull(chatResult);
assertNotNull(chatResult.getResponseModel());
assertNotNull(chatResult.getResponseModel().getMessage());
@@ -515,13 +508,11 @@ class OllamaAPIIntegrationTest {
requestModel.setThink(false);
StringBuffer sb = new StringBuffer();
OllamaChatResult chatResult = api.chat(requestModel, (s) -> {
OllamaChatResult chatResult = api.chat(requestModel, new OllamaChatStreamObserver((s) -> {
LOG.info(s.toUpperCase());
sb.append(s);
}, (s) -> {
LOG.info(s.toLowerCase());
sb.append(s);
});
}));
assertNotNull(chatResult);
assertNotNull(chatResult.getResponseModel());
assertNotNull(chatResult.getResponseModel().getMessage());
@@ -540,13 +531,11 @@ class OllamaAPIIntegrationTest {
.withThinking(true).withKeepAlive("0m").build();
StringBuffer sb = new StringBuffer();
OllamaChatResult chatResult = api.chat(requestModel, (s) -> {
sb.append(s);
OllamaChatResult chatResult = api.chat(requestModel, new OllamaChatStreamObserver((s) -> {
LOG.info(s.toUpperCase());
}, (s) -> {
sb.append(s);
LOG.info(s.toLowerCase());
});
}));
assertNotNull(chatResult);
assertNotNull(chatResult.getResponseModel());
@@ -569,7 +558,7 @@ class OllamaAPIIntegrationTest {
.build();
api.registerAnnotatedTools(new OllamaAPIIntegrationTest());
OllamaChatResult chatResult = api.chat(requestModel);
OllamaChatResult chatResult = api.chat(requestModel, null);
assertNotNull(chatResult);
}
@@ -583,7 +572,7 @@ class OllamaAPIIntegrationTest {
"What's in the picture?", Collections.emptyList(),
List.of(getImageFileFromClasspath("emoji-smile.jpeg"))).build();
OllamaChatResult chatResult = api.chat(requestModel);
OllamaChatResult chatResult = api.chat(requestModel, null);
assertNotNull(chatResult);
assertNotNull(chatResult.getResponseModel());
builder.reset();
@@ -591,7 +580,7 @@ class OllamaAPIIntegrationTest {
requestModel = builder.withMessages(chatResult.getChatHistory())
.withMessage(OllamaChatMessageRole.USER, "What's the color?").build();
chatResult = api.chat(requestModel);
chatResult = api.chat(requestModel, null);
assertNotNull(chatResult);
assertNotNull(chatResult.getResponseModel());
}

View File

@@ -4,7 +4,8 @@ import io.github.ollama4j.models.request.BasicAuth;
import io.github.ollama4j.models.request.BearerAuth;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class TestAuth {

View File

@@ -96,9 +96,12 @@ class TestMockedAPIs {
String model = OllamaModelType.LLAMA2;
String prompt = "some prompt text";
try {
when(ollamaAPI.generateEmbeddings(model, prompt)).thenReturn(new ArrayList<>());
ollamaAPI.generateEmbeddings(model, prompt);
verify(ollamaAPI, times(1)).generateEmbeddings(model, prompt);
OllamaEmbedRequestModel m = new OllamaEmbedRequestModel();
m.setModel(model);
m.setInput(List.of(prompt));
when(ollamaAPI.embed(m)).thenReturn(new OllamaEmbedResponseModel());
ollamaAPI.embed(m);
verify(ollamaAPI, times(1)).embed(m);
} catch (IOException | OllamaBaseException | InterruptedException e) {
throw new RuntimeException(e);
}
@@ -110,9 +113,10 @@ class TestMockedAPIs {
String model = OllamaModelType.LLAMA2;
List<String> inputs = List.of("some prompt text");
try {
when(ollamaAPI.embed(model, inputs)).thenReturn(new OllamaEmbedResponseModel());
ollamaAPI.embed(model, inputs);
verify(ollamaAPI, times(1)).embed(model, inputs);
OllamaEmbedRequestModel m = new OllamaEmbedRequestModel(model, inputs);
when(ollamaAPI.embed(m)).thenReturn(new OllamaEmbedResponseModel());
ollamaAPI.embed(m);
verify(ollamaAPI, times(1)).embed(m);
} catch (IOException | OllamaBaseException | InterruptedException e) {
throw new RuntimeException(e);
}

View File

@@ -5,7 +5,8 @@ import io.github.ollama4j.models.chat.OllamaChatMessageRole;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class TestOllamaChatMessage {

View File

@@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
class TestToolsPromptBuilder {