diff --git a/R/ask_chatgpt.R b/R/ask_chatgpt.R index 7b7a7c7..48648c4 100644 --- a/R/ask_chatgpt.R +++ b/R/ask_chatgpt.R @@ -3,6 +3,9 @@ #' Note: See also `reset_chat_session`. #' #' @param question The question to ask ChatGPT. +#' @param session_id The ID of the session to be used. We can have different conversations by using +#' different session IDs. +#' @param openai_api_key OpenAI's API key. #' #' @examples #' \dontrun{ @@ -13,17 +16,19 @@ #' #' @export #' -ask_chatgpt <- function(question) { +ask_chatgpt <- function(question, session_id = "1", openai_api_key = Sys.getenv("OPENAI_API_KEY")) { # Get the existing chat session messages, and add the new message. - chat_session_messages <- append(get("chat_session_messages", envir = .state), list( + chat_session_messages <- append(get_chat_session(session_id), list( list(role = "user", content = question) )) # Send the query to ChatGPT. - chat_gpt_reply <- parse_response(gpt_get_completions(question, messages = chat_session_messages)) + chat_gpt_reply <- parse_response( + gpt_get_completions(question, openai_api_key, chat_session_messages) + ) chat_session_messages <- append(chat_session_messages, list( list(role = "assistant", content = chat_gpt_reply) )) # Update the chat session messages with the new question and the reply. - assign("chat_session_messages", chat_session_messages, .state) + reset_chat_session(chat_session_messages, session_id) chat_gpt_reply } diff --git a/R/chatgpt-package.R b/R/chatgpt-package.R index 5148f39..00bd200 100644 --- a/R/chatgpt-package.R +++ b/R/chatgpt-package.R @@ -6,10 +6,6 @@ .state <- new.env(parent = emptyenv()) # Empty chat session messages at startup. -assign( - "chat_session_messages", - list(list(role = "system", content = "You are a helpful assistant.")), - envir = .state -) +assign("chat_session_messages", list(), envir = .state) api_url <- Sys.getenv("OPENAI_API_URL", "https://api.openai.com/v1") diff --git a/R/get_chat_session.R b/R/get_chat_session.R new file mode 100644 index 0000000..e8471a7 --- /dev/null +++ b/R/get_chat_session.R @@ -0,0 +1,16 @@ +#' Get Chat Session +#' +#' @param session_id The ID of the session to be used. If `NULL`, it will return an empty session. +#' +get_chat_session <- function(session_id = "1") { + default_session <- list(list(role = "system", content = "You are a helpful assistant.")) + if (is.null(session_id)) { + return(default_session) + } + session <- get("chat_session_messages", envir = .state)[[as.character(session_id)]] + # If the session was not found, then it's a new (default) session. + if (is.null(session)) { + session <- default_session + } + session +} diff --git a/R/reset_chat_session.R b/R/reset_chat_session.R index 45c1fae..b690f4d 100644 --- a/R/reset_chat_session.R +++ b/R/reset_chat_session.R @@ -4,12 +4,22 @@ #' we want to start a new conversation, we must call `reset_chat_session`. #' #' @param system_role ChatGPT's role as an AI assistant. +#' @param session_id The ID of the session to be used. If `NULL`, this function will have no effect. #' #' @export #' -reset_chat_session <- function(system_role = "You are a helpful assistant.") { - assign( - "chat_session_messages", list(list(role = "system", content = system_role)), - envir = .state - ) +reset_chat_session <- function(system_role = "You are a helpful assistant.", session_id = "1") { + if (is.null(session_id)) { + return() + } + if (is.list(system_role)) { + # If `system_role` is a list, then it is a ChatGPT session object. + session <- system_role + } else { + # Otherwise, it's a string specifying ChatGPT's role. + session <- list(list(role = "system", content = system_role)) + } + all_sessions <- get("chat_session_messages", envir = .state) + all_sessions[[as.character(session_id)]] <- session + assign("chat_session_messages", all_sessions, envir = .state) } diff --git a/man/ask_chatgpt.Rd b/man/ask_chatgpt.Rd index 8ec7698..12c5b75 100644 --- a/man/ask_chatgpt.Rd +++ b/man/ask_chatgpt.Rd @@ -4,10 +4,19 @@ \alias{ask_chatgpt} \title{Ask ChatGPT} \usage{ -ask_chatgpt(question) +ask_chatgpt( + question, + session_id = "1", + openai_api_key = Sys.getenv("OPENAI_API_KEY") +) } \arguments{ \item{question}{The question to ask ChatGPT.} + +\item{session_id}{The ID of the session to be used. We can have different conversations by using +different session IDs.} + +\item{openai_api_key}{OpenAI's API key.} } \value{ A character value with the response generated by ChatGPT. diff --git a/man/get_chat_session.Rd b/man/get_chat_session.Rd new file mode 100644 index 0000000..60b9cf4 --- /dev/null +++ b/man/get_chat_session.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_chat_session.R +\name{get_chat_session} +\alias{get_chat_session} +\title{Get Chat Session} +\usage{ +get_chat_session(session_id = "1") +} +\arguments{ +\item{session_id}{The ID of the session to be used. If `NULL`, it will return an empty session.} +} +\description{ +Get Chat Session +} diff --git a/man/reset_chat_session.Rd b/man/reset_chat_session.Rd index ea923e2..cf7baa4 100644 --- a/man/reset_chat_session.Rd +++ b/man/reset_chat_session.Rd @@ -4,10 +4,15 @@ \alias{reset_chat_session} \title{Reset Chat Session} \usage{ -reset_chat_session(system_role = "You are a helpful assistant.") +reset_chat_session( + system_role = "You are a helpful assistant.", + session_id = "1" +) } \arguments{ \item{system_role}{ChatGPT's role as an AI assistant.} + +\item{session_id}{The ID of the session to be used. If `NULL`, this function will have no effect.} } \description{ This function is intended to be used with `ask_chatgpt`. If we are using `ask_chatgpt` to chat with ChatGPT, and