diff --git a/docs/README.md b/docs/README.md index 07dea1f..15a813b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -28,7 +28,14 @@ Setup the completions API configuration w/ optional auth token and model name SET VARIABLE openprompt_api_url = 'http://localhost:11434/v1/chat/completions'; SET VARIABLE openprompt_api_token = 'your_api_key_here'; SET VARIABLE openprompt_model_name = 'qwen2.5:0.5b'; +``` +For persistent usage, configure parameters using DuckDB `SECRETS` +``` +CREATE SECRET 'openprompt_api_url' 'open_prompt' AS 'open_prompt' ('url'='http://localhost:11434/v1/chat/completions'); +CREATE SECRET 'openprompt_api_token' 'open_prompt' AS 'open_prompt' ('token'='your_api_token'); +CREATE SECRET 'openprompt_model_name' 'open_prompt' AS 'open_prompt' ('name'='qwen2.5:0.5b'); +CREATE SECRET 'openprompt_api_timeout' 'open_prompt' AS 'open_prompt' ('timeout'='20'); ``` ### Usage diff --git a/src/open_prompt_extension.cpp b/src/open_prompt_extension.cpp index d4c07e1..05d0a2b 100644 --- a/src/open_prompt_extension.cpp +++ b/src/open_prompt_extension.cpp @@ -6,6 +6,7 @@ #include "duckdb/common/atomic.hpp" #include "duckdb/common/exception/http_exception.hpp" #include +#include "duckdb/main/secret/secret_manager.hpp" #ifdef USE_ZLIB #define CPPHTTPLIB_ZLIB_SUPPORT @@ -141,7 +142,24 @@ namespace duckdb { } // Settings management - static std::string GetConfigValue(ClientContext &context, const string &var_name, const string &default_value) { + static std::string GetConfigValue(ClientContext &context, const std::string &var_name, const std::string &default_value) { + auto &secret_manager = SecretManager::Get(context); + auto transaction = CatalogTransaction::GetSystemCatalogTransaction(context); + auto secret_match = secret_manager.LookupSecret(transaction, "openprompt", var_name); + + if (secret_match.HasMatch()) { + auto &secret = secret_match.GetSecret(); + if (secret.GetType() == "open_prompt") { + const auto *kv_secret = dynamic_cast(&secret); + if (kv_secret) { + Value secret_value; + if (kv_secret->TryGetValue(var_name, secret_value)) { + return secret_value.ToString(); + } + } + } + } + Value value; auto &config = ClientConfig::GetConfig(context); if (!config.GetUserVariable(var_name, value) || value.IsNull()) {