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()
.
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.
See also
Other chatbots:
chat_bedrock()
,
chat_claude()
,
chat_cortex()
,
chat_databricks()
,
chat_gemini()
,
chat_github()
,
chat_groq()
,
chat_ollama()
,
chat_perplexity()
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**: Tibbles have a more concise and modern print method that
#> shows only the first 10 rows and all columns that fit in the screen
#> width, providing additional information about the type of data in each
#> column.
#> - **Data Frame**: Traditional data frames print all columns and rows
#> (up to the maximum number set in R options), which can be overwhelming
#> for wide datasets.
#>
#> - **Subsetting**:
#> - **Tibble**: Tibbles are more strict when subsetting and return
#> another tibble when selecting a single column, while requiring explicit
#> use of `drop` for matrices.
#> - **Data Frame**: Data frames drop to the lowest possible dimension by
#> default when subsetting, which can lead to unexpected behavior (e.g.,
#> selecting a single column returns a vector, unless `drop = FALSE` is
#> specified).
#>
#> - **Column Data Types**:
#> - **Tibble**: Automatically preserves special types like lists or
#> matrices within tibble columns without simplification.
#> - **Data Frame**: May coerce certain data types when they are
#> compatible, simplifying the contents of a column.
#>
#> - **Non-Syntactic Column Names**:
#> - **Tibble**: Can handle non-syntactic column names, allowing spaces
#> and special characters, though accessing these may require backticks.
#> - **Data Frame**: Traditionally expects syntactic names and can have
#> issues with non-standard names.
#>
#> - **Package Origin and Philosophy**:
#> - **Tibble**: Part of the `tidyverse`, follows its philosophy of
#> consistent and predictable behavior with a focus on usability for data
#> analysis.
#> - **Data Frame**: Base R data type with behavior consistent with
#> traditional R practices.
#>
#> - **Performance**:
#> - **Tibble**: Tibbles often have optimized functions for faster
#> operations with large datasets due to their tidy design.
#> - **Data Frame**: May not have the same level of optimization for
#> certain operations compared to tibbles.
#>
#> - **Creation**:
#> - **Tibble**: Created using the `tibble()` function and is strict about
#> data types and column consistency.
#> - **Data Frame**: Created using the `data.frame()` function and may
#> alter data types for compatibility.
#>
#> Overall, tibbles are designed to be more user-friendly with modern
#> features, while traditional data frames follow older R conventions.
chat$chat("Tell me three funny jokes about statistcians")
#> Sure, here are three light-hearted jokes about statisticians:
#>
#> 1. **Probability Humor**:
#> Why don’t statisticians play hide and seek?
#> Because good luck hiding from someone who already knows your location
#> within a 95% confidence interval!
#>
#> 2. **Mean Friends**:
#> Why did the statistician break up with the calculator?
#> Because she couldn't handle his mean jokes and standard deviations!
#>
#> 3. **Pet Statistics**:
#> A statistician’s dog walks into a bar and says, "I’ll have a random
#> drink."
#> The bartender asks, "Can your dog even talk?"
#> The statistician replies, "Well, it's statistically significant!"
#>
#> Hope these bring a smile to your face!