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:**
#>   - **Tibble:** Prints only the first 10 rows and as many columns as 
#> fit on the screen. This makes it more readable and prevents the 
#> console from being overwhelmed with data.
#>   - **Data Frame:** Prints all the data, which can be overwhelming for
#> large datasets.
#> 
#> - **Column Types:**
#>   - **Tibble:** Preserves data types and provides better support for 
#> list-columns. Column types are printed along with the data.
#>   - **Data Frame:** Attempts to simplify data types, which can lead to
#> unexpected behavior (e.g., converting strings to factors by default in
#> base R).
#> 
#> - **Subsetting:**
#>   - **Tibble:** Subsetting with `[` always returns a tibble, and it 
#> returns NULL if a non-existent column is accessed.
#>   - **Data Frame:** Subsetting with `[` may return a vector if a 
#> single column is selected. Accessing a non-existent column produces an
#> error.
#> 
#> - **Package:**
#>   - **Tibble:** Introduced with the `tibble` package in the Tidyverse,
#> specifically designed for modern R data science workflows.
#>   - **Data Frame:** Base R construct, part of R's core data 
#> structures.
#> 
#> - **Row Names:**
#>   - **Tibble:** Do not display row names and discourage their use to 
#> avoid bugs and confusion.
#>   - **Data Frame:** Allow for row names, which can sometimes be useful
#> but also lead to unintended complexities.
#> 
#> - **Performance:**
#>   - **Tibble:** Typically faster for view-related operations due to 
#> optimized printing and other methods.
#>   - **Data Frame:** May be slower in displaying large datasets since 
#> every element is printed.
#> 
#> These differences highlight that tibbles are designed to improve upon 
#> the traditional data frame by making data manipulation more intuitive 
#> and reducing common pitfalls in data analysis.

chat$chat("Tell me three funny jokes about statistcians")
#> Sure, here are three light-hearted jokes about statisticians:
#> 
#> 1. Why don't statisticians play hide and seek?
#>    - Because good luck hiding from someone who can always find the 
#> mean!
#> 
#> 2. A statistician’s wife had twins. He was delighted. He rang the 
#> minister who was also delighted.
#>    - "Bring them to church on Sunday and we’ll baptize them," said the
#> minister.
#>    - "No," replied the statistician. "Baptize one and we’ll keep the 
#> other as a control."
#> 
#> 3. How do statisticians stay cool in a crisis?
#>    - They always keep their standard deviation low!