-
Notifications
You must be signed in to change notification settings - Fork 26
/
gpt-query.R
88 lines (78 loc) · 2.28 KB
/
gpt-query.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
#' Wrapper around selectionGet to help with testthat
#'
#' @return Text selection via `rstudioapi::selectionGet`
#'
#' @export
get_selection <- function() {
rstudioapi::verifyAvailable()
rstudioapi::selectionGet()
}
#' Wrapper around selectionGet to help with testthat
#'
#' @param improved_text Text from model queries to inert into script or document
#'
#' @export
insert_text <- function(improved_text) {
rstudioapi::verifyAvailable()
rstudioapi::insertText(NULL, improved_text)
}
# write a function to take the output of this function; return only the R code
gpt_chat <- function(instructions,
service = getOption("gpttools.service", "openai"),
model = getOption("gpttools.model", "gpt-4")) {
query <- get_selection()
cli_inform("Selection: {query}")
prompt <-
list(
list(
role = "system",
content = glue("{instructions}")
),
list(
role = "user",
content = glue("{query}")
)
)
cli_inform("Service: {service}")
cli_inform("Model: {model}")
cli_inform("Sending query... this can take up to 3 minutes.")
simple_prompt <- prompt |>
purrr::map_chr(.f = "content") |>
paste(collapse = "\n\n")
answer <- chat(
prompt = simple_prompt,
service = service,
model = model,
stream = FALSE
)
cli_process_done(msg_done = "Received response from {service}")
text_to_insert <- as.character(answer)
insert_text(text_to_insert)
}
query_openai <- function(task = "chat/completions",
body = NULL,
api_key = Sys.getenv("OPENAI_API_KEY"),
base_url = "https://api.openai.com/v1",
model = "gpt-3.5-turbo") {
arg_match(task, c("chat/completions", "embeddings"))
req <- request(base_url)
if (task == "chat/completions") {
body <- list(
model = model,
messages = body
)
}
resp <-
req |>
req_url_path_append(task) |>
req_user_agent("gpttools: https://github.com/jameshwade/gpttools") |>
req_headers(
"Authorization" = glue("Bearer {api_key}"),
"Content-Type" = "application/json"
) |>
req_body_json(body) |>
req_retry() |>
req_throttle(4) |>
req_perform()
resp |> resp_body_json(simplifyVector = TRUE)
}