forked from beehive-lab/GPULlama3.java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatboxInteractor.java
More file actions
89 lines (75 loc) · 3.18 KB
/
ChatboxInteractor.java
File metadata and controls
89 lines (75 loc) · 3.18 KB
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
package com.example.gui;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ChatboxInteractor {
private final ChatboxModel model;
public ChatboxInteractor(ChatboxModel viewModel) {
this.model = viewModel;
}
// Run the 'llama-tornado' script in the given Llama3 path, and stream its output to the GUI's output area.
public void runLlamaTornado() {
List<String> commands = new ArrayList<>();
String llama3Path = model.getLlama3Path();
if (llama3Path == null || llama3Path.isEmpty()) {
model.setOutputText("Please set the Llama3 path:");
return;
}
// Format for running 'llama-tornado' depends on the operating system.
if (System.getProperty("os.name").startsWith("Windows")) {
commands.add(String.format("%s\\external\\tornadovm\\.venv\\Scripts\\python", llama3Path));
commands.add("llama-tornado");
} else {
commands.add(String.format("%s/.llama-tornado", llama3Path));
}
ChatboxModel.Engine engine = model.getSelectedEngine();
if (engine == ChatboxModel.Engine.TORNADO_VM) {
commands.add("--gpu");
}
// Assume that models are found in a /models directory.
String selectedModel = model.getSelectedModel();
if (selectedModel == null || selectedModel.isEmpty()) {
model.setOutputText("Please select a model.");
return;
}
String modelPath = String.format("%s/models/%s", llama3Path, selectedModel);
String prompt = String.format("\"%s\"", model.getPromptText());
commands.addAll(Arrays.asList(
"--model", modelPath,
"--prompt", prompt
));
ProcessBuilder processBuilder = new ProcessBuilder(commands);
processBuilder.redirectErrorStream(true);
BufferedReader bufferedReader = null;
Process process;
try {
process = processBuilder.start();
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder builder = new StringBuilder();
// Make sure to output the raw command.
builder.append("Running command: ");
builder.append(String.join(" ", processBuilder.command().toArray(new String[0])));
builder.append(System.getProperty("line.separator"));
String line;
while ((line = bufferedReader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
final String currentOutput = builder.toString();
javafx.application.Platform.runLater(() -> model.setOutputText(currentOutput));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}