From b65aeb5682565887d08608244126a71f7baafbb5 Mon Sep 17 00:00:00 2001 From: Samuelson Date: Sat, 5 Jul 2025 09:05:45 -0400 Subject: [PATCH] add properties file --- .../src/main/java/AnswerService.java | 2 +- javafx-example/src/main/java/ApiKeys.java | 48 +++++++++++++++++-- .../src/main/resources/application.properties | 1 + 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 javafx-example/src/main/resources/application.properties diff --git a/javafx-example/src/main/java/AnswerService.java b/javafx-example/src/main/java/AnswerService.java index 8701f55e..7e30fd99 100644 --- a/javafx-example/src/main/java/AnswerService.java +++ b/javafx-example/src/main/java/AnswerService.java @@ -20,7 +20,7 @@ public void init(SearchAction action) { private void initChat(SearchAction action) { StreamingChatModel model = OpenAiStreamingChatModel.builder() - .apiKey(ApiKeys.OPENAI_API_KEY) + .apiKey(ApiKeys.getOpenAiApiKey()) .modelName(GPT_4_O_MINI) .build(); diff --git a/javafx-example/src/main/java/ApiKeys.java b/javafx-example/src/main/java/ApiKeys.java index 96d8b802..a02954b6 100644 --- a/javafx-example/src/main/java/ApiKeys.java +++ b/javafx-example/src/main/java/ApiKeys.java @@ -1,6 +1,48 @@ +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + public class ApiKeys { - // You can use "demo" api key for demonstration purposes. - // You can get your own OpenAI API key here: https://platform.openai.com/account/api-keys - public static final String OPENAI_API_KEY = "demo"; + private static final Logger LOGGER = LogManager.getLogger(ApiKeys.class); + private static final Properties properties = new Properties(); + + static { + try (InputStream input = ApiKeys.class.getClassLoader() + .getResourceAsStream("application.properties")) { + + if (input == null) { + String message = "Could not find 'application.properties' in the classpath (resources folder)."; + LOGGER.error(message); + throw new RuntimeException(message); + } + + properties.load(input); + LOGGER.info("'application.properties' loaded successfully."); + + } catch (IOException e) { + String message = "Failed to load 'application.properties' file."; + LOGGER.error(message, e); + throw new RuntimeException(message, e); + } + } + + public static String get(String key) { + return properties.getProperty(key); + } + + public static String getOpenAiApiKey() { + String apiKey = get("openai.api.key"); + + if (apiKey == null || apiKey.isBlank()) { + String message = "'openai.api.key' is missing or empty in 'application.properties'. Please define it to proceed."; + LOGGER.error(message); + throw new IllegalStateException(message); + } + + return apiKey; + } } \ No newline at end of file diff --git a/javafx-example/src/main/resources/application.properties b/javafx-example/src/main/resources/application.properties new file mode 100644 index 00000000..1fedd8e6 --- /dev/null +++ b/javafx-example/src/main/resources/application.properties @@ -0,0 +1 @@ +openai.api.key="YOUR_OPENAI_API_KEY" \ No newline at end of file