Skip to content

Commit 0e61bee

Browse files
committed
feat: improve llama server logging
1 parent 1392775 commit 0e61bee

File tree

4 files changed

+112
-39
lines changed

4 files changed

+112
-39
lines changed

src/main/java/ee/carlrobert/codegpt/completions/llama/LlamaServerAgent.java

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ee.carlrobert.codegpt.completions.llama;
22

3+
import static java.lang.String.format;
4+
35
import com.fasterxml.jackson.databind.ObjectMapper;
46
import com.intellij.execution.ExecutionException;
57
import com.intellij.execution.configurations.GeneralCommandLine;
@@ -9,18 +11,21 @@
911
import com.intellij.execution.process.ProcessListener;
1012
import com.intellij.execution.process.ProcessOutputType;
1113
import com.intellij.icons.AllIcons.Actions;
14+
import com.intellij.notification.NotificationType;
1215
import com.intellij.openapi.Disposable;
1316
import com.intellij.openapi.application.ApplicationManager;
1417
import com.intellij.openapi.components.Service;
1518
import com.intellij.openapi.diagnostic.Logger;
1619
import com.intellij.openapi.util.Key;
1720
import com.intellij.ui.components.JBLabel;
21+
import ee.carlrobert.codegpt.CodeGPTBundle;
1822
import ee.carlrobert.codegpt.CodeGPTPlugin;
19-
import ee.carlrobert.codegpt.settings.service.LlamaServiceSelectionForm;
2023
import ee.carlrobert.codegpt.settings.service.ServerProgressPanel;
2124
import ee.carlrobert.codegpt.settings.state.LlamaSettingsState;
25+
import ee.carlrobert.codegpt.util.OverlayUtil;
2226
import java.nio.charset.StandardCharsets;
2327
import java.util.List;
28+
import java.util.concurrent.CopyOnWriteArrayList;
2429
import javax.swing.SwingConstants;
2530
import org.jetbrains.annotations.NotNull;
2631
import org.jetbrains.annotations.Nullable;
@@ -30,19 +35,20 @@ public final class LlamaServerAgent implements Disposable {
3035

3136
private static final Logger LOG = Logger.getInstance(LlamaServerAgent.class);
3237

33-
private static @Nullable OSProcessHandler makeProcessHandler;
34-
private static @Nullable OSProcessHandler startServerProcessHandler;
38+
private @Nullable OSProcessHandler makeProcessHandler;
39+
private @Nullable OSProcessHandler startServerProcessHandler;
3540

3641
public void startAgent(
37-
LlamaServiceSelectionForm llamaServiceSelectionForm,
42+
LlamaServerStartupParams params,
3843
ServerProgressPanel serverProgressPanel,
3944
Runnable onSuccess) {
4045
ApplicationManager.getApplication().invokeLater(() -> {
4146
try {
42-
serverProgressPanel.updateText("Building llama.cpp...");
47+
serverProgressPanel.updateText(
48+
CodeGPTBundle.get("llamaServerAgent.buildingProject.description"));
4349
makeProcessHandler = new OSProcessHandler(getMakeCommandLinde());
4450
makeProcessHandler.addProcessListener(
45-
getMakeProcessListener(llamaServiceSelectionForm, serverProgressPanel, onSuccess));
51+
getMakeProcessListener(params, serverProgressPanel, onSuccess));
4652
makeProcessHandler.startNotify();
4753
} catch (ExecutionException e) {
4854
throw new RuntimeException(e);
@@ -63,9 +69,11 @@ public boolean isServerRunning() {
6369
}
6470

6571
private ProcessListener getMakeProcessListener(
66-
LlamaServiceSelectionForm serviceSelectionForm,
72+
LlamaServerStartupParams params,
6773
ServerProgressPanel serverProgressPanel,
6874
Runnable onSuccess) {
75+
LOG.info("Building llama project");
76+
6977
return new ProcessAdapter() {
7078
@Override
7179
public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType) {
@@ -75,21 +83,16 @@ public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType
7583
@Override
7684
public void processTerminated(@NotNull ProcessEvent event) {
7785
try {
78-
serverProgressPanel.updateText("Booting up server...");
79-
startServerProcessHandler = new OSProcessHandler.Silent(
80-
getServerCommandLine(
81-
serviceSelectionForm.getLlamaModelPreferencesForm().getActualModelPath(),
82-
serviceSelectionForm.getContextSize(),
83-
serviceSelectionForm.getThreads(),
84-
serviceSelectionForm.getServerPort(),
85-
serviceSelectionForm.getListOfAdditionalParameters()));
86-
startServerProcessHandler.addProcessListener(getProcessListener(
87-
serviceSelectionForm.getServerPort(),
88-
serverProgressPanel,
89-
onSuccess));
86+
LOG.info("Booting up llama server");
87+
88+
serverProgressPanel.updateText(
89+
CodeGPTBundle.get("llamaServerAgent.serverBootup.description"));
90+
startServerProcessHandler = new OSProcessHandler.Silent(getServerCommandLine(params));
91+
startServerProcessHandler.addProcessListener(
92+
getProcessListener(params.getPort(), serverProgressPanel, onSuccess));
9093
startServerProcessHandler.startNotify();
9194
} catch (ExecutionException ex) {
92-
LOG.error("Unable to start the server", ex);
95+
LOG.error("Unable to start llama server", ex);
9396
throw new RuntimeException(ex);
9497
}
9598
}
@@ -102,9 +105,18 @@ private ProcessListener getProcessListener(
102105
Runnable onSuccess) {
103106
return new ProcessAdapter() {
104107
private final ObjectMapper objectMapper = new ObjectMapper();
108+
private final List<String> errorLines = new CopyOnWriteArrayList<>();
105109

106110
@Override
107111
public void processTerminated(@NotNull ProcessEvent event) {
112+
if (errorLines.isEmpty()) {
113+
LOG.info(format("Server terminated with code %d", event.getExitCode()));
114+
} else {
115+
var error = String.join("", errorLines);
116+
OverlayUtil.showNotification(error, NotificationType.ERROR);
117+
LOG.error(error);
118+
}
119+
108120
serverProgressPanel.displayComponent(new JBLabel(
109121
"Server terminated",
110122
Actions.Cancel,
@@ -113,12 +125,19 @@ public void processTerminated(@NotNull ProcessEvent event) {
113125

114126
@Override
115127
public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType) {
116-
LOG.debug(event.getText());
128+
if (ProcessOutputType.isStderr(outputType)) {
129+
errorLines.add(event.getText());
130+
return;
131+
}
132+
133+
if (ProcessOutputType.isStdout(outputType)) {
134+
LOG.info(event.getText());
117135

118-
if (outputType == ProcessOutputType.STDOUT) {
119136
try {
120137
var serverMessage = objectMapper.readValue(event.getText(), LlamaServerMessage.class);
121138
if ("HTTP server listening".equals(serverMessage.getMessage())) {
139+
LOG.info("Server up and running!");
140+
122141
LlamaSettingsState.getInstance().setServerPort(port);
123142
onSuccess.run();
124143
}
@@ -139,21 +158,16 @@ private static GeneralCommandLine getMakeCommandLinde() {
139158
return commandLine;
140159
}
141160

142-
private GeneralCommandLine getServerCommandLine(
143-
String modelPath,
144-
int contextLength,
145-
int threads,
146-
int port,
147-
List<String> additionalParameters) {
161+
private GeneralCommandLine getServerCommandLine(LlamaServerStartupParams params) {
148162
GeneralCommandLine commandLine = new GeneralCommandLine().withCharset(StandardCharsets.UTF_8);
149163
commandLine.setExePath("./server");
150164
commandLine.withWorkDirectory(CodeGPTPlugin.getLlamaSourcePath());
151165
commandLine.addParameters(
152-
"-m", modelPath,
153-
"-c", String.valueOf(contextLength),
154-
"--port", String.valueOf(port),
155-
"-t", String.valueOf(threads));
156-
commandLine.addParameters(additionalParameters);
166+
"-m", params.getModelPath(),
167+
"-c", String.valueOf(params.getContextLength()),
168+
"--port", String.valueOf(params.getPort()),
169+
"-t", String.valueOf(params.getThreads()));
170+
commandLine.addParameters(params.getAdditionalParameters());
157171
commandLine.setRedirectErrorStream(false);
158172
return commandLine;
159173
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package ee.carlrobert.codegpt.completions.llama;
2+
3+
import java.util.List;
4+
5+
public class LlamaServerStartupParams {
6+
7+
private final String modelPath;
8+
private final int contextLength;
9+
private final int threads;
10+
private final int port;
11+
private final List<String> additionalParameters;
12+
13+
public LlamaServerStartupParams(
14+
String modelPath,
15+
int contextLength,
16+
int threads,
17+
int port,
18+
List<String> additionalParameters) {
19+
this.modelPath = modelPath;
20+
this.contextLength = contextLength;
21+
this.threads = threads;
22+
this.port = port;
23+
this.additionalParameters = additionalParameters;
24+
}
25+
26+
public String getModelPath() {
27+
return modelPath;
28+
}
29+
30+
public int getContextLength() {
31+
return contextLength;
32+
}
33+
34+
public int getThreads() {
35+
return threads;
36+
}
37+
38+
public int getPort() {
39+
return port;
40+
}
41+
42+
public List<String> getAdditionalParameters() {
43+
return additionalParameters;
44+
}
45+
}

src/main/java/ee/carlrobert/codegpt/settings/service/LlamaServiceSelectionForm.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import ee.carlrobert.codegpt.CodeGPTPlugin;
1919
import ee.carlrobert.codegpt.completions.HuggingFaceModel;
2020
import ee.carlrobert.codegpt.completions.llama.LlamaServerAgent;
21+
import ee.carlrobert.codegpt.completions.llama.LlamaServerStartupParams;
2122
import ee.carlrobert.codegpt.settings.state.LlamaSettingsState;
2223
import ee.carlrobert.codegpt.util.OverlayUtil;
2324
import java.awt.BorderLayout;
@@ -179,11 +180,21 @@ private JButton getServerButton(
179180
llamaServerAgent.stopAgent();
180181
} else {
181182
disableForm(serverButton, serverProgressPanel);
182-
llamaServerAgent.startAgent(this, serverProgressPanel, () -> {
183-
setFormEnabled(false);
184-
serverProgressPanel.displayComponent(
185-
new JBLabel("Server running", Actions.Checked, SwingConstants.LEADING));
186-
});
183+
llamaServerAgent.startAgent(
184+
new LlamaServerStartupParams(
185+
llamaModelPreferencesForm.getActualModelPath(),
186+
getContextSize(),
187+
getThreads(),
188+
getServerPort(),
189+
getListOfAdditionalParameters()),
190+
serverProgressPanel,
191+
() -> {
192+
setFormEnabled(false);
193+
serverProgressPanel.displayComponent(new JBLabel(
194+
CodeGPTBundle.get("settingsConfigurable.service.llama.progress.serverRunning"),
195+
Actions.Checked,
196+
SwingConstants.LEADING));
197+
});
187198
}
188199
});
189200
return serverButton;

src/main/resources/messages/codegpt.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ settingsConfigurable.service.llama.contextSize.label=Prompt context size:
4343
settingsConfigurable.service.llama.contextSize.comment=The size of the prompt context. LLaMA models were built with a context of 2048, which will provide better results for longer input/inference.
4444
settingsConfigurable.service.llama.threads.label=Threads:
4545
settingsConfigurable.service.llama.threads.comment=The number of threads available to execute the model. It is not recommended to specify a number greater than the number of processor cores.
46-
settingsConfigurable.service.llama.additionalParameters.label=Additional parameters
46+
settingsConfigurable.service.llama.additionalParameters.label=Additional parameters:
4747
settingsConfigurable.service.llama.additionalParameters.comment=<html>Additional command-line parameters for the server startup process, separated by commas. See the full <a href="https://github.com/ggerganov/llama.cpp/blob/master/examples/server/README.md">list of options</a>.<p><i>Example: "--n-gpu-layers, 1, --no-mmap, --mlock"</i></p></html>
4848
settingsConfigurable.service.llama.port.label=Port:
4949
settingsConfigurable.service.llama.startServer.label=Start server
5050
settingsConfigurable.service.llama.stopServer.label=Stop server
51+
settingsConfigurable.service.llama.progress.serverRunning=Server running
5152
settingsConfigurable.service.llama.progress.stoppingServer=Stopping a server...
5253
settingsConfigurable.service.llama.progress.startingServer=Starting a server...
5354
settingsConfigurable.service.llama.progress.downloadingModel.title=Downloading Model
@@ -134,3 +135,5 @@ checkForUpdatesTask.title=Checking for CodeGPT update...
134135
checkForUpdatesTask.notification.message=An update for CodeGPT is available.
135136
checkForUpdatesTask.notification.installButton=Install update
136137
checkForUpdatesTask.notification.hideButton=Do not show again
138+
llamaServerAgent.buildingProject.description=Building llama.cpp...
139+
llamaServerAgent.serverBootup.description=Booting up server...

0 commit comments

Comments
 (0)