Compare commits
2 Commits
13800486ba
...
b0e9f1bb4e
| Author | SHA1 | Date | |
|---|---|---|---|
| b0e9f1bb4e | |||
| b8bc715868 |
@@ -10,10 +10,9 @@ import java.util.Properties;
|
||||
|
||||
public class MistralDirectAPI {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String apiKey;
|
||||
private static String apiKey;
|
||||
|
||||
// Load API key from properties file
|
||||
static {
|
||||
try {
|
||||
Properties props = new Properties();
|
||||
FileInputStream input = new FileInputStream("config.properties");
|
||||
@@ -22,18 +21,22 @@ public class MistralDirectAPI {
|
||||
input.close();
|
||||
} catch (IOException e) {
|
||||
System.err.println("Could not load API key: " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
String payload =
|
||||
public static String translateToEmojis(String message) {
|
||||
String payload = String.format(
|
||||
"""
|
||||
{
|
||||
"model": "mistral-medium",
|
||||
"messages": [
|
||||
{ "role": "user", "content": "Reverse turing test." }
|
||||
{ "role": "system", "content": "Please translate the following message to emojis only. Do not include any comments. End the emoji message with ;" },
|
||||
{ "role": "user", "content": "%s" }
|
||||
]
|
||||
}
|
||||
""";
|
||||
""",
|
||||
message
|
||||
);
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create("https://api.mistral.ai/v1/chat/completions"))
|
||||
@@ -49,10 +52,26 @@ public class MistralDirectAPI {
|
||||
request,
|
||||
HttpResponse.BodyHandlers.ofString()
|
||||
);
|
||||
System.out.println("Response Code: " + response.statusCode());
|
||||
System.out.println("Response Body: " + response.body());
|
||||
String responseBody = response.body();
|
||||
return extractContent(responseBody);
|
||||
} catch (IOException | InterruptedException e) {
|
||||
System.err.println("Error occurred: " + e.getMessage());
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
||||
private static String extractContent(String jsonResponse) {
|
||||
// Find the content field in the response
|
||||
int contentIndex = jsonResponse.indexOf("\"content\":\"");
|
||||
if (contentIndex != -1) {
|
||||
int startIndex = contentIndex + "\"content\":\"".length();
|
||||
|
||||
int endIndex = jsonResponse.indexOf(";", startIndex);
|
||||
|
||||
if (endIndex != -1) {
|
||||
return jsonResponse.substring(startIndex, endIndex);
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package clientserver.server;
|
||||
|
||||
import clientserver.client.Client;
|
||||
import clientserver.common.MistralDirectAPI;
|
||||
import java.net.DatagramSocket;
|
||||
|
||||
public class MessageProcessor {
|
||||
@@ -42,6 +43,10 @@ public class MessageProcessor {
|
||||
case "/disconnect":
|
||||
handleDisconnect();
|
||||
break;
|
||||
default:
|
||||
// Broadcast message to all clients
|
||||
broadcastMessageEmoji(message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,6 +100,37 @@ public class MessageProcessor {
|
||||
);
|
||||
}
|
||||
|
||||
private void broadcastMessage(String message) {
|
||||
String formattedMessage = "> " + client.getPseudo() + ": " + message;
|
||||
|
||||
for (ClientHandler handler : Server.getAllClientHandlers()) {
|
||||
Client targetClient = handler.getClient();
|
||||
Server.sendMessage(
|
||||
clientHandlerSocket,
|
||||
formattedMessage,
|
||||
targetClient.getAddress(),
|
||||
targetClient.getPort()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void broadcastMessageEmoji(String message) {
|
||||
// Translate message to emojis
|
||||
String emojiMessage = MistralDirectAPI.translateToEmojis(message);
|
||||
String formattedMessage =
|
||||
"> " + client.getPseudo() + ": " + emojiMessage;
|
||||
|
||||
for (ClientHandler handler : Server.getAllClientHandlers()) {
|
||||
Client targetClient = handler.getClient();
|
||||
Server.sendMessage(
|
||||
clientHandlerSocket,
|
||||
formattedMessage,
|
||||
targetClient.getAddress(),
|
||||
targetClient.getPort()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleMsgCommand() {
|
||||
Server.sendMessage(
|
||||
clientHandlerSocket,
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@@ -179,6 +180,10 @@ public class Server {
|
||||
return mapPseudosConnectedClientsHandlers.get(pseudo);
|
||||
}
|
||||
|
||||
public static Collection<ClientHandler> getAllClientHandlers() {
|
||||
return mapPseudosConnectedClientsHandlers.values();
|
||||
}
|
||||
|
||||
public void addConnectedClientHandler(
|
||||
String pseudo,
|
||||
ClientHandler clientHandler
|
||||
|
||||
Reference in New Issue
Block a user