diff --git a/.github/workflows/unit-tests.yaml b/.github/workflows/unit-tests.yaml index c066a07..ce18507 100644 --- a/.github/workflows/unit-tests.yaml +++ b/.github/workflows/unit-tests.yaml @@ -1,8 +1,12 @@ name: Unit Test CI on: + pull_request: + types: [ opened, synchronize, reopened ] workflow_dispatch: push: + branches: + - main paths: - .github/workflows/unit-tests.yaml - '**.java' diff --git a/jlama-cli/src/main/java/com/github/tjake/jlama/cli/commands/ApiServiceCommand.java b/jlama-cli/src/main/java/com/github/tjake/jlama/cli/commands/ApiServiceCommand.java index 0b5d40c..e6a4039 100644 --- a/jlama-cli/src/main/java/com/github/tjake/jlama/cli/commands/ApiServiceCommand.java +++ b/jlama-cli/src/main/java/com/github/tjake/jlama/cli/commands/ApiServiceCommand.java @@ -18,6 +18,8 @@ import static com.github.tjake.jlama.model.ModelSupport.loadModel; import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; import java.util.Optional; import com.github.tjake.jlama.model.functions.Generator; @@ -26,8 +28,14 @@ import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.server.ConfigurableWebServerFactory; +import org.springframework.boot.web.server.WebServerFactoryCustomizer; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import picocli.CommandLine; @@ -59,29 +67,36 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) { public void run() { try { Path modelPath = SimpleBaseCommand.getModel( - modelName, - modelDirectory, - downloadSection.autoDownload, - downloadSection.branch, - downloadSection.authToken - ); + modelName, + modelDirectory, + downloadSection.autoDownload, + downloadSection.branch, + downloadSection.authToken); m = loadModel( - modelPath.toFile(), - workingDirectory, - advancedSection.workingMemoryType, - advancedSection.workingQuantizationType, - Optional.ofNullable(advancedSection.modelQuantization), - Optional.ofNullable(advancedSection.threadCount) - ); + modelPath.toFile(), + workingDirectory, + advancedSection.workingMemoryType, + advancedSection.workingQuantizationType, + Optional.ofNullable(advancedSection.modelQuantization), + Optional.ofNullable(advancedSection.threadCount)); System.out.println("Chat UI: http://localhost:" + port); System.out.println("OpenAI Chat API: http://localhost:" + port + "/chat/completions"); - new SpringApplicationBuilder(ApiServiceCommand.class).lazyInitialization(true) - .properties("server.port", "" + port, "logging.level.org.springframework.web", "info") - .build() - .run(); + // Use SpringApplicationBuilder with ApplicationContextInitializer to set the port dynamically + new SpringApplicationBuilder(ApiServiceCommand.class) + .initializers(applicationContext -> { + ConfigurableEnvironment environment = applicationContext.getEnvironment(); + Map props = new HashMap<>(); + props.put("server.port", port); // Set the port here before the server starts + environment.getPropertySources().addFirst(new MapPropertySource("customProps", props)); + }) + .properties("logging.level.org.springframework.web", "info") + .lazyInitialization(true) + .build() + .run(); + } catch (Exception e) { e.printStackTrace(); System.exit(2);