-
Notifications
You must be signed in to change notification settings - Fork 26
/
config.R
113 lines (107 loc) · 4.05 KB
/
config.R
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#' Save user configuration settings for gpttools
#'
#' This function saves the user's configuration settings for gpttools service
#' to a YAML file.
#' @param service The name of the service to use, default is "openai".
#' @param model The model to use, default is "gpt-4-1106-preview".
#' @param task The task to perform, default is "Permissive Chat".
#' @param local_embed Whether to use local embedding model. Default is FALSE.
#' @param openai_embed_model The OpenAI embeddings model to use, default is
#' "text-embedding-3-small".
#' @param local_embed_model The local embeddings model to use, default is
#' "BAAI/bge-small-en-v1.5".
#' @param k_context The amount of context to keep, default is 4.
#' @param k_history The amount of history to keep, default is 4.
#' @param save_history Logical indicating whether history should be saved,
#' default is TRUE.
#' @param sources The sources to use, default is "All".
#' @param run_code Whether to execute generated code with `reprex::reprex()`,
#' default is FALSE.
#' @param add_context Whether to add context to the query. Options are
#' `"always"`, `"sometimes"`, and `"never"`. The default is `"sometimes"`.
#' @param persist Logical indicating whether to persist the settings, default
#' is TRUE.
#' @return Invisible NULL.
#' @export
save_user_config <- function(service = "openai",
model = "gpt-4-turbo-preview",
task = "Permissive Chat",
local_embed = FALSE,
openai_embed_model = "text-embedding-3-small",
local_embed_model = "BAAI/bge-small-en-v1.5",
k_context = 4,
k_history = 4,
save_history = TRUE,
add_context = "sometimes",
sources = "All",
run_code = FALSE,
persist = TRUE) {
ops <- tibble::tibble(
service, model, task, local_embed, openai_embed_model, local_embed_model,
k_context, k_history, sources, run_code, save_history, add_context
)
if (persist == TRUE) {
# save file to R user dir
dir <- tools::R_user_dir("gpttools", which = "config")
if (!dir.exists(dir)) dir.create(dir, recursive = TRUE)
path <- file.path(dir, "options.yml")
yaml::write_yaml(ops, path)
}
set_user_config(path)
}
#' Set user configuration settings for gpttools from a file
#'
#' This function sets the user's configuration settings for gpttools by reading
#' a YAML file. If no path is provided, it will look for the file in the default
#' R user config directory.
#' @param path The path to the config YAML file. If NULL, the default path is
#' used.
#' @return Invisible NULL.
#' @export
set_user_config <- function(path = NULL) {
if (is.null(path)) {
path <- file.path(
tools::R_user_dir("gpttools", which = "config"),
"options.yml"
)
}
if (file.exists(path)) {
ops <- yaml::read_yaml(path)
options(
gpttools.service = ops$service,
gpttools.model = ops$model,
gpttools.task = ops$task,
gpttools.local_embed = ops$local_embed,
gpttools.k_context = ops$k_context,
gpttools.k_history = ops$k_history,
gpttools.save_history = ops$save_history,
gpttools.sources = ops$sources,
gpttools.openai_embed_model = ops$openai_embed_model,
gpttools.local_embed_model = ops$local_embed_model,
gpttools.run_code = ops$run_code,
gpttools.add_context = ops$add_context
)
invisible(TRUE)
} else {
invisible(FALSE)
}
}
save_pkgs_to_scrape <- function(pkgs) {
dir <- tools::R_user_dir("gpttools", which = "config")
if (!dir.exists(dir)) dir.create(dir, recursive = TRUE)
path <- file.path(dir, "pkgs_to_scrape.rds")
saveRDS(pkgs, path)
TRUE
}
load_pkgs_to_scrape <- function() {
path <- file.path(
tools::R_user_dir("gpttools", which = "config"),
"pkgs_to_scrape.rds"
)
if (file.exists(path)) {
pkgs <- readRDS(path)
return(pkgs)
} else {
invisible(NULL)
}
}