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.
* <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
* fetches its tags.
* 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 InterruptedException If the operation is interrupted.
* @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)
throws OllamaBaseException, IOException, URISyntaxException, InterruptedException {
List<LibraryModel> libraryModels = this.listModelsFromLibrary();

View File

@ -1,54 +1,19 @@
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 java.util.Map;
@SuppressWarnings("resource")
public class WeatherTool {
private String openWeatherMapAPIKey = null;
private String paramCityName = "cityName";
public WeatherTool(String openWeatherMapAPIKey) {
this.openWeatherMapAPIKey = openWeatherMapAPIKey;
public WeatherTool() {
}
public String getCurrentWeather(Map<String, Object> arguments) {
String city = (String) arguments.get(paramCityName);
System.out.println("Finding weather for city: " + city);
String url = String.format("https://api.openweathermap.org/data/2.5/weather?q=%s&appid=%s&units=metric",
city,
this.openWeatherMapAPIKey);
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
ObjectMapper mapper = new ObjectMapper();
JsonNode root = mapper.readTree(response.body());
JsonNode main = root.path("main");
double temperature = main.path("temp").asDouble();
String description = root.path("weather").get(0).path("description").asText();
return String.format("Weather in %s: %.1f°C, %s", city, temperature, description);
} else {
return "Could not retrieve weather data for " + city + ". Status code: "
+ response.statusCode();
}
} catch (IOException | InterruptedException e) {
return "Error retrieving weather data: " + e.getMessage();
}
return "It is sunny in " + city;
}
public Tools.ToolSpecification getSpecification() {