|
2 | 2 |
|
3 | 3 | import static io.quarkiverse.langchain4j.deployment.LangChain4jDotNames.CHAT_MODEL; |
4 | 4 |
|
| 5 | +import java.io.IOException; |
| 6 | +import java.math.BigDecimal; |
| 7 | +import java.math.RoundingMode; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.util.ArrayList; |
5 | 11 | import java.util.List; |
| 12 | +import java.util.Optional; |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | +import java.util.concurrent.atomic.AtomicReference; |
6 | 15 |
|
7 | 16 | import jakarta.enterprise.context.ApplicationScoped; |
8 | 17 |
|
| 18 | +import org.jboss.logging.Logger; |
| 19 | + |
9 | 20 | import io.quarkiverse.langchain4j.deployment.items.ChatModelProviderCandidateBuildItem; |
10 | 21 | import io.quarkiverse.langchain4j.deployment.items.SelectedChatModelProviderBuildItem; |
| 22 | +import io.quarkiverse.langchain4j.gpullama3.GPULlama3ModelRegistry; |
11 | 23 | import io.quarkiverse.langchain4j.gpullama3.runtime.GPULlama3Recorder; |
| 24 | +import io.quarkiverse.langchain4j.gpullama3.runtime.NameAndQuantization; |
| 25 | +import io.quarkiverse.langchain4j.gpullama3.runtime.config.ChatModelFixedRuntimeConfig; |
| 26 | +import io.quarkiverse.langchain4j.gpullama3.runtime.config.LangChain4jGPULlama3FixedRuntimeConfig; |
| 27 | +import io.quarkiverse.langchain4j.runtime.NamedConfigUtil; |
12 | 28 | import io.quarkus.arc.deployment.SyntheticBeanBuildItem; |
13 | | -import io.quarkus.deployment.annotations.BuildProducer; |
14 | | -import io.quarkus.deployment.annotations.BuildStep; |
15 | | -import io.quarkus.deployment.annotations.ExecutionTime; |
| 29 | +import io.quarkus.builder.item.MultiBuildItem; |
| 30 | +import io.quarkus.deployment.annotations.*; |
16 | 31 | import io.quarkus.deployment.annotations.Record; |
17 | 32 | import io.quarkus.deployment.builditem.FeatureBuildItem; |
| 33 | +import io.quarkus.deployment.builditem.LaunchModeBuildItem; |
| 34 | +import io.quarkus.deployment.builditem.ServiceStartBuildItem; |
| 35 | +import io.quarkus.deployment.console.ConsoleInstalledBuildItem; |
| 36 | +import io.quarkus.deployment.console.StartupLogCompressor; |
| 37 | +import io.quarkus.deployment.logging.LoggingSetupBuildItem; |
18 | 38 |
|
19 | 39 | public class GPULlama3Processor { |
20 | 40 |
|
| 41 | + private final static Logger LOG = Logger.getLogger(GPULlama3Processor.class); |
| 42 | + |
21 | 43 | private static final String PROVIDER = "gpu-llama3"; |
22 | 44 | private static final String FEATURE = "langchain4j-gpu-llama3"; |
23 | 45 |
|
@@ -55,4 +77,138 @@ void generateBeans(GPULlama3Recorder recorder, |
55 | 77 | } |
56 | 78 | } |
57 | 79 | } |
| 80 | + |
| 81 | + @SuppressWarnings("OptionalUsedAsFieldOrParameterType") |
| 82 | + @Produce(ServiceStartBuildItem.class) |
| 83 | + @BuildStep |
| 84 | + void downloadModels(List<SelectedChatModelProviderBuildItem> selectedChatModels, |
| 85 | + LoggingSetupBuildItem loggingSetupBuildItem, |
| 86 | + Optional<ConsoleInstalledBuildItem> consoleInstalledBuildItem, |
| 87 | + LaunchModeBuildItem launchMode, |
| 88 | + LangChain4jGPULlama3BuildTimeConfig buildTimeConfig, |
| 89 | + LangChain4jGPULlama3FixedRuntimeConfig fixedRuntimeConfig, |
| 90 | + BuildProducer<ModelDownloadedBuildItem> modelDownloadedProducer) { |
| 91 | + if (!buildTimeConfig.includeModelsInArtifact()) { |
| 92 | + return; |
| 93 | + } |
| 94 | + GPULlama3ModelRegistry registry = GPULlama3ModelRegistry.getOrCreate(fixedRuntimeConfig.modelsPath()); |
| 95 | + |
| 96 | + BigDecimal ONE_HUNDRED = new BigDecimal("100"); |
| 97 | + |
| 98 | + if (buildTimeConfig.chatModel().enabled().orElse(true)) { |
| 99 | + List<NameAndQuantization> modelsNeeded = new ArrayList<>(); |
| 100 | + for (var selected : selectedChatModels) { |
| 101 | + if (PROVIDER.equals(selected.getProvider())) { |
| 102 | + String configName = selected.getConfigName(); |
| 103 | + |
| 104 | + ChatModelFixedRuntimeConfig matchingConfig = NamedConfigUtil.isDefault(configName) |
| 105 | + ? fixedRuntimeConfig.defaultConfig().chatModel() |
| 106 | + : fixedRuntimeConfig.namedConfig().get(configName).chatModel(); |
| 107 | + modelsNeeded.add(new NameAndQuantization(matchingConfig.modelName(), matchingConfig.quantization())); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (!modelsNeeded.isEmpty()) { |
| 112 | + StartupLogCompressor compressor = new StartupLogCompressor( |
| 113 | + (launchMode.isTest() ? "(test) " : "") + "GPULlama3.java model pull:", |
| 114 | + consoleInstalledBuildItem, |
| 115 | + loggingSetupBuildItem); |
| 116 | + |
| 117 | + for (var model : modelsNeeded) { |
| 118 | + GPULlama3ModelRegistry.ModelInfo modelInfo = GPULlama3ModelRegistry.ModelInfo.from(model.name()); |
| 119 | + Path pathOfModelDirOnDisk = registry.constructModelDirectoryPath(modelInfo); |
| 120 | + // Check if the model is already downloaded |
| 121 | + // this is done automatically by download model, but we want to provide a good progress experience, so we do it again here |
| 122 | + if (Files.exists(pathOfModelDirOnDisk.resolve(GPULlama3ModelRegistry.FINISHED_MARKER))) { |
| 123 | + LOG.debug("Model " + model.name() + "already exists in " + pathOfModelDirOnDisk); |
| 124 | + } else { |
| 125 | + // we pull one model at a time and provide progress updates to the user via logging |
| 126 | + LOG.info("Pulling model " + model.name()); |
| 127 | + |
| 128 | + AtomicReference<Long> LAST_UPDATE_REF = new AtomicReference<>(); |
| 129 | + |
| 130 | + try { |
| 131 | + registry.downloadModel(model.name(), model.quantization(), Optional.empty(), |
| 132 | + Optional.of(new GPULlama3ModelRegistry.ProgressReporter() { |
| 133 | + @Override |
| 134 | + public void update(String filename, long sizeDownloaded, long totalSize) { |
| 135 | + // Jlama downloads a bunch of files for each mode of which only the |
| 136 | + // weights file is large |
| 137 | + // and makes sense to report progress on |
| 138 | + if (totalSize < 100_000) { |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + if (!logUpdate(LAST_UPDATE_REF.get())) { |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + LAST_UPDATE_REF.set(System.nanoTime()); |
| 147 | + |
| 148 | + BigDecimal percentage = new BigDecimal(sizeDownloaded) |
| 149 | + .divide(new BigDecimal(totalSize), |
| 150 | + 4, |
| 151 | + RoundingMode.HALF_DOWN) |
| 152 | + .multiply(ONE_HUNDRED); |
| 153 | + BigDecimal progress = percentage.setScale(2, RoundingMode.HALF_DOWN); |
| 154 | + if (progress.compareTo(ONE_HUNDRED) >= 0) { |
| 155 | + // avoid showing 100% for too long |
| 156 | + LOG.infof("Verifying and cleaning up\n", progress); |
| 157 | + } else { |
| 158 | + LOG.infof("%s - Progress: %s%%\n", model.name(), progress); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @param lastUpdate The last update time in nanoseconds |
| 164 | + * Determines whether we should log an update. |
| 165 | + * This is done in order to not overwhelm the console with updates which might |
| 166 | + * make |
| 167 | + * canceling the download difficult. See |
| 168 | + * <a href= |
| 169 | + * "https://github.com/quarkiverse/quarkus-langchain4j/issues/1044">this</a> |
| 170 | + */ |
| 171 | + private boolean logUpdate(Long lastUpdate) { |
| 172 | + if (lastUpdate == null) { |
| 173 | + return true; |
| 174 | + } else { |
| 175 | + return TimeUnit.NANOSECONDS.toMillis(System.nanoTime()) |
| 176 | + - TimeUnit.NANOSECONDS.toMillis(lastUpdate) > 1_000; |
| 177 | + } |
| 178 | + } |
| 179 | + })); |
| 180 | + } catch (IOException e) { |
| 181 | + compressor.closeAndDumpCaptured(); |
| 182 | + } catch (InterruptedException e) { |
| 183 | + throw new RuntimeException(e); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + modelDownloadedProducer.produce(new ModelDownloadedBuildItem(model, pathOfModelDirOnDisk)); |
| 188 | + } |
| 189 | + |
| 190 | + compressor.close(); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + } |
| 195 | + |
| 196 | + public static final class ModelDownloadedBuildItem extends MultiBuildItem { |
| 197 | + |
| 198 | + private final NameAndQuantization model; |
| 199 | + private final Path directory; |
| 200 | + |
| 201 | + public ModelDownloadedBuildItem(NameAndQuantization model, Path directory) { |
| 202 | + this.model = model; |
| 203 | + this.directory = directory; |
| 204 | + } |
| 205 | + |
| 206 | + public NameAndQuantization getModel() { |
| 207 | + return model; |
| 208 | + } |
| 209 | + |
| 210 | + public Path getDirectory() { |
| 211 | + return directory; |
| 212 | + } |
| 213 | + } |
58 | 214 | } |
0 commit comments