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
")
#> - **Printing Behavior**:  
#>   - Tibbles: Have an enhanced printing behavior. They show only the 
#> first 10 rows and all columns that fit on the screen, making them more
#> user-friendly when working with large datasets.
#>   - Data Frames: Display all rows and columns by default, which can be
#> overwhelming for large datasets.
#> 
#> - **Column Data Types**:  
#>   - Tibbles: Can preserve list-columns and support non-standard column
#> names without issue.
#>   - Data Frames: May not support list-columns as smoothly, and 
#> non-standard names (e.g., names with spaces) can be problematic 
#> without additional handling.
#> 
#> - **Performance**:  
#>   - Tibbles: Introduced as part of the `tibble` package, they are 
#> optimized for performance within the tidyverse suite of packages.
#>   - Data Frames: Base R data structure for tables, may not have the 
#> same performance optimizations for certain tidyverse operations.
#> 
#> - **Subsetting Behavior**:  
#>   - Tibbles: Always return another tibble, making them predictable 
#> when extracting subsets of data.
#>   - Data Frames: May sometimes return simplified data structures 
#> (e.g., vectors) depending on how data is subsetted.
#> 
#> - **Conversion and Compatibility**:  
#>   - Tibbles: Are built on top of data frames and are fully compatible 
#> with base R functions that operate on data frames, allowing smooth 
#> transition and integration.
#>   - Data Frames: Are the standard R data structure and are fully 
#> compatible with base R and classical methods.
#> 
#> - **Default Behavior**:  
#>   - Tibbles: Have stricter rules on data conversion and will not 
#> automatically change data types, preventing some common data errors.
#>   - Data Frames: Tend to convert strings to factors by default, 
#> although this behavior can be controlled with settings.
#> 
#> Overall, while both structures serve similar purposes, tibbles are 
#> typically preferred in the tidyverse context due to their enhanced 
#> features and better user experience.

chat$chat("Tell me three funny jokes about statistcians")
#> Certainly! Here are three light-hearted jokes about statisticians:
#> 
#> 1. **Normal Walks into a Bar**:  
#>    A statistician walks into a bar and orders a drink. The bartender 
#> asks, "What's your poison?" The statistician replies, "I'm here to 
#> keep things normal!"
#> 
#> 2. **Gaussian Band**:  
#>    Why don’t statisticians ever play hide and seek?  
#>    Because good luck hiding from someone who always assumes you’re 
#> around the mean!
#> 
#> 3. **Mid-Life Crisis**:  
#>    How does a statistician deal with a mid-life crisis?  
#>    They recalibrate their life expectations, center them around the 
#> median, and eliminate a few outliers!
#> 
#> I hope these bring a smile to your face!