From b86b137982601e9667679c3ec031c60d9d500ba0 Mon Sep 17 00:00:00 2001 From: lmangani Date: Sun, 29 Dec 2024 14:02:47 +0000 Subject: [PATCH] ENV support --- docs/README.md | 9 +++++++++ src/open_prompt_extension.cpp | 14 +++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index 0fa4564..30803ea 100644 --- a/docs/README.md +++ b/docs/README.md @@ -42,6 +42,15 @@ CREATE SECRET IF NOT EXISTS open_prompt ( ); ``` +Alternatively the following ENV variables can be used at runtime +``` + OPEN_PROMPT_API_URL + OPEN_PROMPT_API_TOKEN + OPEN_PROMPT_MODEL_NAME + OPEN_PROMPT_API_TIMEOUT +``` + + ### Usage ```sql D SELECT open_prompt('Write a one-line poem about ducks') AS response; diff --git a/src/open_prompt_extension.cpp b/src/open_prompt_extension.cpp index 99d8de5..bb09eb7 100644 --- a/src/open_prompt_extension.cpp +++ b/src/open_prompt_extension.cpp @@ -20,6 +20,9 @@ #define CPPHTTPLIB_OPENSSL_SUPPORT #include "httplib.hpp" +#include +#include +#include #include #include #include @@ -148,7 +151,16 @@ namespace duckdb { // Settings management static std::string GetConfigValue(ClientContext &context, const string &var_name, const string &default_value) { - // First try to get from secrets + + // Try environment variables + std::string env_var_name = "OPEN_PROMPT_" + var_name; + std::transform(env_var_name.begin(), env_var_name.end(), env_var_name.begin(), ::toupper); + const char* env_value = std::getenv(env_var_name.c_str()); + if (env_value != nullptr && strlen(env_value) > 0) { + return std::string(env_value); + } + + // Try to get from secrets auto &secret_manager = SecretManager::Get(context); try { auto transaction = CatalogTransaction::GetSystemCatalogTransaction(context);