File size: 5,350 Bytes
6328d10 57ce88d 6328d10 57ce88d 6328d10 57ce88d 6328d10 57ce88d 6328d10 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class Test3 {
private static final String PROXY_URL = "https://ttl0110-llm-proxy.hf.space/ask";
public enum LLMModel {
LAGUNA_S_2_1("Laguna S 2.1 (Free)", "poolside/laguna-s-2.1:free"),
GEMINI_3_6_FLASH("Gemini 3.6 Flash (Paid)", "google/gemini-3.6-flash"),
CLAUDE_OPUS_5("Claude Opus 5 (Paid)", "anthropic/claude-opus-5"),
CLAUDE_FABLE_5("Claude Fable 5 (Paid)", "anthropic/claude-fable-5"),
GPT_5_6_SOL("GPT-5.6 Sol (Paid)", "openai/gpt-5.6-sol"),
GPT_5_6_LUNA("GPT-5.6 Luna (Paid)", "openai/gpt-5.6-luna"),
NEMOTRON_3_ULTRA("Nemotron 3 Ultra (Free)", "nvidia/nemotron-3-ultra-550b-a55b:free");
public final String displayName;
public final String modelId;
LLMModel(String displayName, String modelId) {
this.displayName = displayName;
this.modelId = modelId;
}
}
public static String askLLM(String prompt, LLMModel model) {
HttpURLConnection conn = null;
try {
URL url = new URL(PROXY_URL);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/plain");
conn.setRequestProperty("X-Model", model.modelId);
conn.setConnectTimeout(60000);
conn.setReadTimeout(60000);
conn.setDoOutput(true);
// Gửi request body
try (OutputStream os = conn.getOutputStream()) {
byte[] input = prompt.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
try (BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
StringBuilder response = new StringBuilder();
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim()).append("\n");
}
return response.toString().trim();
}
} else {
return "Error from Server: " + responseCode;
}
} catch (Exception e) {
return "Connection Error: " + e.getMessage();
} finally {
if (conn != null) {
conn.disconnect();
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("=== Java 8 LLM Assistant (HuggingFace Proxy) ===");
// Model Selection
System.out.println("Chon Model LLM:");
LLMModel[] models = LLMModel.values();
for (int i = 0; i < models.length; i++) {
System.out.println((i + 1) + ". " + models[i].displayName);
}
int choice = 0;
while (choice < 1 || choice > models.length) {
System.out.print("Nhap lua chon (1-" + models.length + "): ");
try {
String input = scanner.nextLine();
choice = Integer.parseInt(input);
} catch (Exception e) {
}
}
LLMModel selectedModel = models[choice - 1];
System.out.println("Ban da chon: " + selectedModel.displayName);
System.out.print("Nhap prompt prefix (bo trong neu khong dung): ");
String promptPrefix = scanner.nextLine().trim();
System.out.println("\nHuong dan: Nhap prompt cua ban. Nhap 'SUBMIT' o dong cuoi cung de gui.");
System.out.println("Nhap 'EXIT' o bat ky dau de thoat.");
while (true) {
System.out.println("\nBan [" + selectedModel.name() + "]: ");
StringBuilder promptBuilder = new StringBuilder();
boolean exit = false;
while (true) {
String line = scanner.nextLine();
if (line.equalsIgnoreCase("EXIT")) {
exit = true;
break;
}
if (line.equalsIgnoreCase("SUBMIT")) {
break;
}
promptBuilder.append(line).append("\n");
}
if (exit)
break;
String userPrompt = promptBuilder.toString().trim();
if (userPrompt.isEmpty()) {
System.out.println("Prompt trong, vui long nhap lai.");
continue;
}
String fullPrompt = promptPrefix.isEmpty()
? userPrompt
: promptPrefix + "\n\n" + userPrompt;
System.out.println("Thinking...");
String response = askLLM(fullPrompt, selectedModel);
System.out.println("\n[LLM]: " + response);
System.out.println("-------------------------------------------");
}
scanner.close();
System.out.println("Tam biet!");
}
}
|