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.
|
||||
* <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();
|
||||
|
@ -1,89 +1,54 @@
|
||||
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;
|
||||
}
|
||||
private String paramCityName = "cityName";
|
||||
|
||||
public String getCurrentWeather(Map<String, Object> arguments) {
|
||||
public WeatherTool() {
|
||||
}
|
||||
|
||||
String city = (String) arguments.get(paramCityName);
|
||||
System.out.println("Finding weather for city: " + city);
|
||||
public String getCurrentWeather(Map<String, Object> arguments) {
|
||||
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",
|
||||
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() {
|
||||
return Tools.ToolSpecification.builder()
|
||||
.functionName("weather-reporter")
|
||||
.functionDescription(
|
||||
"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())
|
||||
public Tools.ToolSpecification getSpecification() {
|
||||
return Tools.ToolSpecification.builder()
|
||||
.functionName("weather-reporter")
|
||||
.functionDescription(
|
||||
"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();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user