refactor: add arguments support to run client or server

This commit is contained in:
2025-03-31 20:58:05 +02:00
parent aaebae7850
commit d3b523048f
11 changed files with 198 additions and 50 deletions

View File

@@ -0,0 +1,58 @@
package clientserver.common;
import java.io.FileInputStream;
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.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": "Reverse turing test." }
]
}
""";
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());
}
}
}