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
")
#> - **Class and Structure**:
#> - A tibble is a modern version of the data frame, introduced in the
#> `tibble` package, which is part of the tidyverse collection in R.
#> - A data frame is the base R structure for storing tabular data.
#>
#> - **Printing Behavior**:
#> - Tibbles have a refined print method displaying only the first 10 rows
#> and the columns that fit on screen, providing a cleaner output.
#> - Data frames print the whole dataset unless specifically instructed
#> otherwise, which can be unwieldy for large datasets.
#>
#> - **Column Types**:
#> - Tibbles do not change the types of the input data: they respect
#> column types and do not automatically convert strings to factors.
#> - Data frames may automatically convert character vectors to factors
#> unless stringsAsFactors is set to FALSE.
#>
#> - **Subsetting**:
#> - Subsetting a tibble with a single bracket (`[...]`) always returns a
#> tibble, preserving the data frame structure.
#> - With data frames, subsetting can return a vector if selecting a
#> single column without the `drop = FALSE` argument.
#>
#> - **Name Validation**:
#> - Tibbles do not automatically rename or change invalid column names
#> (e.g., names with spaces). They allow non-standard names by using
#> backticks.
#> - Data frames typically force names to adhere to valid R variable
#> names, converting them as necessary.
#>
#> - **Use within Tidyverse**:
#> - Tibbles are integral to the tidyverse and are designed to work
#> seamlessly with other tidy tools for data manipulation and visualization.
#> - Data frames are the default R structure and are not as tightly
#> integrated into the tidyverse toolkit.
#>
#> - **Error and Warning Messages**:
#> - Tibbles provide more user-friendly error and warning messages
#> compared to the more generic messages returned by operations on data
#> frames.
#>
#> These differences make tibbles particularly useful when working within
#> the tidyverse ecosystem, offering more intuitive and robust data handling
#> for modern data science workflows.
chat$chat("Tell me three funny jokes about statistcians")
#> Sure, here are three jokes about statisticians that might give you a
#> chuckle:
#>
#> 1. Why don’t statisticians play hide and seek?
#> - Because good luck hiding from someone who always checks the outliers
#> first!
#>
#> 2. How many statisticians does it take to change a light bulb?
#> - Just one, but they’ll need to test 20 different light bulbs to
#> determine the mean change time!
#>
#> 3. Two statisticians walk into a bar.
#> - The third one ducks.
#>
#> I hope these bring a smile to your face!