Skip to content

Commit

Permalink
Merge pull request #4 from umatter/tidyverse-style
Browse files Browse the repository at this point in the history
Impose tidyverse style
  • Loading branch information
umatter committed May 12, 2023
2 parents 4806ad0 + 850742e commit acfe924
Show file tree
Hide file tree
Showing 109 changed files with 1,320 additions and 991 deletions.
38 changes: 21 additions & 17 deletions R/add_roxygen.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,56 @@
#'
#' This function adds Roxygen2 documentation to an R function.
#'
#' @param file A character string indicating the path to the file containing the R function.
#' @return If the path provided is a character string, this function returns the documented function as a character string. If the input is a file path, this function returns the path of the file to which documentation was added to the file.
#' @param file A character string indicating the path to the file containing the
#' R function.
#' @param ... Additional arguments to pass to the chat_completion() function.
#' @return If the path provided is a character string, this function returns the
#' documented function as a character string. If the input is a file path, this
#' function returns the path of the file to which documentation was added to the
#' file.
#' @export
#' @author Ulrich Matter umatter@protonmail.com
#'

add_roxygen <- function(file) {
add_roxygen <- function(file, ...) {

# import, process text
r_function <- read_text(file)
text <-
r_function$text %>%
text <-
r_function$text %>%
paste0(collapse = "\n")

# Make sure intput is an R function
if (!contains_r_func(text) | nchar(text)==0){
if (!contains_r_func(text) | nchar(text) == 0) {
stop("The input does not contain a valid R function.")
}

# initial user input
n_msgs <- nrow(add_roxygen_prompt)
add_roxygen_prompt$content[n_msgs] <-
add_roxygen_prompt$content[n_msgs] <-
sprintf(fmt = add_roxygen_prompt$content[n_msgs], text)

# chat
cli::cli_alert_info("Code documentation in progress. Hold on tight!")
resp <- chat_completion(add_roxygen_prompt)
resp <- chat_completion(add_roxygen_prompt, ...)
total_tokens_used <- usage(resp)$total_tokens
info_token <- paste0("Total tokens used: ", total_tokens_used)
cli::cli_inform(info_token)

# extract roxygen2 documentation
output <-
resp %>%
messages_content() %>%
output <-
resp %>%
messages_content() %>%
extract_roxygen2()

# prepare output
filename <- unique(r_function$file)
if (filename == "character string") {
return(output)

} else {
rfunc <- readLines(filename)
rfunc_documented <- c(output, rfunc)
output <- paste0(rfunc_documented, collapse="\n")
output <- paste0(rfunc_documented, collapse = "\n")
cat(output, file = filename)
cli::cli_alert_success(paste0("Added documentation to ", filename))
return(filename)
Expand Down
35 changes: 17 additions & 18 deletions R/add_to_chatlog.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#' Add data to a chat log
#'
#' This function adds data to the in-memory chat log, which is in the global environment. The data
#' can be any R object, such as a vector, list, or data frame. The function supports adding data
#' to the chat log by providing either a data.frame or a chatlog object.
#' This function adds data to the in-memory chat log, which is in the global
#' environment. The data can be any R object, such as a vector, list, or data
#' frame. The function supports adding data to the chat log by providing either
#' a data.frame or a chatlog object.
#'
#' @param msgs A chatlog object or a data.frame containing the messages to be added to the chat log.
#' @param chatlog_id The id of the chatlog object. Required when the provided msgs is a data.frame.
#' @param msgs A chatlog object or a data.frame containing the messages to be
#' added to the chat log.
#' @param chatlog_id The id of the chatlog object. Required when the provided
#' msgs is a data.frame.
#'
#' @return The updated chatlog object with the newly added messages.
#' @author Ulrich Matter umatter@protonmail.com
Expand All @@ -26,34 +29,30 @@
#' merged_chatlog <- add_to_chatlog(chatlog1, chatlog2@chatlog_id)
#' }
#' @export
add_to_chatlog <- function(msgs, chatlog_id=NULL) {
if (is.data.frame(msgs) & !is.null(chatlog_id)){
add_to_chatlog <- function(msgs, chatlog_id = NULL) {

if (is.data.frame(msgs) & !is.null(chatlog_id)) {

# make sure latest state of log is used
current <- get_chatlog(chatlog_id)
# update the chatlog
current@messages <- dplyr::bind_rows(current@messages,
msgs)
assign(chatlog_id, current, envir = OpenAIR_env)

return(current)

}

if (is_chatlog(msgs)){

# make sure latest state of log is used
current <- get_chatlog(msgs@chatlog_id)
# update the chatlog
current@messages <- dplyr::bind_rows(current@messages,
msgs@messages)
assign(chatlog_id, current, envir = OpenAIR_env)

return(current)

}



}
}
94 changes: 50 additions & 44 deletions R/chat.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
#' Start or continue a chat conversation
#'
#' This function starts or continues a chat conversation by adding the user's message to the conversation.
#' If the conversation does not exist, a new one will be initiated. The response can be displayed in the
#' console, returned as a character vector, or returned as a full response object from the ChatGPT API.
#' This function starts or continues a chat conversation by adding the user's
#' message to the conversation.
#' If the conversation does not exist, a new one will be initiated. The response
#' can be displayed in the
#' console, returned as a character vector, or returned as a full response
#' object from the ChatGPT API.
#'
#' @param message A character string representing the message to be added to the chat conversation.
#' @param chatlog_id A character string representing the ID of the chat conversation to start or continue.
#' Default is ".__CURRENTCHAT__".
#' @param output A character string indicating the output format of the response. Default is "message_to_console".
#' Valid options are "message_to_console", "message", or "response_object".
#' @param ... Additional arguments to be passed to the `chat_completion` function.
#' @param message A character string representing the message to be added to the
#' chat conversation.
#' @param chatlog_id A character string representing the ID of the chat
#' conversation to start or continue. Default is ".__CURRENTCHAT__".
#' @param output A character string indicating the output format of the
#' response. Default is "message_to_console". Valid options are
#' "message_to_console", "message", or "response_object".
#' @param ... Additional arguments to pass to the chat_completion() function.
#' @author Ulrich Matter umatter@protonmail.com
#'
#' @return Depending on the value of the 'output' argument, this function returns one of the following:
#' * "message_to_console": a message containing the response text is printed to the console (default).
#' @return Depending on the value of the 'output' argument, this function
#' returns one of the following:
#' * "message_to_console": a message containing the response text is printed to
#' the console (default).
#' * "message": the response text as a character vector.
#' * "response_object": the full response object from the ChatGPT API.
#'
Expand All @@ -28,72 +35,71 @@
#' @export


chat <- function(message, chatlog_id = ".__CURRENTCHAT__", output="message_to_console", ...){

chat <- function(message, chatlog_id = ".__CURRENTCHAT__",
output = "message_to_console", ...) {

# check input validity
if (!output %in% c("message_to_console", "message", "response_object")){
stop("Argument output needs to be one of 'message_to_console', 'message', or 'response_object")
if (!output %in% c("message_to_console", "message", "response_object")) {
stop(paste0("Argument output needs to be one of 'message_to_console', ",
"'message', or 'response_object"))
}

# check if chat is ongoing
# if not, initiate new chat
if (!exists(chatlog_id, envir = OpenAIR_env)){
if (!exists(chatlog_id, envir = OpenAIR_env)) {
# initialize chatlog
cl <- start_chat(chatlog_id=chatlog_id)
cl <- start_chat(chatlog_id = chatlog_id)

} else {
# fetch current chat status
cl <- get_chatlog(chatlog_id)

}

# add new message to conversation, send
resp <-
initialize_messages(initial_role = "user", initial_content = message) %>%
add_to_chatlog(chatlog_id) %>%
resp <-
initialize_messages(initial_role = "user", initial_content = message) %>%
add_to_chatlog(chatlog_id) %>%
chat_completion(...)

# update chatlog
resp %>%
resp %>%
messages() %>%
add_to_chatlog(chatlog_id)
if (output=="message_to_console") {

if (output == "message_to_console") {
# process response
messages <-
resp %>%
messages <-
resp %>%
messages()

# show response message in console
resp_parts <- parse_response(messages$content)

for (i in 1:length(resp_parts)){
part <- resp_parts[[i]]
if (part$type=="text"){
if (part$type == "text") {
cli::cli_text(part$content)
cli::cli_text()
} else {
cli::cli_code(part$content)
cli::cli_text()
}
}
}

if (output=="message") {

}

if (output == "message") {
# process response
messages <-
resp %>%
messages <-
resp %>%
messages()

return(messages$content)

}

if (output == "response_object") {
# return full response object
return(resp)
}


}

0 comments on commit acfe924

Please sign in to comment.