Skip to content

OpenAI provides a number of chat-based models, mostly under the ChatGPT brand. Note that a ChatGPT Plus membership does not grant access to the API. You will need to sign up for a developer account (and pay for it) at the developer platform.

For authentication, we recommend saving your API key to the OPENAI_API_KEY environment variable in your .Renviron file. You can easily edit this file by calling usethis::edit_r_environ().

Usage

chat_openai(
  system_prompt = NULL,
  turns = NULL,
  base_url = "https://api.openai.com/v1",
  api_key = openai_key(),
  model = NULL,
  seed = NULL,
  api_args = list(),
  echo = c("none", "text", "all")
)

Arguments

system_prompt

A system prompt to set the behavior of the assistant.

turns

A list of Turns to start the chat with (i.e., continuing a previous conversation). If not provided, the conversation begins from scratch.

base_url

The base URL to the endpoint; the default uses OpenAI.

api_key

The API key to use for authentication. You generally should not supply this directly, but instead set the OPENAI_API_KEY environment variable.

model

The model to use for the chat. The default, NULL, will pick a reasonable default, and tell you about. We strongly recommend explicitly choosing a model for all but the most casual use.

seed

Optional integer seed that ChatGPT uses to try and make output more reproducible.

api_args

Named list of arbitrary extra arguments appended to the body of every chat API call.

echo

One of the following options:

  • none: don't emit any output (default when running in a function).

  • text: echo text output as it streams in (default when running at the console).

  • all: echo all input and output.

Note this only affects the chat() method.

Value

A Chat object.

Examples

chat <- chat_openai()
#> Using model = "gpt-4o".
chat$chat("
  What is the difference between a tibble and a data frame?
  Answer with a bulleted list
")
#> - **Simplicity and Structure:**
#>   - *Data Frame:* A traditional data structure in R, which is 
#> essentially a list of vectors of equal length. Each vector can have 
#> its own data type.
#>   - *Tibble:* A modern reimagining of data frames that comes from the 
#> `tibble` package, part of the tidyverse. It offers an improved and 
#> more user-friendly version of data frames.
#> 
#> - **Printing Behavior:**
#>   - *Data Frame:* By default, it prints all rows and columns, which 
#> can be overwhelming for large datasets.
#>   - *Tibble:* Prints a preview of the data with the first ten rows and
#> as many columns that fit on the screen, making it more manageable and 
#> readable for large datasets.
#> 
#> - **Column Name Handling:**
#>   - *Data Frame:* Allows non-syntactic names, but accessing such names
#> can be cumbersome (e.g., using backticks).
#>   - *Tibble:* More strict with column names; they can be 
#> non-syntactic, but they emphasize consistency in name handling.
#> 
#> - **Data Type Preservation:**
#>   - *Data Frame:* Can perform some automatic type conversions that 
#> might not be desired by the user.
#>   - *Tibble:* Does not change data input types and preserves them as 
#> is, reducing unexpected conversions.
#> 
#> - **Subsetting:**
#>   - *Data Frame:* Can sometimes return a vector instead of a data 
#> frame when subsetting with a single column.
#>   - *Tibble:* Always returns a tibble when subsetting, even with a 
#> single column, maintaining consistency in the data structure.
#> 
#> - **Package Integration:**
#>   - *Data Frame:* Basic data type supported in base R.
#>   - *Tibble:* Part of the tidyverse, allowing seamless integration and
#> manipulation with other tidyverse packages, which is beneficial for 
#> workflows centered around tidy data principles.
#> 
#> - **Development Origin:**
#>   - *Data Frame:* Native to base R since its early versions.
#>   - *Tibble:* Developed to mimic and improve upon data frames, 
#> providing a more tidyverse-aligned approach.

chat$chat("Tell me three funny jokes about statistcians")
#> Sure, here are three light-hearted jokes about statisticians:
#> 
#> 1. **Sampling at the Beach:**
#>    - Why do statisticians love going to the beach?
#>    - Because they’re always excited to find a good sample!
#> 
#> 2. **Life of the Party:**
#>    - How can you spot an extroverted statistician at a party?
#>    - They’re the one looking at your shoes instead of their own!
#> 
#> 3. **Dating Dilemma:**
#>    - Why did the statistician break up with the data analyst?
#>    - Because they found them to be a mean lover, even though they were
#> significant!
#> 
#> I hope these gave you a chuckle!