diff --git a/docs/docs/apis-model-management/create-model.md b/docs/docs/apis-model-management/create-model.md index 866cf73..a66d815 100644 --- a/docs/docs/apis-model-management/create-model.md +++ b/docs/docs/apis-model-management/create-model.md @@ -1,5 +1,5 @@ --- -sidebar_position: 4 +sidebar_position: 5 --- # Create Model diff --git a/docs/docs/apis-model-management/delete-model.md b/docs/docs/apis-model-management/delete-model.md index edb8197..9b1579e 100644 --- a/docs/docs/apis-model-management/delete-model.md +++ b/docs/docs/apis-model-management/delete-model.md @@ -1,5 +1,5 @@ --- -sidebar_position: 5 +sidebar_position: 6 --- # Delete Model diff --git a/docs/docs/apis-model-management/get-model-details.md b/docs/docs/apis-model-management/get-model-details.md index 0162e29..8605c3a 100644 --- a/docs/docs/apis-model-management/get-model-details.md +++ b/docs/docs/apis-model-management/get-model-details.md @@ -1,5 +1,5 @@ --- -sidebar_position: 3 +sidebar_position: 4 --- # Get Model Details diff --git a/docs/docs/apis-model-management/list-library-models.md b/docs/docs/apis-model-management/list-library-models.md index ec04efd..25aa594 100644 --- a/docs/docs/apis-model-management/list-library-models.md +++ b/docs/docs/apis-model-management/list-library-models.md @@ -1,11 +1,15 @@ --- -sidebar_position: 6 +sidebar_position: 1 --- -# List Models from Ollama Library +# Models from Ollama Library -This API retrieves a list of models from the Ollama library. It fetches available models directly from the Ollama -library page, including details such as the model's name, pull count, popular tags, tag count, and the last update time. +These API retrieves a list of models directly from the Ollama library. + +### List Models from Ollama Library + +This API fetches available models from the Ollama library page, including details such as the model's name, pull count, +popular tags, tag count, and the last update time. ```java title="ListLibraryModels.java" import io.github.ollama4j.OllamaAPI; @@ -28,7 +32,7 @@ public class Main { } ``` -The following is the sample response: +The following is the sample output: ``` [ @@ -37,7 +41,7 @@ The following is the sample response: ] ``` -# Get Tags of a Library Model +### Get Tags of a Library Model This API Fetches the tags associated with a specific model from Ollama library. @@ -63,6 +67,8 @@ public class Main { } ``` +The following is the sample output: + ``` LibraryModelDetail( model=LibraryModel(name=llama3.2-vision, description=Llama 3.2 Vision is a collection of instruction-tuned image reasoning generative models in 11B and 90B sizes., pullCount=21.1K, totalTags=9, popularTags=[vision, 11b, 90b], lastUpdated=yesterday), @@ -74,12 +80,12 @@ LibraryModelDetail( ) ``` -You can use this information to pull models into Ollama server. +### Find a model from Ollama library -```java title="PullLibraryModelTags.java" +This API finds a specific model using model `name` and `tag` from Ollama library. + +```java title="FindLibraryModel.java" import io.github.ollama4j.OllamaAPI; -import io.github.ollama4j.models.response.LibraryModel; -import io.github.ollama4j.models.response.LibraryModelDetail; import io.github.ollama4j.models.response.LibraryModelTag; public class Main { @@ -90,11 +96,36 @@ public class Main { OllamaAPI ollamaAPI = new OllamaAPI(host); - List libraryModels = ollamaAPI.listModelsFromLibrary(); + LibraryModelTag libraryModelTag = ollamaAPI.findModelTagFromLibrary("qwen2.5", "7b"); - LibraryModelDetail libraryModelDetail = ollamaAPI.getLibraryModelDetails(libraryModels.get(0)); + System.out.println(libraryModelTag); + } +} +``` - LibraryModelTag libraryModelTag = libraryModelDetail.getTags().get(0); +The following is the sample output: + +``` +LibraryModelTag(name=qwen2.5, tag=7b, size=4.7GB, lastUpdated=7 weeks ago) +``` + +### Pull model using `LibraryModelTag` + +You can use `LibraryModelTag` to pull models into Ollama server. + +```java title="PullLibraryModelTags.java" +import io.github.ollama4j.OllamaAPI; +import io.github.ollama4j.models.response.LibraryModelTag; + +public class Main { + + public static void main(String[] args) { + + String host = "http://localhost:11434/"; + + OllamaAPI ollamaAPI = new OllamaAPI(host); + + LibraryModelTag libraryModelTag = ollamaAPI.findModelTagFromLibrary("qwen2.5", "7b"); ollamaAPI.pullModel(libraryModelTag); } diff --git a/docs/docs/apis-model-management/list-models.md b/docs/docs/apis-model-management/list-models.md index e55fb62..8ed9a24 100644 --- a/docs/docs/apis-model-management/list-models.md +++ b/docs/docs/apis-model-management/list-models.md @@ -1,8 +1,8 @@ --- -sidebar_position: 1 +sidebar_position: 2 --- -# List Models +# List Local Models This API lets you list downloaded/available models on the Ollama server. diff --git a/docs/docs/apis-model-management/pull-model.md b/docs/docs/apis-model-management/pull-model.md index bfe2e26..69fd013 100644 --- a/docs/docs/apis-model-management/pull-model.md +++ b/docs/docs/apis-model-management/pull-model.md @@ -1,5 +1,5 @@ --- -sidebar_position: 2 +sidebar_position: 3 --- # Pull Model @@ -23,4 +23,12 @@ public class Main { } ``` -Once downloaded, you can see them when you use [list models](./list-models) API. \ No newline at end of file +Once downloaded, you can see them when you use [list models](./list-models) API. + +:::info + +You can even pull models using Ollama model library APIs. This looks up the models directly on the Ollama model library page. Refer +to [this](./list-library-models#pull-model-using-librarymodeltag). + +::: + diff --git a/src/main/java/io/github/ollama4j/OllamaAPI.java b/src/main/java/io/github/ollama4j/OllamaAPI.java index ea3e8b4..e504d7e 100644 --- a/src/main/java/io/github/ollama4j/OllamaAPI.java +++ b/src/main/java/io/github/ollama4j/OllamaAPI.java @@ -204,18 +204,11 @@ public class OllamaAPI { // if name cannot be extracted, skip. continue; } - Optional.ofNullable(names.first()) - .map(Element::text) - .ifPresent(model::setName); + Optional.ofNullable(names.first()).map(Element::text).ifPresent(model::setName); model.setDescription(Optional.ofNullable(desc.first()).map(Element::text).orElse("")); - model.setPopularTags(Optional.of(popularTags) - .map(tags -> tags.stream().map(Element::text).collect(Collectors.toList())) - .orElse(new ArrayList<>())); + model.setPopularTags(Optional.of(popularTags).map(tags -> tags.stream().map(Element::text).collect(Collectors.toList())).orElse(new ArrayList<>())); model.setPullCount(Optional.ofNullable(pullCounts.first()).map(Element::text).orElse("")); - model.setTotalTags(Optional.ofNullable(totalTags.first()) - .map(Element::text) - .map(Integer::parseInt) - .orElse(0)); + model.setTotalTags(Optional.ofNullable(totalTags.first()).map(Element::text).map(Integer::parseInt).orElse(0)); model.setLastUpdated(Optional.ofNullable(lastUpdatedTime.first()).map(Element::text).orElse("")); models.add(model); @@ -263,19 +256,9 @@ public class OllamaAPI { continue; } libraryModelTag.setName(libraryModel.getName()); - Optional.ofNullable(tags.first()) - .map(Element::text) - .ifPresent(libraryModelTag::setTag); - libraryModelTag.setSize(Optional.ofNullable(tagsMetas.first()) - .map(element -> element.text().split("•")) - .filter(parts -> parts.length > 1) - .map(parts -> parts[1].trim()) - .orElse("")); - libraryModelTag.setLastUpdated(Optional.ofNullable(tagsMetas.first()) - .map(element -> element.text().split("•")) - .filter(parts -> parts.length > 1) - .map(parts -> parts[2].trim()) - .orElse("")); + Optional.ofNullable(tags.first()).map(Element::text).ifPresent(libraryModelTag::setTag); + libraryModelTag.setSize(Optional.ofNullable(tagsMetas.first()).map(element -> element.text().split("•")).filter(parts -> parts.length > 1).map(parts -> parts[1].trim()).orElse("")); + libraryModelTag.setLastUpdated(Optional.ofNullable(tagsMetas.first()).map(element -> element.text().split("•")).filter(parts -> parts.length > 1).map(parts -> parts[2].trim()).orElse("")); libraryModelTags.add(libraryModelTag); } LibraryModelDetail libraryModelDetail = new LibraryModelDetail(); @@ -287,6 +270,30 @@ public class OllamaAPI { } } + /** + * Finds a specific model using model name and tag from Ollama library. + *

+ * This method retrieves the model from the Ollama library by its name, then fetches its tags. + * It searches through the tags of the model to find one that matches the specified tag name. + * If the model or the tag is not found, it throws a {@link NoSuchElementException}. + * + * @param modelName The name of the model to search for in the library. + * @param tag The tag name to search for within the specified model. + * @return The {@link LibraryModelTag} associated with the specified model and tag. + * @throws OllamaBaseException If there is a problem with the Ollama library operations. + * @throws IOException If an I/O error occurs during the operation. + * @throws URISyntaxException If there is an error with the URI syntax. + * @throws InterruptedException If the operation is interrupted. + * @throws NoSuchElementException If the model or the tag is not found. + */ + public LibraryModelTag findModelTagFromLibrary(String modelName, String tag) throws OllamaBaseException, IOException, URISyntaxException, InterruptedException { + List libraryModels = this.listModelsFromLibrary(); + LibraryModel libraryModel = libraryModels.stream().filter(model -> model.getName().equals(modelName)).findFirst().orElseThrow(() -> new NoSuchElementException(String.format("Model by name '%s' not found", modelName))); + LibraryModelDetail libraryModelDetail = this.getLibraryModelDetails(libraryModel); + LibraryModelTag libraryModelTag = libraryModelDetail.getTags().stream().filter(tagName -> tagName.getTag().equals(tag)).findFirst().orElseThrow(() -> new NoSuchElementException(String.format("Tag '%s' for model '%s' not found", tag, modelName))); + return libraryModelTag; + } + /** * Pull a model on the Ollama server from the list of available models.