mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-10-14 01:18:58 +02:00
Refactor model response classes and update API methods
- Renamed `ModelProcessesResponse` to `ModelProcessesResult` and updated all related references in the codebase. - Introduced `OllamaEmbedResult` class to replace `OllamaEmbedResponse`, ensuring consistency across the API. - Updated method signatures in `OllamaAPI` to reflect the new class names and adjusted integration tests accordingly.
This commit is contained in:
parent
dd1022a990
commit
61fe8b2b56
@ -12,14 +12,14 @@ This API corresponds to the [PS](https://github.com/ollama/ollama/blob/main/docs
|
|||||||
package io.github.ollama4j.localtests;
|
package io.github.ollama4j.localtests;
|
||||||
|
|
||||||
import io.github.ollama4j.OllamaAPI;
|
import io.github.ollama4j.OllamaAPI;
|
||||||
import io.github.ollama4j.models.ps.ModelProcessesResponse;
|
import io.github.ollama4j.models.ps.ModelProcessesResult;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
OllamaAPI ollamaAPI = new OllamaAPI("http://localhost:11434");
|
OllamaAPI ollamaAPI = new OllamaAPI("http://localhost:11434");
|
||||||
|
|
||||||
ModelProcessesResponse response = ollamaAPI.ps();
|
ModelProcessesResult response = ollamaAPI.ps();
|
||||||
|
|
||||||
System.out.println(response);
|
System.out.println(response);
|
||||||
}
|
}
|
||||||
|
@ -16,11 +16,11 @@ import io.github.ollama4j.metrics.MetricsRecorder;
|
|||||||
import io.github.ollama4j.models.chat.*;
|
import io.github.ollama4j.models.chat.*;
|
||||||
import io.github.ollama4j.models.chat.OllamaChatTokenHandler;
|
import io.github.ollama4j.models.chat.OllamaChatTokenHandler;
|
||||||
import io.github.ollama4j.models.embed.OllamaEmbedRequest;
|
import io.github.ollama4j.models.embed.OllamaEmbedRequest;
|
||||||
import io.github.ollama4j.models.embed.OllamaEmbedResponse;
|
import io.github.ollama4j.models.embed.OllamaEmbedResult;
|
||||||
import io.github.ollama4j.models.generate.OllamaGenerateRequest;
|
import io.github.ollama4j.models.generate.OllamaGenerateRequest;
|
||||||
import io.github.ollama4j.models.generate.OllamaGenerateStreamObserver;
|
import io.github.ollama4j.models.generate.OllamaGenerateStreamObserver;
|
||||||
import io.github.ollama4j.models.generate.OllamaGenerateTokenHandler;
|
import io.github.ollama4j.models.generate.OllamaGenerateTokenHandler;
|
||||||
import io.github.ollama4j.models.ps.ModelProcessesResponse;
|
import io.github.ollama4j.models.ps.ModelProcessesResult;
|
||||||
import io.github.ollama4j.models.request.*;
|
import io.github.ollama4j.models.request.*;
|
||||||
import io.github.ollama4j.models.response.*;
|
import io.github.ollama4j.models.response.*;
|
||||||
import io.github.ollama4j.tools.*;
|
import io.github.ollama4j.tools.*;
|
||||||
@ -186,10 +186,10 @@ public class OllamaAPI {
|
|||||||
/**
|
/**
|
||||||
* Provides a list of running models and details about each model currently loaded into memory.
|
* Provides a list of running models and details about each model currently loaded into memory.
|
||||||
*
|
*
|
||||||
* @return ModelsProcessResponse containing details about the running models
|
* @return ModelsProcessResult containing details about the running models
|
||||||
* @throws OllamaException if the response indicates an error status
|
* @throws OllamaException if the response indicates an error status
|
||||||
*/
|
*/
|
||||||
public ModelProcessesResponse ps() throws OllamaException {
|
public ModelProcessesResult ps() throws OllamaException {
|
||||||
long startTime = System.currentTimeMillis();
|
long startTime = System.currentTimeMillis();
|
||||||
String url = "/api/ps";
|
String url = "/api/ps";
|
||||||
int statusCode = -1;
|
int statusCode = -1;
|
||||||
@ -217,7 +217,7 @@ public class OllamaAPI {
|
|||||||
String responseString = response.body();
|
String responseString = response.body();
|
||||||
if (statusCode == 200) {
|
if (statusCode == 200) {
|
||||||
return Utils.getObjectMapper()
|
return Utils.getObjectMapper()
|
||||||
.readValue(responseString, ModelProcessesResponse.class);
|
.readValue(responseString, ModelProcessesResult.class);
|
||||||
} else {
|
} else {
|
||||||
throw new OllamaException(statusCode + " - " + responseString);
|
throw new OllamaException(statusCode + " - " + responseString);
|
||||||
}
|
}
|
||||||
@ -719,7 +719,7 @@ public class OllamaAPI {
|
|||||||
* @return embeddings
|
* @return embeddings
|
||||||
* @throws OllamaException if the response indicates an error status
|
* @throws OllamaException if the response indicates an error status
|
||||||
*/
|
*/
|
||||||
public OllamaEmbedResponse embed(OllamaEmbedRequest modelRequest) throws OllamaException {
|
public OllamaEmbedResult embed(OllamaEmbedRequest modelRequest) throws OllamaException {
|
||||||
long startTime = System.currentTimeMillis();
|
long startTime = System.currentTimeMillis();
|
||||||
String url = "/api/embed";
|
String url = "/api/embed";
|
||||||
int statusCode = -1;
|
int statusCode = -1;
|
||||||
@ -739,7 +739,7 @@ public class OllamaAPI {
|
|||||||
statusCode = response.statusCode();
|
statusCode = response.statusCode();
|
||||||
String responseBody = response.body();
|
String responseBody = response.body();
|
||||||
if (statusCode == 200) {
|
if (statusCode == 200) {
|
||||||
return Utils.getObjectMapper().readValue(responseBody, OllamaEmbedResponse.class);
|
return Utils.getObjectMapper().readValue(responseBody, OllamaEmbedResult.class);
|
||||||
} else {
|
} else {
|
||||||
throw new OllamaException(statusCode + " - " + responseBody);
|
throw new OllamaException(statusCode + " - " + responseBody);
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ import lombok.Data;
|
|||||||
|
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
@Data
|
@Data
|
||||||
public class OllamaEmbedResponse {
|
public class OllamaEmbedResult {
|
||||||
@JsonProperty("model")
|
@JsonProperty("model")
|
||||||
private String model;
|
private String model;
|
||||||
|
|
@ -17,7 +17,7 @@ import lombok.NoArgsConstructor;
|
|||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public class ModelProcessesResponse {
|
public class ModelProcessesResult {
|
||||||
@JsonProperty("models")
|
@JsonProperty("models")
|
||||||
private List<ModelProcess> models;
|
private List<ModelProcess> models;
|
||||||
|
|
@ -16,7 +16,7 @@ import io.github.ollama4j.impl.ConsoleOutputChatTokenHandler;
|
|||||||
import io.github.ollama4j.impl.ConsoleOutputGenerateTokenHandler;
|
import io.github.ollama4j.impl.ConsoleOutputGenerateTokenHandler;
|
||||||
import io.github.ollama4j.models.chat.*;
|
import io.github.ollama4j.models.chat.*;
|
||||||
import io.github.ollama4j.models.embed.OllamaEmbedRequest;
|
import io.github.ollama4j.models.embed.OllamaEmbedRequest;
|
||||||
import io.github.ollama4j.models.embed.OllamaEmbedResponse;
|
import io.github.ollama4j.models.embed.OllamaEmbedResult;
|
||||||
import io.github.ollama4j.models.generate.OllamaGenerateRequest;
|
import io.github.ollama4j.models.generate.OllamaGenerateRequest;
|
||||||
import io.github.ollama4j.models.generate.OllamaGenerateRequestBuilder;
|
import io.github.ollama4j.models.generate.OllamaGenerateRequestBuilder;
|
||||||
import io.github.ollama4j.models.generate.OllamaGenerateStreamObserver;
|
import io.github.ollama4j.models.generate.OllamaGenerateStreamObserver;
|
||||||
@ -234,7 +234,7 @@ class OllamaAPIIntegrationTest {
|
|||||||
OllamaEmbedRequest m = new OllamaEmbedRequest();
|
OllamaEmbedRequest m = new OllamaEmbedRequest();
|
||||||
m.setModel(EMBEDDING_MODEL);
|
m.setModel(EMBEDDING_MODEL);
|
||||||
m.setInput(Arrays.asList("Why is the sky blue?", "Why is the grass green?"));
|
m.setInput(Arrays.asList("Why is the sky blue?", "Why is the grass green?"));
|
||||||
OllamaEmbedResponse embeddings = api.embed(m);
|
OllamaEmbedResult embeddings = api.embed(m);
|
||||||
assertNotNull(embeddings, "Embeddings should not be null");
|
assertNotNull(embeddings, "Embeddings should not be null");
|
||||||
assertFalse(embeddings.getEmbeddings().isEmpty(), "Embeddings should not be empty");
|
assertFalse(embeddings.getEmbeddings().isEmpty(), "Embeddings should not be empty");
|
||||||
}
|
}
|
||||||
@ -1333,7 +1333,7 @@ class OllamaAPIIntegrationTest {
|
|||||||
requestModel.setInput(
|
requestModel.setInput(
|
||||||
Collections.singletonList("This is a single test sentence for embedding."));
|
Collections.singletonList("This is a single test sentence for embedding."));
|
||||||
|
|
||||||
OllamaEmbedResponse embeddings = api.embed(requestModel);
|
OllamaEmbedResult embeddings = api.embed(requestModel);
|
||||||
|
|
||||||
assertNotNull(embeddings);
|
assertNotNull(embeddings);
|
||||||
assertFalse(embeddings.getEmbeddings().isEmpty());
|
assertFalse(embeddings.getEmbeddings().isEmpty());
|
||||||
|
@ -17,7 +17,7 @@ import io.github.ollama4j.exceptions.OllamaException;
|
|||||||
import io.github.ollama4j.exceptions.RoleNotFoundException;
|
import io.github.ollama4j.exceptions.RoleNotFoundException;
|
||||||
import io.github.ollama4j.models.chat.OllamaChatMessageRole;
|
import io.github.ollama4j.models.chat.OllamaChatMessageRole;
|
||||||
import io.github.ollama4j.models.embed.OllamaEmbedRequest;
|
import io.github.ollama4j.models.embed.OllamaEmbedRequest;
|
||||||
import io.github.ollama4j.models.embed.OllamaEmbedResponse;
|
import io.github.ollama4j.models.embed.OllamaEmbedResult;
|
||||||
import io.github.ollama4j.models.generate.OllamaGenerateRequest;
|
import io.github.ollama4j.models.generate.OllamaGenerateRequest;
|
||||||
import io.github.ollama4j.models.generate.OllamaGenerateRequestBuilder;
|
import io.github.ollama4j.models.generate.OllamaGenerateRequestBuilder;
|
||||||
import io.github.ollama4j.models.generate.OllamaGenerateStreamObserver;
|
import io.github.ollama4j.models.generate.OllamaGenerateStreamObserver;
|
||||||
@ -112,7 +112,7 @@ class TestMockedAPIs {
|
|||||||
OllamaEmbedRequest m = new OllamaEmbedRequest();
|
OllamaEmbedRequest m = new OllamaEmbedRequest();
|
||||||
m.setModel(model);
|
m.setModel(model);
|
||||||
m.setInput(List.of(prompt));
|
m.setInput(List.of(prompt));
|
||||||
when(ollamaAPI.embed(m)).thenReturn(new OllamaEmbedResponse());
|
when(ollamaAPI.embed(m)).thenReturn(new OllamaEmbedResult());
|
||||||
ollamaAPI.embed(m);
|
ollamaAPI.embed(m);
|
||||||
verify(ollamaAPI, times(1)).embed(m);
|
verify(ollamaAPI, times(1)).embed(m);
|
||||||
} catch (OllamaException e) {
|
} catch (OllamaException e) {
|
||||||
@ -127,7 +127,7 @@ class TestMockedAPIs {
|
|||||||
List<String> inputs = List.of("some prompt text");
|
List<String> inputs = List.of("some prompt text");
|
||||||
try {
|
try {
|
||||||
OllamaEmbedRequest m = new OllamaEmbedRequest(model, inputs);
|
OllamaEmbedRequest m = new OllamaEmbedRequest(model, inputs);
|
||||||
when(ollamaAPI.embed(m)).thenReturn(new OllamaEmbedResponse());
|
when(ollamaAPI.embed(m)).thenReturn(new OllamaEmbedResult());
|
||||||
ollamaAPI.embed(m);
|
ollamaAPI.embed(m);
|
||||||
verify(ollamaAPI, times(1)).embed(m);
|
verify(ollamaAPI, times(1)).embed(m);
|
||||||
} catch (OllamaException e) {
|
} catch (OllamaException e) {
|
||||||
@ -142,7 +142,7 @@ class TestMockedAPIs {
|
|||||||
List<String> inputs = List.of("some prompt text");
|
List<String> inputs = List.of("some prompt text");
|
||||||
try {
|
try {
|
||||||
when(ollamaAPI.embed(new OllamaEmbedRequest(model, inputs)))
|
when(ollamaAPI.embed(new OllamaEmbedRequest(model, inputs)))
|
||||||
.thenReturn(new OllamaEmbedResponse());
|
.thenReturn(new OllamaEmbedResult());
|
||||||
ollamaAPI.embed(new OllamaEmbedRequest(model, inputs));
|
ollamaAPI.embed(new OllamaEmbedRequest(model, inputs));
|
||||||
verify(ollamaAPI, times(1)).embed(new OllamaEmbedRequest(model, inputs));
|
verify(ollamaAPI, times(1)).embed(new OllamaEmbedRequest(model, inputs));
|
||||||
} catch (OllamaException e) {
|
} catch (OllamaException e) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user