From 0722c985e7da7ac54afea109c7e4a4e6feda2b90 Mon Sep 17 00:00:00 2001 From: Jake Luciani Date: Wed, 16 Oct 2024 21:39:15 -0400 Subject: [PATCH 1/3] Set http port earlier in spring lifecycle --- .../jlama/cli/commands/ApiServiceCommand.java | 49 ++++++++++++------- 1 file changed, 32 insertions(+), 17 deletions(-) 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); From 6e3cbe89134fe0e873a18e6ace8b85de9742ffd6 Mon Sep 17 00:00:00 2001 From: Jake Luciani Date: Wed, 16 Oct 2024 21:42:58 -0400 Subject: [PATCH 2/3] Fix PR workflow --- .github/workflows/unit-tests.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/unit-tests.yaml b/.github/workflows/unit-tests.yaml index c066a07..55991da 100644 --- a/.github/workflows/unit-tests.yaml +++ b/.github/workflows/unit-tests.yaml @@ -1,8 +1,12 @@ name: Unit Test CI on: + pull_request_target: + types: [ opened, synchronize, reopened ] workflow_dispatch: push: + branches: + - main paths: - .github/workflows/unit-tests.yaml - '**.java' From 1864a0ecbd9ffae5ec1415279ac2fef72558660d Mon Sep 17 00:00:00 2001 From: Jake Luciani Date: Wed, 16 Oct 2024 21:45:42 -0400 Subject: [PATCH 3/3] Fix target --- .github/workflows/unit-tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/unit-tests.yaml b/.github/workflows/unit-tests.yaml index 55991da..ce18507 100644 --- a/.github/workflows/unit-tests.yaml +++ b/.github/workflows/unit-tests.yaml @@ -1,7 +1,7 @@ name: Unit Test CI on: - pull_request_target: + pull_request: types: [ opened, synchronize, reopened ] workflow_dispatch: push: