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: Work with new o1 models #62

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions R/chatgpt-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
assign("chat_session_messages", list(), envir = .state)

api_url <- Sys.getenv("OPENAI_API_URL", "https://api.openai.com/v1")

# GPT Models that doesn't allow the `system` parameter in messages.
systemless_models <- c("o1-mini", "o1-preview")
3 changes: 3 additions & 0 deletions R/get_chat_session.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#'
get_chat_session <- function(session_id = "1") {
default_session <- list(list(role = "system", content = "You are a helpful assistant."))
if (Sys.getenv("OPENAI_MODEL") %in% systemless_models) {
default_session <- list()
}
if (is.null(session_id)) {
return(default_session)
}
Expand Down
17 changes: 12 additions & 5 deletions R/gpt_get_completions.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ gpt_get_completions <- function(prompt, openai_api_key = Sys.getenv("OPENAI_API_
presence_penalty = as.numeric(Sys.getenv("OPENAI_PRESENCE_PENALTY", 0)),
logprobs = as.logical(Sys.getenv("OPENAI_LOGPROBS", FALSE))
)
# These GPT models use different parameters.
if (Sys.getenv("OPENAI_MODEL") %in% systemless_models) {
params$max_completion_tokens <- params$max_tokens
params$max_tokens <- NULL
}
if (get_verbosity()) {
message(paste0("\n*** ChatGPT input:\n\n", prompt, "\n"))
}
return_language <- Sys.getenv("OPENAI_RETURN_LANGUAGE")
if (nchar(return_language) > 0) {
return_language <- paste0("You return all your replies in ", return_language, ".")
}
if (is.null(messages)) {
if (is.null(messages) && Sys.getenv("OPENAI_MODEL") %in% systemless_models) {
messages <- list(
list(
role = "system",
Expand All @@ -42,11 +47,13 @@ gpt_get_completions <- function(prompt, openai_api_key = Sys.getenv("OPENAI_API_
)
} else {
# If there are messages provided, then add the `return_language` if available.
messages[[which(sapply(messages, function(message) message$role == "system"))]]$content <-
paste(
messages[[which(sapply(messages, function(message) message$role == "system"))]]$content,
return_language
system_msg_index <- which(sapply(messages, function(message) message$role == "system"))
if (length(system_msg_index) == 1) {
# If this GPT model has `system` parameter, assign the language to it.
messages[[system_msg_index]]$content <- paste(
messages[[system_msg_index]]$content, return_language
)
}
}
# Get the proxy to use, if provided.
proxy <- NULL
Expand Down
5 changes: 4 additions & 1 deletion R/reset_chat_session.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ reset_chat_session <- function(system_role = "You are a helpful assistant.", ses
if (is.list(system_role)) {
# If `system_role` is a list, then it is a ChatGPT session object.
session <- system_role
} else {
} else if (!Sys.getenv("OPENAI_MODEL") %in% systemless_models) {
# Otherwise, it's a string specifying ChatGPT's role.
session <- list(list(role = "system", content = system_role))
} else {
warning("Couldn't assign default session to this GPT model.")
session <- list()
}
all_sessions <- get("chat_session_messages", envir = .state)
all_sessions[[as.character(session_id)]] <- session
Expand Down
Loading