Overview
Analyze text variables with a large language model, OpenAI’s ChatGPT (https://platform.openai.com/docs/api-reference).
There are already r packages for other uses of the GPT models. If you want a package that minimally wraps the openai api, check out the openai package. If you want to chat with GPT via the R console, for example to assist you with programming tasks, check out the OpenAIR package. If you have many snippets of text that you want to code with the same instructions and get the responses in a tidy format, then gptworkr is your friend.
Installation
You can install the development version of gptworkr like so:
# If using devtools:
# install.packages("devtools")
devtools::install_git("https://codeberg.org/hocolim/gptworkr/")
# If using pak:
# install.packages("pak")
pak::pkg_install("git::https://codeberg.org/hocolim/gptworkr/")Usage
First load the package and set the OpenAI API key.
library(gptworkr)
set_api_key() # this will open a prompt. paste your openai api key into the prompt.You can instruct GPT to follow the same instructions on all the strings in a character column. Here, I use it on the wines dataset to extract all adjectives used to describe some wines.
adjectives <- gpt(
data = wines$description
instruction = "You are a helpful assistant. You'll be provided with a description of a wine. Please return all adjectives used to describe that wine as a comma separated list."
)You can also force GPT to choose among some predetermined classes with gpt_classify(). Here, I force gpt to classify whether the wines were made in Narnia, Atlantis or Middle Earth.
country <- gpt_classify(
data = wines$description,
instruction = "You are a helpful assistant. Where does the wine come from?",
classes = c("Narnia", "Atlantis", "Middle Earth")
)Get more help in vignette("Tutorial").
Citation
Please cite me if you use the package. Use the citation:
#> To cite package 'gptworkr' in publications use:
#>
#> Stovner, B. R (2024). "gptworkr: Analyze Text Data with ChatGPT."
#> doi:10.5281/zenodo.10551772
#> <https://doi.org/10.5281/zenodo.10551772>.
#>
#> A BibTeX entry for LaTeX users is
#>
#> @Misc{,
#> title = {gptworkr: Analyze Text Data with ChatGPT},
#> author = {{Stovner} and Roar B.},
#> year = {2024},
#> doi = {10.5281/zenodo.10551772},
#> version = {0.1.0},
#> }