Deprecate findModelTagFromLibrary and simplify WeatherTool

Marked findModelTagFromLibrary as deprecated in OllamaAPI due to reliance on unstable HTML structure. Simplified WeatherTool by removing external API calls and returning a static weather response for demonstration purposes.
This commit is contained in:
amithkoujalgi 2025-08-31 01:33:57 +05:30
parent 6ae6f4f25b
commit 5f5fa8ecae
No known key found for this signature in database
GPG Key ID: E29A37746AF94B70
2 changed files with 50 additions and 78 deletions

View File

@ -371,6 +371,11 @@ public class OllamaAPI {
/** /**
* Finds a specific model using model name and tag from Ollama library. * Finds a specific model using model name and tag from Ollama library.
* <p> * <p>
* <b>Deprecated:</b> This method relies on the HTML structure of the Ollama website,
* which is subject to change at any time. As a result, it is difficult to keep this API
* method consistently updated and reliable. Therefore, this method is deprecated and
* may be removed in future releases.
* <p>
* This method retrieves the model from the Ollama library by its name, then * This method retrieves the model from the Ollama library by its name, then
* fetches its tags. * fetches its tags.
* It searches through the tags of the model to find one that matches the * It searches through the tags of the model to find one that matches the
@ -388,7 +393,9 @@ public class OllamaAPI {
* @throws URISyntaxException If there is an error with the URI syntax. * @throws URISyntaxException If there is an error with the URI syntax.
* @throws InterruptedException If the operation is interrupted. * @throws InterruptedException If the operation is interrupted.
* @throws NoSuchElementException If the model or the tag is not found. * @throws NoSuchElementException If the model or the tag is not found.
* @deprecated This method relies on the HTML structure of the Ollama website, which can change at any time and break this API. It is deprecated and may be removed in the future.
*/ */
@Deprecated
public LibraryModelTag findModelTagFromLibrary(String modelName, String tag) public LibraryModelTag findModelTagFromLibrary(String modelName, String tag)
throws OllamaBaseException, IOException, URISyntaxException, InterruptedException { throws OllamaBaseException, IOException, URISyntaxException, InterruptedException {
List<LibraryModel> libraryModels = this.listModelsFromLibrary(); List<LibraryModel> libraryModels = this.listModelsFromLibrary();

View File

@ -1,89 +1,54 @@
package io.github.ollama4j.tools.sampletools; package io.github.ollama4j.tools.sampletools;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Map;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.ollama4j.tools.Tools; import io.github.ollama4j.tools.Tools;
import java.util.Map;
@SuppressWarnings("resource") @SuppressWarnings("resource")
public class WeatherTool { public class WeatherTool {
private String openWeatherMapAPIKey = null; private String paramCityName = "cityName";
private String paramCityName = "cityName";
public WeatherTool(String openWeatherMapAPIKey) {
this.openWeatherMapAPIKey = openWeatherMapAPIKey;
}
public String getCurrentWeather(Map<String, Object> arguments) { public WeatherTool() {
}
String city = (String) arguments.get(paramCityName); public String getCurrentWeather(Map<String, Object> arguments) {
System.out.println("Finding weather for city: " + city); String city = (String) arguments.get(paramCityName);
return "It is sunny in " + city;
}
String url = String.format("https://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=metric", public Tools.ToolSpecification getSpecification() {
city, return Tools.ToolSpecification.builder()
this.openWeatherMapAPIKey); .functionName("weather-reporter")
.functionDescription(
HttpClient client = HttpClient.newHttpClient(); "You are a tool who simply finds the city name from the user's message input/query about weather.")
HttpRequest request = HttpRequest.newBuilder() .toolFunction(this::getCurrentWeather)
.uri(URI.create(url)) .toolPrompt(
.build(); Tools.PromptFuncDefinition.builder()
try { .type("prompt")
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); .function(
if (response.statusCode() == 200) { Tools.PromptFuncDefinition.PromptFuncSpec
ObjectMapper mapper = new ObjectMapper(); .builder()
JsonNode root = mapper.readTree(response.body()); .name("get-city-name")
JsonNode main = root.path("main"); .description("Get the city name")
double temperature = main.path("temp").asDouble(); .parameters(
String description = root.path("weather").get(0).path("description").asText(); Tools.PromptFuncDefinition.Parameters
return String.format("Weather in %s: %.1f°C, %s", city, temperature, description); .builder()
} else { .type("object")
return "Could not retrieve weather data for " + city + ". Status code: " .properties(
+ response.statusCode(); Map.of(
} paramCityName,
} catch (IOException | InterruptedException e) { Tools.PromptFuncDefinition.Property
return "Error retrieving weather data: " + e.getMessage(); .builder()
} .type("string")
} .description(
"The name of the city. e.g. Bengaluru")
public Tools.ToolSpecification getSpecification() { .required(true)
return Tools.ToolSpecification.builder() .build()))
.functionName("weather-reporter") .required(java.util.List
.functionDescription( .of(paramCityName))
"You are a tool who simply finds the city name from the user's message input/query about weather.")
.toolFunction(this::getCurrentWeather)
.toolPrompt(
Tools.PromptFuncDefinition.builder()
.type("prompt")
.function(
Tools.PromptFuncDefinition.PromptFuncSpec
.builder()
.name("get-city-name")
.description("Get the city name")
.parameters(
Tools.PromptFuncDefinition.Parameters
.builder()
.type("object")
.properties(
Map.of(
paramCityName,
Tools.PromptFuncDefinition.Property
.builder()
.type("string")
.description(
"The name of the city. e.g. Bengaluru")
.required(true)
.build()))
.required(java.util.List
.of(paramCityName))
.build())
.build())
.build()) .build())
.build(); .build())
} .build())
.build();
}
} }