-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from jcrodriguez1989/develop
Release v0.2.4
- Loading branch information
Showing
20 changed files
with
238 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#' Build Prompt Content | ||
#' | ||
#' @param question The question to ask ChatGPT. | ||
#' @param images A list of images to attach to the question. It could be a list of URLs or paths. | ||
#' | ||
#' @importFrom xfun base64_encode | ||
#' | ||
build_prompt_content <- function(question, images) { | ||
if (length(images) == 0) { | ||
return(question) | ||
} | ||
prompt_content <- list(list(type = "text", text = question)) | ||
append(prompt_content, lapply(images, function(image) { | ||
# If it's a local file, then transform it to base 64 encoding. If not, return the "URL". | ||
image_url <- image | ||
if (file.exists(image)) { | ||
image_url <- paste0("data:image/jpeg;base64,", base64_encode(image)) | ||
} | ||
list(type = "image_url", image_url = list(url = image_url)) | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#' Generate an Image With DALL-E 3 | ||
#' | ||
#' @param prompt The prompt for image generation. | ||
#' @param out_file The path where to save the generated image. | ||
#' @param openai_api_key OpenAI's API key. | ||
#' | ||
#' @importFrom httr add_headers content content_type_json POST stop_for_status | ||
#' @importFrom jsonlite fromJSON toJSON | ||
#' @importFrom utils download.file | ||
#' | ||
#' @export | ||
#' | ||
generate_image <- function(prompt, out_file = tempfile(fileext = ".png"), | ||
openai_api_key = Sys.getenv("OPENAI_API_KEY")) { | ||
post_res <- POST( | ||
paste0(api_url, "/images/generations"), | ||
add_headers("Authorization" = paste("Bearer", openai_api_key)), | ||
content_type_json(), | ||
body = toJSON( | ||
list(model = "dall-e-3", prompt = prompt, n = 1, size = "1024x1024"), | ||
auto_unbox = TRUE | ||
) | ||
) | ||
stop_for_status(post_res) | ||
download.file(fromJSON(content(post_res, as = "text", encoding = "UTF-8"))$data$url, out_file) | ||
return(out_file) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#' ChatGPT: List Models | ||
#' | ||
#' @param openai_api_key OpenAI's API key. | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' list_models() | ||
#' } | ||
#' | ||
#' @importFrom httr add_headers content GET stop_for_status | ||
#' @importFrom jsonlite fromJSON | ||
#' | ||
#' @return A data.frame with the available models to be used by OpenAI's API. | ||
#' | ||
#' @export | ||
#' | ||
list_models <- function(openai_api_key = Sys.getenv("OPENAI_API_KEY")) { | ||
get_res <- GET( | ||
paste0(api_url, "/models"), | ||
add_headers("Authorization" = paste("Bearer", openai_api_key)) | ||
) | ||
stop_for_status(get_res) | ||
fromJSON(content(get_res, as = "text", encoding = "UTF-8"))$data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.