Skip to content

Commit

Permalink
Add setting for custom LLama server executable (carlrobertoh#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilKes authored Jan 30, 2024
1 parent 9ad12f8 commit 390d8cd
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,19 @@ public void startAgent(
Runnable onSuccess,
Runnable onServerTerminated) {
ApplicationManager.getApplication().invokeLater(() -> {
try {
serverProgressPanel.updateText(
CodeGPTBundle.get("llamaServerAgent.buildingProject.description"));
makeProcessHandler = new OSProcessHandler(getMakeCommandLinde());
makeProcessHandler.addProcessListener(
getMakeProcessListener(params, serverProgressPanel, onSuccess, onServerTerminated));
makeProcessHandler.startNotify();
} catch (ExecutionException e) {
throw new RuntimeException(e);
if (!params.isUseCustomServer()) {
try {
serverProgressPanel.updateText(
CodeGPTBundle.get("llamaServerAgent.buildingProject.description"));
makeProcessHandler = new OSProcessHandler(getMakeCommandLinde());
makeProcessHandler.addProcessListener(
getMakeProcessListener(params, serverProgressPanel, onSuccess, onServerTerminated));
makeProcessHandler.startNotify();
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
} else {
startServer(params, serverProgressPanel, onSuccess, onServerTerminated);
}
});
}
Expand Down Expand Up @@ -79,23 +83,31 @@ public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType

@Override
public void processTerminated(@NotNull ProcessEvent event) {
try {
LOG.info("Booting up llama server");

serverProgressPanel.updateText(
CodeGPTBundle.get("llamaServerAgent.serverBootup.description"));
startServerProcessHandler = new OSProcessHandler.Silent(getServerCommandLine(params));
startServerProcessHandler.addProcessListener(
getProcessListener(params.getPort(), onSuccess, onServerTerminated));
startServerProcessHandler.startNotify();
} catch (ExecutionException ex) {
LOG.error("Unable to start llama server", ex);
throw new RuntimeException(ex);
}
startServer(params, serverProgressPanel, onSuccess, onServerTerminated);
}
};
}

private void startServer(
LlamaServerStartupParams params,
ServerProgressPanel serverProgressPanel,
Runnable onSuccess,
Runnable onServerTerminated) {
try {
LOG.info("Booting up llama server");

serverProgressPanel.updateText(
CodeGPTBundle.get("llamaServerAgent.serverBootup.description"));
startServerProcessHandler = new OSProcessHandler.Silent(getServerCommandLine(params));
startServerProcessHandler.addProcessListener(
getProcessListener(params.getPort(), onSuccess, onServerTerminated));
startServerProcessHandler.startNotify();
} catch (ExecutionException ex) {
LOG.error("Unable to start llama server", ex);
throw new RuntimeException(ex);
}
}

private ProcessListener getProcessListener(
int port,
Runnable onSuccess,
Expand Down Expand Up @@ -152,8 +164,8 @@ private static GeneralCommandLine getMakeCommandLinde() {

private GeneralCommandLine getServerCommandLine(LlamaServerStartupParams params) {
GeneralCommandLine commandLine = new GeneralCommandLine().withCharset(StandardCharsets.UTF_8);
commandLine.setExePath("./server");
commandLine.withWorkDirectory(CodeGPTPlugin.getLlamaSourcePath());
commandLine.setExePath("./" + params.getServerFileName());
commandLine.withWorkDirectory(params.getServerDirectory());
commandLine.addParameters(
"-m", params.getModelPath(),
"-c", String.valueOf(params.getContextLength()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@
package ee.carlrobert.codegpt.completions.llama;

import java.io.File;
import java.util.List;

public class LlamaServerStartupParams {

private final String serverPath;
private final boolean useCustomServer;
private final String modelPath;
private final int contextLength;
private final int threads;
private final int port;
private final List<String> additionalParameters;

public LlamaServerStartupParams(
String modelPath,
String serverPath,
boolean useCustomServer, String modelPath,
int contextLength,
int threads,
int port,
List<String> additionalParameters) {
this.serverPath = serverPath;
this.useCustomServer = useCustomServer;
this.modelPath = modelPath;
this.contextLength = contextLength;
this.threads = threads;
this.port = port;
this.additionalParameters = additionalParameters;
}

public String getServerPath() {
return serverPath;
}

public String getServerFileName() {
return serverPath.substring(serverPath.lastIndexOf(File.separator) + 1);
}

public String getServerDirectory() {
return serverPath.substring(0, serverPath.lastIndexOf(File.separator) + 1);
}

public boolean isUseCustomServer() {
return useCustomServer;
}

public String getModelPath() {
return modelPath;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ee.carlrobert.codegpt.settings.service;

import static ee.carlrobert.codegpt.ui.UIUtil.createRadioButtonsPanel;
import static java.lang.String.format;
import static java.util.stream.Collectors.toList;

Expand Down Expand Up @@ -151,7 +152,8 @@ public LlamaModelPreferencesForm() {

public JPanel getForm() {
JPanel finalPanel = new JPanel(new BorderLayout());
finalPanel.add(createRadioButtonsPanel(), BorderLayout.NORTH);
finalPanel.add(createRadioButtonsPanel(predefinedModelRadioButton, customModelRadioButton),
BorderLayout.NORTH);
finalPanel.add(createFormPanelCards(), BorderLayout.CENTER);
return finalPanel;
}
Expand Down Expand Up @@ -227,20 +229,6 @@ private JPanel createFormPanelCards() {
return formPanelCards;
}

private JPanel createRadioButtonsPanel() {
var buttonGroup = new ButtonGroup();
buttonGroup.add(predefinedModelRadioButton);
buttonGroup.add(customModelRadioButton);

var radioPanel = new JPanel();
radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.PAGE_AXIS));
radioPanel.add(predefinedModelRadioButton);
radioPanel.add(Box.createVerticalStrut(4));
radioPanel.add(customModelRadioButton);
radioPanel.add(Box.createVerticalStrut(8));
return radioPanel;
}

private JPanel createCustomModelForm() {
var customModelHelpText = ComponentPanelBuilder.createCommentComponent(
CodeGPTBundle.get("settingsConfigurable.service.llama.customModelPath.comment"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package ee.carlrobert.codegpt.settings.service;

import static ee.carlrobert.codegpt.ui.UIUtil.createRadioButtonsPanel;
import static java.util.stream.Collectors.toList;

import com.intellij.icons.AllIcons.Actions;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.TextBrowseFolderListener;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.ui.panel.ComponentPanelBuilder;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.ui.PortField;
import com.intellij.ui.TitledSeparator;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBRadioButton;
import com.intellij.ui.components.JBTextField;
import com.intellij.ui.components.fields.IntegerField;
import com.intellij.util.ui.FormBuilder;
Expand All @@ -22,6 +27,7 @@
import ee.carlrobert.codegpt.settings.state.LlamaSettingsState;
import ee.carlrobert.codegpt.ui.OverlayUtil;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -34,12 +40,19 @@

public class LlamaServiceSelectionForm extends JPanel {

private static final String BUNDLED_SERVER_FORM_CARD_CODE = "BundledServerSettings";
private static final String CUSTOM_SERVER_FORM_CARD_CODE = "CustomServerSettings";

private final LlamaModelPreferencesForm llamaModelPreferencesForm;
private final LlamaRequestPreferencesForm llamaRequestPreferencesForm;
private final PortField portField;
private final IntegerField maxTokensField;
private final IntegerField threadsField;
private final JBTextField additionalParametersField;
private final CardLayout cardLayout;
private final JBRadioButton bundledServerRadioButton;
private final JBRadioButton customServerRadioButton;
private final TextFieldWithBrowseButton browsableCustomServerTextField;

public LlamaServiceSelectionForm() {
var llamaServerAgent =
Expand All @@ -65,6 +78,15 @@ public LlamaServiceSelectionForm() {
additionalParametersField = new JBTextField(llamaSettings.getAdditionalParameters(), 30);
additionalParametersField.setEnabled(!serverRunning);

cardLayout = new CardLayout();
bundledServerRadioButton = new JBRadioButton("Use bundled server",
!llamaSettings.isUseCustomServer());
customServerRadioButton = new JBRadioButton("Use custom server",
llamaSettings.isUseCustomServer());
browsableCustomServerTextField = createBrowsableCustomServerTextField(
!llamaServerAgent.isServerRunning());
browsableCustomServerTextField.setText(llamaSettings.getCustomLlamaServerPath());

init(llamaServerAgent);
}

Expand All @@ -89,6 +111,12 @@ private JComponent withEmptyLeftBorder(JComponent component) {
return component;
}

public String getActualServerPath() {
return isUseCustomServer()
? getCustomServerPath()
: CodeGPTPlugin.getLlamaSourcePath() + File.separator + "server";
}

public int getContextSize() {
return maxTokensField.getValue();
}
Expand Down Expand Up @@ -123,6 +151,22 @@ public List<String> getListOfAdditionalParameters() {
.collect(toList());
}

public void setIsUseCustomServer(boolean useCustomServer) {
customServerRadioButton.setSelected(useCustomServer);
}

public boolean isUseCustomServer() {
return customServerRadioButton.isSelected();
}

public void setCustomServerPath(String customServerPath) {
browsableCustomServerTextField.setText(customServerPath);
}

public String getCustomServerPath() {
return browsableCustomServerTextField.getText();
}

private void init(LlamaServerAgent llamaServerAgent) {
var serverProgressPanel = new ServerProgressPanel();
serverProgressPanel.setBorder(JBUI.Borders.emptyRight(16));
Expand All @@ -134,6 +178,7 @@ private void init(LlamaServerAgent llamaServerAgent) {
.addComponent(new TitledSeparator(
CodeGPTBundle.get("settingsConfigurable.service.llama.serverPreferences.title")))
.addComponent(withEmptyLeftBorder(FormBuilder.createFormBuilder()
.addComponent(getForm())
.addLabeledComponent(
CodeGPTBundle.get("shared.port"),
JBUI.Panels.simplePanel()
Expand Down Expand Up @@ -165,6 +210,60 @@ private void init(LlamaServerAgent llamaServerAgent) {
.getPanel());
}

public JPanel getForm() {
JPanel finalPanel = new JPanel(new BorderLayout());
finalPanel.add(createRadioButtonsPanel(bundledServerRadioButton, customServerRadioButton),
BorderLayout.NORTH);
finalPanel.add(createFormPanelCards(), BorderLayout.CENTER);
return finalPanel;
}

private JPanel createFormPanelCards() {
var formPanelCards = new JPanel(cardLayout);
formPanelCards.setBorder(JBUI.Borders.emptyLeft(16));
formPanelCards.add(new JPanel(), BUNDLED_SERVER_FORM_CARD_CODE);
formPanelCards.add(createCustomServerForm(), CUSTOM_SERVER_FORM_CARD_CODE);
cardLayout.show(
formPanelCards,
bundledServerRadioButton.isSelected()
? BUNDLED_SERVER_FORM_CARD_CODE
: CUSTOM_SERVER_FORM_CARD_CODE);

bundledServerRadioButton.addActionListener(e ->
cardLayout.show(formPanelCards, BUNDLED_SERVER_FORM_CARD_CODE));
customServerRadioButton.addActionListener(e ->
cardLayout.show(formPanelCards, CUSTOM_SERVER_FORM_CARD_CODE));

return formPanelCards;
}

private JPanel createCustomServerForm() {
var customModelHelpText = ComponentPanelBuilder.createCommentComponent(
CodeGPTBundle.get("settingsConfigurable.service.llama.customServerPath.comment"),
true);
customModelHelpText.setBorder(JBUI.Borders.empty(0, 4));

return FormBuilder.createFormBuilder()
.addLabeledComponent(
CodeGPTBundle.get("settingsConfigurable.service.llama.customServerPath.label"),
browsableCustomServerTextField)
.addComponentToRightColumn(customModelHelpText)
.addVerticalGap(4)
.addComponentFillVertically(new JPanel(), 0)
.getPanel();
}

private TextFieldWithBrowseButton createBrowsableCustomServerTextField(boolean enabled) {
var browseButton = new TextFieldWithBrowseButton();
browseButton.setEnabled(enabled);

var fileChooserDescriptor = FileChooserDescriptorFactory.createSingleFileDescriptor();
fileChooserDescriptor.setForcedToUseIdeaFileChooser(true);
fileChooserDescriptor.setHideIgnored(false);
browseButton.addBrowseFolderListener(new TextBrowseFolderListener(fileChooserDescriptor));
return browseButton;
}

private JLabel createComment(String messageKey) {
var comment = ComponentPanelBuilder.createCommentComponent(
CodeGPTBundle.get(messageKey), true);
Expand Down Expand Up @@ -193,6 +292,8 @@ private JButton getServerButton(
disableForm(serverButton, serverProgressPanel);
llamaServerAgent.startAgent(
new LlamaServerStartupParams(
getActualServerPath(),
isUseCustomServer(),
llamaModelPreferencesForm.getActualModelPath(),
getContextSize(),
getThreads(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,4 +432,20 @@ public String getAdditionalParameters() {
public void setAdditionalParameters(String additionalParameters) {
llamaServiceSectionPanel.setAdditionalParameters(additionalParameters);
}

public void setUseCustomLlamaServer(boolean useCustomLlamaServer) {
llamaServiceSectionPanel.setIsUseCustomServer(useCustomLlamaServer);
}

public boolean isUseCustomLlamaServer() {
return llamaServiceSectionPanel.isUseCustomServer();
}

public void setCustomLlamaServerPath(String serverPath) {
llamaServiceSectionPanel.setCustomServerPath(serverPath);
}

public String getCustomLlamaServerPath() {
return llamaServiceSectionPanel.getCustomServerPath();
}
}
Loading

0 comments on commit 390d8cd

Please sign in to comment.