mirror of
https://github.com/amithkoujalgi/ollama4j.git
synced 2025-09-16 03:39:05 +02:00
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:
parent
6ae6f4f25b
commit
5f5fa8ecae
@ -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();
|
||||||
|
@ -1,54 +1,19 @@
|
|||||||
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 WeatherTool() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCurrentWeather(Map<String, Object> arguments) {
|
public String getCurrentWeather(Map<String, Object> arguments) {
|
||||||
|
|
||||||
String city = (String) arguments.get(paramCityName);
|
String city = (String) arguments.get(paramCityName);
|
||||||
System.out.println("Finding weather for city: " + city);
|
return "It is sunny in " + 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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tools.ToolSpecification getSpecification() {
|
public Tools.ToolSpecification getSpecification() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user