mirror of
				https://github.com/amithkoujalgi/ollama4j.git
				synced 2025-11-04 02:20:50 +01:00 
			
		
		
		
	Added ps() API
				
					
				
			Signed-off-by: Amith Koujalgi <koujalgi.amith@gmail.com>
This commit is contained in:
		@@ -1,5 +1,6 @@
 | 
			
		||||
package io.github.ollama4j;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.core.JsonProcessingException;
 | 
			
		||||
import io.github.ollama4j.exceptions.OllamaBaseException;
 | 
			
		||||
import io.github.ollama4j.exceptions.ToolInvocationException;
 | 
			
		||||
import io.github.ollama4j.exceptions.ToolNotFoundException;
 | 
			
		||||
@@ -12,6 +13,7 @@ import io.github.ollama4j.models.embeddings.OllamaEmbeddingResponseModel;
 | 
			
		||||
import io.github.ollama4j.models.embeddings.OllamaEmbeddingsRequestModel;
 | 
			
		||||
import io.github.ollama4j.models.generate.OllamaGenerateRequestModel;
 | 
			
		||||
import io.github.ollama4j.models.generate.OllamaStreamHandler;
 | 
			
		||||
import io.github.ollama4j.models.ps.ModelsProcessResponse;
 | 
			
		||||
import io.github.ollama4j.models.request.*;
 | 
			
		||||
import io.github.ollama4j.tools.*;
 | 
			
		||||
import io.github.ollama4j.utils.Options;
 | 
			
		||||
@@ -110,6 +112,37 @@ public class OllamaAPI {
 | 
			
		||||
        return statusCode == 200;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * List models that are currently loaded into memory.
 | 
			
		||||
     *
 | 
			
		||||
     * @return ModelsProcessResponse
 | 
			
		||||
     */
 | 
			
		||||
    public ModelsProcessResponse ps() throws IOException, InterruptedException, OllamaBaseException {
 | 
			
		||||
        String url = this.host + "/api/ps";
 | 
			
		||||
        HttpClient httpClient = HttpClient.newHttpClient();
 | 
			
		||||
        HttpRequest httpRequest = null;
 | 
			
		||||
        try {
 | 
			
		||||
            httpRequest =
 | 
			
		||||
                    getRequestBuilderDefault(new URI(url))
 | 
			
		||||
                            .header("Accept", "application/json")
 | 
			
		||||
                            .header("Content-type", "application/json")
 | 
			
		||||
                            .GET()
 | 
			
		||||
                            .build();
 | 
			
		||||
        } catch (URISyntaxException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        }
 | 
			
		||||
        HttpResponse<String> response = null;
 | 
			
		||||
        response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
 | 
			
		||||
        int statusCode = response.statusCode();
 | 
			
		||||
        String responseString = response.body();
 | 
			
		||||
        if (statusCode == 200) {
 | 
			
		||||
            return Utils.getObjectMapper()
 | 
			
		||||
                    .readValue(responseString, ModelsProcessResponse.class);
 | 
			
		||||
        } else {
 | 
			
		||||
            throw new OllamaBaseException(statusCode + " - " + responseString);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * List available models from Ollama server.
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,35 @@
 | 
			
		||||
package io.github.ollama4j.models.ps;
 | 
			
		||||
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import lombok.NoArgsConstructor;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
@Data
 | 
			
		||||
@NoArgsConstructor
 | 
			
		||||
public class ModelsProcessResponse {
 | 
			
		||||
    private List<ModelProcess> models;
 | 
			
		||||
 | 
			
		||||
    @Data
 | 
			
		||||
    @NoArgsConstructor
 | 
			
		||||
    public static class ModelProcess {
 | 
			
		||||
        private String name;
 | 
			
		||||
        private String model;
 | 
			
		||||
        private long size;
 | 
			
		||||
        private String digest;
 | 
			
		||||
        private ModelDetails details;
 | 
			
		||||
        private String expiresAt;
 | 
			
		||||
        private long sizeVram;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Data
 | 
			
		||||
    @NoArgsConstructor
 | 
			
		||||
    public static class ModelDetails {
 | 
			
		||||
        private String parentModel;
 | 
			
		||||
        private String format;
 | 
			
		||||
        private String family;
 | 
			
		||||
        private List<String> families;
 | 
			
		||||
        private String parameterSize;
 | 
			
		||||
        private String quantizationLevel;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user