Learn R Programming

OpenAIR

Integrate OpenAI's GPT models into your R workflows.

Installation

You can install the development version of TheOpenAIR from GitHub with:

# install.packages("devtools")
devtools::install_github("umatter/OpenAIR")

Usage

To use the package, you'll need to have an OpenAI API key. You can sign up for an API key here. Once you have your API key, you can start using TheOpenAIR:

# load the package
library("TheOpenAIR")

# register your api key
openai_api_key("YOUR-API-KEY")

Then, you can start chatting with the model. The simplest way of interacting with ChatGPT is the chat()-function. Using it is essentially the same as interacting with ChatGPT via the web GUI (https://chat.openai.com/chat).

# use chat() to interact with ChatGPT through the R console
chat("Write a 100 words essay on why TheOpenAIR, 
an R package to integrate GPT models into your R workflows is fantastic. 
Write it in the style of product launches moderated by Steve Jobs.")
Ladies and gentlemen, let me introduce you to TheOpenAIR, the latest and greatest addition to the R community. Thanks to TheOpenAIR, you can now integrate the cutting-edge GPT models right into your R workflows, unlocking a world of possibilities for predictive modeling and natural language processing. Whether you're an experienced data scientist or a newcomer to the field, TheOpenAIR makes it easier than ever to create powerful models that generate high-quality text. From business applications to academic research, TheOpenAIR has the power to transform the way we work with language data. So what are you waiting for? Join the excitement and embrace the future of data science with TheOpenAIR!
chat("Now shorten the essay to 30 words.")
TheOpenAIR - unleash the full power of GPT models in R workflows.

chat() is primarily useful for interactive mode. For writing short scripts and developing functions based on TheOpenAIR, the infix function %c% provides an alternative syntax to send messages to the model and indicate what output format is required.

# output to console
"Remove all numeric characters from '5XC-2a09ujnap9o2q0MP'" %c% "message_to_console"
The string without numeric characters would be 'XC-aujnapqMP'.
# return the message as a string
resp <- 
"Remove all numeric characters from '5XC-2a09ujnap9o2q0MP'
In your response, only provide what remains from the string" %c%
"message" 

resp
[1] "XC-aujnapoqMP"

Code of Conduct

For information about our code of conduct, please see the Code of Conduct page.

Copy Link

Version

Install

install.packages('TheOpenAIR')

Monthly Downloads

294

Version

0.1.0

License

MIT + file LICENSE

Maintainer

Ulrich Matter

Last Published

April 27th, 2023

Functions in TheOpenAIR (0.1.0)

extract_blocks_content

Extract the content parts of blocks
extract_entities

Extract Entities from a Text
created

Extract Created DateTime from OpenAI API response
edit_code

Edit code based on user's input
df_to_csv

Convert a data.frame to a CSV-formatted character string
contains_roxygen

Check if a text file or character string contains Roxygen2 documentation
count_tokens

Count the number of tokens in a text string
extract_blocks

Extract blocks of a specified type from a list of blocks
extract_entities_prompt

Input: message content template for extract_entities()
is_chatlog

Determine if an object is a chatlog
is_json

Check if the provided string is in valid JSON format.
extract_r_code

Extract R code and comments from a given input string.
extract_roxygen2

Extract roxygen2 documentation lines from a function definition
get_chatlog

Get the current chatlog
is_python

Check if a Character String Contains Valid Python Code
%c%

Send a message to ChatGPT and assign the response to a variable
is_r

Check if a character string contains valid R code
java_to_r

Convert Java code to R code
is_xml

Check if the provided string is in valid XML format.
parse_response

Parse a Response From the API
initialize_messages

Initialize a new messages object for OpenAI API chat completions
messages_content

Extract Messages Content from OpenAI API Response
object

Extract object information from OpenAI API response
model

Extract Model from OpenAI API response
install_tiktoken

Install the tiktoken Python package
nested_to_pipe_prompt

Input: message content template for nested_to_pipe()
refactor

Refactor R Code with AI Assistance
messages

Extract messages from a response object or a chatlog object
nested_to_pipe

Convert nested R code to pipe syntax
refactor_prompt

Input: message content template for refactor()
python_to_r

Convert Python code to R code
python_to_r_prompt

Input: message content template for python_to_r()
java_to_r_prompt

Input: message content template for java_to_r()
%>%

Pipe operator
openai_api_key

Set OpenAI API Key as an Environment Variable
replace_file_extension

Replace File Extension
regenerate

Regenerate the last response in an ongoing chat
id

Extract ID from OpenAI API response
start_chat

Start a new chat session
num_tokens

Get number of tokens in a string using OpenAI's tiktoken library
trump

Essay on How Donald Trump Became President of the United States
r_to_python

Convert R Code to Python Code
r_to_python_prompt

Input: message content template for r_to_python()
num_tokens_file

Compute total number of tokens in a text file
references_to_bibtex

Convert references in plain text to BibTeX format
write_test_prompt

Input: message content template for write_test()
write_test

Write test for an R function
references_to_bibtex_prompt

Input: message content template for references_to_bibtex()
set_chatlog

Set up a new chatlog
read_text_batches

Load data from text files in batches
usage

Extract usage data from OpenAI API response
split_text

Split Text into Chunks
read_text

Convert Text to Tidy-Text Format
write_code

Write code based on user's input
clean_output

Clean Output From Code Responses
TheOpenAIR-package

TheOpenAIR: Integrate 'OpenAI' Large Language Models into Your 'R' Workflows
chat_completion

Generate Text Using the OpenAI API's Chat Endpoint
add_roxygen

Add Roxygen2 documentation to an R function
chatlog-class

chatlog class
contains_r_func

Check if a text file or character string contains an R function definition
clear_chatlog

Clear a chat log
chat

Start or continue a chat conversation
add_to_chatlog

Add data to a chat log
add_roxygen_prompt

Input: message content template for add_roxygen()