Skip to content

Commit

Permalink
Merge pull request #59 from jcrodriguez1989/feature.jcr.allow_multipl…
Browse files Browse the repository at this point in the history
…e_chat_sessions

Feature: Allow multiple chat sessions
  • Loading branch information
jcrodriguez1989 committed Aug 5, 2024
2 parents a2c39a0 + 217b19c commit 10192e3
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 16 deletions.
13 changes: 9 additions & 4 deletions R/ask_chatgpt.R
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
}
6 changes: 1 addition & 5 deletions R/chatgpt-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
16 changes: 16 additions & 0 deletions R/get_chat_session.R
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 15 additions & 5 deletions R/reset_chat_session.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
11 changes: 10 additions & 1 deletion man/ask_chatgpt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/get_chat_session.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion man/reset_chat_session.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 10192e3

Please sign in to comment.