|
|
|
|
| import java.net.URI; |
| import java.net.http.HttpClient; |
| import java.net.http.HttpRequest; |
| import java.net.http.HttpResponse; |
| import java.util.Scanner; |
| import java.time.Duration; |
|
|
| public class Test2 { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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) { |
| try { |
| HttpClient client = HttpClient.newBuilder() |
| .connectTimeout(Duration.ofSeconds(60)) |
| .build(); |
|
|
| HttpRequest request = HttpRequest.newBuilder() |
| .uri(URI.create(PROXY_URL)) |
| .header("Content-Type", "text/plain") |
| .header("X-Model", model.modelId) |
| .POST(HttpRequest.BodyPublishers.ofString(prompt)) |
| .build(); |
|
|
| HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
|
|
| if (response.statusCode() == 200) { |
| return response.body(); |
| } else { |
| return "Error from Server: " + response.statusCode() + " - " + response.body(); |
| } |
|
|
| } catch (Exception e) { |
| return "Connection Error: " + e.getMessage(); |
| } |
| } |
|
|
| public static void main(String[] args) { |
| Scanner scanner = new Scanner(System.in); |
| System.out.println("=== Java LLM Assistant (HuggingFace Proxy) ==="); |
|
|
| |
| 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 { |
| choice = Integer.parseInt(scanner.nextLine()); |
| } 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!"); |
| } |
| } |
|
|