Learn R Programming

⚠️There's a newer version (0.4.1) of this package.Take me there.

openai

Overview

{openai} is an R wrapper of OpenAI API endpoints. This package covers Engines, Completions, Edits, Files, Fine-tunes, Embeddings and legacy Searches, Classifications, and Answers endpoints (will be removed on December 3, 2022).

Installation

The easiest way to install {openai} from CRAN is to use the “official” install.packages() function:

install.packages("openai")

You can also install the development version of {openai} from GitHub with:

if (!require(remotes))
    install.packages("remotes")
remotes::install_github("irudnyts/openai")

Authentication

To use the OpenAI API, you need to provide an API key. First, sign up for OpenAI API on this page. Once you signed up and logged in, you need to open this page, click on Personal, and select View API keys in drop-down menu. You can then copy the key by clicking on the green text Copy.

By default, functions of {openai} will look for SPOTIFY_CLIENT_ID environment variable. If you want to set a global environment variable, you can use the following command (where xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx should be replaced with your actual key):

Sys.setenv(
    SPOTIFY_CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
)

Otherwise, you can add the key to the .Renviron file of the project. The following commands will open .Renviron for editing:

if (!require(usethis))
    install.packages("usethis")

usethis::edit_r_environ(scope = "project")

You can add the following line to the file (again, replace xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx with your actual key):

OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Note: If you are using GitHub/Gitlab, do not forget to add .Renviron to .gitignore!

Finally, you can always provide the key manually to the functions of the package.

Example

Functions of {openai} have self-explanatory names. For example, to create a completion, one can use create_completion() function:

library(openai)

create_completion(
    engine_id = "ada",
    prompt = "Generate a question and an answer"
)
#> $id
#> [1] "cmpl-6JlmRJ6SD4vVTrIy5CTWIkQy4cUWm"
#> 
#> $object
#> [1] "text_completion"
#> 
#> $created
#> [1] 1670169919
#> 
#> $model
#> [1] "ada"
#> 
#> $choices
#>                                                text index logprobs
#> 1  dialog\n\nStatus\n\n$filters = filter('thrid = #     0       NA
#>   finish_reason
#> 1        length
#> 
#> $usage
#> $usage$prompt_tokens
#> [1] 7
#> 
#> $usage$completion_tokens
#> [1] 16
#> 
#> $usage$total_tokens
#> [1] 23

Further, one can generate an image using DALL·E text-to-image model with create_image():

create_image("An astronaut riding a horse in a photorealistic style")

Copy Link

Version

Install

install.packages('openai')

Monthly Downloads

1,929

Version

0.2.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Iegor Rudnytskyi

Last Published

December 4th, 2022

Functions in openai (0.2.0)

delete_file

Delete file
%>%

Pipe operator
list_files

List files
create_moderation

Create moderation
list_fine_tunes

Lists fine-tunes
list_models

List models
list_fine_tune_events

List fine-tune events
list_engines

List engines
create_search

Create search
delete_fine_tune_model

Delete fine_tune model
retrieve_engine

Retrieve engine
retrieve_model

Retrieve model
retrieve_file_content

Retrieve file content
upload_file

Upload file
retrieve_fine_tune

Retrieve fine-tune
retrieve_file

Retrieve file
create_image_variation

Create image variation
create_image_edit

Create image edit
create_completion

Create completion
create_edit

Create edit
cancel_fine_tune

Cancel fine-tune
create_answer

Create answer
create_fine_tune

Create fine-tune
create_embedding

Create embeddings
create_classification

Create classification
create_image

Create image