feat: add Mistral support

This commit is contained in:
2025-03-28 10:26:37 +01:00
parent cd3b58f7f8
commit dd361e8cea
8 changed files with 61 additions and 2205 deletions

View File

@@ -9,6 +9,6 @@ public class App {
public static void main(String[] args) {
System.out.println(new App().getGreeting());
Server.scannerUDP(0, 5000);
MistralDirectAPI.main(new String[] {});
}
}

View File

@@ -0,0 +1,58 @@
package clientserver;
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.io.FileInputStream;
import java.util.Properties;
public class MistralDirectAPI {
public static void main(String[] args) {
String apiKey;
// Load API key from properties file
try {
Properties props = new Properties();
FileInputStream input = new FileInputStream("config.properties");
props.load(input);
apiKey = props.getProperty("mistral.api.key");
input.close();
} catch (IOException e) {
System.err.println("Could not load API key: " + e.getMessage());
return;
}
String payload =
"""
{
"model": "mistral-medium",
"messages": [
{ "role": "user", "content": "Explain recursion like I'm five." }
]
}
""";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.mistral.ai/v1/chat/completions"))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(payload))
.build();
HttpClient client = HttpClient.newHttpClient();
try {
HttpResponse<String> response = client.send(
request,
HttpResponse.BodyHandlers.ofString()
);
System.out.println("Response Code: " + response.statusCode());
System.out.println("Response Body: " + response.body());
} catch (IOException | InterruptedException e) {
System.err.println("Error occurred: " + e.getMessage());
}
}
}