1+ import java .io .BufferedReader ;
2+ import java .io .IOException ;
3+ import java .io .InputStreamReader ;
4+ import java .io .OutputStream ;
5+ import java .util .Scanner ;
6+
7+ public class LLMCLI {
8+ public static void main (String [] args ) {
9+ // Path to the .exe file
10+ String exePath = "bin/llama-cli.exe" ;
11+
12+ System .out .println ("Enter -h for help" );
13+ // Scanner to take user input for various commands
14+ Scanner scanner = new Scanner (System .in );
15+
16+ while (true ) {
17+ String commandInput = scanner .nextLine ();
18+
19+ // Split user input into command array for ProcessBuilder
20+ String [] commands = commandInput .split (" " );
21+
22+ // Create an array to hold both the executable path and the commands
23+ String [] fullCommand = new String [commands .length + 1 ];
24+ fullCommand [0 ] = exePath ; // First element is the executable path
25+ System .arraycopy (commands , 0 , fullCommand , 1 , commands .length ); // Copy the user commands after the exe path
26+
27+ Process process = null ;
28+
29+ try {
30+ // Create a ProcessBuilder with the executable and dynamic commands
31+ ProcessBuilder processBuilder = new ProcessBuilder (fullCommand );
32+
33+ // Redirect error stream to read both error and output in one stream
34+ processBuilder .redirectErrorStream (true );
35+
36+ // Start the process
37+ process = processBuilder .start ();
38+
39+ // Capture output in a separate thread
40+ Process finalProcess = process ;
41+ new Thread (() -> {
42+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (finalProcess .getInputStream ()))) {
43+ String line ;
44+ while ((line = reader .readLine ()) != null ) {
45+ System .out .println (line );
46+ }
47+ } catch (IOException e ) {
48+ e .printStackTrace ();
49+ }
50+ }).start ();
51+
52+ // Use OutputStream to send input to the process (if needed)
53+ try (OutputStream processInput = process .getOutputStream ()) {
54+ String userInput ;
55+ while (scanner .hasNextLine () && process .isAlive ()) {
56+ userInput = scanner .nextLine ();
57+ processInput .write ((userInput + "\n " ).getBytes ());
58+ processInput .flush (); // Ensure input is sent immediately
59+ }
60+ }
61+
62+ // Wait for the process to complete and get the exit code
63+ int exitCode = process .waitFor ();
64+ } catch (IOException | InterruptedException e ) {
65+ e .printStackTrace ();
66+ } finally {
67+ // Ensure the process is destroyed if still running
68+ if (process != null ) {
69+ process .destroy ();
70+ }
71+ }
72+ }
73+ }
74+ }
0 commit comments