Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SECRETS Support #19

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion src/open_prompt_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "duckdb/common/atomic.hpp"
#include "duckdb/common/exception/http_exception.hpp"
#include <duckdb/parser/parsed_data/create_scalar_function_info.hpp>
#include "duckdb/main/secret/secret_manager.hpp"

#ifdef USE_ZLIB
#define CPPHTTPLIB_ZLIB_SUPPORT
Expand Down Expand Up @@ -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<const KeyValueSecret*>(&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()) {
Expand Down
Loading