File size: 1,726 Bytes
b3c7a65
 
 
 
7107d66
b3c7a65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
chat <- function(user_message, 
                 history = NULL,
                 system_prompt = c("general", "code"),
                 api_key = Sys.getenv("OPENAI_API_KEY")) {
  cat("API Key: ", api_key)
  system   <- get_system_prompt(system_prompt)
  prompt   <- prepare_prompt(user_message, system, history)
  base_url <- "https://api.openai.com/v1"
  body     <- list(model = "gpt-3.5-turbo",
                   messages = prompt)
  req <- 
    resp <-
    request(base_url) |>  
    req_url_path_append("chat/completions") |> 
    req_auth_bearer_token(token = api_key) |> 
    req_headers("Content-Type" = "application/json") |> 
    req_user_agent("James Wade @jameshwade | OpenAI tutorial") |> 
    req_body_json(body) |> 
    req_retry(max_tries = 4) |> 
    req_throttle(rate = 15) |> 
    req_perform()
  
  openai_chat_response <- resp |> resp_body_json(simplifyVector = TRUE)
  
  openai_chat_response$choices$message$content
}

get_system_prompt <- function(system = c("general", "code")) {
  rlang::arg_match(system)
  instructions <- 
    switch(system,
           "general" = "You are a helpful assistant.",
           "code"    = "You are a helpful chat bot that answers questions for an R programmer working in the RStudio IDE.")
  list(list(role = "system", content = instructions))
}

prepare_prompt <- function(user_message, system_prompt, history) {
  user_prompt <-  list(list(role = "user", content = user_message))
  c(system_prompt, history, user_prompt) |> purrr::compact()
}

update_history <- function(history, user_message, response) {
  c(history,
    list(
      list(role = "user", content = user_message),
      list(role = "assistant", content = response)
    )
  ) |> purrr::compact()
}