mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-05-15 03:47:13 +02:00
added listModelsFromLibrary
API
This commit is contained in:
parent
5f19eb17ac
commit
9c3fc49df1
1355
docs/docs/apis-model-management/list-library-models.md
Normal file
1355
docs/docs/apis-model-management/list-library-models.md
Normal file
File diff suppressed because it is too large
Load Diff
5
pom.xml
5
pom.xml
@ -140,6 +140,11 @@
|
|||||||
<version>${lombok.version}</version>
|
<version>${lombok.version}</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jsoup</groupId>
|
||||||
|
<artifactId>jsoup</artifactId>
|
||||||
|
<version>1.18.1</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.fasterxml.jackson.core</groupId>
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
<artifactId>jackson-databind</artifactId>
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
@ -18,8 +18,6 @@ import io.github.ollama4j.tools.*;
|
|||||||
import io.github.ollama4j.utils.Options;
|
import io.github.ollama4j.utils.Options;
|
||||||
import io.github.ollama4j.utils.Utils;
|
import io.github.ollama4j.utils.Utils;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
@ -32,6 +30,14 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.jsoup.Jsoup;
|
||||||
|
import org.jsoup.nodes.Document;
|
||||||
|
import org.jsoup.nodes.Element;
|
||||||
|
import org.jsoup.select.Elements;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base Ollama API class.
|
* The base Ollama API class.
|
||||||
@ -164,15 +170,43 @@ public class OllamaAPI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> listDownloadableModels() throws OllamaBaseException, IOException, InterruptedException, URISyntaxException {
|
/**
|
||||||
|
* Retrieves a list of models from the Ollama library. This method fetches the available models directly from Ollama
|
||||||
|
* library page, including model details such as the name, pull count, popular tags, tag count, and the time when model was updated.
|
||||||
|
*
|
||||||
|
* @return A list of {@link LibraryModel} objects representing the models available in the Ollama library.
|
||||||
|
* @throws OllamaBaseException If the HTTP request fails or the response is not successful (non-200 status code).
|
||||||
|
* @throws IOException If an I/O error occurs during the HTTP request or response processing.
|
||||||
|
* @throws InterruptedException If the thread executing the request is interrupted.
|
||||||
|
* @throws URISyntaxException If there is an error creating the URI for the HTTP request.
|
||||||
|
*/
|
||||||
|
public List<LibraryModel> listModelsFromLibrary() throws OllamaBaseException, IOException, InterruptedException, URISyntaxException {
|
||||||
String url = "https://ollama.com/library";
|
String url = "https://ollama.com/library";
|
||||||
HttpClient httpClient = HttpClient.newHttpClient();
|
HttpClient httpClient = HttpClient.newHttpClient();
|
||||||
HttpRequest httpRequest = getRequestBuilderDefault(new URI(url)).header("Accept", "application/json").header("Content-type", "application/json").GET().build();
|
HttpRequest httpRequest = getRequestBuilderDefault(new URI(url)).header("Accept", "application/json").header("Content-type", "application/json").GET().build();
|
||||||
HttpResponse<String> response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
|
||||||
int statusCode = response.statusCode();
|
int statusCode = response.statusCode();
|
||||||
String responseString = response.body();
|
String responseString = response.body();
|
||||||
|
List<LibraryModel> models = new ArrayList<>();
|
||||||
if (statusCode == 200) {
|
if (statusCode == 200) {
|
||||||
return null;
|
Document doc = Jsoup.parse(responseString);
|
||||||
|
Elements modelSections = doc.selectXpath("//*[@id='repo']/ul/li/a");
|
||||||
|
for (Element e : modelSections) {
|
||||||
|
LibraryModel model = new LibraryModel();
|
||||||
|
Elements names = e.select("div > h2 > span");
|
||||||
|
Elements pullCounts = e.select("div:nth-of-type(2) > p > span:first-of-type > span:first-of-type");
|
||||||
|
Elements popularTags = e.select("div > div > span");
|
||||||
|
Elements tagCount = e.select("div:nth-of-type(2) > p > span:nth-of-type(2) > span:first-of-type");
|
||||||
|
Elements updatedAt = e.select("div:nth-of-type(2) > p > span:nth-of-type(3) > span:nth-of-type(2)");
|
||||||
|
|
||||||
|
model.setName(names.first().text());
|
||||||
|
model.setPullCount(pullCounts.first().text());
|
||||||
|
model.setPopularTags(popularTags.stream().map(Element::text).collect(Collectors.toList()));
|
||||||
|
model.setNumTags(Integer.parseInt(tagCount.first().text()));
|
||||||
|
model.setUpdatedAt(updatedAt.first().text());
|
||||||
|
models.add(model);
|
||||||
|
}
|
||||||
|
return models;
|
||||||
} else {
|
} else {
|
||||||
throw new OllamaBaseException(statusCode + " - " + responseString);
|
throw new OllamaBaseException(statusCode + " - " + responseString);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
package io.github.ollama4j.models.response;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class LibraryModel {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private String pullCount;
|
||||||
|
private int numTags;
|
||||||
|
private List<String> popularTags = new ArrayList<>();
|
||||||
|
private String updatedAt;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user