mirror of
https://github.com/carlrobertoh/ProxyAI.git
synced 2026-04-28 11:41:28 +00:00
* Initial implementation of integrating llama.cpp to run LLaMA models locally * Move submodule * Copy llama submodule to bundle * Support for downloading models from IDE * Code cleanup * Store port field * Replace service selection radio group with dropdown * Add quantization support + other fixes * Add option to override host * Fix override host handler * Disable port field when override host enabled * Design updates * Fix llama settings configuration, design changes, clean up code * Improve You.com coupon design * Add new Phind model and help tooltip * Fetch you.com subscription * Add CodeBooga model, fix downloadable model selection * Chat history support * Code refactoring, minor bug fixes * UI updates, several bug fixes, removed code llama python model * Code cleanup, enable llama port only on macOS * Change downloaded gguf models path * Move some of the labels to codegpt bundle * Minor fixes * Remove ToRA model, add help texts * Fix test * Modify description
98 lines
No EOL
3.6 KiB
Java
98 lines
No EOL
3.6 KiB
Java
package ee.carlrobert.codegpt.settings.service;
|
|
|
|
import static java.lang.String.format;
|
|
|
|
import com.intellij.openapi.actionSystem.AnAction;
|
|
import com.intellij.openapi.actionSystem.AnActionEvent;
|
|
import com.intellij.openapi.diagnostic.Logger;
|
|
import com.intellij.openapi.progress.ProgressIndicator;
|
|
import com.intellij.openapi.progress.ProgressManager;
|
|
import com.intellij.openapi.progress.Task;
|
|
import com.intellij.openapi.project.Project;
|
|
import ee.carlrobert.codegpt.CodeGPTBundle;
|
|
import ee.carlrobert.codegpt.completions.HuggingFaceModel;
|
|
import ee.carlrobert.codegpt.util.DownloadingUtils;
|
|
import ee.carlrobert.codegpt.util.file.FileUtils;
|
|
import java.io.IOException;
|
|
import java.net.URL;
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
import java.util.concurrent.ScheduledFuture;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.function.Consumer;
|
|
import javax.swing.DefaultComboBoxModel;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
public class DownloadModelAction extends AnAction {
|
|
|
|
private static final Logger LOG = Logger.getInstance(DownloadModelAction.class);
|
|
|
|
private final Consumer<ProgressIndicator> onDownload;
|
|
private final Runnable onDownloaded;
|
|
private final Consumer<Exception> onFailed;
|
|
private final Consumer<String> onUpdateProgress;
|
|
private final DefaultComboBoxModel<HuggingFaceModel> comboBoxModel;
|
|
|
|
public DownloadModelAction(
|
|
Consumer<ProgressIndicator> onDownload,
|
|
Runnable onDownloaded,
|
|
Consumer<Exception> onFailed,
|
|
Consumer<String> onUpdateProgress,
|
|
DefaultComboBoxModel<HuggingFaceModel> comboBoxModel) {
|
|
this.onDownload = onDownload;
|
|
this.onDownloaded = onDownloaded;
|
|
this.onFailed = onFailed;
|
|
this.onUpdateProgress = onUpdateProgress;
|
|
this.comboBoxModel = comboBoxModel;
|
|
}
|
|
|
|
@Override
|
|
public void actionPerformed(@NotNull AnActionEvent e) {
|
|
ProgressManager.getInstance().run(new DownloadBackgroundTask(e.getProject()));
|
|
}
|
|
|
|
class DownloadBackgroundTask extends Task.Backgroundable {
|
|
|
|
DownloadBackgroundTask(Project project) {
|
|
super(project, CodeGPTBundle.get("settingsConfigurable.service.llama.progress.downloadingModel.title"), true);
|
|
}
|
|
|
|
@Override
|
|
public void run(@NotNull ProgressIndicator indicator) {
|
|
var model = (HuggingFaceModel) comboBoxModel.getSelectedItem();
|
|
URL url = model.getFileURL();
|
|
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
|
ScheduledFuture<?> progressUpdateScheduler = null;
|
|
|
|
try {
|
|
onDownload.accept(indicator);
|
|
|
|
indicator.setIndeterminate(false);
|
|
indicator.setText(format(CodeGPTBundle.get("settingsConfigurable.service.llama.progress.downloadingModelIndicator.text"), model.getFileName()));
|
|
|
|
long fileSize = url.openConnection().getContentLengthLong();
|
|
long[] bytesRead = {0};
|
|
long startTime = System.currentTimeMillis();
|
|
|
|
progressUpdateScheduler = executorService.scheduleAtFixedRate(() ->
|
|
onUpdateProgress.accept(
|
|
DownloadingUtils.getFormattedDownloadProgress(startTime, fileSize, bytesRead[0])),
|
|
0, 1, TimeUnit.SECONDS);
|
|
FileUtils.copyFileWithProgress(model.getFileName(), url, bytesRead, fileSize, indicator);
|
|
} catch (IOException ex) {
|
|
LOG.error("Unable to open connection", ex);
|
|
onFailed.accept(ex);
|
|
} finally {
|
|
if (progressUpdateScheduler != null) {
|
|
progressUpdateScheduler.cancel(true);
|
|
}
|
|
executorService.shutdown();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onSuccess() {
|
|
onDownloaded.run();
|
|
}
|
|
}
|
|
} |