This repository was archived by the owner on Sep 27, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathkeys.R
More file actions
87 lines (84 loc) · 2.61 KB
/
Copy pathkeys.R
File metadata and controls
87 lines (84 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#' Save an API key
#'
#' Save an API key string to a config file for easier use later.
#'
#' @param key A string containing the API key
#' @param keyname An optional string containing the name of the key (to store
#' multiple keys for use with `load_key()`).
#' @export
#' @examples
#' save_key("MY-KEY-HERE", keyname = "openai")
save_key <- function(key, keyname = "key") {
config_dir <- rappdirs::user_config_dir("lmprompt", "R")
keyfile <- file.path(config_dir, paste0(keyname, ".rds"))
if (file.exists(keyfile)) {
saveRDS(key, keyfile)
cli::cli_alert_success("Keyfile updated.")
} else {
if (!dir.exists(config_dir)) dir.create(config_dir, recursive = TRUE)
saveRDS(key, keyfile)
cli::cli_alert_success("Keyfile created.")
}
}
#' Load an API key
#'
#' Load an API key string from a config file created by `save_key()`.
#'
#' @param keyname An optional string containing the name of the key to load.
#' (default = "key")
#' @param quietly An optional logical indicating whether to suppress the
#' successfully loaded message. (default = FALSE)
#' @return A string containing the loaded key.
#' @export
#' @examples
#' key <- load_key("openai")
#'
load_key <- function(keyname = "key", quietly = FALSE) {
config_dir <- rappdirs::user_config_dir("lmprompt", "R")
keyfile <- file.path(config_dir, paste0(keyname, ".rds"))
if (file.exists(keyfile)) {
key <- readRDS(keyfile)
if (quietly == FALSE) {
cli::cli_alert_success("Keyfile read successfully.")
}
} else {
key <- NULL
cli::cli_abort("No keys with that name were found.")
}
key
}
#' Find all saved API keys
#'
#' Find the names of all API keys that have been saved using `save_key()` so
#' that they can be loaded using `load_key()` or removed using `remove_key()`.
#'
#' @return A character vector containing all API keys found.
#' @export
#' @examples
#' find_keys()
#'
find_keys <- function() {
config_dir <- rappdirs::user_config_dir("lmprompt", "R")
keyfiles <- list.files(config_dir, ".rds$")
tools::file_path_sans_ext(keyfiles)
}
#' Remove a saved API key
#'
#' Delete from your computer the API key stored under `name`. Find all stored
#' API keys using `find_keys()`.
#'
#' @param keyname A string containing the name of the key to remove.
#' @export
#' @examples
#' remove_key("openai")
#'
remove_key <- function(keyname) {
config_dir <- rappdirs::user_config_dir("lmprompt", "R")
keyfile <- file.path(config_dir, paste0(keyname, ".rds"))
if (file.exists(keyfile)) {
file.remove(keyfile)
cli::cli_alert_success("Key successfully removed.")
} else {
cli::cli_abort("No keys with that name were found.")
}
}