Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Allow multiple chat sessions #59

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading