Mitt arbete är för att man ska skriva in en film titel och man får en overview av den filmen Men det finns små fel som kom nu i slutet och jag har inte hunit fixa det änu :(

This commit is contained in:
Noah F
2025-06-09 01:03:32 +02:00
parent 70e7e24aaa
commit f8680b1cab
11 changed files with 216 additions and 13 deletions

20
MovieSugest/build.gradle Normal file
View File

@@ -0,0 +1,20 @@
plugins {
id 'java'
}
group = 'me.zacharias'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
implementation project(":Core")
}
test {
useJUnitPlatform()
}

View File

@@ -0,0 +1,86 @@
package me.noah.movie.sugest;
import me.noah.movie.sugest.endpoints.GetMovieByTitle;
import me.zacharias.chat.core.Core;
import me.zacharias.chat.ollama.OllamaFunctionTools;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
public class MovieSugestTool {
private OllamaFunctionTools movieSugestTools;
private static final String BASE_URL = "https://api.themoviedb.org/3";
private String authentication;
public MovieSugestTool(){
this.movieSugestTools = OllamaFunctionTools.builder()
.addTool(new GetMovieByTitle(this), Core.Source.INTERNAL)
.build();
try {
JSONObject obj = new JSONObject(Files.readString(Path.of(Core.DATA_DIR + "/movieapi.json")));
this.authentication = obj.getString("API-key");
//this.Client_Secret = obj.getString("client_secret");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public OllamaFunctionTools getMovieSugestTools() {
return movieSugestTools;
}
public JSONObject APIRequest(String endpoint, HashMap<String, String> params) {
try {
String urlParams = ParameterStringBuilder.getParamsString(params);
URL url = new URI(this.BASE_URL + endpoint + "?" + urlParams).toURL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("api_key", this.authentication);
connection.setDoOutput(true);
connection.setConnectTimeout(80 * 1000);
int responseCode = connection.getResponseCode();
// HTTP_OK or 200 response code generally means that the server ran successfully without any errors
StringBuilder response = new StringBuilder();
// Read response content
// connection.getInputStream() purpose is to obtain an input stream for reading the server's response.
try (
BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
response.append(line); // Adds every line to response till the end of file.
}
}
if (responseCode == HttpURLConnection.HTTP_OK) {
connection.disconnect();
return new JSONObject(response.toString());
}
else {
connection.disconnect();
System.err.println("Error: HTTP Response code - " + responseCode + "\n"+response.toString());
throw new RuntimeException("HTTP Response code - " + responseCode);
}
}catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Error: " + e.getMessage());
}
}
}

View File

@@ -0,0 +1,27 @@
package me.noah.movie.sugest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
public class ParameterStringBuilder {
public static String getParamsString(Map<String, String> params)
throws UnsupportedEncodingException {
if(params == null)
return "";
StringBuilder result = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
result.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8));
result.append("&");
}
String resultString = result.toString();
return !resultString.isEmpty()
? resultString.substring(0, resultString.length() - 1)
: resultString;
}
}

View File

@@ -0,0 +1,63 @@
package me.noah.movie.sugest.endpoints;
import me.noah.movie.sugest.MovieSugestTool;
import me.zacharias.chat.ollama.OllamaFunctionArgument;
import me.zacharias.chat.ollama.OllamaFunctionTool;
import me.zacharias.chat.ollama.OllamaPerameter;
import me.zacharias.chat.ollama.OllamaToolRespnce;
import me.zacharias.chat.ollama.exceptions.OllamaToolErrorException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class GetMovieByTitle extends OllamaFunctionTool{
private final MovieSugestTool instance;
public GetMovieByTitle(MovieSugestTool instance) {
this.instance = instance;
}
@Override
public String name() {
return "movie_description";
}
@Override
public String description() {
return "get the details of movie";
}
@Override
public OllamaPerameter parameters() {
return OllamaPerameter.builder()
.addProperty("query", OllamaPerameter.OllamaPerameterBuilder.Type.STRING, "Movie or sereis title", true)
.build();
}
@Override
public OllamaToolRespnce function(OllamaFunctionArgument... args) {
String query = null;
if (args.length > 0){
query = (String) args[0] .value();
}
else {
throw new OllamaToolErrorException(name(), "query most be specefied");
}
HashMap < String, String > map = new HashMap<>();
map.put("query", query);
map.put("include_adult", "true");
JSONObject obj = instance.APIRequest("search/movie", map);
ArrayList < String > overviews = new ArrayList<>();
for (Object obj1: obj.getJSONArray("results")){
if (obj instanceof JSONObject result){
overviews.add(result.getString("overview"));
}
}
return new OllamaToolRespnce(name(), overviews.toString());
}
}