├── .github ├── .gitignore └── workflows │ ├── R-CMD-check.yaml │ ├── test-coverage.yaml │ └── pkgdown.yaml ├── tests ├── manual │ ├── test-retrieve_file_content.R │ ├── test-list_files.R │ ├── test-list_engines.R │ ├── test-list_models.R │ ├── test-list_fine_tunes.R │ ├── test-retrieve_model.R │ ├── test-retrieve_engine.R │ ├── test-create_moderation.R │ ├── test-upload_file.R │ ├── test-create_edit.R │ ├── test-create_transcription.R │ ├── test-create_translation.R │ ├── test-delete_file.R │ ├── test-retrieve_file.R │ ├── test-create_embedding.R │ ├── test-create_image_variation.R │ ├── test-delete_fine_tune_model.R │ ├── test-create_image_edit.R │ ├── test-create_completion.R │ ├── test-create_image.R │ ├── test-create_chat_completion.R │ ├── test-create_fine_tune.R │ ├── test-cancel_fine_tune.R │ ├── test-retrieve_fine_tune.R │ └── test-list_fine_tune_events.R ├── testthat.R └── testthat │ ├── test-list_fine_tunes.R │ ├── test-list_files.R │ ├── test-list_models.R │ ├── test-list_engines.R │ ├── test-cancel_fine_tune.R │ ├── test-retrieve_fine_tune.R │ ├── test-delete_file.R │ ├── test-retrieve_file.R │ ├── test-retrieve_model.R │ ├── test-delete_fine_tune_model.R │ ├── test-retrieve_file_content.R │ ├── test-retrieve_engine.R │ ├── test-upload_file.R │ ├── test-create_moderation.R │ ├── test-list_fine_tune_events.R │ ├── test-create_embedding.R │ ├── test-create_translation.R │ ├── test-create_image.R │ ├── test-create_edit.R │ ├── test-create_image_variation.R │ ├── test-create_transcription.R │ ├── test-create_image_edit.R │ ├── test-create_chat_completion.R │ ├── helper-test_argument_validation.R │ ├── test-create_fine_tune.R │ └── test-create_completion.R ├── LICENSE ├── R ├── openai.R ├── verify_mime_type.R ├── utils-pipe.R ├── utils-assertions.R ├── list_files.R ├── list_fine_tunes.R ├── list_models.R ├── retrieve_file_content.R ├── retrieve_model.R ├── delete_file.R ├── retrieve_file.R ├── delete_fine_tune_model.R ├── list_engines.R ├── create_moderation.R ├── retrieve_engine.R ├── cancel_fine_tune.R ├── retrieve_fine_tune.R ├── upload_file.R ├── create_embedding.R ├── list_fine_tune_events.R ├── create_image.R ├── create_image_variation.R ├── create_translation.R ├── create_edit.R ├── create_transcription.R └── create_image_edit.R ├── inst ├── extdata │ ├── mask.png │ ├── astronaut.png │ ├── sample-en.m4a │ ├── sample-ua.m4a │ └── classification-file.jsonl ├── figures │ └── mini-dalle.png └── other │ └── generate-hex-sticker.R ├── man ├── figures │ ├── logo.png │ ├── astronaut.png │ ├── lifecycle-stable.svg │ ├── lifecycle-defunct.svg │ ├── lifecycle-archived.svg │ ├── lifecycle-maturing.svg │ ├── lifecycle-deprecated.svg │ ├── lifecycle-superseded.svg │ ├── lifecycle-experimental.svg │ └── lifecycle-questioning.svg ├── pipe.Rd ├── list_models.Rd ├── list_files.Rd ├── retrieve_model.Rd ├── create_moderation.Rd ├── list_fine_tunes.Rd ├── retrieve_file_content.Rd ├── delete_file.Rd ├── retrieve_file.Rd ├── delete_fine_tune_model.Rd ├── list_engines.Rd ├── upload_file.Rd ├── create_embedding.Rd ├── retrieve_engine.Rd ├── create_translation.Rd ├── create_image.Rd ├── create_edit.Rd ├── create_transcription.Rd ├── cancel_fine_tune.Rd ├── create_image_variation.Rd ├── retrieve_fine_tune.Rd ├── list_fine_tune_events.Rd ├── create_image_edit.Rd ├── create_chat_completion.Rd ├── create_fine_tune.Rd └── create_completion.Rd ├── _pkgdown.yml ├── .gitignore ├── pkgdown └── favicon │ ├── favicon.ico │ ├── favicon-16x16.png │ ├── favicon-32x32.png │ ├── apple-touch-icon.png │ ├── apple-touch-icon-60x60.png │ ├── apple-touch-icon-76x76.png │ ├── apple-touch-icon-120x120.png │ ├── apple-touch-icon-152x152.png │ └── apple-touch-icon-180x180.png ├── CRAN-SUBMISSION ├── .Rbuildignore ├── codecov.yml ├── openai.Rproj ├── NAMESPACE ├── DESCRIPTION ├── LICENSE.md ├── cran-comments.md └── NEWS.md /.github/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | -------------------------------------------------------------------------------- /tests/manual/test-retrieve_file_content.R: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/manual/test-list_files.R: -------------------------------------------------------------------------------- 1 | list_files() 2 | -------------------------------------------------------------------------------- /tests/manual/test-list_engines.R: -------------------------------------------------------------------------------- 1 | list_engines() 2 | -------------------------------------------------------------------------------- /tests/manual/test-list_models.R: -------------------------------------------------------------------------------- 1 | list_models() 2 | -------------------------------------------------------------------------------- /tests/manual/test-list_fine_tunes.R: -------------------------------------------------------------------------------- 1 | list_fine_tunes() 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2022 2 | COPYRIGHT HOLDER: openai authors 3 | -------------------------------------------------------------------------------- /R/openai.R: -------------------------------------------------------------------------------- 1 | #' @importFrom lifecycle deprecated 2 | NULL 3 | -------------------------------------------------------------------------------- /tests/manual/test-retrieve_model.R: -------------------------------------------------------------------------------- 1 | retrieve_model("text-davinci-002") 2 | -------------------------------------------------------------------------------- /tests/manual/test-retrieve_engine.R: -------------------------------------------------------------------------------- 1 | retrieve_engine("text-davinci-002") 2 | -------------------------------------------------------------------------------- /inst/extdata/mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irudnyts/openai/HEAD/inst/extdata/mask.png -------------------------------------------------------------------------------- /man/figures/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irudnyts/openai/HEAD/man/figures/logo.png -------------------------------------------------------------------------------- /_pkgdown.yml: -------------------------------------------------------------------------------- 1 | url: https://irudnyts.github.io/openai/ 2 | template: 3 | bootstrap: 5 4 | 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | .Renviron 6 | .DS_Store 7 | docs 8 | -------------------------------------------------------------------------------- /inst/extdata/astronaut.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irudnyts/openai/HEAD/inst/extdata/astronaut.png -------------------------------------------------------------------------------- /inst/extdata/sample-en.m4a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irudnyts/openai/HEAD/inst/extdata/sample-en.m4a -------------------------------------------------------------------------------- /inst/extdata/sample-ua.m4a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irudnyts/openai/HEAD/inst/extdata/sample-ua.m4a -------------------------------------------------------------------------------- /man/figures/astronaut.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irudnyts/openai/HEAD/man/figures/astronaut.png -------------------------------------------------------------------------------- /inst/figures/mini-dalle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irudnyts/openai/HEAD/inst/figures/mini-dalle.png -------------------------------------------------------------------------------- /pkgdown/favicon/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irudnyts/openai/HEAD/pkgdown/favicon/favicon.ico -------------------------------------------------------------------------------- /CRAN-SUBMISSION: -------------------------------------------------------------------------------- 1 | Version: 0.4.1 2 | Date: 2023-03-15 00:08:58 UTC 3 | SHA: 231a7610cabd182db4fbe7a44931499de0cad74c 4 | -------------------------------------------------------------------------------- /pkgdown/favicon/favicon-16x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irudnyts/openai/HEAD/pkgdown/favicon/favicon-16x16.png -------------------------------------------------------------------------------- /pkgdown/favicon/favicon-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irudnyts/openai/HEAD/pkgdown/favicon/favicon-32x32.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irudnyts/openai/HEAD/pkgdown/favicon/apple-touch-icon.png -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(openai) 3 | 4 | test_check("openai") 5 | # test_dir(path = "tests/testthat/") 6 | -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-60x60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irudnyts/openai/HEAD/pkgdown/favicon/apple-touch-icon-60x60.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-76x76.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irudnyts/openai/HEAD/pkgdown/favicon/apple-touch-icon-76x76.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-120x120.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irudnyts/openai/HEAD/pkgdown/favicon/apple-touch-icon-120x120.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-152x152.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irudnyts/openai/HEAD/pkgdown/favicon/apple-touch-icon-152x152.png -------------------------------------------------------------------------------- /pkgdown/favicon/apple-touch-icon-180x180.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/irudnyts/openai/HEAD/pkgdown/favicon/apple-touch-icon-180x180.png -------------------------------------------------------------------------------- /tests/manual/test-create_moderation.R: -------------------------------------------------------------------------------- 1 | create_moderation( 2 | input = "I want to kill them all.", 3 | model = "text-moderation-stable" 4 | ) 5 | -------------------------------------------------------------------------------- /tests/manual/test-upload_file.R: -------------------------------------------------------------------------------- 1 | file <- system.file("extdata", "classification-file.jsonl", package = "openai") 2 | upload_file(file = file, purpose = "classification") 3 | -------------------------------------------------------------------------------- /inst/extdata/classification-file.jsonl: -------------------------------------------------------------------------------- 1 | {"text": "It's kinda crazy, but I adore this movie", "label": "Positive"} 2 | {"text": "I hate those turns in the plot.", "label": "Negative"} 3 | -------------------------------------------------------------------------------- /tests/manual/test-create_edit.R: -------------------------------------------------------------------------------- 1 | create_edit( 2 | model = "text-davinci-edit-001", 3 | input = "What day of the wek is it?", 4 | instruction = "Fix the spelling mistakes" 5 | ) 6 | -------------------------------------------------------------------------------- /tests/manual/test-create_transcription.R: -------------------------------------------------------------------------------- 1 | voice_sample_en <- system.file("extdata", "sample-en.m4a", package = "openai") 2 | create_transcription(file = voice_sample_en, model = "whisper-1") 3 | -------------------------------------------------------------------------------- /tests/manual/test-create_translation.R: -------------------------------------------------------------------------------- 1 | voice_sample_ua <- system.file("extdata", "sample-ua.m4a", package = "openai") 2 | create_translation(file = voice_sample_ua, model = "whisper-1") 3 | -------------------------------------------------------------------------------- /tests/manual/test-delete_file.R: -------------------------------------------------------------------------------- 1 | file <- system.file("extdata", "classification-file.jsonl", package = "openai") 2 | file_info <- upload_file(file = file, purpose = "classification") 3 | delete_file(file_info$id) 4 | -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^LICENSE\.md$ 4 | ^_pkgdown\.yml$ 5 | ^docs$ 6 | ^pkgdown$ 7 | ^\.github$ 8 | ^README\.Rmd$ 9 | ^codecov\.yml$ 10 | ^cran-comments\.md$ 11 | ^CRAN-SUBMISSION$ 12 | -------------------------------------------------------------------------------- /tests/manual/test-retrieve_file.R: -------------------------------------------------------------------------------- 1 | file <- system.file("extdata", "classification-file.jsonl", package = "openai") 2 | file_info <- upload_file(file = file, purpose = "classification") 3 | retrieve_file(file_info$id) 4 | -------------------------------------------------------------------------------- /tests/manual/test-create_embedding.R: -------------------------------------------------------------------------------- 1 | create_embedding( 2 | model = "text-embedding-ada-002", 3 | input = c( 4 | "Ah, it is so boring to write documentation", 5 | "But examples are really crucial" 6 | ) 7 | ) 8 | -------------------------------------------------------------------------------- /tests/manual/test-create_image_variation.R: -------------------------------------------------------------------------------- 1 | image <- system.file("extdata", "astronaut.png", package = "openai") 2 | create_image_variation( 3 | image = image, 4 | n = 1, 5 | size = "256x256", 6 | response_format = "url" 7 | ) 8 | -------------------------------------------------------------------------------- /tests/manual/test-delete_fine_tune_model.R: -------------------------------------------------------------------------------- 1 | fine_tunes <- list_fine_tunes() 2 | 3 | fine_tunes <- fine_tunes$data 4 | 5 | id <- fine_tunes[!is.na(fine_tunes[, "fine_tuned_model"]), "fine_tuned_model"] 6 | 7 | delete_fine_tune_model(model = id[1]) 8 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | comment: false 2 | 3 | coverage: 4 | status: 5 | project: 6 | default: 7 | target: auto 8 | threshold: 1% 9 | informational: true 10 | patch: 11 | default: 12 | target: auto 13 | threshold: 1% 14 | informational: true 15 | -------------------------------------------------------------------------------- /tests/manual/test-create_image_edit.R: -------------------------------------------------------------------------------- 1 | image <- system.file("extdata", "astronaut.png", package = "openai") 2 | mask <- system.file("extdata", "mask.png", package = "openai") 3 | 4 | create_image_edit( 5 | image = image, 6 | mask = mask, 7 | prompt = "goat", 8 | n = 1, 9 | response_format = "url" 10 | ) 11 | -------------------------------------------------------------------------------- /R/verify_mime_type.R: -------------------------------------------------------------------------------- 1 | verify_mime_type <- function(result) { 2 | 3 | if (httr::http_type(result) != "application/json") { 4 | paste( 5 | "OpenAI API probably has been changed. If you see this, please", 6 | "rise an issue at: https://github.com/irudnyts/openai/issues" 7 | ) %>% 8 | stop() 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /tests/manual/test-create_completion.R: -------------------------------------------------------------------------------- 1 | create_completion( 2 | model = "text-davinci-002", 3 | prompt = "Say this is a test", 4 | max_tokens = 5 5 | ) 6 | 7 | logit_bias <- list( 8 | "11" = -100, 9 | "13" = -100 10 | ) 11 | create_completion( 12 | model = "ada", 13 | prompt = "Generate a question and an answer", 14 | n = 4, 15 | best_of = 4, 16 | logit_bias = logit_bias 17 | ) 18 | -------------------------------------------------------------------------------- /R/utils-pipe.R: -------------------------------------------------------------------------------- 1 | #' Pipe operator 2 | #' 3 | #' See `magrittr::[\%>\%][magrittr::pipe]` for details. 4 | #' 5 | #' @name %>% 6 | #' @rdname pipe 7 | #' @keywords internal 8 | #' @export 9 | #' @importFrom magrittr %>% 10 | #' @usage lhs \%>\% rhs 11 | #' @param lhs A value or the magrittr placeholder. 12 | #' @param rhs A function call using the magrittr semantics. 13 | #' @return The result of calling `rhs(lhs)`. 14 | NULL 15 | -------------------------------------------------------------------------------- /inst/other/generate-hex-sticker.R: -------------------------------------------------------------------------------- 1 | library(hexSticker) 2 | library(here) 3 | 4 | image <- here("inst/figures/mini-dalle.png") 5 | 6 | sticker( 7 | image, 8 | package = "openai", 9 | p_size = 20, 10 | s_x = 1, 11 | s_y = .75, 12 | p_color = "#419072", 13 | h_fill = "#eef5f4", 14 | h_color = "#419072", 15 | s_width = .4, 16 | s_height = .4, 17 | filename = "man/figures/logo.png" 18 | ) 19 | -------------------------------------------------------------------------------- /openai.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 4 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | -------------------------------------------------------------------------------- /tests/testthat/test-list_fine_tunes.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # list_fine_tunes() 3 | 4 | test_argument_validation( 5 | function_name = "list_fine_tunes", 6 | argument_name = "openai_api_key", 7 | argument_type = "string", 8 | allow_null = FALSE 9 | ) 10 | 11 | test_argument_validation( 12 | function_name = "list_fine_tunes", 13 | argument_name = "openai_organization", 14 | argument_type = "string", 15 | allow_null = TRUE 16 | ) 17 | -------------------------------------------------------------------------------- /man/pipe.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/utils-pipe.R 3 | \name{\%>\%} 4 | \alias{\%>\%} 5 | \title{Pipe operator} 6 | \usage{ 7 | lhs \%>\% rhs 8 | } 9 | \arguments{ 10 | \item{lhs}{A value or the magrittr placeholder.} 11 | 12 | \item{rhs}{A function call using the magrittr semantics.} 13 | } 14 | \value{ 15 | The result of calling \code{rhs(lhs)}. 16 | } 17 | \description{ 18 | See \verb{magrittr::[\\\%>\\\%][magrittr::pipe]} for details. 19 | } 20 | \keyword{internal} 21 | -------------------------------------------------------------------------------- /tests/testthat/test-list_files.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # list_files() 3 | 4 | function_name <- "list_files" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "openai_api_key", 9 | argument_type = "string", 10 | allow_null = FALSE 11 | ) 12 | 13 | test_argument_validation( 14 | function_name = function_name, 15 | argument_name = "openai_organization", 16 | argument_type = "string", 17 | allow_null = TRUE 18 | ) 19 | -------------------------------------------------------------------------------- /tests/testthat/test-list_models.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # list_models() 3 | 4 | function_name <- "list_models" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "openai_api_key", 9 | argument_type = "string", 10 | allow_null = FALSE 11 | ) 12 | 13 | test_argument_validation( 14 | function_name = function_name, 15 | argument_name = "openai_organization", 16 | argument_type = "string", 17 | allow_null = TRUE 18 | ) 19 | -------------------------------------------------------------------------------- /tests/manual/test-create_image.R: -------------------------------------------------------------------------------- 1 | create_image( 2 | prompt = "An astronaut riding a horse in a photorealistic style", 3 | n = 1, 4 | size = "256x256", 5 | response_format = "url" 6 | ) 7 | 8 | # test `response_format = "b64_json"` 9 | 10 | # response <- create_image( 11 | # prompt = "An astronaut riding a horse in a photorealistic style", 12 | # size = "256x256", 13 | # response_format = "b64_json" 14 | # ) 15 | # 16 | # image <- jsonlite::base64_dec(response$data$b64_json) %>% 17 | # png::readPNG() 18 | # 19 | # grid::grid.raster(image) 20 | -------------------------------------------------------------------------------- /tests/testthat/test-list_engines.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # list_engines() 3 | 4 | function_name <- "list_engines" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "openai_api_key", 9 | argument_type = "string", 10 | allow_null = FALSE, 11 | suppress_warnings = TRUE 12 | ) 13 | 14 | test_argument_validation( 15 | function_name = function_name, 16 | argument_name = "openai_organization", 17 | argument_type = "string", 18 | allow_null = TRUE, 19 | suppress_warnings = TRUE 20 | ) 21 | -------------------------------------------------------------------------------- /tests/manual/test-create_chat_completion.R: -------------------------------------------------------------------------------- 1 | create_chat_completion( 2 | model = "gpt-3.5-turbo", 3 | messages = list( 4 | list( 5 | "role" = "system", 6 | "content" = "You are a helpful assistant." 7 | ), 8 | list( 9 | "role" = "user", 10 | "content" = "Who won the world series in 2020?" 11 | ), 12 | list( 13 | "role" = "assistant", 14 | "content" = "The Los Angeles Dodgers won the World Series in 2020." 15 | ), 16 | list( 17 | "role" = "user", 18 | "content" = "Where was it played?" 19 | ) 20 | ) 21 | ) 22 | -------------------------------------------------------------------------------- /tests/manual/test-create_fine_tune.R: -------------------------------------------------------------------------------- 1 | training_file <- system.file( 2 | "extdata", "sport_prepared_train.jsonl", package = "openai" 3 | ) 4 | validation_file <- system.file( 5 | "extdata", "sport_prepared_train.jsonl", package = "openai" 6 | ) 7 | 8 | training_info <- upload_file(training_file, "fine-tune") 9 | validation_info <- upload_file(validation_file, "fine-tune") 10 | 11 | info <- create_fine_tune( 12 | training_file = training_info$id, 13 | validation_file = validation_info$id, 14 | model = "ada", 15 | compute_classification_metrics = TRUE, 16 | classification_positive_class = " baseball" # Mind space in front 17 | ) 18 | -------------------------------------------------------------------------------- /tests/testthat/test-cancel_fine_tune.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # cancel_fine_tune() 3 | 4 | test_argument_validation( 5 | function_name = "cancel_fine_tune", 6 | argument_name = "fine_tune_id", 7 | argument_type = "string", 8 | allow_null = FALSE 9 | ) 10 | 11 | test_argument_validation( 12 | function_name = "cancel_fine_tune", 13 | argument_name = "openai_api_key", 14 | argument_type = "string", 15 | allow_null = FALSE 16 | ) 17 | 18 | test_argument_validation( 19 | function_name = "cancel_fine_tune", 20 | argument_name = "openai_organization", 21 | argument_type = "string", 22 | allow_null = TRUE 23 | ) 24 | -------------------------------------------------------------------------------- /tests/testthat/test-retrieve_fine_tune.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # retrieve_fine_tune() 3 | 4 | test_argument_validation( 5 | function_name = "retrieve_fine_tune", 6 | argument_name = "fine_tune_id", 7 | argument_type = "string", 8 | allow_null = FALSE 9 | ) 10 | 11 | test_argument_validation( 12 | function_name = "retrieve_fine_tune", 13 | argument_name = "openai_api_key", 14 | argument_type = "string", 15 | allow_null = FALSE 16 | ) 17 | 18 | test_argument_validation( 19 | function_name = "retrieve_fine_tune", 20 | argument_name = "openai_organization", 21 | argument_type = "string", 22 | allow_null = TRUE 23 | ) 24 | -------------------------------------------------------------------------------- /tests/testthat/test-delete_file.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # delete_file() 3 | 4 | function_name <- "delete_file" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "file_id", 9 | argument_type = "string", 10 | allow_null = FALSE 11 | ) 12 | 13 | test_argument_validation( 14 | function_name = function_name, 15 | argument_name = "openai_api_key", 16 | argument_type = "string", 17 | allow_null = FALSE 18 | ) 19 | 20 | test_argument_validation( 21 | function_name = function_name, 22 | argument_name = "openai_organization", 23 | argument_type = "string", 24 | allow_null = TRUE 25 | ) 26 | -------------------------------------------------------------------------------- /tests/testthat/test-retrieve_file.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # retrieve_file() 3 | 4 | function_name <- "retrieve_file" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "file_id", 9 | argument_type = "string", 10 | allow_null = FALSE 11 | ) 12 | 13 | test_argument_validation( 14 | function_name = function_name, 15 | argument_name = "openai_api_key", 16 | argument_type = "string", 17 | allow_null = FALSE 18 | ) 19 | 20 | test_argument_validation( 21 | function_name = function_name, 22 | argument_name = "openai_organization", 23 | argument_type = "string", 24 | allow_null = TRUE 25 | ) 26 | -------------------------------------------------------------------------------- /tests/testthat/test-retrieve_model.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # retrieve_model() 3 | 4 | function_name <- "retrieve_model" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "model", 9 | argument_type = "string", 10 | allow_null = FALSE 11 | ) 12 | 13 | test_argument_validation( 14 | function_name = function_name, 15 | argument_name = "openai_api_key", 16 | argument_type = "string", 17 | allow_null = FALSE 18 | ) 19 | 20 | test_argument_validation( 21 | function_name = function_name, 22 | argument_name = "openai_organization", 23 | argument_type = "string", 24 | allow_null = TRUE 25 | ) 26 | -------------------------------------------------------------------------------- /tests/testthat/test-delete_fine_tune_model.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # delete_fine_tune_model() 3 | 4 | test_argument_validation( 5 | function_name = "delete_fine_tune_model", 6 | argument_name = "model", 7 | argument_type = "string", 8 | allow_null = FALSE 9 | ) 10 | 11 | test_argument_validation( 12 | function_name = "delete_fine_tune_model", 13 | argument_name = "openai_api_key", 14 | argument_type = "string", 15 | allow_null = FALSE 16 | ) 17 | 18 | test_argument_validation( 19 | function_name = "delete_fine_tune_model", 20 | argument_name = "openai_organization", 21 | argument_type = "string", 22 | allow_null = TRUE 23 | ) 24 | -------------------------------------------------------------------------------- /tests/testthat/test-retrieve_file_content.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # retrieve_file_content() 3 | 4 | function_name <- "retrieve_file_content" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "file_id", 9 | argument_type = "string", 10 | allow_null = FALSE 11 | ) 12 | 13 | test_argument_validation( 14 | function_name = function_name, 15 | argument_name = "openai_api_key", 16 | argument_type = "string", 17 | allow_null = FALSE 18 | ) 19 | 20 | test_argument_validation( 21 | function_name = function_name, 22 | argument_name = "openai_organization", 23 | argument_type = "string", 24 | allow_null = TRUE 25 | ) 26 | -------------------------------------------------------------------------------- /tests/manual/test-cancel_fine_tune.R: -------------------------------------------------------------------------------- 1 | training_file <- system.file( 2 | "extdata", "sport_prepared_train.jsonl", package = "openai" 3 | ) 4 | validation_file <- system.file( 5 | "extdata", "sport_prepared_train.jsonl", package = "openai" 6 | ) 7 | 8 | training_info <- upload_file(training_file, "fine-tune") 9 | validation_info <- upload_file(validation_file, "fine-tune") 10 | 11 | info <- create_fine_tune( 12 | training_file = training_info$id, 13 | validation_file = validation_info$id, 14 | model = "ada", 15 | compute_classification_metrics = TRUE, 16 | classification_positive_class = " baseball" # Mind space in front 17 | ) 18 | 19 | id <- ifelse( 20 | length(info$data$id) > 1, 21 | info$data$id[length(info$data$id)], 22 | info$data$id 23 | ) 24 | 25 | cancel_fine_tune(fine_tune_id = id) 26 | -------------------------------------------------------------------------------- /tests/manual/test-retrieve_fine_tune.R: -------------------------------------------------------------------------------- 1 | training_file <- system.file( 2 | "extdata", "sport_prepared_train.jsonl", package = "openai" 3 | ) 4 | validation_file <- system.file( 5 | "extdata", "sport_prepared_train.jsonl", package = "openai" 6 | ) 7 | 8 | training_info <- upload_file(training_file, "fine-tune") 9 | validation_info <- upload_file(validation_file, "fine-tune") 10 | 11 | info <- create_fine_tune( 12 | training_file = training_info$id, 13 | validation_file = validation_info$id, 14 | model = "ada", 15 | compute_classification_metrics = TRUE, 16 | classification_positive_class = " baseball" # Mind space in front 17 | ) 18 | 19 | id <- ifelse( 20 | length(info$data$id) > 1, 21 | info$data$id[length(info$data$id)], 22 | info$data$id 23 | ) 24 | 25 | retrieve_fine_tune(fine_tune_id = id) 26 | -------------------------------------------------------------------------------- /tests/manual/test-list_fine_tune_events.R: -------------------------------------------------------------------------------- 1 | training_file <- system.file( 2 | "extdata", "sport_prepared_train.jsonl", package = "openai" 3 | ) 4 | validation_file <- system.file( 5 | "extdata", "sport_prepared_train.jsonl", package = "openai" 6 | ) 7 | 8 | training_info <- upload_file(training_file, "fine-tune") 9 | validation_info <- upload_file(validation_file, "fine-tune") 10 | 11 | info <- create_fine_tune( 12 | training_file = training_info$id, 13 | validation_file = validation_info$id, 14 | model = "ada", 15 | compute_classification_metrics = TRUE, 16 | classification_positive_class = " baseball" # Mind space in front 17 | ) 18 | 19 | id <- ifelse( 20 | length(info$data$id) > 1, 21 | info$data$id[length(info$data$id)], 22 | info$data$id 23 | ) 24 | 25 | list_fine_tune_events(fine_tune_id = id) 26 | -------------------------------------------------------------------------------- /.github/workflows/R-CMD-check.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/master/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | 9 | name: R-CMD-check 10 | 11 | jobs: 12 | R-CMD-check: 13 | runs-on: ubuntu-latest 14 | env: 15 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 16 | R_KEEP_PKG_SOURCE: yes 17 | steps: 18 | - uses: actions/checkout@v2 19 | 20 | - uses: r-lib/actions/setup-r@v2 21 | with: 22 | use-public-rspm: true 23 | 24 | - uses: r-lib/actions/setup-r-dependencies@v2 25 | with: 26 | extra-packages: rcmdcheck 27 | 28 | - uses: r-lib/actions/check-r-package@v2 29 | -------------------------------------------------------------------------------- /tests/testthat/test-retrieve_engine.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # retrieve_engine() 3 | 4 | function_name <- "retrieve_engine" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "engine_id", 9 | argument_type = "string", 10 | allow_null = FALSE, 11 | suppress_warnings = TRUE 12 | ) 13 | 14 | test_argument_validation( 15 | function_name = function_name, 16 | argument_name = "openai_api_key", 17 | argument_type = "string", 18 | allow_null = FALSE, 19 | suppress_warnings = TRUE 20 | ) 21 | 22 | test_argument_validation( 23 | function_name = function_name, 24 | argument_name = "openai_organization", 25 | argument_type = "string", 26 | allow_null = TRUE, 27 | suppress_warnings = TRUE 28 | ) 29 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export("%>%") 4 | export(cancel_fine_tune) 5 | export(create_chat_completion) 6 | export(create_completion) 7 | export(create_edit) 8 | export(create_embedding) 9 | export(create_fine_tune) 10 | export(create_image) 11 | export(create_image_edit) 12 | export(create_image_variation) 13 | export(create_moderation) 14 | export(create_transcription) 15 | export(create_translation) 16 | export(delete_file) 17 | export(delete_fine_tune_model) 18 | export(list_engines) 19 | export(list_files) 20 | export(list_fine_tune_events) 21 | export(list_fine_tunes) 22 | export(list_models) 23 | export(retrieve_engine) 24 | export(retrieve_file) 25 | export(retrieve_file_content) 26 | export(retrieve_fine_tune) 27 | export(retrieve_model) 28 | export(upload_file) 29 | importFrom(lifecycle,deprecated) 30 | importFrom(magrittr,"%>%") 31 | -------------------------------------------------------------------------------- /tests/testthat/test-upload_file.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # upload_file() 3 | 4 | function_name <- "upload_file" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "file", 9 | argument_type = "string", 10 | allow_null = FALSE 11 | ) 12 | 13 | test_argument_validation( 14 | function_name = function_name, 15 | argument_name = "purpose", 16 | argument_type = "string", 17 | allow_null = FALSE 18 | ) 19 | 20 | test_argument_validation( 21 | function_name = function_name, 22 | argument_name = "openai_api_key", 23 | argument_type = "string", 24 | allow_null = FALSE 25 | ) 26 | 27 | test_argument_validation( 28 | function_name = function_name, 29 | argument_name = "openai_organization", 30 | argument_type = "string", 31 | allow_null = TRUE 32 | ) 33 | -------------------------------------------------------------------------------- /.github/workflows/test-coverage.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/master/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | 9 | name: test-coverage 10 | 11 | jobs: 12 | test-coverage: 13 | runs-on: ubuntu-latest 14 | env: 15 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 16 | 17 | steps: 18 | - uses: actions/checkout@v2 19 | 20 | - uses: r-lib/actions/setup-r@v2 21 | with: 22 | use-public-rspm: true 23 | 24 | - uses: r-lib/actions/setup-r-dependencies@v2 25 | with: 26 | extra-packages: any::covr 27 | needs: coverage 28 | 29 | - name: Test coverage 30 | run: covr::codecov(quiet = FALSE) 31 | shell: Rscript {0} 32 | -------------------------------------------------------------------------------- /tests/testthat/test-create_moderation.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # create_moderation() 3 | 4 | function_name <- "create_moderation" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "input", 9 | argument_type = "character", 10 | allow_null = FALSE 11 | ) 12 | 13 | test_argument_validation( 14 | function_name = function_name, 15 | argument_name = "model", 16 | argument_type = "string", 17 | allow_null = FALSE 18 | ) 19 | 20 | test_argument_validation( 21 | function_name = function_name, 22 | argument_name = "openai_api_key", 23 | argument_type = "string", 24 | allow_null = FALSE 25 | ) 26 | 27 | test_argument_validation( 28 | function_name = function_name, 29 | argument_name = "openai_organization", 30 | argument_type = "string", 31 | allow_null = TRUE 32 | ) 33 | -------------------------------------------------------------------------------- /tests/testthat/test-list_fine_tune_events.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # list_fine_tune_events() 3 | 4 | test_argument_validation( 5 | function_name = "list_fine_tune_events", 6 | argument_name = "fine_tune_id", 7 | argument_type = "string", 8 | allow_null = FALSE 9 | ) 10 | 11 | test_argument_validation( 12 | function_name = "list_fine_tune_events", 13 | argument_name = "stream", 14 | argument_type = "flag", 15 | allow_null = FALSE 16 | ) 17 | 18 | test_argument_validation( 19 | function_name = "list_fine_tune_events", 20 | argument_name = "openai_api_key", 21 | argument_type = "string", 22 | allow_null = FALSE 23 | ) 24 | 25 | test_argument_validation( 26 | function_name = "list_fine_tune_events", 27 | argument_name = "openai_organization", 28 | argument_type = "string", 29 | allow_null = TRUE 30 | ) 31 | -------------------------------------------------------------------------------- /man/figures/lifecycle-stable.svg: -------------------------------------------------------------------------------- 1 | lifecyclelifecyclestablestable -------------------------------------------------------------------------------- /man/figures/lifecycle-defunct.svg: -------------------------------------------------------------------------------- 1 | lifecyclelifecycledefunctdefunct -------------------------------------------------------------------------------- /man/figures/lifecycle-archived.svg: -------------------------------------------------------------------------------- 1 | lifecyclelifecyclearchivedarchived -------------------------------------------------------------------------------- /man/figures/lifecycle-maturing.svg: -------------------------------------------------------------------------------- 1 | lifecyclelifecyclematuringmaturing -------------------------------------------------------------------------------- /man/figures/lifecycle-deprecated.svg: -------------------------------------------------------------------------------- 1 | lifecyclelifecycledeprecateddeprecated -------------------------------------------------------------------------------- /man/figures/lifecycle-superseded.svg: -------------------------------------------------------------------------------- 1 | lifecyclelifecyclesupersededsuperseded -------------------------------------------------------------------------------- /man/figures/lifecycle-experimental.svg: -------------------------------------------------------------------------------- 1 | lifecyclelifecycleexperimentalexperimental -------------------------------------------------------------------------------- /man/figures/lifecycle-questioning.svg: -------------------------------------------------------------------------------- 1 | lifecyclelifecyclequestioningquestioning -------------------------------------------------------------------------------- /tests/testthat/test-create_embedding.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # create_embedding() 3 | 4 | function_name <- "create_embedding" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "model", 9 | argument_type = "string", 10 | allow_null = FALSE 11 | ) 12 | 13 | test_argument_validation( 14 | function_name = function_name, 15 | argument_name = "input", 16 | argument_type = "character", 17 | allow_null = FALSE 18 | ) 19 | 20 | test_argument_validation( 21 | function_name = function_name, 22 | argument_name = "user", 23 | argument_type = "string", 24 | allow_null = TRUE 25 | ) 26 | 27 | test_argument_validation( 28 | function_name = function_name, 29 | argument_name = "openai_api_key", 30 | argument_type = "string", 31 | allow_null = FALSE 32 | ) 33 | 34 | test_argument_validation( 35 | function_name = function_name, 36 | argument_name = "openai_organization", 37 | argument_type = "string", 38 | allow_null = TRUE 39 | ) 40 | -------------------------------------------------------------------------------- /.github/workflows/pkgdown.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/master/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | release: 7 | types: [published] 8 | workflow_dispatch: 9 | 10 | name: pkgdown 11 | 12 | jobs: 13 | pkgdown: 14 | runs-on: ubuntu-latest 15 | env: 16 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 17 | steps: 18 | - uses: actions/checkout@v2 19 | 20 | - uses: r-lib/actions/setup-pandoc@v2 21 | 22 | - uses: r-lib/actions/setup-r@v2 23 | with: 24 | use-public-rspm: true 25 | 26 | - uses: r-lib/actions/setup-r-dependencies@v2 27 | with: 28 | extra-packages: any::pkgdown, local::. 29 | needs: website 30 | 31 | - name: Deploy package 32 | run: | 33 | git config --local user.name "$GITHUB_ACTOR" 34 | git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" 35 | Rscript -e 'pkgdown::deploy_to_branch(new_process = FALSE)' 36 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: openai 2 | Type: Package 3 | Title: R Wrapper for OpenAI API 4 | Version: 0.4.1 5 | Date: 2023-03-14 6 | Authors@R: 7 | person("Iegor", "Rudnytskyi", , "iegor.rudnytskyi@gmail.com", c("aut", "cre")) 8 | Description: An R wrapper of OpenAI API endpoints (see 9 | for details). This package 10 | covers Models, Completions, Chat, Edits, Images, Embeddings, Audio, Files, 11 | Fine-tunes, Moderations, and legacy Engines endpoints. 12 | License: MIT + file LICENSE 13 | Encoding: UTF-8 14 | LazyData: true 15 | RoxygenNote: 7.2.1 16 | URL: https://github.com/irudnyts/openai, 17 | https://irudnyts.github.io/openai/ 18 | BugReports: https://github.com/irudnyts/openai/issues 19 | Depends: 20 | R (>= 3.5) 21 | Imports: 22 | assertthat (>= 0.2.1), 23 | glue (>= 1.6.2), 24 | httr (>= 1.4.3), 25 | jsonlite (>= 1.8.0), 26 | lifecycle (>= 1.0.1), 27 | magrittr (>= 2.0.3) 28 | Suggests: 29 | testthat (>= 3.0.0), 30 | purrr (>= 0.3.4), 31 | covr (>= 3.5.1) 32 | Config/testthat/edition: 3 33 | Roxygen: list(markdown = TRUE) 34 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2022 openai authors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /cran-comments.md: -------------------------------------------------------------------------------- 1 | ## Test environments 2 | 3 | * local MacOS 12.5.1, R 3.5.1 and R 4.2.0 4 | * R-hub Windows Server 2022, R-devel, 64 bit 5 | * R-hub Fedora Linux, R-devel, clang, gfortran 6 | * R-hub Ubuntu Linux 20.04.1 LTS, R-release, GCC 7 | * win-builder (R-release, R-devel) 8 | 9 | ## R CMD check results 10 | 11 | 0 errors | 0 warnings | 1 note 12 | 13 | * Windows (Server 2022, R-devel 64-bit): 14 | 15 | ``` 16 | * checking for detritus in the temp directory ... NOTE 17 | Found the following files/directories: 18 | 'lastMiKTeXException' 19 | ``` 20 | 21 | It seems like a leftover auto-generated file since none of the examples in the documentation launches the browser. 22 | 23 | ## Downstream dependencies 24 | 25 | There are currently no downstream dependencies for this package. 26 | 27 | ## Resubmission 28 | 29 | This is a resubmission. In this version I have: 30 | 31 | * Relaxed validation of `model` argument in functions `create_chat_completion()`, `create_fine_tune()`, `create_moderation()`, `create_embedding()`, `create_transcription()`, and `create_translation()`. Otherwise, each time OpenAI will roll out a new model, the list of models has to be updated 32 | -------------------------------------------------------------------------------- /tests/testthat/test-create_translation.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # create_translation() 3 | 4 | function_name <- "create_translation" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "file", 9 | argument_type = "string", 10 | allow_null = FALSE 11 | ) 12 | 13 | test_argument_validation( 14 | function_name = function_name, 15 | argument_name = "model", 16 | argument_type = "string", 17 | allow_null = FALSE 18 | ) 19 | 20 | test_argument_validation( 21 | function_name = function_name, 22 | argument_name = "prompt", 23 | argument_type = "character", 24 | allow_null = TRUE 25 | ) 26 | 27 | test_argument_validation( 28 | function_name = function_name, 29 | argument_name = "response_format", 30 | argument_type = "string", 31 | allow_null = FALSE 32 | ) 33 | 34 | test_argument_validation( 35 | function_name = function_name, 36 | argument_name = "temperature", 37 | argument_type = "number", 38 | allow_null = FALSE 39 | ) 40 | 41 | test_argument_validation( 42 | function_name = function_name, 43 | argument_name = "openai_organization", 44 | argument_type = "string", 45 | allow_null = TRUE 46 | ) 47 | -------------------------------------------------------------------------------- /NEWS.md: -------------------------------------------------------------------------------- 1 | # openai 0.4.1 2 | 3 | * Relax validation of `model` argument in functions `create_chat_completion()`, `create_fine_tune()`, `create_moderation()`, `create_embedding()`, `create_transcription()`, and `create_translation()`. Otherwise, each time OpenAI will roll out a new model, the list of models has to be updated 4 | 5 | # openai 0.4.0 6 | 7 | * Add endpoints `create_chat_completion()`, `create_transcription()`, and `create_translation()` 8 | * Downgrade R dependence to 3.5 9 | * Remove redundant options of `upload_file()`'s argument `purpose`, namely `"search"`, `"answers"`, and `"classifications"` 10 | * Update links in documentation 11 | 12 | # openai 0.3.0 13 | 14 | * Remove outdated endpoints `create_answer()`, `create_classification()`, and `create_search()` 15 | * Deprecate `retrieve_engine()` and `list_engines()` 16 | * Deprecate `engine_id` argument in `create_completion()`, `create_edit()`, and `create_embedding()` 17 | 18 | # openai 0.2.0 19 | 20 | * Add new DALL-E functions, namely `create_image()`, `create_image_edit()`, and `create_image_variation()` 21 | 22 | * Add the new function `create_moderation()` that checks whether the input violates OpenAI's Content Policy 23 | 24 | * Add new model-related functions, namely `list_models()` and `retrieve_model()` 25 | 26 | # openai 0.1.0 27 | 28 | * Initial version 29 | -------------------------------------------------------------------------------- /man/list_models.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/list_models.R 3 | \name{list_models} 4 | \alias{list_models} 5 | \title{List models} 6 | \usage{ 7 | list_models( 8 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 9 | openai_organization = NULL 10 | ) 11 | } 12 | \arguments{ 13 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 14 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 15 | character vector. Specifies OpenAI API key.} 16 | 17 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 18 | character vector. Specifies OpenAI organization.} 19 | } 20 | \value{ 21 | Returns a list, an element of which is a data frame containing 22 | information about models. 23 | } 24 | \description{ 25 | Lists the currently available models, and provides basic information about 26 | each one such as the owner and availability. See 27 | \href{https://platform.openai.com/docs/api-reference/models/list}{this page} for 28 | details. 29 | } 30 | \details{ 31 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/models/list}{official documentation}. 32 | } 33 | \examples{ 34 | \dontrun{ 35 | list_models() 36 | } 37 | } 38 | \seealso{ 39 | Other model functions: 40 | \code{\link{retrieve_model}()} 41 | } 42 | \concept{model functions} 43 | -------------------------------------------------------------------------------- /man/list_files.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/list_files.R 3 | \name{list_files} 4 | \alias{list_files} 5 | \title{List files} 6 | \usage{ 7 | list_files( 8 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 9 | openai_organization = NULL 10 | ) 11 | } 12 | \arguments{ 13 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 14 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 15 | character vector. Specifies OpenAI API key.} 16 | 17 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 18 | character vector. Specifies OpenAI organization.} 19 | } 20 | \value{ 21 | Returns a list, an element of which is a data frame containing 22 | information about files. 23 | } 24 | \description{ 25 | Lists files uploaded by user's organization. See \href{https://platform.openai.com/docs/api-reference/files/list}{this page} for details. 26 | } 27 | \details{ 28 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/files/list}{official documentation}. 29 | } 30 | \examples{ 31 | \dontrun{ 32 | list_files() 33 | } 34 | } 35 | \seealso{ 36 | Other file functions: 37 | \code{\link{delete_file}()}, 38 | \code{\link{retrieve_file_content}()}, 39 | \code{\link{retrieve_file}()}, 40 | \code{\link{upload_file}()} 41 | } 42 | \concept{file functions} 43 | -------------------------------------------------------------------------------- /tests/testthat/test-create_image.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # create_image() 3 | 4 | function_name <- "create_image" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "prompt", 9 | argument_type = "string", 10 | allow_null = FALSE 11 | ) 12 | 13 | test_argument_validation( 14 | function_name = function_name, 15 | argument_name = "n", 16 | argument_type = "count", 17 | allow_null = FALSE 18 | ) 19 | 20 | test_argument_validation( 21 | function_name = function_name, 22 | argument_name = "size", 23 | argument_type = "string", 24 | allow_null = FALSE 25 | ) 26 | 27 | test_argument_validation( 28 | function_name = function_name, 29 | argument_name = "response_format", 30 | argument_type = "string", 31 | allow_null = FALSE 32 | ) 33 | 34 | test_argument_validation( 35 | function_name = function_name, 36 | argument_name = "user", 37 | argument_type = "string", 38 | allow_null = TRUE 39 | ) 40 | 41 | test_argument_validation( 42 | function_name = function_name, 43 | argument_name = "openai_api_key", 44 | argument_type = "string", 45 | allow_null = FALSE 46 | ) 47 | 48 | test_argument_validation( 49 | function_name = function_name, 50 | argument_name = "openai_organization", 51 | argument_type = "string", 52 | allow_null = TRUE 53 | ) 54 | -------------------------------------------------------------------------------- /tests/testthat/test-create_edit.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # create_edit() 3 | 4 | function_name <- "create_edit" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "model", 9 | argument_type = "string", 10 | allow_null = FALSE 11 | ) 12 | 13 | test_argument_validation( 14 | function_name = function_name, 15 | argument_name = "input", 16 | argument_type = "string", 17 | allow_null = FALSE 18 | ) 19 | 20 | test_argument_validation( 21 | function_name = function_name, 22 | argument_name = "instruction", 23 | argument_type = "character", 24 | allow_null = FALSE 25 | ) 26 | 27 | test_argument_validation( 28 | function_name = function_name, 29 | argument_name = "temperature", 30 | argument_type = "number", 31 | allow_null = FALSE 32 | ) 33 | 34 | test_argument_validation( 35 | function_name = function_name, 36 | argument_name = "top_p", 37 | argument_type = "number", 38 | allow_null = FALSE 39 | ) 40 | 41 | test_argument_validation( 42 | function_name = function_name, 43 | argument_name = "openai_api_key", 44 | argument_type = "string", 45 | allow_null = FALSE 46 | ) 47 | 48 | test_argument_validation( 49 | function_name = function_name, 50 | argument_name = "openai_organization", 51 | argument_type = "string", 52 | allow_null = TRUE 53 | ) 54 | -------------------------------------------------------------------------------- /tests/testthat/test-create_image_variation.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # create_image_edit() 3 | 4 | function_name <- "create_image_variation" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "image", 9 | argument_type = "string", 10 | allow_null = FALSE 11 | ) 12 | 13 | test_argument_validation( 14 | function_name = function_name, 15 | argument_name = "n", 16 | argument_type = "count", 17 | allow_null = FALSE 18 | ) 19 | 20 | test_argument_validation( 21 | function_name = function_name, 22 | argument_name = "size", 23 | argument_type = "string", 24 | allow_null = FALSE 25 | ) 26 | 27 | test_argument_validation( 28 | function_name = function_name, 29 | argument_name = "response_format", 30 | argument_type = "string", 31 | allow_null = FALSE 32 | ) 33 | 34 | test_argument_validation( 35 | function_name = function_name, 36 | argument_name = "user", 37 | argument_type = "string", 38 | allow_null = TRUE 39 | ) 40 | 41 | test_argument_validation( 42 | function_name = function_name, 43 | argument_name = "openai_api_key", 44 | argument_type = "string", 45 | allow_null = FALSE 46 | ) 47 | 48 | test_argument_validation( 49 | function_name = function_name, 50 | argument_name = "openai_organization", 51 | argument_type = "string", 52 | allow_null = TRUE 53 | ) 54 | -------------------------------------------------------------------------------- /man/retrieve_model.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/retrieve_model.R 3 | \name{retrieve_model} 4 | \alias{retrieve_model} 5 | \title{Retrieve model} 6 | \usage{ 7 | retrieve_model( 8 | model, 9 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 10 | openai_organization = NULL 11 | ) 12 | } 13 | \arguments{ 14 | \item{model}{required; a length one character vector.} 15 | 16 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 17 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 18 | character vector. Specifies OpenAI API key.} 19 | 20 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 21 | character vector. Specifies OpenAI organization.} 22 | } 23 | \value{ 24 | Returns a list, elements of which contain information about the 25 | model. 26 | } 27 | \description{ 28 | Retrieves a model instance, providing basic information about the model 29 | such as the owner and permissioning. See \href{https://platform.openai.com/docs/api-reference/models/retrieve}{this page} for 30 | details. 31 | } 32 | \details{ 33 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/models/retrieve}{official documentation}. 34 | } 35 | \examples{ 36 | \dontrun{ 37 | retrieve_model("text-davinci-002") 38 | } 39 | } 40 | \seealso{ 41 | Other model functions: 42 | \code{\link{list_models}()} 43 | } 44 | \concept{model functions} 45 | -------------------------------------------------------------------------------- /man/create_moderation.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/create_moderation.R 3 | \name{create_moderation} 4 | \alias{create_moderation} 5 | \title{Create moderation} 6 | \usage{ 7 | create_moderation( 8 | input, 9 | model, 10 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 11 | openai_organization = NULL 12 | ) 13 | } 14 | \arguments{ 15 | \item{input}{required; an arbitrary length character vector.} 16 | 17 | \item{model}{required; a length one character vector.} 18 | 19 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 20 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 21 | character vector. Specifies OpenAI API key.} 22 | 23 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 24 | character vector. Specifies OpenAI organization.} 25 | } 26 | \value{ 27 | Returns a list, elements of which contain information about the 28 | model. 29 | } 30 | \description{ 31 | Classifies if text violates OpenAI's Content Policy. See \href{https://platform.openai.com/docs/api-reference/moderations/create}{this page} for 32 | details. 33 | } 34 | \details{ 35 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/completions/create}{official documentation}. 36 | } 37 | \examples{ 38 | \dontrun{ 39 | create_moderation( 40 | input = "I want to kill them all.", 41 | model = "text-moderation-stable" 42 | ) 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /man/list_fine_tunes.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/list_fine_tunes.R 3 | \name{list_fine_tunes} 4 | \alias{list_fine_tunes} 5 | \title{Lists fine-tunes} 6 | \usage{ 7 | list_fine_tunes( 8 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 9 | openai_organization = NULL 10 | ) 11 | } 12 | \arguments{ 13 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 14 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 15 | character vector. Specifies OpenAI API key.} 16 | 17 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 18 | character vector. Specifies OpenAI organization.} 19 | } 20 | \value{ 21 | Returns a list, an element of which is a data frame containing 22 | information about fine-tunes. 23 | } 24 | \description{ 25 | Lists organization's fine-tuning jobs. See \href{https://platform.openai.com/docs/api-reference/fine-tunes/list}{this page} for 26 | details. 27 | } 28 | \details{ 29 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/fine-tunes/list}{official documentation}. 30 | } 31 | \examples{ 32 | \dontrun{ 33 | list_fine_tunes() 34 | } 35 | } 36 | \seealso{ 37 | Other fine-tune functions: 38 | \code{\link{cancel_fine_tune}()}, 39 | \code{\link{create_fine_tune}()}, 40 | \code{\link{delete_fine_tune_model}()}, 41 | \code{\link{list_fine_tune_events}()}, 42 | \code{\link{retrieve_fine_tune}()} 43 | } 44 | \concept{fine-tune functions} 45 | -------------------------------------------------------------------------------- /man/retrieve_file_content.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/retrieve_file_content.R 3 | \name{retrieve_file_content} 4 | \alias{retrieve_file_content} 5 | \title{Retrieve file content} 6 | \usage{ 7 | retrieve_file_content( 8 | file_id, 9 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 10 | openai_organization = NULL 11 | ) 12 | } 13 | \arguments{ 14 | \item{file_id}{required; a length one character vector.} 15 | 16 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 17 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 18 | character vector. Specifies OpenAI API key.} 19 | 20 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 21 | character vector. Specifies OpenAI organization.} 22 | } 23 | \value{ 24 | Returns a list, an element of which contains the content of the file. 25 | } 26 | \description{ 27 | Returns the content of the specified file. See \href{https://platform.openai.com/docs/api-reference/files/retrieve-content}{this page} for 28 | details. Please note that only output files are allowed to be downloaded, not 29 | the input ones. 30 | } 31 | \details{ 32 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/files/retrieve-content}{official documentation}. 33 | } 34 | \seealso{ 35 | Other file functions: 36 | \code{\link{delete_file}()}, 37 | \code{\link{list_files}()}, 38 | \code{\link{retrieve_file}()}, 39 | \code{\link{upload_file}()} 40 | } 41 | \concept{file functions} 42 | -------------------------------------------------------------------------------- /man/delete_file.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/delete_file.R 3 | \name{delete_file} 4 | \alias{delete_file} 5 | \title{Delete file} 6 | \usage{ 7 | delete_file( 8 | file_id, 9 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 10 | openai_organization = NULL 11 | ) 12 | } 13 | \arguments{ 14 | \item{file_id}{required; a length one character vector.} 15 | 16 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 17 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 18 | character vector. Specifies OpenAI API key.} 19 | 20 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 21 | character vector. Specifies OpenAI organization.} 22 | } 23 | \value{ 24 | Returns a list, elements of which contains ID of the deleted file and 25 | status whether the file is deleted. 26 | } 27 | \description{ 28 | Deletes a file. See \href{https://platform.openai.com/docs/api-reference/files/delete}{this page} for details. 29 | } 30 | \details{ 31 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/files/delete}{official documentation}. 32 | } 33 | \examples{ 34 | \dontrun{ 35 | file <- system.file("extdata", "classification-file.jsonl", package = "openai") 36 | file_info <- upload_file(file = file, purpose = "classification") 37 | delete_file(file_info$id) 38 | } 39 | } 40 | \seealso{ 41 | Other file functions: 42 | \code{\link{list_files}()}, 43 | \code{\link{retrieve_file_content}()}, 44 | \code{\link{retrieve_file}()}, 45 | \code{\link{upload_file}()} 46 | } 47 | \concept{file functions} 48 | -------------------------------------------------------------------------------- /man/retrieve_file.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/retrieve_file.R 3 | \name{retrieve_file} 4 | \alias{retrieve_file} 5 | \title{Retrieve file} 6 | \usage{ 7 | retrieve_file( 8 | file_id, 9 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 10 | openai_organization = NULL 11 | ) 12 | } 13 | \arguments{ 14 | \item{file_id}{required; a length one character vector.} 15 | 16 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 17 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 18 | character vector. Specifies OpenAI API key.} 19 | 20 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 21 | character vector. Specifies OpenAI organization.} 22 | } 23 | \value{ 24 | Returns a list, elements of which contains information about the 25 | file. 26 | } 27 | \description{ 28 | Provides information about a specific file. See \href{https://platform.openai.com/docs/api-reference/files/retrieve}{this page} for details. 29 | } 30 | \details{ 31 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/files/retrieve}{official documentation}. 32 | } 33 | \examples{ 34 | \dontrun{ 35 | file <- system.file("extdata", "classification-file.jsonl", package = "openai") 36 | file_info <- upload_file(file = file, purpose = "classification") 37 | retrieve_file(file_info$id) 38 | } 39 | } 40 | \seealso{ 41 | Other file functions: 42 | \code{\link{delete_file}()}, 43 | \code{\link{list_files}()}, 44 | \code{\link{retrieve_file_content}()}, 45 | \code{\link{upload_file}()} 46 | } 47 | \concept{file functions} 48 | -------------------------------------------------------------------------------- /tests/testthat/test-create_transcription.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # create_transcription() 3 | 4 | function_name <- "create_transcription" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "file", 9 | argument_type = "string", 10 | allow_null = FALSE 11 | ) 12 | 13 | test_argument_validation( 14 | function_name = function_name, 15 | argument_name = "model", 16 | argument_type = "string", 17 | allow_null = FALSE 18 | ) 19 | 20 | test_argument_validation( 21 | function_name = function_name, 22 | argument_name = "prompt", 23 | argument_type = "character", 24 | allow_null = TRUE 25 | ) 26 | 27 | test_argument_validation( 28 | function_name = function_name, 29 | argument_name = "response_format", 30 | argument_type = "string", 31 | allow_null = FALSE 32 | ) 33 | 34 | test_argument_validation( 35 | function_name = function_name, 36 | argument_name = "temperature", 37 | argument_type = "number", 38 | allow_null = FALSE 39 | ) 40 | 41 | test_argument_validation( 42 | function_name = function_name, 43 | argument_name = "language", 44 | argument_type = "string", 45 | allow_null = TRUE 46 | ) 47 | 48 | test_argument_validation( 49 | function_name = function_name, 50 | argument_name = "openai_api_key", 51 | argument_type = "string", 52 | allow_null = FALSE 53 | ) 54 | 55 | test_argument_validation( 56 | function_name = function_name, 57 | argument_name = "openai_organization", 58 | argument_type = "string", 59 | allow_null = TRUE 60 | ) 61 | -------------------------------------------------------------------------------- /tests/testthat/test-create_image_edit.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # create_image_edit() 3 | 4 | function_name <- "create_image_edit" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "image", 9 | argument_type = "string", 10 | allow_null = FALSE 11 | ) 12 | 13 | test_argument_validation( 14 | function_name = function_name, 15 | argument_name = "mask", 16 | argument_type = "string", 17 | allow_null = FALSE 18 | ) 19 | 20 | test_argument_validation( 21 | function_name = function_name, 22 | argument_name = "prompt", 23 | argument_type = "string", 24 | allow_null = FALSE 25 | ) 26 | 27 | test_argument_validation( 28 | function_name = function_name, 29 | argument_name = "n", 30 | argument_type = "count", 31 | allow_null = FALSE 32 | ) 33 | 34 | test_argument_validation( 35 | function_name = function_name, 36 | argument_name = "size", 37 | argument_type = "string", 38 | allow_null = FALSE 39 | ) 40 | 41 | test_argument_validation( 42 | function_name = function_name, 43 | argument_name = "response_format", 44 | argument_type = "string", 45 | allow_null = FALSE 46 | ) 47 | 48 | test_argument_validation( 49 | function_name = function_name, 50 | argument_name = "user", 51 | argument_type = "string", 52 | allow_null = TRUE 53 | ) 54 | 55 | test_argument_validation( 56 | function_name = function_name, 57 | argument_name = "openai_api_key", 58 | argument_type = "string", 59 | allow_null = FALSE 60 | ) 61 | 62 | test_argument_validation( 63 | function_name = function_name, 64 | argument_name = "openai_organization", 65 | argument_type = "string", 66 | allow_null = TRUE 67 | ) 68 | -------------------------------------------------------------------------------- /man/delete_fine_tune_model.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/delete_fine_tune_model.R 3 | \name{delete_fine_tune_model} 4 | \alias{delete_fine_tune_model} 5 | \title{Delete fine_tune model} 6 | \usage{ 7 | delete_fine_tune_model( 8 | model, 9 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 10 | openai_organization = NULL 11 | ) 12 | } 13 | \arguments{ 14 | \item{model}{required; a length one character vector.} 15 | 16 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 17 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 18 | character vector. Specifies OpenAI API key.} 19 | 20 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 21 | character vector. Specifies OpenAI organization.} 22 | } 23 | \value{ 24 | Returns a list, elements of which contains information about the 25 | deleted model. 26 | } 27 | \description{ 28 | Deletes a fine-tuned model. See \href{https://platform.openai.com/docs/api-reference/fine-tunes/delete-model}{this page} for 29 | details. 30 | } 31 | \details{ 32 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/fine-tunes/delete-model}{official documentation}. 33 | } 34 | \examples{ 35 | \dontrun{ 36 | fine_tunes <- list_fine_tunes() 37 | 38 | fine_tunes <- fine_tunes$data 39 | 40 | id <- fine_tunes[!is.na(fine_tunes[, "fine_tuned_model"]), "fine_tuned_model"] 41 | 42 | delete_fine_tune_model(model = id[1]) 43 | } 44 | } 45 | \seealso{ 46 | Other fine-tune functions: 47 | \code{\link{cancel_fine_tune}()}, 48 | \code{\link{create_fine_tune}()}, 49 | \code{\link{list_fine_tune_events}()}, 50 | \code{\link{list_fine_tunes}()}, 51 | \code{\link{retrieve_fine_tune}()} 52 | } 53 | \concept{fine-tune functions} 54 | -------------------------------------------------------------------------------- /man/list_engines.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/list_engines.R 3 | \name{list_engines} 4 | \alias{list_engines} 5 | \title{List engines} 6 | \usage{ 7 | list_engines( 8 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 9 | openai_organization = NULL 10 | ) 11 | } 12 | \arguments{ 13 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 14 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 15 | character vector. Specifies OpenAI API key.} 16 | 17 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 18 | character vector. Specifies OpenAI organization.} 19 | } 20 | \value{ 21 | Returns a list, an element of which is a data frame containing 22 | information about engines. 23 | } 24 | \description{ 25 | \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} 26 | 27 | \strong{Note:} This endpoint is deprecated and soon will be removed. Please use 28 | its replacement, 29 | \href{https://platform.openai.com/docs/api-reference/models}{Models}, instead. The 30 | replacement function in this package is \code{list_models()}. 31 | 32 | Lists available engines and provides basic information about such engines. 33 | See \href{https://platform.openai.com/docs/api-reference/engines/list}{this page} 34 | for details. 35 | } 36 | \details{ 37 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/engines/list}{official documentation}. 38 | } 39 | \examples{ 40 | \dontrun{ 41 | list_engines() 42 | # -> 43 | list_models() 44 | } 45 | } 46 | \seealso{ 47 | Other engine functions: 48 | \code{\link{retrieve_engine}()} 49 | } 50 | \concept{engine functions} 51 | \keyword{internal} 52 | -------------------------------------------------------------------------------- /man/upload_file.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/upload_file.R 3 | \name{upload_file} 4 | \alias{upload_file} 5 | \title{Upload file} 6 | \usage{ 7 | upload_file( 8 | file, 9 | purpose = c("search", "answers", "classifications", "fine-tune"), 10 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 11 | openai_organization = NULL 12 | ) 13 | } 14 | \arguments{ 15 | \item{file}{required; a length one character vector.} 16 | 17 | \item{purpose}{required; defaults to \code{"fine-tune"}; a length one character 18 | vector equals to\code{"fine-tune"}.} 19 | 20 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 21 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 22 | character vector. Specifies OpenAI API key.} 23 | 24 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 25 | character vector. Specifies OpenAI organization.} 26 | } 27 | \value{ 28 | Returns a list, elements of which contains ID of the uploaded file 29 | and other supplementary information. 30 | } 31 | \description{ 32 | Uploads a file that will be used for various purposes. The size of the 33 | storage is limited to 1 Gb. See \href{https://platform.openai.com/docs/api-reference/files/upload}{this page} for details. 34 | } 35 | \details{ 36 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/files/upload}{official documentation}. 37 | } 38 | \examples{ 39 | \dontrun{ 40 | file <- system.file("extdata", "classification-file.jsonl", package = "openai") 41 | upload_file(file = file, purpose = "classification") 42 | } 43 | } 44 | \seealso{ 45 | Other file functions: 46 | \code{\link{delete_file}()}, 47 | \code{\link{list_files}()}, 48 | \code{\link{retrieve_file_content}()}, 49 | \code{\link{retrieve_file}()} 50 | } 51 | \concept{file functions} 52 | -------------------------------------------------------------------------------- /man/create_embedding.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/create_embedding.R 3 | \name{create_embedding} 4 | \alias{create_embedding} 5 | \title{Create embeddings} 6 | \usage{ 7 | create_embedding( 8 | engine_id = deprecated(), 9 | model, 10 | input, 11 | user = NULL, 12 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 13 | openai_organization = NULL 14 | ) 15 | } 16 | \arguments{ 17 | \item{engine_id}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}}} 18 | 19 | \item{model}{required; a length one character vector.} 20 | 21 | \item{input}{required; an arbitrary length character vector.} 22 | 23 | \item{user}{optional; defaults to \code{NULL}; a length one character vector.} 24 | 25 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 26 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 27 | character vector. Specifies OpenAI API key.} 28 | 29 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 30 | character vector. Specifies OpenAI organization.} 31 | } 32 | \value{ 33 | Returns a list, an element of which contains embedding vector(s) for 34 | a given input. 35 | } 36 | \description{ 37 | Creates an embedding vector that represents the provided input. See \href{https://platform.openai.com/docs/api-reference/embeddings/create}{this page} for 38 | details. 39 | } 40 | \details{ 41 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/embeddings/create}{official documentation}. 42 | } 43 | \examples{ 44 | \dontrun{ 45 | create_embedding( 46 | model = "text-embedding-ada-002", 47 | input = c( 48 | "Ah, it is so boring to write documentation", 49 | "But examples are really crucial" 50 | ) 51 | ) 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /man/retrieve_engine.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/retrieve_engine.R 3 | \name{retrieve_engine} 4 | \alias{retrieve_engine} 5 | \title{Retrieve engine} 6 | \usage{ 7 | retrieve_engine( 8 | engine_id, 9 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 10 | openai_organization = NULL 11 | ) 12 | } 13 | \arguments{ 14 | \item{engine_id}{required; a length one character vector.} 15 | 16 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 17 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 18 | character vector. Specifies OpenAI API key.} 19 | 20 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 21 | character vector. Specifies OpenAI organization.} 22 | } 23 | \value{ 24 | Returns a list, elements of which contain information about the 25 | engine. 26 | } 27 | \description{ 28 | \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} 29 | 30 | \strong{Note:} This endpoint is deprecated and soon will be removed. Please use 31 | its replacement, 32 | \href{https://platform.openai.com/docs/api-reference/models}{Models}, instead. The 33 | replacement function in this package is \code{retrieve_model()}. 34 | 35 | Provides information about a specified engine. See \href{https://platform.openai.com/docs/api-reference/engines/retrieve}{this page} for 36 | details. 37 | } 38 | \details{ 39 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/engines/retrieve}{official documentation}. 40 | } 41 | \examples{ 42 | \dontrun{ 43 | retrieve_engine("text-davinci-002") 44 | # -> 45 | retrieve_model("text-davinci-002") 46 | } 47 | } 48 | \seealso{ 49 | Other engine functions: 50 | \code{\link{list_engines}()} 51 | } 52 | \concept{engine functions} 53 | \keyword{internal} 54 | -------------------------------------------------------------------------------- /man/create_translation.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/create_translation.R 3 | \name{create_translation} 4 | \alias{create_translation} 5 | \title{Create translation} 6 | \usage{ 7 | create_translation( 8 | file, 9 | model, 10 | prompt = NULL, 11 | response_format = "json", 12 | temperature = 0, 13 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 14 | openai_organization = NULL 15 | ) 16 | } 17 | \arguments{ 18 | \item{file}{required; a length one character vector.} 19 | 20 | \item{model}{required; a length one character vector.} 21 | 22 | \item{prompt}{optional; defaults to \code{NULL}; a length one character vector.} 23 | 24 | \item{response_format}{required; defaults to \code{"json"}; length one character 25 | vector equals to \code{"json"}. \strong{Currently only \code{"json"} is implemented.}} 26 | 27 | \item{temperature}{required; defaults to \code{1}; a length one numeric vector 28 | with the value between \code{0} and \code{2}.} 29 | 30 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 31 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 32 | character vector. Specifies OpenAI API key.} 33 | 34 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 35 | character vector. Specifies OpenAI organization.} 36 | } 37 | \value{ 38 | Returns a list, elements of which contain a transcription and 39 | supplementary information. 40 | } 41 | \description{ 42 | Translates audio into into English. See 43 | \href{https://platform.openai.com/docs/api-reference/audio/create}{this page} 44 | for details. 45 | } 46 | \details{ 47 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/audio/create}{official documentation}. 48 | } 49 | \examples{ 50 | \dontrun{ 51 | voice_sample_ua <- system.file( 52 | "extdata", "sample-ua.m4a", package = "openai" 53 | ) 54 | create_translation(file = voice_sample_ua, model = "whisper-1") 55 | } 56 | } 57 | \seealso{ 58 | Other audio functions: 59 | \code{\link{create_transcription}()} 60 | } 61 | \concept{audio functions} 62 | -------------------------------------------------------------------------------- /R/utils-assertions.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | 3 | value_between <- function(x, lower, upper) { 4 | x >= lower && x <= upper 5 | } 6 | 7 | assertthat::on_failure(value_between) <- function(call, env) { 8 | paste0( 9 | deparse(call$x), 10 | " is not between ", 11 | deparse(call$lower), 12 | " and ", 13 | deparse(call$upper) 14 | ) 15 | } 16 | 17 | #------------------------------------------------------------------------------- 18 | 19 | both_specified <- function(x, y) { 20 | x != 1 && y != 1 21 | } 22 | 23 | #------------------------------------------------------------------------------- 24 | 25 | length_between <- function(x, lower, upper) { 26 | length(x) >= lower && length(x) <= upper 27 | } 28 | 29 | assertthat::on_failure(length_between) <- function(call, env) { 30 | paste0( 31 | "Length of ", 32 | deparse(call$x), 33 | " is not between ", 34 | deparse(call$lower), 35 | " and ", 36 | deparse(call$upper) 37 | ) 38 | } 39 | 40 | #------------------------------------------------------------------------------- 41 | 42 | n_characters_between <- function(x, lower, upper) { 43 | nchar(x) >= lower && nchar(x) <= upper 44 | } 45 | 46 | assertthat::on_failure(n_characters_between) <- function(call, env) { 47 | paste0( 48 | "Number of characters of ", 49 | deparse(call$x), 50 | " is not between ", 51 | deparse(call$lower), 52 | " and ", 53 | deparse(call$upper) 54 | ) 55 | } 56 | 57 | #------------------------------------------------------------------------------- 58 | 59 | is_false <- function(x) { 60 | !x 61 | } 62 | 63 | assertthat::on_failure(is_false) <- function(call, env) { 64 | paste0(deparse(call$x), " is not yet implemented.") 65 | } 66 | 67 | #------------------------------------------------------------------------------- 68 | 69 | is_null <- function(x) { 70 | is.null(x) 71 | } 72 | 73 | assertthat::on_failure(is_null) <- function(call, env) { 74 | paste0(deparse(call$x), " is not yet implemented.") 75 | } 76 | -------------------------------------------------------------------------------- /man/create_image.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/create_image.R 3 | \name{create_image} 4 | \alias{create_image} 5 | \title{Create image} 6 | \usage{ 7 | create_image( 8 | prompt, 9 | n = 1, 10 | size = c("1024x1024", "256x256", "512x512"), 11 | response_format = c("url", "b64_json"), 12 | user = NULL, 13 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 14 | openai_organization = NULL 15 | ) 16 | } 17 | \arguments{ 18 | \item{prompt}{required; a length one character vector.} 19 | 20 | \item{n}{required; defaults to \code{1}; a length one numeric vector with the 21 | integer value greater than \code{0}.} 22 | 23 | \item{size}{required; defaults to \code{"1024x1024"}; a length one character 24 | vector, one among \code{"256x256"}, \code{"512x512"}, and \code{"1024x1024"}.} 25 | 26 | \item{response_format}{required; defaults to \code{"url"}; a length one character 27 | vector, one among \code{"url"} and \code{"b64_json"}.} 28 | 29 | \item{user}{optional; defaults to \code{NULL}; a length one character vector.} 30 | 31 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 32 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 33 | character vector. Specifies OpenAI API key.} 34 | 35 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 36 | character vector. Specifies OpenAI organization.} 37 | } 38 | \value{ 39 | Returns a list, an element of which contain either a link to the 40 | generated image or the generated image decoded in Base64. 41 | } 42 | \description{ 43 | Creates an image given a prompt. See \href{https://platform.openai.com/docs/api-reference/images/create}{this page} for 44 | details. 45 | } 46 | \details{ 47 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/images/create}{official documentation}. 48 | } 49 | \examples{ 50 | \dontrun{ 51 | create_image("An astronaut riding a horse in a photorealistic style") 52 | } 53 | } 54 | \seealso{ 55 | Other image functions: 56 | \code{\link{create_image_edit}()}, 57 | \code{\link{create_image_variation}()} 58 | } 59 | \concept{image functions} 60 | -------------------------------------------------------------------------------- /man/create_edit.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/create_edit.R 3 | \name{create_edit} 4 | \alias{create_edit} 5 | \title{Create edit} 6 | \usage{ 7 | create_edit( 8 | engine_id = deprecated(), 9 | model, 10 | input = "\\"", 11 | instruction, 12 | temperature = 1, 13 | top_p = 1, 14 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 15 | openai_organization = NULL 16 | ) 17 | } 18 | \arguments{ 19 | \item{engine_id}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}}} 20 | 21 | \item{model}{required; a length one character vector.} 22 | 23 | \item{input}{required; defaults to \code{'"'}; a length one character vector.} 24 | 25 | \item{instruction}{required; a length one character vector.} 26 | 27 | \item{temperature}{required; defaults to \code{1}; a length one numeric vector 28 | with the value between \code{0} and \code{2}.} 29 | 30 | \item{top_p}{required; defaults to \code{1}; a length one numeric vector with the 31 | value between \code{0} and \code{1}.} 32 | 33 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 34 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 35 | character vector. Specifies OpenAI API key.} 36 | 37 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 38 | character vector. Specifies OpenAI organization.} 39 | } 40 | \value{ 41 | Returns a list, elements of which contain edited version of prompt 42 | and supplementary information. 43 | } 44 | \description{ 45 | Creates an edit based on the provided input, instruction, and parameters. See 46 | \href{https://platform.openai.com/docs/api-reference/edits/create}{this page} for 47 | details. 48 | } 49 | \details{ 50 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/edits/create}{official documentation}. 51 | } 52 | \examples{ 53 | \dontrun{ 54 | create_edit( 55 | model = "text-davinci-edit-001", 56 | input = "What day of the wek is it?", 57 | instruction = "Fix the spelling mistakes" 58 | ) 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /man/create_transcription.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/create_transcription.R 3 | \name{create_transcription} 4 | \alias{create_transcription} 5 | \title{Create transcription} 6 | \usage{ 7 | create_transcription( 8 | file, 9 | model, 10 | prompt = NULL, 11 | response_format = "json", 12 | temperature = 0, 13 | language = NULL, 14 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 15 | openai_organization = NULL 16 | ) 17 | } 18 | \arguments{ 19 | \item{file}{required; a length one character vector.} 20 | 21 | \item{model}{required; a length one character vector.} 22 | 23 | \item{prompt}{optional; defaults to \code{NULL}; a length one character vector.} 24 | 25 | \item{response_format}{required; defaults to \code{"json"}; length one character 26 | vector equals to \code{"json"}. \strong{Currently only \code{"json"} is implemented.}} 27 | 28 | \item{temperature}{required; defaults to \code{1}; a length one numeric vector 29 | with the value between \code{0} and \code{2}.} 30 | 31 | \item{language}{optional; defaults to \code{NULL}; a length one character vector.} 32 | 33 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 34 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 35 | character vector. Specifies OpenAI API key.} 36 | 37 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 38 | character vector. Specifies OpenAI organization.} 39 | } 40 | \value{ 41 | Returns a list, elements of which contain a transcription and 42 | supplementary information. 43 | } 44 | \description{ 45 | Transcribes audio into the input language. See 46 | \href{https://platform.openai.com/docs/api-reference/audio/create}{this page} 47 | for details. 48 | } 49 | \details{ 50 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/audio/create}{official documentation}. 51 | } 52 | \examples{ 53 | \dontrun{ 54 | voice_sample_en <- system.file( 55 | "extdata", "sample-en.m4a", package = "openai" 56 | ) 57 | create_transcription(file = voice_sample_en, model = "whisper-1") 58 | } 59 | } 60 | \seealso{ 61 | Other audio functions: 62 | \code{\link{create_translation}()} 63 | } 64 | \concept{audio functions} 65 | -------------------------------------------------------------------------------- /tests/testthat/test-create_chat_completion.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # create_chat_completion() 3 | 4 | function_name <- "create_chat_completion" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "model", 9 | argument_type = "string", 10 | allow_null = FALSE 11 | ) 12 | 13 | test_argument_validation( 14 | function_name = function_name, 15 | argument_name = "temperature", 16 | argument_type = "number", 17 | allow_null = FALSE 18 | ) 19 | 20 | test_argument_validation( 21 | function_name = function_name, 22 | argument_name = "top_p", 23 | argument_type = "number", 24 | allow_null = FALSE 25 | ) 26 | 27 | test_argument_validation( 28 | function_name = function_name, 29 | argument_name = "n", 30 | argument_type = "count", 31 | allow_null = FALSE 32 | ) 33 | 34 | test_argument_validation( 35 | function_name = function_name, 36 | argument_name = "stream", 37 | argument_type = "flag", 38 | allow_null = FALSE 39 | ) 40 | 41 | test_argument_validation( 42 | function_name = function_name, 43 | argument_name = "stop", 44 | argument_type = "character", 45 | allow_null = TRUE 46 | ) 47 | 48 | test_argument_validation( 49 | function_name = function_name, 50 | argument_name = "max_tokens", 51 | argument_type = "count", 52 | allow_null = FALSE 53 | ) 54 | 55 | test_argument_validation( 56 | function_name = function_name, 57 | argument_name = "presence_penalty", 58 | argument_type = "number", 59 | allow_null = FALSE 60 | ) 61 | 62 | test_argument_validation( 63 | function_name = function_name, 64 | argument_name = "frequency_penalty", 65 | argument_type = "number", 66 | allow_null = FALSE 67 | ) 68 | 69 | test_argument_validation( 70 | function_name = function_name, 71 | argument_name = "user", 72 | argument_type = "string", 73 | allow_null = TRUE 74 | ) 75 | 76 | test_argument_validation( 77 | function_name = function_name, 78 | argument_name = "openai_api_key", 79 | argument_type = "string", 80 | allow_null = FALSE 81 | ) 82 | 83 | test_argument_validation( 84 | function_name = function_name, 85 | argument_name = "openai_organization", 86 | argument_type = "string", 87 | allow_null = TRUE 88 | ) 89 | -------------------------------------------------------------------------------- /man/cancel_fine_tune.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cancel_fine_tune.R 3 | \name{cancel_fine_tune} 4 | \alias{cancel_fine_tune} 5 | \title{Cancel fine-tune} 6 | \usage{ 7 | cancel_fine_tune( 8 | fine_tune_id, 9 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 10 | openai_organization = NULL 11 | ) 12 | } 13 | \arguments{ 14 | \item{fine_tune_id}{required; a length one character vector.} 15 | 16 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 17 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 18 | character vector. Specifies OpenAI API key.} 19 | 20 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 21 | character vector. Specifies OpenAI organization.} 22 | } 23 | \value{ 24 | Returns a list, elements of which contains information about the 25 | cancelled fine-tune. 26 | } 27 | \description{ 28 | Cancel a running fine-tune job. See \href{https://platform.openai.com/docs/api-reference/fine-tunes/cancel}{this page} for 29 | details. 30 | } 31 | \details{ 32 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/fine-tunes/cancel}{official documentation}. 33 | } 34 | \examples{ 35 | \dontrun{ 36 | training_file <- system.file( 37 | "extdata", "sport_prepared_train.jsonl", package = "openai" 38 | ) 39 | validation_file <- system.file( 40 | "extdata", "sport_prepared_train.jsonl", package = "openai" 41 | ) 42 | 43 | training_info <- upload_file(training_file, "fine-tune") 44 | validation_info <- upload_file(validation_file, "fine-tune") 45 | 46 | info <- create_fine_tune( 47 | training_file = training_info$id, 48 | validation_file = validation_info$id, 49 | model = "ada", 50 | compute_classification_metrics = TRUE, 51 | classification_positive_class = " baseball" # Mind space in front 52 | ) 53 | 54 | id <- ifelse( 55 | length(info$data$id) > 1, 56 | info$data$id[length(info$data$id)], 57 | info$data$id 58 | ) 59 | 60 | cancel_fine_tune(fine_tune_id = id) 61 | } 62 | } 63 | \seealso{ 64 | Other fine-tune functions: 65 | \code{\link{create_fine_tune}()}, 66 | \code{\link{delete_fine_tune_model}()}, 67 | \code{\link{list_fine_tune_events}()}, 68 | \code{\link{list_fine_tunes}()}, 69 | \code{\link{retrieve_fine_tune}()} 70 | } 71 | \concept{fine-tune functions} 72 | -------------------------------------------------------------------------------- /man/create_image_variation.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/create_image_variation.R 3 | \name{create_image_variation} 4 | \alias{create_image_variation} 5 | \title{Create image variation} 6 | \usage{ 7 | create_image_variation( 8 | image, 9 | n = 1, 10 | size = c("1024x1024", "256x256", "512x512"), 11 | response_format = c("url", "b64_json"), 12 | user = NULL, 13 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 14 | openai_organization = NULL 15 | ) 16 | } 17 | \arguments{ 18 | \item{image}{required; a length one character vector.} 19 | 20 | \item{n}{required; defaults to \code{1}; a length one numeric vector with the 21 | integer value greater than \code{0}.} 22 | 23 | \item{size}{required; defaults to \code{"1024x1024"}; a length one character 24 | vector, one among \code{"256x256"}, \code{"512x512"}, and \code{"1024x1024"}.} 25 | 26 | \item{response_format}{required; defaults to \code{"url"}; a length one character 27 | vector, one among \code{"url"} and \code{"b64_json"}.} 28 | 29 | \item{user}{optional; defaults to \code{NULL}; a length one character vector.} 30 | 31 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 32 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 33 | character vector. Specifies OpenAI API key.} 34 | 35 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 36 | character vector. Specifies OpenAI organization.} 37 | } 38 | \value{ 39 | Returns a list, an element of which contain either a link to the 40 | image variation or the image variation decoded in Base64. 41 | } 42 | \description{ 43 | Creates a variation of a given image. See 44 | \href{https://platform.openai.com/docs/api-reference/images/create-variation}{this page} 45 | for details. 46 | } 47 | \details{ 48 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/images/create-variation}{official documentation}. 49 | } 50 | \examples{ 51 | \dontrun{ 52 | image <- system.file("extdata", "astronaut.png", package = "openai") 53 | create_image_variation( 54 | image = image, 55 | n = 1, 56 | size = "256x256", 57 | response_format = "url" 58 | ) 59 | } 60 | } 61 | \seealso{ 62 | Other image functions: 63 | \code{\link{create_image_edit}()}, 64 | \code{\link{create_image}()} 65 | } 66 | \concept{image functions} 67 | -------------------------------------------------------------------------------- /tests/testthat/helper-test_argument_validation.R: -------------------------------------------------------------------------------- 1 | test_argument_validation <- function( 2 | function_name, 3 | argument_name, 4 | argument_type = c("character", "string", "number", "count", "flag"), 5 | allow_null = FALSE, 6 | suppress_warnings = FALSE 7 | ) { 8 | 9 | argument_type <- match.arg(argument_type) 10 | 11 | if (argument_type == "character") { 12 | non_valid_values <- list(NA_character_, 35, NA) 13 | } else if (argument_type == "string") { 14 | non_valid_values <- list(NA_character_, c("one", "two"), 35, NA) 15 | } else if (argument_type == "number") { 16 | non_valid_values <- list(NA_character_, NA_integer_, NaN) 17 | } else if (argument_type == "count") { 18 | non_valid_values <- list(NA_character_, -10, 0.5, NA_integer_) 19 | } else if (argument_type == "flag") { 20 | non_valid_values <- list(NA, NA_integer_, 32, "TRUE") 21 | } 22 | 23 | if (!allow_null) { 24 | non_valid_values <- append(non_valid_values, list(NULL)) 25 | } 26 | 27 | test_that( 28 | paste0(function_name, "() validates ", argument_name), { 29 | purrr::walk( 30 | non_valid_values, 31 | function(x) { 32 | 33 | function_object <- match.fun(function_name) 34 | 35 | argument_object <- list() 36 | 37 | argument_object[[argument_name]] <- x 38 | 39 | if (is.null(x)) 40 | argument_object[argument_name] <- list(NULL) 41 | 42 | if (suppress_warnings) { 43 | suppressWarnings( 44 | expect_error( 45 | do.call( 46 | what = function_object, 47 | args = argument_object 48 | ) 49 | ) 50 | ) 51 | } else { 52 | expect_error( 53 | do.call( 54 | what = function_object, 55 | args = argument_object 56 | ) 57 | ) 58 | } 59 | 60 | 61 | 62 | } 63 | ) 64 | }) 65 | 66 | } 67 | -------------------------------------------------------------------------------- /man/retrieve_fine_tune.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/retrieve_fine_tune.R 3 | \name{retrieve_fine_tune} 4 | \alias{retrieve_fine_tune} 5 | \title{Retrieve fine-tune} 6 | \usage{ 7 | retrieve_fine_tune( 8 | fine_tune_id, 9 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 10 | openai_organization = NULL 11 | ) 12 | } 13 | \arguments{ 14 | \item{fine_tune_id}{required; a length one character vector.} 15 | 16 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 17 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 18 | character vector. Specifies OpenAI API key.} 19 | 20 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 21 | character vector. Specifies OpenAI organization.} 22 | } 23 | \value{ 24 | Returns a list, elements of which contains information about the 25 | fine-tune. 26 | } 27 | \description{ 28 | Returns information about the specified fine-tune job. See \href{https://platform.openai.com/docs/api-reference/fine-tunes/retrieve}{this page} for 29 | details. 30 | } 31 | \details{ 32 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/fine-tunes/retrieve}{official documentation}. 33 | } 34 | \examples{ 35 | \dontrun{ 36 | training_file <- system.file( 37 | "extdata", "sport_prepared_train.jsonl", package = "openai" 38 | ) 39 | validation_file <- system.file( 40 | "extdata", "sport_prepared_train.jsonl", package = "openai" 41 | ) 42 | 43 | training_info <- upload_file(training_file, "fine-tune") 44 | validation_info <- upload_file(validation_file, "fine-tune") 45 | 46 | info <- create_fine_tune( 47 | training_file = training_info$id, 48 | validation_file = validation_info$id, 49 | model = "ada", 50 | compute_classification_metrics = TRUE, 51 | classification_positive_class = " baseball" # Mind space in front 52 | ) 53 | 54 | id <- ifelse( 55 | length(info$data$id) > 1, 56 | info$data$id[length(info$data$id)], 57 | info$data$id 58 | ) 59 | 60 | retrieve_fine_tune(fine_tune_id = id) 61 | } 62 | } 63 | \seealso{ 64 | Other fine-tune functions: 65 | \code{\link{cancel_fine_tune}()}, 66 | \code{\link{create_fine_tune}()}, 67 | \code{\link{delete_fine_tune_model}()}, 68 | \code{\link{list_fine_tune_events}()}, 69 | \code{\link{list_fine_tunes}()} 70 | } 71 | \concept{fine-tune functions} 72 | -------------------------------------------------------------------------------- /man/list_fine_tune_events.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/list_fine_tune_events.R 3 | \name{list_fine_tune_events} 4 | \alias{list_fine_tune_events} 5 | \title{List fine-tune events} 6 | \usage{ 7 | list_fine_tune_events( 8 | fine_tune_id, 9 | stream = FALSE, 10 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 11 | openai_organization = NULL 12 | ) 13 | } 14 | \arguments{ 15 | \item{fine_tune_id}{required; a length one character vector.} 16 | 17 | \item{stream}{required; defaults to \code{FALSE}; a length one logical vector. 18 | \strong{Currently is not implemented.}} 19 | 20 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 21 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 22 | character vector. Specifies OpenAI API key.} 23 | 24 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 25 | character vector. Specifies OpenAI organization.} 26 | } 27 | \value{ 28 | Returns a list, elements of which contains information about the 29 | fine-tune events. 30 | } 31 | \description{ 32 | Returns events related to a specified fine-tune job. See \href{https://platform.openai.com/docs/api-reference/fine-tunes/events}{this page} for 33 | details. 34 | } 35 | \details{ 36 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/fine-tunes/events}{official documentation}. 37 | } 38 | \examples{ 39 | \dontrun{ 40 | training_file <- system.file( 41 | "extdata", "sport_prepared_train.jsonl", package = "openai" 42 | ) 43 | validation_file <- system.file( 44 | "extdata", "sport_prepared_train.jsonl", package = "openai" 45 | ) 46 | 47 | training_info <- upload_file(training_file, "fine-tune") 48 | validation_info <- upload_file(validation_file, "fine-tune") 49 | 50 | info <- create_fine_tune( 51 | training_file = training_info$id, 52 | validation_file = validation_info$id, 53 | model = "ada", 54 | compute_classification_metrics = TRUE, 55 | classification_positive_class = " baseball" # Mind space in front 56 | ) 57 | 58 | id <- ifelse( 59 | length(info$data$id) > 1, 60 | info$data$id[length(info$data$id)], 61 | info$data$id 62 | ) 63 | 64 | list_fine_tune_events(fine_tune_id = id) 65 | } 66 | } 67 | \seealso{ 68 | Other fine-tune functions: 69 | \code{\link{cancel_fine_tune}()}, 70 | \code{\link{create_fine_tune}()}, 71 | \code{\link{delete_fine_tune_model}()}, 72 | \code{\link{list_fine_tunes}()}, 73 | \code{\link{retrieve_fine_tune}()} 74 | } 75 | \concept{fine-tune functions} 76 | -------------------------------------------------------------------------------- /man/create_image_edit.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/create_image_edit.R 3 | \name{create_image_edit} 4 | \alias{create_image_edit} 5 | \title{Create image edit} 6 | \usage{ 7 | create_image_edit( 8 | image, 9 | mask, 10 | prompt, 11 | n = 1, 12 | size = c("1024x1024", "256x256", "512x512"), 13 | response_format = c("url", "b64_json"), 14 | user = NULL, 15 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 16 | openai_organization = NULL 17 | ) 18 | } 19 | \arguments{ 20 | \item{image}{required; a length one character vector.} 21 | 22 | \item{mask}{required; a length one character vector.} 23 | 24 | \item{prompt}{required; a length one character vector.} 25 | 26 | \item{n}{required; defaults to \code{1}; a length one numeric vector with the 27 | integer value greater than \code{0}.} 28 | 29 | \item{size}{required; defaults to \code{"1024x1024"}; a length one character 30 | vector, one among \code{"256x256"}, \code{"512x512"}, and \code{"1024x1024"}.} 31 | 32 | \item{response_format}{required; defaults to \code{"url"}; a length one character 33 | vector, one among \code{"url"} and \code{"b64_json"}.} 34 | 35 | \item{user}{optional; defaults to \code{NULL}; a length one character vector.} 36 | 37 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 38 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 39 | character vector. Specifies OpenAI API key.} 40 | 41 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 42 | character vector. Specifies OpenAI organization.} 43 | } 44 | \value{ 45 | Returns a list, an element of which contain either a link to the 46 | edited image or the edited image decoded in Base64. 47 | } 48 | \description{ 49 | Creates an edited or extended image given an original image and a prompt. See 50 | \href{https://platform.openai.com/docs/api-reference/images/create-edit}{this page} 51 | for details. 52 | } 53 | \details{ 54 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/images/create-edit}{official documentation}. 55 | } 56 | \examples{ 57 | \dontrun{ 58 | image <- system.file("extdata", "astronaut.png", package = "openai") 59 | mask <- system.file("extdata", "mask.png", package = "openai") 60 | create_image_edit( 61 | image = image, 62 | mask = mask, 63 | prompt = "goat", 64 | n = 1, 65 | response_format = "url" 66 | ) 67 | } 68 | } 69 | \seealso{ 70 | Other image functions: 71 | \code{\link{create_image_variation}()}, 72 | \code{\link{create_image}()} 73 | } 74 | \concept{image functions} 75 | -------------------------------------------------------------------------------- /tests/testthat/test-create_fine_tune.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # create_fine_tune() 3 | 4 | test_argument_validation( 5 | function_name = "create_fine_tune", 6 | argument_name = "training_file", 7 | argument_type = "string", 8 | allow_null = FALSE 9 | ) 10 | 11 | test_argument_validation( 12 | function_name = "create_fine_tune", 13 | argument_name = "validation_file", 14 | argument_type = "string", 15 | allow_null = TRUE 16 | ) 17 | 18 | test_argument_validation( 19 | function_name = "create_fine_tune", 20 | argument_name = "model", 21 | argument_type = "string", 22 | allow_null = FALSE 23 | ) 24 | 25 | test_argument_validation( 26 | function_name = "create_fine_tune", 27 | argument_name = "n_epochs", 28 | argument_type = "count", 29 | allow_null = FALSE 30 | ) 31 | 32 | test_argument_validation( 33 | function_name = "create_fine_tune", 34 | argument_name = "batch_size", 35 | argument_type = "count", 36 | allow_null = TRUE 37 | ) 38 | 39 | test_argument_validation( 40 | function_name = "create_fine_tune", 41 | argument_name = "learning_rate_multiplier", 42 | argument_type = "number", 43 | allow_null = TRUE 44 | ) 45 | 46 | test_argument_validation( 47 | function_name = "create_fine_tune", 48 | argument_name = "prompt_loss_weight", 49 | argument_type = "number", 50 | allow_null = FALSE 51 | ) 52 | 53 | test_argument_validation( 54 | function_name = "create_fine_tune", 55 | argument_name = "compute_classification_metrics", 56 | argument_type = "flag", 57 | allow_null = FALSE 58 | ) 59 | 60 | test_argument_validation( 61 | function_name = "create_fine_tune", 62 | argument_name = "classification_n_classes", 63 | argument_type = "count", 64 | allow_null = TRUE 65 | ) 66 | 67 | test_argument_validation( 68 | function_name = "create_fine_tune", 69 | argument_name = "classification_positive_class", 70 | argument_type = "string", 71 | allow_null = TRUE 72 | ) 73 | 74 | test_argument_validation( 75 | function_name = "create_fine_tune", 76 | argument_name = "suffix", 77 | argument_type = "string", 78 | allow_null = TRUE 79 | ) 80 | 81 | test_argument_validation( 82 | function_name = "create_fine_tune", 83 | argument_name = "openai_api_key", 84 | argument_type = "string", 85 | allow_null = FALSE 86 | ) 87 | 88 | test_argument_validation( 89 | function_name = "create_fine_tune", 90 | argument_name = "openai_organization", 91 | argument_type = "string", 92 | allow_null = TRUE 93 | ) 94 | -------------------------------------------------------------------------------- /R/list_files.R: -------------------------------------------------------------------------------- 1 | #' List files 2 | #' 3 | #' Lists files uploaded by user's organization. See [this 4 | #' page](https://platform.openai.com/docs/api-reference/files/list) for details. 5 | #' 6 | #' For arguments description please refer to the [official 7 | #' documentation](https://platform.openai.com/docs/api-reference/files/list). 8 | #' 9 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 10 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 11 | #' character vector. Specifies OpenAI API key. 12 | #' @param openai_organization optional; defaults to `NULL`; a length one 13 | #' character vector. Specifies OpenAI organization. 14 | #' @return Returns a list, an element of which is a data frame containing 15 | #' information about files. 16 | #' @examples \dontrun{ 17 | #' list_files() 18 | #' } 19 | #' @family file functions 20 | #' @export 21 | list_files <- function( 22 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 23 | openai_organization = NULL 24 | ) { 25 | 26 | #--------------------------------------------------------------------------- 27 | # Validate arguments 28 | 29 | assertthat::assert_that( 30 | assertthat::is.string(openai_api_key), 31 | assertthat::noNA(openai_api_key) 32 | ) 33 | 34 | if (!is.null(openai_organization)) { 35 | assertthat::assert_that( 36 | assertthat::is.string(openai_organization), 37 | assertthat::noNA(openai_organization) 38 | ) 39 | } 40 | 41 | #--------------------------------------------------------------------------- 42 | # Build parameters of the request 43 | 44 | base_url <- "https://api.openai.com/v1/files" 45 | 46 | headers <- c( 47 | "Authorization" = paste("Bearer", openai_api_key), 48 | "Content-Type" = "application/json" 49 | ) 50 | 51 | if (!is.null(openai_organization)) { 52 | headers["OpenAI-Organization"] <- openai_organization 53 | } 54 | 55 | #--------------------------------------------------------------------------- 56 | # Make a request and parse it 57 | 58 | response <- httr::GET( 59 | url = base_url, 60 | httr::add_headers(.headers = headers), 61 | encode = "json" 62 | ) 63 | 64 | verify_mime_type(response) 65 | 66 | parsed <- response %>% 67 | httr::content(as = "text", encoding = "UTF-8") %>% 68 | jsonlite::fromJSON(flatten = TRUE) 69 | 70 | #--------------------------------------------------------------------------- 71 | # Check whether request failed and return parsed 72 | 73 | if (httr::http_error(response)) { 74 | paste0( 75 | "OpenAI API request failed [", 76 | httr::status_code(response), 77 | "]:\n\n", 78 | parsed$error$message 79 | ) %>% 80 | stop(call. = FALSE) 81 | } 82 | 83 | parsed 84 | 85 | } 86 | -------------------------------------------------------------------------------- /R/list_fine_tunes.R: -------------------------------------------------------------------------------- 1 | #' Lists fine-tunes 2 | #' 3 | #' Lists organization's fine-tuning jobs. See [this 4 | #' page](https://platform.openai.com/docs/api-reference/fine-tunes/list) for 5 | #' details. 6 | #' 7 | #' For arguments description please refer to the [official 8 | #' documentation](https://platform.openai.com/docs/api-reference/fine-tunes/list). 9 | #' 10 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 11 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 12 | #' character vector. Specifies OpenAI API key. 13 | #' @param openai_organization optional; defaults to `NULL`; a length one 14 | #' character vector. Specifies OpenAI organization. 15 | #' @return Returns a list, an element of which is a data frame containing 16 | #' information about fine-tunes. 17 | #' @examples \dontrun{ 18 | #' list_fine_tunes() 19 | #' } 20 | #' @family fine-tune functions 21 | #' @export 22 | list_fine_tunes <- function( 23 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 24 | openai_organization = NULL 25 | ) { 26 | 27 | #--------------------------------------------------------------------------- 28 | # Validate arguments 29 | 30 | assertthat::assert_that( 31 | assertthat::is.string(openai_api_key), 32 | assertthat::noNA(openai_api_key) 33 | ) 34 | 35 | if (!is.null(openai_organization)) { 36 | assertthat::assert_that( 37 | assertthat::is.string(openai_organization), 38 | assertthat::noNA(openai_organization) 39 | ) 40 | } 41 | 42 | #--------------------------------------------------------------------------- 43 | # Build parameters of the request 44 | 45 | base_url <- "https://api.openai.com/v1/fine-tunes" 46 | 47 | headers <- c( 48 | "Authorization" = paste("Bearer", openai_api_key), 49 | "Content-Type" = "application/json" 50 | ) 51 | 52 | if (!is.null(openai_organization)) { 53 | headers["OpenAI-Organization"] <- openai_organization 54 | } 55 | 56 | #--------------------------------------------------------------------------- 57 | # Make a request and parse it 58 | 59 | response <- httr::GET( 60 | url = base_url, 61 | httr::add_headers(.headers = headers), 62 | encode = "json" 63 | ) 64 | 65 | verify_mime_type(response) 66 | 67 | parsed <- response %>% 68 | httr::content(as = "text", encoding = "UTF-8") %>% 69 | jsonlite::fromJSON(flatten = TRUE) 70 | 71 | #--------------------------------------------------------------------------- 72 | # Check whether request failed and return parsed 73 | 74 | if (httr::http_error(response)) { 75 | paste0( 76 | "OpenAI API request failed [", 77 | httr::status_code(response), 78 | "]:\n\n", 79 | parsed$error$message 80 | ) %>% 81 | stop(call. = FALSE) 82 | } 83 | 84 | parsed 85 | 86 | } 87 | -------------------------------------------------------------------------------- /R/list_models.R: -------------------------------------------------------------------------------- 1 | #' List models 2 | #' 3 | #' Lists the currently available models, and provides basic information about 4 | #' each one such as the owner and availability. See 5 | #' [this page](https://platform.openai.com/docs/api-reference/models/list) for 6 | #' details. 7 | #' 8 | #' For arguments description please refer to the [official 9 | #' documentation](https://platform.openai.com/docs/api-reference/models/list). 10 | #' 11 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 12 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 13 | #' character vector. Specifies OpenAI API key. 14 | #' @param openai_organization optional; defaults to `NULL`; a length one 15 | #' character vector. Specifies OpenAI organization. 16 | #' @return Returns a list, an element of which is a data frame containing 17 | #' information about models. 18 | #' @examples \dontrun{ 19 | #' list_models() 20 | #' } 21 | #' @family model functions 22 | #' @export 23 | list_models <- function( 24 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 25 | openai_organization = NULL 26 | ) { 27 | 28 | #--------------------------------------------------------------------------- 29 | # Validate arguments 30 | 31 | assertthat::assert_that( 32 | assertthat::is.string(openai_api_key), 33 | assertthat::noNA(openai_api_key) 34 | ) 35 | 36 | if (!is.null(openai_organization)) { 37 | assertthat::assert_that( 38 | assertthat::is.string(openai_organization), 39 | assertthat::noNA(openai_organization) 40 | ) 41 | } 42 | 43 | #--------------------------------------------------------------------------- 44 | # Build parameters of the request 45 | 46 | base_url <- "https://api.openai.com/v1/models" 47 | 48 | headers <- c( 49 | "Authorization" = paste("Bearer", openai_api_key), 50 | "Content-Type" = "application/json" 51 | ) 52 | 53 | if (!is.null(openai_organization)) { 54 | headers["OpenAI-Organization"] <- openai_organization 55 | } 56 | 57 | #--------------------------------------------------------------------------- 58 | # Make a request and parse it 59 | 60 | response <- httr::GET( 61 | url = base_url, 62 | httr::add_headers(.headers = headers), 63 | encode = "json" 64 | ) 65 | 66 | verify_mime_type(response) 67 | 68 | parsed <- response %>% 69 | httr::content(as = "text", encoding = "UTF-8") %>% 70 | jsonlite::fromJSON(flatten = TRUE) 71 | 72 | #--------------------------------------------------------------------------- 73 | # Check whether request failed and return parsed 74 | 75 | if (httr::http_error(response)) { 76 | paste0( 77 | "OpenAI API request failed [", 78 | httr::status_code(response), 79 | "]:\n\n", 80 | parsed$error$message 81 | ) %>% 82 | stop(call. = FALSE) 83 | } 84 | 85 | parsed 86 | 87 | } 88 | -------------------------------------------------------------------------------- /R/retrieve_file_content.R: -------------------------------------------------------------------------------- 1 | #' Retrieve file content 2 | #' 3 | #' Returns the content of the specified file. See [this 4 | #' page](https://platform.openai.com/docs/api-reference/files/retrieve-content) for 5 | #' details. Please note that only output files are allowed to be downloaded, not 6 | #' the input ones. 7 | #' 8 | #' For arguments description please refer to the [official 9 | #' documentation](https://platform.openai.com/docs/api-reference/files/retrieve-content). 10 | #' 11 | #' @param file_id required; a length one character vector. 12 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 13 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 14 | #' character vector. Specifies OpenAI API key. 15 | #' @param openai_organization optional; defaults to `NULL`; a length one 16 | #' character vector. Specifies OpenAI organization. 17 | #' @return Returns a list, an element of which contains the content of the file. 18 | #' @family file functions 19 | #' @export 20 | retrieve_file_content <- function( 21 | file_id, 22 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 23 | openai_organization = NULL 24 | ) { 25 | 26 | #--------------------------------------------------------------------------- 27 | # Validate arguments 28 | 29 | assertthat::assert_that( 30 | assertthat::is.string(file_id), 31 | assertthat::noNA(file_id) 32 | ) 33 | 34 | assertthat::assert_that( 35 | assertthat::is.string(openai_api_key), 36 | assertthat::noNA(openai_api_key) 37 | ) 38 | 39 | if (!is.null(openai_organization)) { 40 | assertthat::assert_that( 41 | assertthat::is.string(openai_organization), 42 | assertthat::noNA(openai_organization) 43 | ) 44 | } 45 | 46 | #--------------------------------------------------------------------------- 47 | # Build parameters of the request 48 | 49 | base_url <- glue::glue("https://api.openai.com/v1/files/{file_id}/content") 50 | 51 | headers <- c( 52 | "Authorization" = paste("Bearer", openai_api_key), 53 | "Content-Type" = "application/json" 54 | ) 55 | 56 | if (!is.null(openai_organization)) { 57 | headers["OpenAI-Organization"] <- openai_organization 58 | } 59 | 60 | #--------------------------------------------------------------------------- 61 | # Make a request and parse it 62 | 63 | response <- httr::GET( 64 | url = base_url, 65 | httr::add_headers(.headers = headers), 66 | encode = "json" 67 | ) 68 | 69 | verify_mime_type(response) 70 | 71 | parsed <- response %>% 72 | httr::content(as = "text", encoding = "UTF-8") %>% 73 | jsonlite::fromJSON(flatten = TRUE) 74 | 75 | #--------------------------------------------------------------------------- 76 | # Check whether request failed and return parsed 77 | 78 | if (httr::http_error(response)) { 79 | paste0( 80 | "OpenAI API request failed [", 81 | httr::status_code(response), 82 | "]:\n\n", 83 | parsed$error$message 84 | ) %>% 85 | stop(call. = FALSE) 86 | } 87 | 88 | parsed 89 | 90 | } 91 | -------------------------------------------------------------------------------- /R/retrieve_model.R: -------------------------------------------------------------------------------- 1 | #' Retrieve model 2 | #' 3 | #' Retrieves a model instance, providing basic information about the model 4 | #' such as the owner and permissioning. See [this 5 | #' page](https://platform.openai.com/docs/api-reference/models/retrieve) for 6 | #' details. 7 | #' 8 | #' For arguments description please refer to the [official 9 | #' documentation](https://platform.openai.com/docs/api-reference/models/retrieve). 10 | #' 11 | #' @param model required; a length one character vector. 12 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 13 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 14 | #' character vector. Specifies OpenAI API key. 15 | #' @param openai_organization optional; defaults to `NULL`; a length one 16 | #' character vector. Specifies OpenAI organization. 17 | #' @return Returns a list, elements of which contain information about the 18 | #' model. 19 | #' @examples \dontrun{ 20 | #' retrieve_model("text-davinci-002") 21 | #' } 22 | #' @family model functions 23 | #' @export 24 | retrieve_model <- function( 25 | model, 26 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 27 | openai_organization = NULL 28 | ) { 29 | 30 | #--------------------------------------------------------------------------- 31 | # Validate arguments 32 | 33 | assertthat::assert_that( 34 | assertthat::is.string(model), 35 | assertthat::noNA(model) 36 | ) 37 | 38 | assertthat::assert_that( 39 | assertthat::is.string(openai_api_key), 40 | assertthat::noNA(openai_api_key) 41 | ) 42 | 43 | if (!is.null(openai_organization)) { 44 | assertthat::assert_that( 45 | assertthat::is.string(openai_organization), 46 | assertthat::noNA(openai_organization) 47 | ) 48 | } 49 | 50 | #--------------------------------------------------------------------------- 51 | # Build parameters of the request 52 | 53 | base_url <- glue::glue("https://api.openai.com/v1/models/{model}") 54 | 55 | headers <- c( 56 | "Authorization" = paste("Bearer", openai_api_key), 57 | "Content-Type" = "application/json" 58 | ) 59 | 60 | if (!is.null(openai_organization)) { 61 | headers["OpenAI-Organization"] <- openai_organization 62 | } 63 | 64 | #--------------------------------------------------------------------------- 65 | # Make a request and parse it 66 | 67 | response <- httr::GET( 68 | url = base_url, 69 | httr::add_headers(.headers = headers), 70 | encode = "json" 71 | ) 72 | 73 | verify_mime_type(response) 74 | 75 | parsed <- response %>% 76 | httr::content(as = "text", encoding = "UTF-8") %>% 77 | jsonlite::fromJSON(flatten = TRUE) 78 | 79 | #--------------------------------------------------------------------------- 80 | # Check whether request failed and return parsed 81 | 82 | if (httr::http_error(response)) { 83 | paste0( 84 | "OpenAI API request failed [", 85 | httr::status_code(response), 86 | "]:\n\n", 87 | parsed$error$message 88 | ) %>% 89 | stop(call. = FALSE) 90 | } 91 | 92 | parsed 93 | 94 | } 95 | -------------------------------------------------------------------------------- /R/delete_file.R: -------------------------------------------------------------------------------- 1 | #' Delete file 2 | #' 3 | #' Deletes a file. See [this 4 | #' page](https://platform.openai.com/docs/api-reference/files/delete) for details. 5 | #' 6 | #' For arguments description please refer to the [official 7 | #' documentation](https://platform.openai.com/docs/api-reference/files/delete). 8 | #' 9 | #' @param file_id required; a length one character vector. 10 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 11 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 12 | #' character vector. Specifies OpenAI API key. 13 | #' @param openai_organization optional; defaults to `NULL`; a length one 14 | #' character vector. Specifies OpenAI organization. 15 | #' @return Returns a list, elements of which contains ID of the deleted file and 16 | #' status whether the file is deleted. 17 | #' @examples \dontrun{ 18 | #' file <- system.file("extdata", "classification-file.jsonl", package = "openai") 19 | #' file_info <- upload_file(file = file, purpose = "classification") 20 | #' delete_file(file_info$id) 21 | #' } 22 | #' @family file functions 23 | #' @export 24 | delete_file <- function( 25 | file_id, 26 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 27 | openai_organization = NULL 28 | ) { 29 | 30 | #--------------------------------------------------------------------------- 31 | # Validate arguments 32 | 33 | assertthat::assert_that( 34 | assertthat::is.string(file_id), 35 | assertthat::noNA(file_id) 36 | ) 37 | 38 | assertthat::assert_that( 39 | assertthat::is.string(openai_api_key), 40 | assertthat::noNA(openai_api_key) 41 | ) 42 | 43 | if (!is.null(openai_organization)) { 44 | assertthat::assert_that( 45 | assertthat::is.string(openai_organization), 46 | assertthat::noNA(openai_organization) 47 | ) 48 | } 49 | 50 | #--------------------------------------------------------------------------- 51 | # Build parameters of the request 52 | 53 | base_url <- glue::glue("https://api.openai.com/v1/files/{file_id}") 54 | 55 | headers <- c( 56 | "Authorization" = paste("Bearer", openai_api_key), 57 | "Content-Type" = "application/json" 58 | ) 59 | 60 | if (!is.null(openai_organization)) { 61 | headers["OpenAI-Organization"] <- openai_organization 62 | } 63 | 64 | #--------------------------------------------------------------------------- 65 | # Make a request and parse it 66 | 67 | response <- httr::DELETE( 68 | url = base_url, 69 | httr::add_headers(.headers = headers), 70 | encode = "json" 71 | ) 72 | 73 | verify_mime_type(response) 74 | 75 | parsed <- response %>% 76 | httr::content(as = "text", encoding = "UTF-8") %>% 77 | jsonlite::fromJSON(flatten = TRUE) 78 | 79 | #--------------------------------------------------------------------------- 80 | # Check whether request failed and return parsed 81 | 82 | if (httr::http_error(response)) { 83 | paste0( 84 | "OpenAI API request failed [", 85 | httr::status_code(response), 86 | "]:\n\n", 87 | parsed$error$message 88 | ) %>% 89 | stop(call. = FALSE) 90 | } 91 | 92 | parsed 93 | 94 | } 95 | -------------------------------------------------------------------------------- /R/retrieve_file.R: -------------------------------------------------------------------------------- 1 | #' Retrieve file 2 | #' 3 | #' Provides information about a specific file. See [this 4 | #' page](https://platform.openai.com/docs/api-reference/files/retrieve) for details. 5 | #' 6 | #' For arguments description please refer to the [official 7 | #' documentation](https://platform.openai.com/docs/api-reference/files/retrieve). 8 | #' 9 | #' @param file_id required; a length one character vector. 10 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 11 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 12 | #' character vector. Specifies OpenAI API key. 13 | #' @param openai_organization optional; defaults to `NULL`; a length one 14 | #' character vector. Specifies OpenAI organization. 15 | #' @return Returns a list, elements of which contains information about the 16 | #' file. 17 | #' @examples \dontrun{ 18 | #' file <- system.file("extdata", "classification-file.jsonl", package = "openai") 19 | #' file_info <- upload_file(file = file, purpose = "classification") 20 | #' retrieve_file(file_info$id) 21 | #' } 22 | #' @family file functions 23 | #' @export 24 | retrieve_file <- function( 25 | file_id, 26 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 27 | openai_organization = NULL 28 | ) { 29 | 30 | #--------------------------------------------------------------------------- 31 | # Validate arguments 32 | 33 | assertthat::assert_that( 34 | assertthat::is.string(file_id), 35 | assertthat::noNA(file_id) 36 | ) 37 | 38 | assertthat::assert_that( 39 | assertthat::is.string(openai_api_key), 40 | assertthat::noNA(openai_api_key) 41 | ) 42 | 43 | if (!is.null(openai_organization)) { 44 | assertthat::assert_that( 45 | assertthat::is.string(openai_organization), 46 | assertthat::noNA(openai_organization) 47 | ) 48 | } 49 | 50 | #--------------------------------------------------------------------------- 51 | # Build parameters of the request 52 | 53 | base_url <- glue::glue("https://api.openai.com/v1/files/{file_id}") 54 | 55 | headers <- c( 56 | "Authorization" = paste("Bearer", openai_api_key), 57 | "Content-Type" = "application/json" 58 | ) 59 | 60 | if (!is.null(openai_organization)) { 61 | headers["OpenAI-Organization"] <- openai_organization 62 | } 63 | 64 | #--------------------------------------------------------------------------- 65 | # Make a request and parse it 66 | 67 | response <- httr::GET( 68 | url = base_url, 69 | httr::add_headers(.headers = headers), 70 | encode = "json" 71 | ) 72 | 73 | verify_mime_type(response) 74 | 75 | parsed <- response %>% 76 | httr::content(as = "text", encoding = "UTF-8") %>% 77 | jsonlite::fromJSON(flatten = TRUE) 78 | 79 | #--------------------------------------------------------------------------- 80 | # Check whether request failed and return parsed 81 | 82 | if (httr::http_error(response)) { 83 | paste0( 84 | "OpenAI API request failed [", 85 | httr::status_code(response), 86 | "]:\n\n", 87 | parsed$error$message 88 | ) %>% 89 | stop(call. = FALSE) 90 | } 91 | 92 | parsed 93 | 94 | } 95 | -------------------------------------------------------------------------------- /R/delete_fine_tune_model.R: -------------------------------------------------------------------------------- 1 | #' Delete fine_tune model 2 | #' 3 | #' Deletes a fine-tuned model. See [this 4 | #' page](https://platform.openai.com/docs/api-reference/fine-tunes/delete-model) for 5 | #' details. 6 | #' 7 | #' For arguments description please refer to the [official 8 | #' documentation](https://platform.openai.com/docs/api-reference/fine-tunes/delete-model). 9 | #' 10 | #' @param model required; a length one character vector. 11 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 12 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 13 | #' character vector. Specifies OpenAI API key. 14 | #' @param openai_organization optional; defaults to `NULL`; a length one 15 | #' character vector. Specifies OpenAI organization. 16 | #' @return Returns a list, elements of which contains information about the 17 | #' deleted model. 18 | #' @examples \dontrun{ 19 | #' fine_tunes <- list_fine_tunes() 20 | #' 21 | #' fine_tunes <- fine_tunes$data 22 | #' 23 | #' id <- fine_tunes[!is.na(fine_tunes[, "fine_tuned_model"]), "fine_tuned_model"] 24 | #' 25 | #' delete_fine_tune_model(model = id[1]) 26 | #' } 27 | #' @family fine-tune functions 28 | #' @export 29 | delete_fine_tune_model <- function( 30 | model, 31 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 32 | openai_organization = NULL 33 | ) { 34 | 35 | #--------------------------------------------------------------------------- 36 | # Validate arguments 37 | 38 | assertthat::assert_that( 39 | assertthat::is.string(model), 40 | assertthat::noNA(model) 41 | ) 42 | 43 | assertthat::assert_that( 44 | assertthat::is.string(openai_api_key), 45 | assertthat::noNA(openai_api_key) 46 | ) 47 | 48 | if (!is.null(openai_organization)) { 49 | assertthat::assert_that( 50 | assertthat::is.string(openai_organization), 51 | assertthat::noNA(openai_organization) 52 | ) 53 | } 54 | 55 | #--------------------------------------------------------------------------- 56 | # Build parameters of the request 57 | 58 | base_url <- glue::glue("https://api.openai.com/v1/models/{model}") 59 | 60 | headers <- c( 61 | "Authorization" = paste("Bearer", openai_api_key), 62 | "Content-Type" = "application/json" 63 | ) 64 | 65 | if (!is.null(openai_organization)) { 66 | headers["OpenAI-Organization"] <- openai_organization 67 | } 68 | 69 | #--------------------------------------------------------------------------- 70 | # Make a request and parse it 71 | 72 | response <- httr::DELETE( 73 | url = base_url, 74 | httr::add_headers(.headers = headers), 75 | encode = "json" 76 | ) 77 | 78 | verify_mime_type(response) 79 | 80 | parsed <- response %>% 81 | httr::content(as = "text", encoding = "UTF-8") %>% 82 | jsonlite::fromJSON(flatten = TRUE) 83 | 84 | #--------------------------------------------------------------------------- 85 | # Check whether request failed and return parsed 86 | 87 | if (httr::http_error(response)) { 88 | paste0( 89 | "OpenAI API request failed [", 90 | httr::status_code(response), 91 | "]:\n\n", 92 | parsed$error$message 93 | ) %>% 94 | stop(call. = FALSE) 95 | } 96 | 97 | parsed 98 | 99 | } 100 | -------------------------------------------------------------------------------- /man/create_chat_completion.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/create_chat_completion.R 3 | \name{create_chat_completion} 4 | \alias{create_chat_completion} 5 | \title{Create chat completion} 6 | \usage{ 7 | create_chat_completion( 8 | model, 9 | messages = NULL, 10 | temperature = 1, 11 | top_p = 1, 12 | n = 1, 13 | stream = FALSE, 14 | stop = NULL, 15 | max_tokens = NULL, 16 | presence_penalty = 0, 17 | frequency_penalty = 0, 18 | logit_bias = NULL, 19 | user = NULL, 20 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 21 | openai_organization = NULL 22 | ) 23 | } 24 | \arguments{ 25 | \item{model}{required; a length one character vector.} 26 | 27 | \item{messages}{required; defaults to \code{NULL}; a list in the following 28 | format: \verb{list(list("role" = "user", "content" = "Hey! How old are you?")}} 29 | 30 | \item{temperature}{required; defaults to \code{1}; a length one numeric vector 31 | with the value between \code{0} and \code{2}.} 32 | 33 | \item{top_p}{required; defaults to \code{1}; a length one numeric vector with the 34 | value between \code{0} and \code{1}.} 35 | 36 | \item{n}{required; defaults to \code{1}; a length one numeric vector with the 37 | integer value greater than \code{0}.} 38 | 39 | \item{stream}{required; defaults to \code{FALSE}; a length one logical vector. 40 | \strong{Currently is not implemented.}} 41 | 42 | \item{stop}{optional; defaults to \code{NULL}; a character vector of length 43 | between one and four.} 44 | 45 | \item{max_tokens}{required; defaults to \verb{(4096 - prompt tokens)}; a length 46 | one numeric vector with the integer value greater than \code{0}.} 47 | 48 | \item{presence_penalty}{required; defaults to \code{0}; a length one numeric 49 | vector with a value between \code{-2} and \code{2}.} 50 | 51 | \item{frequency_penalty}{required; defaults to \code{0}; a length one numeric 52 | vector with a value between \code{-2} and \code{2}.} 53 | 54 | \item{logit_bias}{optional; defaults to \code{NULL}; a named list.} 55 | 56 | \item{user}{optional; defaults to \code{NULL}; a length one character vector.} 57 | 58 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 59 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 60 | character vector. Specifies OpenAI API key.} 61 | 62 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 63 | character vector. Specifies OpenAI organization.} 64 | } 65 | \value{ 66 | Returns a list, elements of which contain chat completion(s) and 67 | supplementary information. 68 | } 69 | \description{ 70 | Creates a completion for the chat message. See \href{https://platform.openai.com/docs/api-reference/chat/create}{this page} for 71 | details. 72 | } 73 | \details{ 74 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/chat/create}{official documentation}. 75 | } 76 | \examples{ 77 | \dontrun{ 78 | create_chat_completion( 79 | model = "gpt-3.5-turbo", 80 | messages = list( 81 | list( 82 | "role" = "system", 83 | "content" = "You are a helpful assistant." 84 | ), 85 | list( 86 | "role" = "user", 87 | "content" = "Who won the world series in 2020?" 88 | ), 89 | list( 90 | "role" = "assistant", 91 | "content" = "The Los Angeles Dodgers won the World Series in 2020." 92 | ), 93 | list( 94 | "role" = "user", 95 | "content" = "Where was it played?" 96 | ) 97 | ) 98 | ) 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /tests/testthat/test-create_completion.R: -------------------------------------------------------------------------------- 1 | #------------------------------------------------------------------------------- 2 | # create_completion() 3 | 4 | function_name <- "create_completion" 5 | 6 | test_argument_validation( 7 | function_name = function_name, 8 | argument_name = "model", 9 | argument_type = "string", 10 | allow_null = FALSE 11 | ) 12 | 13 | test_argument_validation( 14 | function_name = function_name, 15 | argument_name = "prompt", 16 | argument_type = "character", 17 | allow_null = FALSE 18 | ) 19 | 20 | test_argument_validation( 21 | function_name = function_name, 22 | argument_name = "suffix", 23 | argument_type = "character", 24 | allow_null = TRUE 25 | ) 26 | 27 | test_argument_validation( 28 | function_name = function_name, 29 | argument_name = "max_tokens", 30 | argument_type = "count", 31 | allow_null = FALSE 32 | ) 33 | 34 | test_argument_validation( 35 | function_name = function_name, 36 | argument_name = "temperature", 37 | argument_type = "number", 38 | allow_null = FALSE 39 | ) 40 | 41 | test_argument_validation( 42 | function_name = function_name, 43 | argument_name = "top_p", 44 | argument_type = "number", 45 | allow_null = FALSE 46 | ) 47 | 48 | test_argument_validation( 49 | function_name = function_name, 50 | argument_name = "n", 51 | argument_type = "count", 52 | allow_null = FALSE 53 | ) 54 | 55 | test_argument_validation( 56 | function_name = function_name, 57 | argument_name = "stream", 58 | argument_type = "flag", 59 | allow_null = FALSE 60 | ) 61 | 62 | test_argument_validation( 63 | function_name = function_name, 64 | argument_name = "logprobs", 65 | argument_type = "count", 66 | allow_null = TRUE 67 | ) 68 | 69 | test_argument_validation( 70 | function_name = function_name, 71 | argument_name = "echo", 72 | argument_type = "flag", 73 | allow_null = FALSE 74 | ) 75 | 76 | test_argument_validation( 77 | function_name = function_name, 78 | argument_name = "stop", 79 | argument_type = "character", 80 | allow_null = TRUE 81 | ) 82 | 83 | test_argument_validation( 84 | function_name = function_name, 85 | argument_name = "presence_penalty", 86 | argument_type = "number", 87 | allow_null = FALSE 88 | ) 89 | 90 | test_argument_validation( 91 | function_name = function_name, 92 | argument_name = "frequency_penalty", 93 | argument_type = "number", 94 | allow_null = FALSE 95 | ) 96 | 97 | test_argument_validation( 98 | function_name = function_name, 99 | argument_name = "best_of", 100 | argument_type = "count", 101 | allow_null = FALSE 102 | ) 103 | 104 | # test_argument_validation( 105 | # function_name = function_name, 106 | # argument_name = "logit_bias", 107 | # argument_type = "count", 108 | # allow_null = FALSE 109 | # ) 110 | 111 | test_argument_validation( 112 | function_name = function_name, 113 | argument_name = "user", 114 | argument_type = "string", 115 | allow_null = TRUE 116 | ) 117 | 118 | test_argument_validation( 119 | function_name = function_name, 120 | argument_name = "openai_api_key", 121 | argument_type = "string", 122 | allow_null = FALSE 123 | ) 124 | 125 | test_argument_validation( 126 | function_name = function_name, 127 | argument_name = "openai_api_key", 128 | argument_type = "string", 129 | allow_null = FALSE 130 | ) 131 | 132 | test_argument_validation( 133 | function_name = function_name, 134 | argument_name = "openai_organization", 135 | argument_type = "string", 136 | allow_null = TRUE 137 | ) 138 | -------------------------------------------------------------------------------- /R/list_engines.R: -------------------------------------------------------------------------------- 1 | #' List engines 2 | #' 3 | #' @description `r lifecycle::badge("deprecated")` 4 | #' 5 | #' **Note:** This endpoint is deprecated and soon will be removed. Please use 6 | #' its replacement, 7 | #' [Models](https://platform.openai.com/docs/api-reference/models), instead. The 8 | #' replacement function in this package is `list_models()`. 9 | #' 10 | #' Lists available engines and provides basic information about such engines. 11 | #' See [this page](https://platform.openai.com/docs/api-reference/engines/list) 12 | #' for details. 13 | #' 14 | #' @details For arguments description please refer to the [official 15 | #' documentation](https://platform.openai.com/docs/api-reference/engines/list). 16 | #' 17 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 18 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 19 | #' character vector. Specifies OpenAI API key. 20 | #' @param openai_organization optional; defaults to `NULL`; a length one 21 | #' character vector. Specifies OpenAI organization. 22 | #' @return Returns a list, an element of which is a data frame containing 23 | #' information about engines. 24 | #' @examples \dontrun{ 25 | #' list_engines() 26 | #' # -> 27 | #' list_models() 28 | #' } 29 | #' @family engine functions 30 | #' @keywords internal 31 | #' @export 32 | list_engines <- function( 33 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 34 | openai_organization = NULL 35 | ) { 36 | 37 | lifecycle::deprecate_warn( 38 | when = "0.3.0", 39 | what = "list_engines()", 40 | with = "list_models()", 41 | details = paste( 42 | "Please use its replacement instead:", 43 | "https://platform.openai.com/docs/api-reference/models" 44 | ) 45 | ) 46 | 47 | #--------------------------------------------------------------------------- 48 | # Validate arguments 49 | 50 | assertthat::assert_that( 51 | assertthat::is.string(openai_api_key), 52 | assertthat::noNA(openai_api_key) 53 | ) 54 | 55 | if (!is.null(openai_organization)) { 56 | assertthat::assert_that( 57 | assertthat::is.string(openai_organization), 58 | assertthat::noNA(openai_organization) 59 | ) 60 | } 61 | 62 | #--------------------------------------------------------------------------- 63 | # Build parameters of the request 64 | 65 | base_url <- "https://api.openai.com/v1/engines" 66 | 67 | headers <- c( 68 | "Authorization" = paste("Bearer", openai_api_key), 69 | "Content-Type" = "application/json" 70 | ) 71 | 72 | if (!is.null(openai_organization)) { 73 | headers["OpenAI-Organization"] <- openai_organization 74 | } 75 | 76 | #--------------------------------------------------------------------------- 77 | # Make a request and parse it 78 | 79 | response <- httr::GET( 80 | url = base_url, 81 | httr::add_headers(.headers = headers), 82 | encode = "json" 83 | ) 84 | 85 | verify_mime_type(response) 86 | 87 | parsed <- response %>% 88 | httr::content(as = "text", encoding = "UTF-8") %>% 89 | jsonlite::fromJSON(flatten = TRUE) 90 | 91 | #--------------------------------------------------------------------------- 92 | # Check whether request failed and return parsed 93 | 94 | if (httr::http_error(response)) { 95 | paste0( 96 | "OpenAI API request failed [", 97 | httr::status_code(response), 98 | "]:\n\n", 99 | parsed$error$message 100 | ) %>% 101 | stop(call. = FALSE) 102 | } 103 | 104 | parsed 105 | 106 | } 107 | -------------------------------------------------------------------------------- /R/create_moderation.R: -------------------------------------------------------------------------------- 1 | #' Create moderation 2 | #' 3 | #' Classifies if text violates OpenAI's Content Policy. See [this 4 | #' page](https://platform.openai.com/docs/api-reference/moderations/create) for 5 | #' details. 6 | #' 7 | #' For arguments description please refer to the [official 8 | #' documentation](https://platform.openai.com/docs/api-reference/completions/create). 9 | #' 10 | #' @param input required; an arbitrary length character vector. 11 | #' @param model required; a length one character vector. 12 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 13 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 14 | #' character vector. Specifies OpenAI API key. 15 | #' @param openai_organization optional; defaults to `NULL`; a length one 16 | #' character vector. Specifies OpenAI organization. 17 | #' @return Returns a list, elements of which contain information about the 18 | #' model. 19 | #' @examples \dontrun{ 20 | #' create_moderation( 21 | #' input = "I want to kill them all.", 22 | #' model = "text-moderation-stable" 23 | #' ) 24 | #' } 25 | #' @export 26 | create_moderation <- function( 27 | input, 28 | model, 29 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 30 | openai_organization = NULL 31 | ) { 32 | 33 | #--------------------------------------------------------------------------- 34 | # Validate arguments 35 | 36 | assertthat::assert_that( 37 | is.character(input), 38 | assertthat::noNA(input) 39 | ) 40 | 41 | assertthat::assert_that( 42 | assertthat::is.string(model), 43 | assertthat::noNA(model) 44 | ) 45 | 46 | assertthat::assert_that( 47 | assertthat::is.string(openai_api_key), 48 | assertthat::noNA(openai_api_key) 49 | ) 50 | 51 | if (!is.null(openai_organization)) { 52 | assertthat::assert_that( 53 | assertthat::is.string(openai_organization), 54 | assertthat::noNA(openai_organization) 55 | ) 56 | } 57 | 58 | #--------------------------------------------------------------------------- 59 | # Build path parameters 60 | 61 | task <- "moderations" 62 | 63 | base_url <- glue::glue("https://api.openai.com/v1/{task}") 64 | 65 | headers <- c( 66 | "Authorization" = paste("Bearer", openai_api_key), 67 | "Content-Type" = "application/json" 68 | ) 69 | 70 | if (!is.null(openai_organization)) { 71 | headers["OpenAI-Organization"] <- openai_organization 72 | } 73 | 74 | #--------------------------------------------------------------------------- 75 | # Build request body 76 | 77 | body <- list() 78 | body[["input"]] <- input 79 | body[["model"]] <- model 80 | 81 | #--------------------------------------------------------------------------- 82 | # Make a request and parse it 83 | 84 | response <- httr::POST( 85 | url = base_url, 86 | httr::add_headers(.headers = headers), 87 | body = body, 88 | encode = "json" 89 | ) 90 | 91 | verify_mime_type(response) 92 | 93 | parsed <- response %>% 94 | httr::content(as = "text", encoding = "UTF-8") %>% 95 | jsonlite::fromJSON(flatten = TRUE) 96 | 97 | #--------------------------------------------------------------------------- 98 | # Check whether request failed and return parsed 99 | 100 | if (httr::http_error(response)) { 101 | paste0( 102 | "OpenAI API request failed [", 103 | httr::status_code(response), 104 | "]:\n\n", 105 | parsed$error$message 106 | ) %>% 107 | stop(call. = FALSE) 108 | } 109 | 110 | parsed 111 | 112 | } 113 | -------------------------------------------------------------------------------- /man/create_fine_tune.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/create_fine_tune.R 3 | \name{create_fine_tune} 4 | \alias{create_fine_tune} 5 | \title{Create fine-tune} 6 | \usage{ 7 | create_fine_tune( 8 | training_file, 9 | validation_file = NULL, 10 | model, 11 | n_epochs = 4, 12 | batch_size = NULL, 13 | learning_rate_multiplier = NULL, 14 | prompt_loss_weight = 0.1, 15 | compute_classification_metrics = FALSE, 16 | classification_n_classes = NULL, 17 | classification_positive_class = NULL, 18 | classification_betas = NULL, 19 | suffix = NULL, 20 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 21 | openai_organization = NULL 22 | ) 23 | } 24 | \arguments{ 25 | \item{training_file}{required; a length one character vector.} 26 | 27 | \item{validation_file}{optional; defaults to \code{NULL}; a length one character 28 | vector.} 29 | 30 | \item{model}{required; a length one character vector.} 31 | 32 | \item{n_epochs}{required; defaults to \code{4}; a length one numeric vector with 33 | the integer value greater than \code{0}.} 34 | 35 | \item{batch_size}{optional; defaults to \code{NULL}; a length one numeric vector 36 | with the integer value greater than \code{0}.} 37 | 38 | \item{learning_rate_multiplier}{optional; defaults to \code{NULL}; a length one 39 | numeric vector with the value greater than \code{0}.} 40 | 41 | \item{prompt_loss_weight}{required; defaults to \code{0.1}; a length one numeric 42 | vector.} 43 | 44 | \item{compute_classification_metrics}{required; defaults to \code{FLASE}; a length 45 | one logical vector.} 46 | 47 | \item{classification_n_classes}{optional; defaults to \code{NULL}; a length one 48 | numeric vector with the value greater than \code{0}.} 49 | 50 | \item{classification_positive_class}{optional; defaults to \code{NULL}; a length 51 | one character vector.} 52 | 53 | \item{classification_betas}{optional; defaults to \code{NULL}; a list elements of 54 | which are numeric values greater than \code{0}.} 55 | 56 | \item{suffix}{optional; defaults to \code{NULL}; a length one character vector.} 57 | 58 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 59 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 60 | character vector. Specifies OpenAI API key.} 61 | 62 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 63 | character vector. Specifies OpenAI organization.} 64 | } 65 | \value{ 66 | Returns a list, elements of which contain information about the 67 | fine-tune. 68 | } 69 | \description{ 70 | Creates a job that fine-tunes a specified model based on a given dataset. See 71 | \href{https://platform.openai.com/docs/api-reference/fine-tunes/create}{this page} for 72 | details. 73 | } 74 | \details{ 75 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/fine-tunes/create}{official documentation}. 76 | } 77 | \examples{ 78 | \dontrun{ 79 | training_file <- system.file( 80 | "extdata", "sport_prepared_train.jsonl", package = "openai" 81 | ) 82 | validation_file <- system.file( 83 | "extdata", "sport_prepared_train.jsonl", package = "openai" 84 | ) 85 | 86 | training_info <- upload_file(training_file, "fine-tune") 87 | validation_info <- upload_file(validation_file, "fine-tune") 88 | 89 | info <- create_fine_tune( 90 | training_file = training_info$id, 91 | validation_file = validation_info$id, 92 | model = "ada", 93 | compute_classification_metrics = TRUE, 94 | classification_positive_class = " baseball" # Mind space in front 95 | ) 96 | } 97 | } 98 | \seealso{ 99 | Other fine-tune functions: 100 | \code{\link{cancel_fine_tune}()}, 101 | \code{\link{delete_fine_tune_model}()}, 102 | \code{\link{list_fine_tune_events}()}, 103 | \code{\link{list_fine_tunes}()}, 104 | \code{\link{retrieve_fine_tune}()} 105 | } 106 | \concept{fine-tune functions} 107 | -------------------------------------------------------------------------------- /R/retrieve_engine.R: -------------------------------------------------------------------------------- 1 | #' Retrieve engine 2 | #' 3 | #' @description `r lifecycle::badge("deprecated")` 4 | #' 5 | #' **Note:** This endpoint is deprecated and soon will be removed. Please use 6 | #' its replacement, 7 | #' [Models](https://platform.openai.com/docs/api-reference/models), instead. The 8 | #' replacement function in this package is `retrieve_model()`. 9 | #' 10 | #' Provides information about a specified engine. See [this 11 | #' page](https://platform.openai.com/docs/api-reference/engines/retrieve) for 12 | #' details. 13 | #' 14 | #' @details For arguments description please refer to the [official 15 | #' documentation](https://platform.openai.com/docs/api-reference/engines/retrieve). 16 | #' 17 | #' @param engine_id required; a length one character vector. 18 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 19 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 20 | #' character vector. Specifies OpenAI API key. 21 | #' @param openai_organization optional; defaults to `NULL`; a length one 22 | #' character vector. Specifies OpenAI organization. 23 | #' @return Returns a list, elements of which contain information about the 24 | #' engine. 25 | #' @examples \dontrun{ 26 | #' retrieve_engine("text-davinci-002") 27 | #' # -> 28 | #' retrieve_model("text-davinci-002") 29 | #' } 30 | #' @family engine functions 31 | #' @keywords internal 32 | #' @export 33 | retrieve_engine <- function( 34 | engine_id, 35 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 36 | openai_organization = NULL 37 | ) { 38 | 39 | lifecycle::deprecate_warn( 40 | when = "0.3.0", 41 | what = "retrieve_engine()", 42 | with = "retrieve_model()", 43 | details = paste( 44 | "Please use its replacement instead:", 45 | "https://platform.openai.com/docs/api-reference/models" 46 | ) 47 | ) 48 | 49 | #--------------------------------------------------------------------------- 50 | # Validate arguments 51 | 52 | assertthat::assert_that( 53 | assertthat::is.string(engine_id), 54 | assertthat::noNA(engine_id) 55 | ) 56 | 57 | assertthat::assert_that( 58 | assertthat::is.string(openai_api_key), 59 | assertthat::noNA(openai_api_key) 60 | ) 61 | 62 | if (!is.null(openai_organization)) { 63 | assertthat::assert_that( 64 | assertthat::is.string(openai_organization), 65 | assertthat::noNA(openai_organization) 66 | ) 67 | } 68 | 69 | #--------------------------------------------------------------------------- 70 | # Build parameters of the request 71 | 72 | base_url <- glue::glue("https://api.openai.com/v1/engines/{engine_id}") 73 | 74 | headers <- c( 75 | "Authorization" = paste("Bearer", openai_api_key), 76 | "Content-Type" = "application/json" 77 | ) 78 | 79 | if (!is.null(openai_organization)) { 80 | headers["OpenAI-Organization"] <- openai_organization 81 | } 82 | 83 | #--------------------------------------------------------------------------- 84 | # Make a request and parse it 85 | 86 | response <- httr::GET( 87 | url = base_url, 88 | httr::add_headers(.headers = headers), 89 | encode = "json" 90 | ) 91 | 92 | verify_mime_type(response) 93 | 94 | parsed <- response %>% 95 | httr::content(as = "text", encoding = "UTF-8") %>% 96 | jsonlite::fromJSON(flatten = TRUE) 97 | 98 | #--------------------------------------------------------------------------- 99 | # Check whether request failed and return parsed 100 | 101 | if (httr::http_error(response)) { 102 | paste0( 103 | "OpenAI API request failed [", 104 | httr::status_code(response), 105 | "]:\n\n", 106 | parsed$error$message 107 | ) %>% 108 | stop(call. = FALSE) 109 | } 110 | 111 | parsed 112 | 113 | } 114 | -------------------------------------------------------------------------------- /man/create_completion.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/create_completion.R 3 | \name{create_completion} 4 | \alias{create_completion} 5 | \title{Create completion} 6 | \usage{ 7 | create_completion( 8 | engine_id = deprecated(), 9 | model, 10 | prompt = "<|endoftext|>", 11 | suffix = NULL, 12 | max_tokens = 16, 13 | temperature = 1, 14 | top_p = 1, 15 | n = 1, 16 | stream = FALSE, 17 | logprobs = NULL, 18 | echo = FALSE, 19 | stop = NULL, 20 | presence_penalty = 0, 21 | frequency_penalty = 0, 22 | best_of = 1, 23 | logit_bias = NULL, 24 | user = NULL, 25 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 26 | openai_organization = NULL 27 | ) 28 | } 29 | \arguments{ 30 | \item{engine_id}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}}} 31 | 32 | \item{model}{required; a length one character vector.} 33 | 34 | \item{prompt}{required; defaults to \code{"<|endoftext|>"}; an arbitrary length 35 | character vector.} 36 | 37 | \item{suffix}{optional; defaults to \code{NULL}; a length one character vector.} 38 | 39 | \item{max_tokens}{required; defaults to \code{16}; a length one numeric vector 40 | with the integer value greater than \code{0}.} 41 | 42 | \item{temperature}{required; defaults to \code{1}; a length one numeric vector 43 | with the value between \code{0} and \code{2}.} 44 | 45 | \item{top_p}{required; defaults to \code{1}; a length one numeric vector with the 46 | value between \code{0} and \code{1}.} 47 | 48 | \item{n}{required; defaults to \code{1}; a length one numeric vector with the 49 | integer value greater than \code{0}.} 50 | 51 | \item{stream}{required; defaults to \code{FALSE}; a length one logical vector. 52 | \strong{Currently is not implemented.}} 53 | 54 | \item{logprobs}{optional; defaults to \code{NULL}; a length one numeric vector 55 | with the integer value between \code{0} and \code{5}.} 56 | 57 | \item{echo}{required; defaults to \code{FALSE}; a length one logical vector.} 58 | 59 | \item{stop}{optional; defaults to \code{NULL}; a character vector of length 60 | between one and four.} 61 | 62 | \item{presence_penalty}{required; defaults to \code{0}; a length one numeric 63 | vector with a value between \code{-2} and \code{2}.} 64 | 65 | \item{frequency_penalty}{required; defaults to \code{0}; a length one numeric 66 | vector with a value between \code{-2} and \code{2}.} 67 | 68 | \item{best_of}{required; defaults to \code{1}; a length one numeric vector with 69 | the integer value greater than \code{0}.} 70 | 71 | \item{logit_bias}{optional; defaults to \code{NULL}; a named list.} 72 | 73 | \item{user}{optional; defaults to \code{NULL}; a length one character vector.} 74 | 75 | \item{openai_api_key}{required; defaults to \code{Sys.getenv("OPENAI_API_KEY")} 76 | (i.e., the value is retrieved from the \code{.Renviron} file); a length one 77 | character vector. Specifies OpenAI API key.} 78 | 79 | \item{openai_organization}{optional; defaults to \code{NULL}; a length one 80 | character vector. Specifies OpenAI organization.} 81 | } 82 | \value{ 83 | Returns a list, elements of which contain completion(s) and 84 | supplementary information. 85 | } 86 | \description{ 87 | Creates a completion based on the provided prompt and parameters. See \href{https://platform.openai.com/docs/api-reference/completions/create}{this page} for 88 | details. 89 | } 90 | \details{ 91 | For arguments description please refer to the \href{https://platform.openai.com/docs/api-reference/completions/create}{official documentation}. 92 | } 93 | \examples{ 94 | \dontrun{ 95 | create_completion( 96 | model = "text-davinci-002", 97 | prompt = "Say this is a test", 98 | max_tokens = 5 99 | ) 100 | 101 | logit_bias <- list( 102 | "11" = -100, 103 | "13" = -100 104 | ) 105 | create_completion( 106 | model = "ada", 107 | prompt = "Generate a question and an answer", 108 | n = 4, 109 | best_of = 4, 110 | logit_bias = logit_bias 111 | ) 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /R/cancel_fine_tune.R: -------------------------------------------------------------------------------- 1 | #' Cancel fine-tune 2 | #' 3 | #' Cancel a running fine-tune job. See [this 4 | #' page](https://platform.openai.com/docs/api-reference/fine-tunes/cancel) for 5 | #' details. 6 | #' 7 | #' For arguments description please refer to the [official 8 | #' documentation](https://platform.openai.com/docs/api-reference/fine-tunes/cancel). 9 | #' 10 | #' @param fine_tune_id required; a length one character vector. 11 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 12 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 13 | #' character vector. Specifies OpenAI API key. 14 | #' @param openai_organization optional; defaults to `NULL`; a length one 15 | #' character vector. Specifies OpenAI organization. 16 | #' @return Returns a list, elements of which contains information about the 17 | #' cancelled fine-tune. 18 | #' @examples \dontrun{ 19 | #' training_file <- system.file( 20 | #' "extdata", "sport_prepared_train.jsonl", package = "openai" 21 | #' ) 22 | #' validation_file <- system.file( 23 | #' "extdata", "sport_prepared_train.jsonl", package = "openai" 24 | #' ) 25 | #' 26 | #' training_info <- upload_file(training_file, "fine-tune") 27 | #' validation_info <- upload_file(validation_file, "fine-tune") 28 | #' 29 | #' info <- create_fine_tune( 30 | #' training_file = training_info$id, 31 | #' validation_file = validation_info$id, 32 | #' model = "ada", 33 | #' compute_classification_metrics = TRUE, 34 | #' classification_positive_class = " baseball" # Mind space in front 35 | #' ) 36 | #' 37 | #' id <- ifelse( 38 | #' length(info$data$id) > 1, 39 | #' info$data$id[length(info$data$id)], 40 | #' info$data$id 41 | #' ) 42 | #' 43 | #' cancel_fine_tune(fine_tune_id = id) 44 | #' } 45 | #' @family fine-tune functions 46 | #' @export 47 | cancel_fine_tune <- function( 48 | fine_tune_id, 49 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 50 | openai_organization = NULL 51 | ) { 52 | 53 | #--------------------------------------------------------------------------- 54 | # Validate arguments 55 | 56 | assertthat::assert_that( 57 | assertthat::is.string(fine_tune_id), 58 | assertthat::noNA(fine_tune_id) 59 | ) 60 | 61 | assertthat::assert_that( 62 | assertthat::is.string(openai_api_key), 63 | assertthat::noNA(openai_api_key) 64 | ) 65 | 66 | if (!is.null(openai_organization)) { 67 | assertthat::assert_that( 68 | assertthat::is.string(openai_organization), 69 | assertthat::noNA(openai_organization) 70 | ) 71 | } 72 | 73 | #--------------------------------------------------------------------------- 74 | # Build parameters of the request 75 | 76 | base_url <- glue::glue( 77 | "https://api.openai.com/v1/fine-tunes/{fine_tune_id}/cancel" 78 | ) 79 | 80 | headers <- c( 81 | "Authorization" = paste("Bearer", openai_api_key), 82 | "Content-Type" = "application/json" 83 | ) 84 | 85 | if (!is.null(openai_organization)) { 86 | headers["OpenAI-Organization"] <- openai_organization 87 | } 88 | 89 | #--------------------------------------------------------------------------- 90 | # Make a request and parse it 91 | 92 | response <- httr::POST( 93 | url = base_url, 94 | httr::add_headers(.headers = headers), 95 | encode = "json" 96 | ) 97 | 98 | verify_mime_type(response) 99 | 100 | parsed <- response %>% 101 | httr::content(as = "text", encoding = "UTF-8") %>% 102 | jsonlite::fromJSON(flatten = TRUE) 103 | 104 | #--------------------------------------------------------------------------- 105 | # Check whether request failed and return parsed 106 | 107 | if (httr::http_error(response)) { 108 | paste0( 109 | "OpenAI API request failed [", 110 | httr::status_code(response), 111 | "]:\n\n", 112 | parsed$error$message 113 | ) %>% 114 | stop(call. = FALSE) 115 | } 116 | 117 | parsed 118 | 119 | } 120 | -------------------------------------------------------------------------------- /R/retrieve_fine_tune.R: -------------------------------------------------------------------------------- 1 | #' Retrieve fine-tune 2 | #' 3 | #' Returns information about the specified fine-tune job. See [this 4 | #' page](https://platform.openai.com/docs/api-reference/fine-tunes/retrieve) for 5 | #' details. 6 | #' 7 | #' For arguments description please refer to the [official 8 | #' documentation](https://platform.openai.com/docs/api-reference/fine-tunes/retrieve). 9 | #' 10 | #' @param fine_tune_id required; a length one character vector. 11 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 12 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 13 | #' character vector. Specifies OpenAI API key. 14 | #' @param openai_organization optional; defaults to `NULL`; a length one 15 | #' character vector. Specifies OpenAI organization. 16 | #' @return Returns a list, elements of which contains information about the 17 | #' fine-tune. 18 | #' @examples \dontrun{ 19 | #' training_file <- system.file( 20 | #' "extdata", "sport_prepared_train.jsonl", package = "openai" 21 | #' ) 22 | #' validation_file <- system.file( 23 | #' "extdata", "sport_prepared_train.jsonl", package = "openai" 24 | #' ) 25 | #' 26 | #' training_info <- upload_file(training_file, "fine-tune") 27 | #' validation_info <- upload_file(validation_file, "fine-tune") 28 | #' 29 | #' info <- create_fine_tune( 30 | #' training_file = training_info$id, 31 | #' validation_file = validation_info$id, 32 | #' model = "ada", 33 | #' compute_classification_metrics = TRUE, 34 | #' classification_positive_class = " baseball" # Mind space in front 35 | #' ) 36 | #' 37 | #' id <- ifelse( 38 | #' length(info$data$id) > 1, 39 | #' info$data$id[length(info$data$id)], 40 | #' info$data$id 41 | #' ) 42 | #' 43 | #' retrieve_fine_tune(fine_tune_id = id) 44 | #' } 45 | #' @family fine-tune functions 46 | #' @export 47 | retrieve_fine_tune <- function( 48 | fine_tune_id, 49 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 50 | openai_organization = NULL 51 | ) { 52 | 53 | #--------------------------------------------------------------------------- 54 | # Validate arguments 55 | 56 | assertthat::assert_that( 57 | assertthat::is.string(fine_tune_id), 58 | assertthat::noNA(fine_tune_id) 59 | ) 60 | 61 | assertthat::assert_that( 62 | assertthat::is.string(openai_api_key), 63 | assertthat::noNA(openai_api_key) 64 | ) 65 | 66 | if (!is.null(openai_organization)) { 67 | assertthat::assert_that( 68 | assertthat::is.string(openai_organization), 69 | assertthat::noNA(openai_organization) 70 | ) 71 | } 72 | 73 | #--------------------------------------------------------------------------- 74 | # Build parameters of the request 75 | 76 | base_url <- glue::glue( 77 | "https://api.openai.com/v1/fine-tunes/{fine_tune_id}" 78 | ) 79 | 80 | headers <- c( 81 | "Authorization" = paste("Bearer", openai_api_key), 82 | "Content-Type" = "application/json" 83 | ) 84 | 85 | if (!is.null(openai_organization)) { 86 | headers["OpenAI-Organization"] <- openai_organization 87 | } 88 | 89 | #--------------------------------------------------------------------------- 90 | # Make a request and parse it 91 | 92 | response <- httr::GET( 93 | url = base_url, 94 | httr::add_headers(.headers = headers), 95 | encode = "json" 96 | ) 97 | 98 | verify_mime_type(response) 99 | 100 | parsed <- response %>% 101 | httr::content(as = "text", encoding = "UTF-8") %>% 102 | jsonlite::fromJSON(flatten = TRUE) 103 | 104 | #--------------------------------------------------------------------------- 105 | # Check whether request failed and return parsed 106 | 107 | if (httr::http_error(response)) { 108 | paste0( 109 | "OpenAI API request failed [", 110 | httr::status_code(response), 111 | "]:\n\n", 112 | parsed$error$message 113 | ) %>% 114 | stop(call. = FALSE) 115 | } 116 | 117 | parsed 118 | 119 | } 120 | -------------------------------------------------------------------------------- /R/upload_file.R: -------------------------------------------------------------------------------- 1 | #' Upload file 2 | #' 3 | #' Uploads a file that will be used for various purposes. The size of the 4 | #' storage is limited to 1 Gb. See [this 5 | #' page](https://platform.openai.com/docs/api-reference/files/upload) for details. 6 | #' 7 | #' For arguments description please refer to the [official 8 | #' documentation](https://platform.openai.com/docs/api-reference/files/upload). 9 | #' 10 | #' @param file required; a length one character vector. 11 | #' @param purpose required; defaults to `"fine-tune"`; a length one character 12 | #' vector equals to`"fine-tune"`. 13 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 14 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 15 | #' character vector. Specifies OpenAI API key. 16 | #' @param openai_organization optional; defaults to `NULL`; a length one 17 | #' character vector. Specifies OpenAI organization. 18 | #' @return Returns a list, elements of which contains ID of the uploaded file 19 | #' and other supplementary information. 20 | #' @examples \dontrun{ 21 | #' file <- system.file("extdata", "classification-file.jsonl", package = "openai") 22 | #' upload_file(file = file, purpose = "classification") 23 | #' } 24 | #' @family file functions 25 | #' @export 26 | upload_file <- function( 27 | file, 28 | purpose = c("search", "answers", "classifications", "fine-tune"), 29 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 30 | openai_organization = NULL 31 | ) { 32 | 33 | purpose <- match.arg(purpose) 34 | 35 | #--------------------------------------------------------------------------- 36 | # Validate arguments 37 | 38 | assertthat::assert_that( 39 | assertthat::is.string(file), 40 | assertthat::noNA(file), 41 | file.exists(file), 42 | assertthat::is.readable(file) 43 | ) 44 | 45 | assertthat::assert_that( 46 | assertthat::is.string(purpose), 47 | assertthat::noNA(purpose) 48 | ) 49 | 50 | assertthat::assert_that( 51 | assertthat::is.string(openai_api_key), 52 | assertthat::noNA(openai_api_key) 53 | ) 54 | 55 | assertthat::assert_that( 56 | assertthat::is.string(openai_api_key), 57 | assertthat::noNA(openai_api_key) 58 | ) 59 | 60 | if (!is.null(openai_organization)) { 61 | assertthat::assert_that( 62 | assertthat::is.string(openai_organization), 63 | assertthat::noNA(openai_organization) 64 | ) 65 | } 66 | 67 | #--------------------------------------------------------------------------- 68 | # Build parameters of the request 69 | 70 | base_url <- "https://api.openai.com/v1/files" 71 | 72 | headers <- c( 73 | "Authorization" = paste("Bearer", openai_api_key), 74 | "Content-Type" = "multipart/form-data" 75 | ) 76 | 77 | if (!is.null(openai_organization)) { 78 | headers["OpenAI-Organization"] <- openai_organization 79 | } 80 | 81 | #--------------------------------------------------------------------------- 82 | # Build request body 83 | 84 | body <- list() 85 | body[["file"]] <- httr::upload_file(file) 86 | body[["purpose"]] <- purpose 87 | 88 | #--------------------------------------------------------------------------- 89 | # Make a request and parse it 90 | 91 | response <- httr::POST( 92 | url = base_url, 93 | httr::add_headers(.headers = headers), 94 | body = body, 95 | encode = "multipart" 96 | ) 97 | 98 | verify_mime_type(response) 99 | 100 | parsed <- response %>% 101 | httr::content(as = "text", encoding = "UTF-8") %>% 102 | jsonlite::fromJSON(flatten = TRUE) 103 | 104 | #--------------------------------------------------------------------------- 105 | # Check whether request failed and return parsed 106 | 107 | if (httr::http_error(response)) { 108 | paste0( 109 | "OpenAI API request failed [", 110 | httr::status_code(response), 111 | "]:\n\n", 112 | parsed$error$message 113 | ) %>% 114 | stop(call. = FALSE) 115 | } 116 | 117 | parsed 118 | 119 | } 120 | -------------------------------------------------------------------------------- /R/create_embedding.R: -------------------------------------------------------------------------------- 1 | #' Create embeddings 2 | #' 3 | #' Creates an embedding vector that represents the provided input. See [this 4 | #' page](https://platform.openai.com/docs/api-reference/embeddings/create) for 5 | #' details. 6 | #' 7 | #' For arguments description please refer to the [official 8 | #' documentation](https://platform.openai.com/docs/api-reference/embeddings/create). 9 | #' 10 | #' @param engine_id `r lifecycle::badge("deprecated")` 11 | #' @param model required; a length one character vector. 12 | #' @param input required; an arbitrary length character vector. 13 | #' @param user optional; defaults to `NULL`; a length one character vector. 14 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 15 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 16 | #' character vector. Specifies OpenAI API key. 17 | #' @param openai_organization optional; defaults to `NULL`; a length one 18 | #' character vector. Specifies OpenAI organization. 19 | #' @return Returns a list, an element of which contains embedding vector(s) for 20 | #' a given input. 21 | #' @examples \dontrun{ 22 | #' create_embedding( 23 | #' model = "text-embedding-ada-002", 24 | #' input = c( 25 | #' "Ah, it is so boring to write documentation", 26 | #' "But examples are really crucial" 27 | #' ) 28 | #' ) 29 | #' } 30 | #' @export 31 | create_embedding <- function( 32 | engine_id = deprecated(), 33 | model, 34 | input, 35 | user = NULL, 36 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 37 | openai_organization = NULL 38 | ) { 39 | 40 | if (lifecycle::is_present(engine_id)) { 41 | lifecycle::deprecate_warn( 42 | "0.3.0", 43 | "create_completion(engine_id)", 44 | "create_completion(model)" 45 | ) 46 | model <- engine_id 47 | } 48 | 49 | #--------------------------------------------------------------------------- 50 | # Validate arguments 51 | 52 | assertthat::assert_that( 53 | assertthat::is.string(model), 54 | assertthat::noNA(model) 55 | ) 56 | 57 | assertthat::assert_that( 58 | is.character(input), 59 | assertthat::noNA(input) 60 | ) 61 | 62 | if (!is.null(user)) { 63 | assertthat::assert_that( 64 | assertthat::is.string(user), 65 | assertthat::noNA(user) 66 | ) 67 | } 68 | 69 | assertthat::assert_that( 70 | assertthat::is.string(openai_api_key), 71 | assertthat::noNA(openai_api_key) 72 | ) 73 | 74 | if (!is.null(openai_organization)) { 75 | assertthat::assert_that( 76 | assertthat::is.string(openai_organization), 77 | assertthat::noNA(openai_organization) 78 | ) 79 | } 80 | 81 | #--------------------------------------------------------------------------- 82 | # Build path parameters 83 | 84 | task <- "embeddings" 85 | 86 | base_url <- glue::glue("https://api.openai.com/v1/{task}") 87 | 88 | headers <- c( 89 | "Authorization" = paste("Bearer", openai_api_key), 90 | "Content-Type" = "application/json" 91 | ) 92 | 93 | if (!is.null(openai_organization)) { 94 | headers["OpenAI-Organization"] <- openai_organization 95 | } 96 | 97 | #--------------------------------------------------------------------------- 98 | # Build request body 99 | 100 | body <- list() 101 | body[["model"]] <- model 102 | body[["input"]] <- input 103 | body[["user"]] <- user 104 | 105 | #--------------------------------------------------------------------------- 106 | # Make a request and parse it 107 | 108 | response <- httr::POST( 109 | url = base_url, 110 | httr::add_headers(.headers = headers), 111 | body = body, 112 | encode = "json" 113 | ) 114 | 115 | verify_mime_type(response) 116 | 117 | parsed <- response %>% 118 | httr::content(as = "text", encoding = "UTF-8") %>% 119 | jsonlite::fromJSON(flatten = TRUE) 120 | 121 | #--------------------------------------------------------------------------- 122 | # Check whether request failed and return parsed 123 | 124 | if (httr::http_error(response)) { 125 | paste0( 126 | "OpenAI API request failed [", 127 | httr::status_code(response), 128 | "]:\n\n", 129 | parsed$error$message 130 | ) %>% 131 | stop(call. = FALSE) 132 | } 133 | 134 | parsed 135 | 136 | } 137 | -------------------------------------------------------------------------------- /R/list_fine_tune_events.R: -------------------------------------------------------------------------------- 1 | #' List fine-tune events 2 | #' 3 | #' Returns events related to a specified fine-tune job. See [this 4 | #' page](https://platform.openai.com/docs/api-reference/fine-tunes/events) for 5 | #' details. 6 | #' 7 | #' For arguments description please refer to the [official 8 | #' documentation](https://platform.openai.com/docs/api-reference/fine-tunes/events). 9 | #' 10 | #' @param fine_tune_id required; a length one character vector. 11 | #' @param stream required; defaults to `FALSE`; a length one logical vector. 12 | #' **Currently is not implemented.** 13 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 14 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 15 | #' character vector. Specifies OpenAI API key. 16 | #' @param openai_organization optional; defaults to `NULL`; a length one 17 | #' character vector. Specifies OpenAI organization. 18 | #' @return Returns a list, elements of which contains information about the 19 | #' fine-tune events. 20 | #' @examples \dontrun{ 21 | #' training_file <- system.file( 22 | #' "extdata", "sport_prepared_train.jsonl", package = "openai" 23 | #' ) 24 | #' validation_file <- system.file( 25 | #' "extdata", "sport_prepared_train.jsonl", package = "openai" 26 | #' ) 27 | #' 28 | #' training_info <- upload_file(training_file, "fine-tune") 29 | #' validation_info <- upload_file(validation_file, "fine-tune") 30 | #' 31 | #' info <- create_fine_tune( 32 | #' training_file = training_info$id, 33 | #' validation_file = validation_info$id, 34 | #' model = "ada", 35 | #' compute_classification_metrics = TRUE, 36 | #' classification_positive_class = " baseball" # Mind space in front 37 | #' ) 38 | #' 39 | #' id <- ifelse( 40 | #' length(info$data$id) > 1, 41 | #' info$data$id[length(info$data$id)], 42 | #' info$data$id 43 | #' ) 44 | #' 45 | #' list_fine_tune_events(fine_tune_id = id) 46 | #' } 47 | #' @family fine-tune functions 48 | #' @export 49 | list_fine_tune_events <- function( 50 | fine_tune_id, 51 | stream = FALSE, 52 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 53 | openai_organization = NULL 54 | ) { 55 | 56 | #--------------------------------------------------------------------------- 57 | # Validate arguments 58 | 59 | assertthat::assert_that( 60 | assertthat::is.string(fine_tune_id), 61 | assertthat::noNA(fine_tune_id) 62 | ) 63 | 64 | assertthat::assert_that( 65 | assertthat::is.flag(stream), 66 | assertthat::noNA(stream), 67 | is_false(stream) 68 | ) 69 | 70 | assertthat::assert_that( 71 | assertthat::is.string(openai_api_key), 72 | assertthat::noNA(openai_api_key) 73 | ) 74 | 75 | if (!is.null(openai_organization)) { 76 | assertthat::assert_that( 77 | assertthat::is.string(openai_organization), 78 | assertthat::noNA(openai_organization) 79 | ) 80 | } 81 | 82 | #--------------------------------------------------------------------------- 83 | # Build parameters of the request 84 | 85 | base_url <- glue::glue( 86 | "https://api.openai.com/v1/fine-tunes/{fine_tune_id}/events" 87 | ) 88 | 89 | headers <- c( 90 | "Authorization" = paste("Bearer", openai_api_key), 91 | "Content-Type" = "application/json" 92 | ) 93 | 94 | if (!is.null(openai_organization)) { 95 | headers["OpenAI-Organization"] <- openai_organization 96 | } 97 | 98 | #--------------------------------------------------------------------------- 99 | # Build request body 100 | 101 | body <- list() 102 | body[["stream"]] <- stream 103 | 104 | #--------------------------------------------------------------------------- 105 | # Make a request and parse it 106 | 107 | response <- httr::GET( 108 | url = base_url, 109 | httr::add_headers(.headers = headers), 110 | body = body, 111 | encode = "json" 112 | ) 113 | 114 | verify_mime_type(response) 115 | 116 | parsed <- response %>% 117 | httr::content(as = "text", encoding = "UTF-8") %>% 118 | jsonlite::fromJSON(flatten = TRUE) 119 | 120 | #--------------------------------------------------------------------------- 121 | # Check whether request failed and return parsed 122 | 123 | if (httr::http_error(response)) { 124 | paste0( 125 | "OpenAI API request failed [", 126 | httr::status_code(response), 127 | "]:\n\n", 128 | parsed$error$message 129 | ) %>% 130 | stop(call. = FALSE) 131 | } 132 | 133 | parsed 134 | 135 | } 136 | -------------------------------------------------------------------------------- /R/create_image.R: -------------------------------------------------------------------------------- 1 | #' Create image 2 | #' 3 | #' Creates an image given a prompt. See [this 4 | #' page](https://platform.openai.com/docs/api-reference/images/create) for 5 | #' details. 6 | #' 7 | #' For arguments description please refer to the [official 8 | #' documentation](https://platform.openai.com/docs/api-reference/images/create). 9 | #' 10 | #' @param prompt required; a length one character vector. 11 | #' @param n required; defaults to `1`; a length one numeric vector with the 12 | #' integer value greater than `0`. 13 | #' @param size required; defaults to `"1024x1024"`; a length one character 14 | #' vector, one among `"256x256"`, `"512x512"`, and `"1024x1024"`. 15 | #' @param response_format required; defaults to `"url"`; a length one character 16 | #' vector, one among `"url"` and `"b64_json"`. 17 | #' @param user optional; defaults to `NULL`; a length one character vector. 18 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 19 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 20 | #' character vector. Specifies OpenAI API key. 21 | #' @param openai_organization optional; defaults to `NULL`; a length one 22 | #' character vector. Specifies OpenAI organization. 23 | #' @return Returns a list, an element of which contain either a link to the 24 | #' generated image or the generated image decoded in Base64. 25 | #' @examples \dontrun{ 26 | #' create_image("An astronaut riding a horse in a photorealistic style") 27 | #' } 28 | #' @family image functions 29 | #' @export 30 | create_image <- function( 31 | prompt, 32 | n = 1, 33 | size = c("1024x1024", "256x256", "512x512"), 34 | response_format = c("url", "b64_json"), 35 | user = NULL, 36 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 37 | openai_organization = NULL 38 | ) { 39 | 40 | size <- match.arg(size) 41 | response_format <- match.arg(response_format) 42 | 43 | #--------------------------------------------------------------------------- 44 | # Validate arguments 45 | 46 | assertthat::assert_that( 47 | assertthat::is.string(prompt), 48 | assertthat::noNA(prompt) 49 | ) 50 | 51 | assertthat::assert_that( 52 | assertthat::is.count(n) 53 | ) 54 | 55 | assertthat::assert_that( 56 | assertthat::is.string(size), 57 | assertthat::noNA(size) 58 | ) 59 | 60 | assertthat::assert_that( 61 | assertthat::is.string(response_format), 62 | assertthat::noNA(response_format) 63 | ) 64 | 65 | if (!is.null(user)) { 66 | assertthat::assert_that( 67 | assertthat::is.string(user), 68 | assertthat::noNA(user) 69 | ) 70 | } 71 | 72 | assertthat::assert_that( 73 | assertthat::is.string(openai_api_key), 74 | assertthat::noNA(openai_api_key) 75 | ) 76 | 77 | if (!is.null(openai_organization)) { 78 | assertthat::assert_that( 79 | assertthat::is.string(openai_organization), 80 | assertthat::noNA(openai_organization) 81 | ) 82 | } 83 | 84 | #--------------------------------------------------------------------------- 85 | # Build path parameters 86 | 87 | task <- "images/generations" 88 | 89 | base_url <- glue::glue("https://api.openai.com/v1/{task}") 90 | 91 | headers <- c( 92 | "Authorization" = paste("Bearer", openai_api_key), 93 | "Content-Type" = "application/json" 94 | ) 95 | 96 | if (!is.null(openai_organization)) { 97 | headers["OpenAI-Organization"] <- openai_organization 98 | } 99 | 100 | #--------------------------------------------------------------------------- 101 | # Build request body 102 | 103 | body <- list() 104 | body[["prompt"]] <- prompt 105 | body[["n"]] <- n 106 | body[["size"]] <- size 107 | body[["response_format"]] <- response_format 108 | body[["user"]] <- user 109 | 110 | #--------------------------------------------------------------------------- 111 | # Make a request and parse it 112 | 113 | response <- httr::POST( 114 | url = base_url, 115 | httr::add_headers(.headers = headers), 116 | body = body, 117 | encode = "json" 118 | ) 119 | 120 | verify_mime_type(response) 121 | 122 | parsed <- response %>% 123 | httr::content(as = "text", encoding = "UTF-8") %>% 124 | jsonlite::fromJSON(flatten = TRUE) 125 | 126 | #--------------------------------------------------------------------------- 127 | # Check whether request failed and return parsed 128 | 129 | if (httr::http_error(response)) { 130 | paste0( 131 | "OpenAI API request failed [", 132 | httr::status_code(response), 133 | "]:\n\n", 134 | parsed$error$message 135 | ) %>% 136 | stop(call. = FALSE) 137 | } 138 | 139 | parsed 140 | 141 | } 142 | -------------------------------------------------------------------------------- /R/create_image_variation.R: -------------------------------------------------------------------------------- 1 | #' Create image variation 2 | #' 3 | #' Creates a variation of a given image. See 4 | #' [this page](https://platform.openai.com/docs/api-reference/images/create-variation) 5 | #' for details. 6 | #' 7 | #' For arguments description please refer to the [official 8 | #' documentation](https://platform.openai.com/docs/api-reference/images/create-variation). 9 | #' 10 | #' @param image required; a length one character vector. 11 | #' @param n required; defaults to `1`; a length one numeric vector with the 12 | #' integer value greater than `0`. 13 | #' @param size required; defaults to `"1024x1024"`; a length one character 14 | #' vector, one among `"256x256"`, `"512x512"`, and `"1024x1024"`. 15 | #' @param response_format required; defaults to `"url"`; a length one character 16 | #' vector, one among `"url"` and `"b64_json"`. 17 | #' @param user optional; defaults to `NULL`; a length one character vector. 18 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 19 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 20 | #' character vector. Specifies OpenAI API key. 21 | #' @param openai_organization optional; defaults to `NULL`; a length one 22 | #' character vector. Specifies OpenAI organization. 23 | #' @return Returns a list, an element of which contain either a link to the 24 | #' image variation or the image variation decoded in Base64. 25 | #' @examples \dontrun{ 26 | #' image <- system.file("extdata", "astronaut.png", package = "openai") 27 | #' create_image_variation( 28 | #' image = image, 29 | #' n = 1, 30 | #' size = "256x256", 31 | #' response_format = "url" 32 | #' ) 33 | #' } 34 | #' @family image functions 35 | #' @export 36 | create_image_variation <- function( 37 | image, 38 | n = 1, 39 | size = c("1024x1024", "256x256", "512x512"), 40 | response_format = c("url", "b64_json"), 41 | user = NULL, 42 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 43 | openai_organization = NULL 44 | ) { 45 | 46 | size <- match.arg(size) 47 | response_format <- match.arg(response_format) 48 | 49 | #--------------------------------------------------------------------------- 50 | # Validate arguments 51 | 52 | assertthat::assert_that( 53 | assertthat::is.string(image), 54 | assertthat::noNA(image), 55 | file.exists(image), 56 | assertthat::is.readable(image) 57 | ) 58 | 59 | assertthat::assert_that( 60 | assertthat::is.count(n) 61 | ) 62 | 63 | assertthat::assert_that( 64 | assertthat::is.string(size), 65 | assertthat::noNA(size) 66 | ) 67 | 68 | assertthat::assert_that( 69 | assertthat::is.string(response_format), 70 | assertthat::noNA(response_format) 71 | ) 72 | 73 | if (!is.null(user)) { 74 | assertthat::assert_that( 75 | assertthat::is.string(user), 76 | assertthat::noNA(user) 77 | ) 78 | } 79 | 80 | assertthat::assert_that( 81 | assertthat::is.string(openai_api_key), 82 | assertthat::noNA(openai_api_key) 83 | ) 84 | 85 | if (!is.null(openai_organization)) { 86 | assertthat::assert_that( 87 | assertthat::is.string(openai_organization), 88 | assertthat::noNA(openai_organization) 89 | ) 90 | } 91 | 92 | #--------------------------------------------------------------------------- 93 | # Build path parameters 94 | 95 | task <- "images/variations" 96 | 97 | base_url <- glue::glue("https://api.openai.com/v1/{task}") 98 | 99 | headers <- c( 100 | "Authorization" = paste("Bearer", openai_api_key), 101 | "Content-Type" = "multipart/form-data" 102 | ) 103 | 104 | if (!is.null(openai_organization)) { 105 | headers["OpenAI-Organization"] <- openai_organization 106 | } 107 | 108 | #--------------------------------------------------------------------------- 109 | # Build request body 110 | 111 | body <- list() 112 | body[["image"]] <- httr::upload_file(image) 113 | body[["n"]] <- n 114 | body[["size"]] <- size 115 | body[["response_format"]] <- response_format 116 | body[["user"]] <- user 117 | 118 | #--------------------------------------------------------------------------- 119 | # Make a request and parse it 120 | 121 | response <- httr::POST( 122 | url = base_url, 123 | httr::add_headers(.headers = headers), 124 | body = body, 125 | encode = "multipart" 126 | ) 127 | 128 | verify_mime_type(response) 129 | 130 | parsed <- response %>% 131 | httr::content(as = "text", encoding = "UTF-8") %>% 132 | jsonlite::fromJSON(flatten = TRUE) 133 | 134 | #--------------------------------------------------------------------------- 135 | # Check whether request failed and return parsed 136 | 137 | if (httr::http_error(response)) { 138 | paste0( 139 | "OpenAI API request failed [", 140 | httr::status_code(response), 141 | "]:\n\n", 142 | parsed$error$message 143 | ) %>% 144 | stop(call. = FALSE) 145 | } 146 | 147 | parsed 148 | 149 | 150 | } 151 | -------------------------------------------------------------------------------- /R/create_translation.R: -------------------------------------------------------------------------------- 1 | #' Create translation 2 | #' 3 | #' Translates audio into into English. See 4 | #' [this page](https://platform.openai.com/docs/api-reference/audio/create) 5 | #' for details. 6 | #' 7 | #' For arguments description please refer to the [official 8 | #' documentation](https://platform.openai.com/docs/api-reference/audio/create). 9 | #' 10 | #' @param file required; a length one character vector. 11 | #' @param model required; a length one character vector. 12 | #' @param prompt optional; defaults to `NULL`; a length one character vector. 13 | #' @param response_format required; defaults to `"json"`; length one character 14 | #' vector equals to `"json"`. **Currently only `"json"` is implemented.** 15 | #' @param temperature required; defaults to `1`; a length one numeric vector 16 | #' with the value between `0` and `2`. 17 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 18 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 19 | #' character vector. Specifies OpenAI API key. 20 | #' @param openai_organization optional; defaults to `NULL`; a length one 21 | #' character vector. Specifies OpenAI organization. 22 | #' @return Returns a list, elements of which contain a transcription and 23 | #' supplementary information. 24 | #' @examples \dontrun{ 25 | #' voice_sample_ua <- system.file( 26 | #' "extdata", "sample-ua.m4a", package = "openai" 27 | #' ) 28 | #' create_translation(file = voice_sample_ua, model = "whisper-1") 29 | #' } 30 | #' @family audio functions 31 | #' @export 32 | create_translation <- function( 33 | file, 34 | model, 35 | prompt = NULL, 36 | response_format = "json", # json, text, srt, verbose_json, or vtt 37 | temperature = 0, 38 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 39 | openai_organization = NULL 40 | ) { 41 | 42 | response_format <- match.arg(response_format) 43 | 44 | #--------------------------------------------------------------------------- 45 | # Validate arguments 46 | 47 | allowed_extenssions <- c( 48 | "mp3", "mp4", "mpeg", "mpga", "m4a", "wav", "webm" 49 | ) 50 | assertthat::assert_that( 51 | assertthat::is.string(file), 52 | assertthat::noNA(file), 53 | file.exists(file), 54 | assertthat::is.readable(file), 55 | tools::file_ext(file) %in% allowed_extenssions 56 | ) 57 | 58 | assertthat::assert_that( 59 | assertthat::is.string(model), 60 | assertthat::noNA(model) 61 | ) 62 | 63 | if (!is.null(prompt)) { 64 | assertthat::assert_that( 65 | assertthat::is.string(prompt), 66 | assertthat::noNA(prompt) 67 | ) 68 | } 69 | 70 | assertthat::assert_that( 71 | assertthat::is.string(response_format), 72 | assertthat::noNA(response_format) 73 | ) 74 | 75 | assertthat::assert_that( 76 | assertthat::is.number(temperature), 77 | assertthat::noNA(temperature), 78 | value_between(temperature, 0, 2) 79 | ) 80 | 81 | assertthat::assert_that( 82 | assertthat::is.string(openai_api_key), 83 | assertthat::noNA(openai_api_key) 84 | ) 85 | 86 | if (!is.null(openai_organization)) { 87 | assertthat::assert_that( 88 | assertthat::is.string(openai_organization), 89 | assertthat::noNA(openai_organization) 90 | ) 91 | } 92 | 93 | #--------------------------------------------------------------------------- 94 | # Build path parameters 95 | 96 | task <- "audio/translations" 97 | 98 | base_url <- glue::glue("https://api.openai.com/v1/{task}") 99 | 100 | headers <- c( 101 | "Authorization" = paste("Bearer", openai_api_key), 102 | "Content-Type" = "multipart/form-data" 103 | ) 104 | 105 | if (!is.null(openai_organization)) { 106 | headers["OpenAI-Organization"] <- openai_organization 107 | } 108 | 109 | #--------------------------------------------------------------------------- 110 | # Build request body 111 | 112 | body <- list() 113 | body[["file"]] <- httr::upload_file(file) 114 | body[["model"]] <- model 115 | body[["prompt"]] <- prompt 116 | body[["response_format"]] <- response_format 117 | body[["temperature"]] <- temperature 118 | 119 | #--------------------------------------------------------------------------- 120 | # Make a request and parse it 121 | 122 | response <- httr::POST( 123 | url = base_url, 124 | httr::add_headers(.headers = headers), 125 | body = body, 126 | encode = "multipart" 127 | ) 128 | 129 | verify_mime_type(response) 130 | 131 | parsed <- response %>% 132 | httr::content(as = "text", encoding = "UTF-8") %>% 133 | jsonlite::fromJSON(flatten = TRUE) 134 | 135 | #--------------------------------------------------------------------------- 136 | # Check whether request failed and return parsed 137 | 138 | if (httr::http_error(response)) { 139 | paste0( 140 | "OpenAI API request failed [", 141 | httr::status_code(response), 142 | "]:\n\n", 143 | parsed$error$message 144 | ) %>% 145 | stop(call. = FALSE) 146 | } 147 | 148 | parsed 149 | 150 | } 151 | -------------------------------------------------------------------------------- /R/create_edit.R: -------------------------------------------------------------------------------- 1 | #' Create edit 2 | #' 3 | #' Creates an edit based on the provided input, instruction, and parameters. See 4 | #' [this page](https://platform.openai.com/docs/api-reference/edits/create) for 5 | #' details. 6 | #' 7 | #' For arguments description please refer to the [official 8 | #' documentation](https://platform.openai.com/docs/api-reference/edits/create). 9 | #' 10 | #' @param engine_id `r lifecycle::badge("deprecated")` 11 | #' @param model required; a length one character vector. 12 | #' @param input required; defaults to `'"'`; a length one character vector. 13 | #' @param instruction required; a length one character vector. 14 | #' @param temperature required; defaults to `1`; a length one numeric vector 15 | #' with the value between `0` and `2`. 16 | #' @param top_p required; defaults to `1`; a length one numeric vector with the 17 | #' value between `0` and `1`. 18 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 19 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 20 | #' character vector. Specifies OpenAI API key. 21 | #' @param openai_organization optional; defaults to `NULL`; a length one 22 | #' character vector. Specifies OpenAI organization. 23 | #' @return Returns a list, elements of which contain edited version of prompt 24 | #' and supplementary information. 25 | #' @examples \dontrun{ 26 | #' create_edit( 27 | #' model = "text-davinci-edit-001", 28 | #' input = "What day of the wek is it?", 29 | #' instruction = "Fix the spelling mistakes" 30 | #' ) 31 | #' } 32 | #' @export 33 | create_edit <- function( 34 | engine_id = deprecated(), 35 | model, 36 | input = '"', 37 | instruction, 38 | temperature = 1, 39 | top_p = 1, 40 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 41 | openai_organization = NULL 42 | ) { 43 | 44 | if (lifecycle::is_present(engine_id)) { 45 | lifecycle::deprecate_warn( 46 | "0.3.0", 47 | "create_completion(engine_id)", 48 | "create_completion(model)" 49 | ) 50 | model <- engine_id 51 | } 52 | 53 | #--------------------------------------------------------------------------- 54 | # Validate arguments 55 | 56 | assertthat::assert_that( 57 | assertthat::is.string(model), 58 | assertthat::noNA(model) 59 | ) 60 | 61 | assertthat::assert_that( 62 | assertthat::is.string(input), 63 | assertthat::noNA(input) 64 | ) 65 | 66 | assertthat::assert_that( 67 | assertthat::is.string(instruction), 68 | assertthat::noNA(instruction) 69 | ) 70 | 71 | assertthat::assert_that( 72 | assertthat::is.number(temperature), 73 | assertthat::noNA(temperature), 74 | value_between(temperature, 0, 2) 75 | ) 76 | 77 | assertthat::assert_that( 78 | assertthat::is.number(top_p), 79 | assertthat::noNA(top_p), 80 | value_between(top_p, 0, 1) 81 | ) 82 | 83 | if (both_specified(temperature, top_p)) { 84 | warning( 85 | "It is recommended NOT to specify temperature and top_p at a time." 86 | ) 87 | } 88 | 89 | assertthat::assert_that( 90 | assertthat::is.string(openai_api_key), 91 | assertthat::noNA(openai_api_key) 92 | ) 93 | 94 | if (!is.null(openai_organization)) { 95 | assertthat::assert_that( 96 | assertthat::is.string(openai_organization), 97 | assertthat::noNA(openai_organization) 98 | ) 99 | } 100 | 101 | #--------------------------------------------------------------------------- 102 | # Build path parameters 103 | 104 | task <- "edits" 105 | 106 | base_url <- glue::glue("https://api.openai.com/v1/{task}") 107 | 108 | headers <- c( 109 | "Authorization" = paste("Bearer", openai_api_key), 110 | "Content-Type" = "application/json" 111 | ) 112 | 113 | if (!is.null(openai_organization)) { 114 | headers["OpenAI-Organization"] <- openai_organization 115 | } 116 | 117 | #--------------------------------------------------------------------------- 118 | # Build request body 119 | 120 | body <- list() 121 | body[["model"]] <- model 122 | body[["input"]] <- input 123 | body[["instruction"]] <- instruction 124 | body[["temperature"]] <- temperature 125 | body[["top_p"]] <- top_p 126 | 127 | #--------------------------------------------------------------------------- 128 | # Make a request and parse it 129 | 130 | response <- httr::POST( 131 | url = base_url, 132 | httr::add_headers(.headers = headers), 133 | body = body, 134 | encode = "json" 135 | ) 136 | 137 | verify_mime_type(response) 138 | 139 | parsed <- response %>% 140 | httr::content(as = "text", encoding = "UTF-8") %>% 141 | jsonlite::fromJSON(flatten = TRUE) 142 | 143 | #--------------------------------------------------------------------------- 144 | # Check whether request failed and return parsed 145 | 146 | if (httr::http_error(response)) { 147 | paste0( 148 | "OpenAI API request failed [", 149 | httr::status_code(response), 150 | "]:\n\n", 151 | parsed$error$message 152 | ) %>% 153 | stop(call. = FALSE) 154 | } 155 | 156 | parsed 157 | 158 | } 159 | -------------------------------------------------------------------------------- /R/create_transcription.R: -------------------------------------------------------------------------------- 1 | #' Create transcription 2 | #' 3 | #' Transcribes audio into the input language. See 4 | #' [this page](https://platform.openai.com/docs/api-reference/audio/create) 5 | #' for details. 6 | #' 7 | #' For arguments description please refer to the [official 8 | #' documentation](https://platform.openai.com/docs/api-reference/audio/create). 9 | #' 10 | #' @param file required; a length one character vector. 11 | #' @param model required; a length one character vector. 12 | #' @param prompt optional; defaults to `NULL`; a length one character vector. 13 | #' @param response_format required; defaults to `"json"`; length one character 14 | #' vector equals to `"json"`. **Currently only `"json"` is implemented.** 15 | #' @param temperature required; defaults to `1`; a length one numeric vector 16 | #' with the value between `0` and `2`. 17 | #' @param language optional; defaults to `NULL`; a length one character vector. 18 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 19 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 20 | #' character vector. Specifies OpenAI API key. 21 | #' @param openai_organization optional; defaults to `NULL`; a length one 22 | #' character vector. Specifies OpenAI organization. 23 | #' @return Returns a list, elements of which contain a transcription and 24 | #' supplementary information. 25 | #' @examples \dontrun{ 26 | #' voice_sample_en <- system.file( 27 | #' "extdata", "sample-en.m4a", package = "openai" 28 | #' ) 29 | #' create_transcription(file = voice_sample_en, model = "whisper-1") 30 | #' } 31 | #' @family audio functions 32 | #' @export 33 | create_transcription <- function( 34 | file, 35 | model, 36 | prompt = NULL, 37 | response_format = "json", # json, text, srt, verbose_json, or vtt 38 | temperature = 0, 39 | language = NULL, 40 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 41 | openai_organization = NULL 42 | ) { 43 | 44 | response_format <- match.arg(response_format) 45 | 46 | #--------------------------------------------------------------------------- 47 | # Validate arguments 48 | 49 | allowed_extenssions <- c( 50 | "mp3", "mp4", "mpeg", "mpga", "m4a", "wav", "webm" 51 | ) 52 | assertthat::assert_that( 53 | assertthat::is.string(file), 54 | assertthat::noNA(file), 55 | file.exists(file), 56 | assertthat::is.readable(file), 57 | tools::file_ext(file) %in% allowed_extenssions 58 | ) 59 | 60 | assertthat::assert_that( 61 | assertthat::is.string(model), 62 | assertthat::noNA(model) 63 | ) 64 | 65 | if (!is.null(prompt)) { 66 | assertthat::assert_that( 67 | assertthat::is.string(prompt), 68 | assertthat::noNA(prompt) 69 | ) 70 | } 71 | 72 | assertthat::assert_that( 73 | assertthat::is.string(response_format), 74 | assertthat::noNA(response_format) 75 | ) 76 | 77 | assertthat::assert_that( 78 | assertthat::is.number(temperature), 79 | assertthat::noNA(temperature), 80 | value_between(temperature, 0, 2) 81 | ) 82 | 83 | if (!is.null(language)) { 84 | assertthat::assert_that( 85 | assertthat::is.string(language), 86 | assertthat::noNA(language) 87 | ) 88 | } 89 | 90 | assertthat::assert_that( 91 | assertthat::is.string(openai_api_key), 92 | assertthat::noNA(openai_api_key) 93 | ) 94 | 95 | if (!is.null(openai_organization)) { 96 | assertthat::assert_that( 97 | assertthat::is.string(openai_organization), 98 | assertthat::noNA(openai_organization) 99 | ) 100 | } 101 | 102 | #--------------------------------------------------------------------------- 103 | # Build path parameters 104 | 105 | task <- "audio/transcriptions" 106 | 107 | base_url <- glue::glue("https://api.openai.com/v1/{task}") 108 | 109 | headers <- c( 110 | "Authorization" = paste("Bearer", openai_api_key), 111 | "Content-Type" = "multipart/form-data" 112 | ) 113 | 114 | if (!is.null(openai_organization)) { 115 | headers["OpenAI-Organization"] <- openai_organization 116 | } 117 | 118 | #--------------------------------------------------------------------------- 119 | # Build request body 120 | 121 | body <- list() 122 | body[["file"]] <- httr::upload_file(file) 123 | body[["model"]] <- model 124 | body[["prompt"]] <- prompt 125 | body[["response_format"]] <- response_format 126 | body[["temperature"]] <- temperature 127 | body[["language"]] <- language 128 | 129 | #--------------------------------------------------------------------------- 130 | # Make a request and parse it 131 | 132 | response <- httr::POST( 133 | url = base_url, 134 | httr::add_headers(.headers = headers), 135 | body = body, 136 | encode = "multipart" 137 | ) 138 | 139 | verify_mime_type(response) 140 | 141 | parsed <- response %>% 142 | httr::content(as = "text", encoding = "UTF-8") %>% 143 | jsonlite::fromJSON(flatten = TRUE) 144 | 145 | #--------------------------------------------------------------------------- 146 | # Check whether request failed and return parsed 147 | 148 | if (httr::http_error(response)) { 149 | paste0( 150 | "OpenAI API request failed [", 151 | httr::status_code(response), 152 | "]:\n\n", 153 | parsed$error$message 154 | ) %>% 155 | stop(call. = FALSE) 156 | } 157 | 158 | parsed 159 | 160 | } 161 | -------------------------------------------------------------------------------- /R/create_image_edit.R: -------------------------------------------------------------------------------- 1 | #' Create image edit 2 | #' 3 | #' Creates an edited or extended image given an original image and a prompt. See 4 | #' [this page](https://platform.openai.com/docs/api-reference/images/create-edit) 5 | #' for details. 6 | #' 7 | #' For arguments description please refer to the [official 8 | #' documentation](https://platform.openai.com/docs/api-reference/images/create-edit). 9 | #' 10 | #' @param image required; a length one character vector. 11 | #' @param mask required; a length one character vector. 12 | #' @param prompt required; a length one character vector. 13 | #' @param n required; defaults to `1`; a length one numeric vector with the 14 | #' integer value greater than `0`. 15 | #' @param size required; defaults to `"1024x1024"`; a length one character 16 | #' vector, one among `"256x256"`, `"512x512"`, and `"1024x1024"`. 17 | #' @param response_format required; defaults to `"url"`; a length one character 18 | #' vector, one among `"url"` and `"b64_json"`. 19 | #' @param user optional; defaults to `NULL`; a length one character vector. 20 | #' @param openai_api_key required; defaults to `Sys.getenv("OPENAI_API_KEY")` 21 | #' (i.e., the value is retrieved from the `.Renviron` file); a length one 22 | #' character vector. Specifies OpenAI API key. 23 | #' @param openai_organization optional; defaults to `NULL`; a length one 24 | #' character vector. Specifies OpenAI organization. 25 | #' @return Returns a list, an element of which contain either a link to the 26 | #' edited image or the edited image decoded in Base64. 27 | #' @examples \dontrun{ 28 | #' image <- system.file("extdata", "astronaut.png", package = "openai") 29 | #' mask <- system.file("extdata", "mask.png", package = "openai") 30 | #' create_image_edit( 31 | #' image = image, 32 | #' mask = mask, 33 | #' prompt = "goat", 34 | #' n = 1, 35 | #' response_format = "url" 36 | #' ) 37 | #' } 38 | #' @family image functions 39 | #' @export 40 | create_image_edit <- function( 41 | image, 42 | mask, 43 | prompt, 44 | n = 1, 45 | size = c("1024x1024", "256x256", "512x512"), 46 | response_format = c("url", "b64_json"), 47 | user = NULL, 48 | openai_api_key = Sys.getenv("OPENAI_API_KEY"), 49 | openai_organization = NULL 50 | ) { 51 | 52 | size <- match.arg(size) 53 | response_format <- match.arg(response_format) 54 | 55 | #--------------------------------------------------------------------------- 56 | # Validate arguments 57 | 58 | assertthat::assert_that( 59 | assertthat::is.string(image), 60 | assertthat::noNA(image), 61 | file.exists(image), 62 | assertthat::is.readable(image) 63 | ) 64 | 65 | assertthat::assert_that( 66 | assertthat::is.string(mask), 67 | assertthat::noNA(mask), 68 | file.exists(mask), 69 | assertthat::is.readable(mask) 70 | ) 71 | 72 | assertthat::assert_that( 73 | assertthat::is.string(prompt), 74 | assertthat::noNA(prompt) 75 | ) 76 | 77 | assertthat::assert_that( 78 | assertthat::is.count(n) 79 | ) 80 | 81 | assertthat::assert_that( 82 | assertthat::is.string(size), 83 | assertthat::noNA(size) 84 | ) 85 | 86 | assertthat::assert_that( 87 | assertthat::is.string(response_format), 88 | assertthat::noNA(response_format) 89 | ) 90 | 91 | if (!is.null(user)) { 92 | assertthat::assert_that( 93 | assertthat::is.string(user), 94 | assertthat::noNA(user) 95 | ) 96 | } 97 | 98 | assertthat::assert_that( 99 | assertthat::is.string(openai_api_key), 100 | assertthat::noNA(openai_api_key) 101 | ) 102 | 103 | if (!is.null(openai_organization)) { 104 | assertthat::assert_that( 105 | assertthat::is.string(openai_organization), 106 | assertthat::noNA(openai_organization) 107 | ) 108 | } 109 | 110 | #--------------------------------------------------------------------------- 111 | # Build path parameters 112 | 113 | task <- "images/edits" 114 | 115 | base_url <- glue::glue("https://api.openai.com/v1/{task}") 116 | 117 | headers <- c( 118 | "Authorization" = paste("Bearer", openai_api_key), 119 | "Content-Type" = "multipart/form-data" 120 | ) 121 | 122 | if (!is.null(openai_organization)) { 123 | headers["OpenAI-Organization"] <- openai_organization 124 | } 125 | 126 | #--------------------------------------------------------------------------- 127 | # Build request body 128 | 129 | body <- list() 130 | body[["image"]] <- httr::upload_file(image) 131 | body[["mask"]] <- httr::upload_file(mask) 132 | body[["prompt"]] <- prompt 133 | body[["n"]] <- n 134 | body[["size"]] <- size 135 | body[["response_format"]] <- response_format 136 | body[["user"]] <- user 137 | 138 | #--------------------------------------------------------------------------- 139 | # Make a request and parse it 140 | 141 | response <- httr::POST( 142 | url = base_url, 143 | httr::add_headers(.headers = headers), 144 | body = body, 145 | encode = "multipart" 146 | ) 147 | 148 | verify_mime_type(response) 149 | 150 | parsed <- response %>% 151 | httr::content(as = "text", encoding = "UTF-8") %>% 152 | jsonlite::fromJSON(flatten = TRUE) 153 | 154 | #--------------------------------------------------------------------------- 155 | # Check whether request failed and return parsed 156 | 157 | if (httr::http_error(response)) { 158 | paste0( 159 | "OpenAI API request failed [", 160 | httr::status_code(response), 161 | "]:\n\n", 162 | parsed$error$message 163 | ) %>% 164 | stop(call. = FALSE) 165 | } 166 | 167 | parsed 168 | 169 | 170 | } 171 | --------------------------------------------------------------------------------