Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion javafx-example/src/main/java/AnswerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
48 changes: 45 additions & 3 deletions javafx-example/src/main/java/ApiKeys.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
1 change: 1 addition & 0 deletions javafx-example/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
openai.api.key="YOUR_OPENAI_API_KEY"