Skip to content

Commit

Permalink
Merge pull request #62 from jcrodriguez1989/feature.jcr.work_with_new…
Browse files Browse the repository at this point in the history
…_models

Feature: Work with new o1 models
  • Loading branch information
jcrodriguez1989 authored Jan 31, 2025
2 parents 2590e92 + 9e143c1 commit d886bbb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
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

0 comments on commit d886bbb

Please sign in to comment.