llm-proxy / Test3.java
trung.le
feat: add C++ client and refresh client features
57ce88d
Raw
History Blame Contribute Delete
5.35 kB
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!");
}
}