Skip to content

Commit 390d8cd

Browse files
authored
Add setting for custom LLama server executable (#344)
1 parent 9ad12f8 commit 390d8cd

File tree

8 files changed

+222
-40
lines changed

8 files changed

+222
-40
lines changed

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

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,19 @@ public void startAgent(
3939
Runnable onSuccess,
4040
Runnable onServerTerminated) {
4141
ApplicationManager.getApplication().invokeLater(() -> {
42-
try {
43-
serverProgressPanel.updateText(
44-
CodeGPTBundle.get("llamaServerAgent.buildingProject.description"));
45-
makeProcessHandler = new OSProcessHandler(getMakeCommandLinde());
46-
makeProcessHandler.addProcessListener(
47-
getMakeProcessListener(params, serverProgressPanel, onSuccess, onServerTerminated));
48-
makeProcessHandler.startNotify();
49-
} catch (ExecutionException e) {
50-
throw new RuntimeException(e);
42+
if (!params.isUseCustomServer()) {
43+
try {
44+
serverProgressPanel.updateText(
45+
CodeGPTBundle.get("llamaServerAgent.buildingProject.description"));
46+
makeProcessHandler = new OSProcessHandler(getMakeCommandLinde());
47+
makeProcessHandler.addProcessListener(
48+
getMakeProcessListener(params, serverProgressPanel, onSuccess, onServerTerminated));
49+
makeProcessHandler.startNotify();
50+
} catch (ExecutionException e) {
51+
throw new RuntimeException(e);
52+
}
53+
} else {
54+
startServer(params, serverProgressPanel, onSuccess, onServerTerminated);
5155
}
5256
});
5357
}
@@ -79,23 +83,31 @@ public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType
7983

8084
@Override
8185
public void processTerminated(@NotNull ProcessEvent event) {
82-
try {
83-
LOG.info("Booting up llama server");
84-
85-
serverProgressPanel.updateText(
86-
CodeGPTBundle.get("llamaServerAgent.serverBootup.description"));
87-
startServerProcessHandler = new OSProcessHandler.Silent(getServerCommandLine(params));
88-
startServerProcessHandler.addProcessListener(
89-
getProcessListener(params.getPort(), onSuccess, onServerTerminated));
90-
startServerProcessHandler.startNotify();
91-
} catch (ExecutionException ex) {
92-
LOG.error("Unable to start llama server", ex);
93-
throw new RuntimeException(ex);
94-
}
86+
startServer(params, serverProgressPanel, onSuccess, onServerTerminated);
9587
}
9688
};
9789
}
9890

91+
private void startServer(
92+
LlamaServerStartupParams params,
93+
ServerProgressPanel serverProgressPanel,
94+
Runnable onSuccess,
95+
Runnable onServerTerminated) {
96+
try {
97+
LOG.info("Booting up llama server");
98+
99+
serverProgressPanel.updateText(
100+
CodeGPTBundle.get("llamaServerAgent.serverBootup.description"));
101+
startServerProcessHandler = new OSProcessHandler.Silent(getServerCommandLine(params));
102+
startServerProcessHandler.addProcessListener(
103+
getProcessListener(params.getPort(), onSuccess, onServerTerminated));
104+
startServerProcessHandler.startNotify();
105+
} catch (ExecutionException ex) {
106+
LOG.error("Unable to start llama server", ex);
107+
throw new RuntimeException(ex);
108+
}
109+
}
110+
99111
private ProcessListener getProcessListener(
100112
int port,
101113
Runnable onSuccess,
@@ -152,8 +164,8 @@ private static GeneralCommandLine getMakeCommandLinde() {
152164

153165
private GeneralCommandLine getServerCommandLine(LlamaServerStartupParams params) {
154166
GeneralCommandLine commandLine = new GeneralCommandLine().withCharset(StandardCharsets.UTF_8);
155-
commandLine.setExePath("./server");
156-
commandLine.withWorkDirectory(CodeGPTPlugin.getLlamaSourcePath());
167+
commandLine.setExePath("./" + params.getServerFileName());
168+
commandLine.withWorkDirectory(params.getServerDirectory());
157169
commandLine.addParameters(
158170
"-m", params.getModelPath(),
159171
"-c", String.valueOf(params.getContextLength()),

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,50 @@
11
package ee.carlrobert.codegpt.completions.llama;
22

3+
import java.io.File;
34
import java.util.List;
45

56
public class LlamaServerStartupParams {
67

8+
private final String serverPath;
9+
private final boolean useCustomServer;
710
private final String modelPath;
811
private final int contextLength;
912
private final int threads;
1013
private final int port;
1114
private final List<String> additionalParameters;
1215

1316
public LlamaServerStartupParams(
14-
String modelPath,
17+
String serverPath,
18+
boolean useCustomServer, String modelPath,
1519
int contextLength,
1620
int threads,
1721
int port,
1822
List<String> additionalParameters) {
23+
this.serverPath = serverPath;
24+
this.useCustomServer = useCustomServer;
1925
this.modelPath = modelPath;
2026
this.contextLength = contextLength;
2127
this.threads = threads;
2228
this.port = port;
2329
this.additionalParameters = additionalParameters;
2430
}
2531

32+
public String getServerPath() {
33+
return serverPath;
34+
}
35+
36+
public String getServerFileName() {
37+
return serverPath.substring(serverPath.lastIndexOf(File.separator) + 1);
38+
}
39+
40+
public String getServerDirectory() {
41+
return serverPath.substring(0, serverPath.lastIndexOf(File.separator) + 1);
42+
}
43+
44+
public boolean isUseCustomServer() {
45+
return useCustomServer;
46+
}
47+
2648
public String getModelPath() {
2749
return modelPath;
2850
}

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ee.carlrobert.codegpt.settings.service;
22

3+
import static ee.carlrobert.codegpt.ui.UIUtil.createRadioButtonsPanel;
34
import static java.lang.String.format;
45
import static java.util.stream.Collectors.toList;
56

@@ -151,7 +152,8 @@ public LlamaModelPreferencesForm() {
151152

152153
public JPanel getForm() {
153154
JPanel finalPanel = new JPanel(new BorderLayout());
154-
finalPanel.add(createRadioButtonsPanel(), BorderLayout.NORTH);
155+
finalPanel.add(createRadioButtonsPanel(predefinedModelRadioButton, customModelRadioButton),
156+
BorderLayout.NORTH);
155157
finalPanel.add(createFormPanelCards(), BorderLayout.CENTER);
156158
return finalPanel;
157159
}
@@ -227,20 +229,6 @@ private JPanel createFormPanelCards() {
227229
return formPanelCards;
228230
}
229231

230-
private JPanel createRadioButtonsPanel() {
231-
var buttonGroup = new ButtonGroup();
232-
buttonGroup.add(predefinedModelRadioButton);
233-
buttonGroup.add(customModelRadioButton);
234-
235-
var radioPanel = new JPanel();
236-
radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.PAGE_AXIS));
237-
radioPanel.add(predefinedModelRadioButton);
238-
radioPanel.add(Box.createVerticalStrut(4));
239-
radioPanel.add(customModelRadioButton);
240-
radioPanel.add(Box.createVerticalStrut(8));
241-
return radioPanel;
242-
}
243-
244232
private JPanel createCustomModelForm() {
245233
var customModelHelpText = ComponentPanelBuilder.createCommentComponent(
246234
CodeGPTBundle.get("settingsConfigurable.service.llama.customModelPath.comment"),

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package ee.carlrobert.codegpt.settings.service;
22

3+
import static ee.carlrobert.codegpt.ui.UIUtil.createRadioButtonsPanel;
34
import static java.util.stream.Collectors.toList;
45

56
import com.intellij.icons.AllIcons.Actions;
67
import com.intellij.openapi.application.ApplicationManager;
8+
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
79
import com.intellij.openapi.ui.MessageType;
10+
import com.intellij.openapi.ui.TextBrowseFolderListener;
11+
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
812
import com.intellij.openapi.ui.panel.ComponentPanelBuilder;
913
import com.intellij.openapi.util.io.FileUtil;
1014
import com.intellij.ui.PortField;
1115
import com.intellij.ui.TitledSeparator;
1216
import com.intellij.ui.components.JBLabel;
17+
import com.intellij.ui.components.JBRadioButton;
1318
import com.intellij.ui.components.JBTextField;
1419
import com.intellij.ui.components.fields.IntegerField;
1520
import com.intellij.util.ui.FormBuilder;
@@ -22,6 +27,7 @@
2227
import ee.carlrobert.codegpt.settings.state.LlamaSettingsState;
2328
import ee.carlrobert.codegpt.ui.OverlayUtil;
2429
import java.awt.BorderLayout;
30+
import java.awt.CardLayout;
2531
import java.io.File;
2632
import java.util.Arrays;
2733
import java.util.Collections;
@@ -34,12 +40,19 @@
3440

3541
public class LlamaServiceSelectionForm extends JPanel {
3642

43+
private static final String BUNDLED_SERVER_FORM_CARD_CODE = "BundledServerSettings";
44+
private static final String CUSTOM_SERVER_FORM_CARD_CODE = "CustomServerSettings";
45+
3746
private final LlamaModelPreferencesForm llamaModelPreferencesForm;
3847
private final LlamaRequestPreferencesForm llamaRequestPreferencesForm;
3948
private final PortField portField;
4049
private final IntegerField maxTokensField;
4150
private final IntegerField threadsField;
4251
private final JBTextField additionalParametersField;
52+
private final CardLayout cardLayout;
53+
private final JBRadioButton bundledServerRadioButton;
54+
private final JBRadioButton customServerRadioButton;
55+
private final TextFieldWithBrowseButton browsableCustomServerTextField;
4356

4457
public LlamaServiceSelectionForm() {
4558
var llamaServerAgent =
@@ -65,6 +78,15 @@ public LlamaServiceSelectionForm() {
6578
additionalParametersField = new JBTextField(llamaSettings.getAdditionalParameters(), 30);
6679
additionalParametersField.setEnabled(!serverRunning);
6780

81+
cardLayout = new CardLayout();
82+
bundledServerRadioButton = new JBRadioButton("Use bundled server",
83+
!llamaSettings.isUseCustomServer());
84+
customServerRadioButton = new JBRadioButton("Use custom server",
85+
llamaSettings.isUseCustomServer());
86+
browsableCustomServerTextField = createBrowsableCustomServerTextField(
87+
!llamaServerAgent.isServerRunning());
88+
browsableCustomServerTextField.setText(llamaSettings.getCustomLlamaServerPath());
89+
6890
init(llamaServerAgent);
6991
}
7092

@@ -89,6 +111,12 @@ private JComponent withEmptyLeftBorder(JComponent component) {
89111
return component;
90112
}
91113

114+
public String getActualServerPath() {
115+
return isUseCustomServer()
116+
? getCustomServerPath()
117+
: CodeGPTPlugin.getLlamaSourcePath() + File.separator + "server";
118+
}
119+
92120
public int getContextSize() {
93121
return maxTokensField.getValue();
94122
}
@@ -123,6 +151,22 @@ public List<String> getListOfAdditionalParameters() {
123151
.collect(toList());
124152
}
125153

154+
public void setIsUseCustomServer(boolean useCustomServer) {
155+
customServerRadioButton.setSelected(useCustomServer);
156+
}
157+
158+
public boolean isUseCustomServer() {
159+
return customServerRadioButton.isSelected();
160+
}
161+
162+
public void setCustomServerPath(String customServerPath) {
163+
browsableCustomServerTextField.setText(customServerPath);
164+
}
165+
166+
public String getCustomServerPath() {
167+
return browsableCustomServerTextField.getText();
168+
}
169+
126170
private void init(LlamaServerAgent llamaServerAgent) {
127171
var serverProgressPanel = new ServerProgressPanel();
128172
serverProgressPanel.setBorder(JBUI.Borders.emptyRight(16));
@@ -134,6 +178,7 @@ private void init(LlamaServerAgent llamaServerAgent) {
134178
.addComponent(new TitledSeparator(
135179
CodeGPTBundle.get("settingsConfigurable.service.llama.serverPreferences.title")))
136180
.addComponent(withEmptyLeftBorder(FormBuilder.createFormBuilder()
181+
.addComponent(getForm())
137182
.addLabeledComponent(
138183
CodeGPTBundle.get("shared.port"),
139184
JBUI.Panels.simplePanel()
@@ -165,6 +210,60 @@ private void init(LlamaServerAgent llamaServerAgent) {
165210
.getPanel());
166211
}
167212

213+
public JPanel getForm() {
214+
JPanel finalPanel = new JPanel(new BorderLayout());
215+
finalPanel.add(createRadioButtonsPanel(bundledServerRadioButton, customServerRadioButton),
216+
BorderLayout.NORTH);
217+
finalPanel.add(createFormPanelCards(), BorderLayout.CENTER);
218+
return finalPanel;
219+
}
220+
221+
private JPanel createFormPanelCards() {
222+
var formPanelCards = new JPanel(cardLayout);
223+
formPanelCards.setBorder(JBUI.Borders.emptyLeft(16));
224+
formPanelCards.add(new JPanel(), BUNDLED_SERVER_FORM_CARD_CODE);
225+
formPanelCards.add(createCustomServerForm(), CUSTOM_SERVER_FORM_CARD_CODE);
226+
cardLayout.show(
227+
formPanelCards,
228+
bundledServerRadioButton.isSelected()
229+
? BUNDLED_SERVER_FORM_CARD_CODE
230+
: CUSTOM_SERVER_FORM_CARD_CODE);
231+
232+
bundledServerRadioButton.addActionListener(e ->
233+
cardLayout.show(formPanelCards, BUNDLED_SERVER_FORM_CARD_CODE));
234+
customServerRadioButton.addActionListener(e ->
235+
cardLayout.show(formPanelCards, CUSTOM_SERVER_FORM_CARD_CODE));
236+
237+
return formPanelCards;
238+
}
239+
240+
private JPanel createCustomServerForm() {
241+
var customModelHelpText = ComponentPanelBuilder.createCommentComponent(
242+
CodeGPTBundle.get("settingsConfigurable.service.llama.customServerPath.comment"),
243+
true);
244+
customModelHelpText.setBorder(JBUI.Borders.empty(0, 4));
245+
246+
return FormBuilder.createFormBuilder()
247+
.addLabeledComponent(
248+
CodeGPTBundle.get("settingsConfigurable.service.llama.customServerPath.label"),
249+
browsableCustomServerTextField)
250+
.addComponentToRightColumn(customModelHelpText)
251+
.addVerticalGap(4)
252+
.addComponentFillVertically(new JPanel(), 0)
253+
.getPanel();
254+
}
255+
256+
private TextFieldWithBrowseButton createBrowsableCustomServerTextField(boolean enabled) {
257+
var browseButton = new TextFieldWithBrowseButton();
258+
browseButton.setEnabled(enabled);
259+
260+
var fileChooserDescriptor = FileChooserDescriptorFactory.createSingleFileDescriptor();
261+
fileChooserDescriptor.setForcedToUseIdeaFileChooser(true);
262+
fileChooserDescriptor.setHideIgnored(false);
263+
browseButton.addBrowseFolderListener(new TextBrowseFolderListener(fileChooserDescriptor));
264+
return browseButton;
265+
}
266+
168267
private JLabel createComment(String messageKey) {
169268
var comment = ComponentPanelBuilder.createCommentComponent(
170269
CodeGPTBundle.get(messageKey), true);
@@ -193,6 +292,8 @@ private JButton getServerButton(
193292
disableForm(serverButton, serverProgressPanel);
194293
llamaServerAgent.startAgent(
195294
new LlamaServerStartupParams(
295+
getActualServerPath(),
296+
isUseCustomServer(),
196297
llamaModelPreferencesForm.getActualModelPath(),
197298
getContextSize(),
198299
getThreads(),

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,4 +432,20 @@ public String getAdditionalParameters() {
432432
public void setAdditionalParameters(String additionalParameters) {
433433
llamaServiceSectionPanel.setAdditionalParameters(additionalParameters);
434434
}
435+
436+
public void setUseCustomLlamaServer(boolean useCustomLlamaServer) {
437+
llamaServiceSectionPanel.setIsUseCustomServer(useCustomLlamaServer);
438+
}
439+
440+
public boolean isUseCustomLlamaServer() {
441+
return llamaServiceSectionPanel.isUseCustomServer();
442+
}
443+
444+
public void setCustomLlamaServerPath(String serverPath) {
445+
llamaServiceSectionPanel.setCustomServerPath(serverPath);
446+
}
447+
448+
public String getCustomLlamaServerPath() {
449+
return llamaServiceSectionPanel.getCustomServerPath();
450+
}
435451
}

0 commit comments

Comments
 (0)