Learn R Programming

lares (version 5.2.2)

gpt_ask: ChatGPT API Interaction with R

Description

This function lets the user ask ChatGPT via its API, and returns the rendered reply. There are a couple of specific verbs (functions) with a preset prompt to help fetch the data in specific formats. We also store the prompts and replies in current session with their respective time-stamps so user can gather historical results.

Usage

gpt_ask(
  ask,
  secret_key = get_credentials()$openai$secret_key,
  url = Sys.getenv("LARES_GPT_URL"),
  model = Sys.getenv("LARES_GPT_MODEL"),
  quiet = FALSE,
  ...
)

gpt_history()

gpt_classify(x, categories, quiet = TRUE, ...)

gpt_tag(x, tags, quiet = TRUE, ...)

gpt_extract(x, extract, quiet = TRUE, ...)

gpt_format(x, format, quiet = TRUE, ...)

gpt_convert(x, unit, quiet = TRUE, ...)

gpt_table(ask, quiet = TRUE, ...)

gpt_translate(x, language, quiet = TRUE, ...)

Value

(Invisible) list. Content returned from API POST and processed.

Arguments

ask

Character. Redacted prompt to ask ChatGPT. If multiple asks are requested, they will be concatenated with "+" into a single request.

secret_key

Character. Secret Key. Get yours in: platform.openai.com.

url

Character. Base URL for OpenAI's ChatGPT API.

model

Character. OpenAI model to use.

quiet

Boolean. Keep quiet? If not, show informative messages.

...

Additional parameters.

x

Vector. List items you wish to process

categories, tags

Vector. List of possible categories/tags to consider.

extract, format, unit

Character. Length 1 or same as x to extract/format/unit information from x. For example: email, country of phone number, country, amount as number, currency ISO code, ISO, Fahrenheit, etc.

language

Character. Language to translate to

See Also

Other API: bring_api(), fb_accounts(), fb_ads(), fb_creatives(), fb_insights(), fb_process(), fb_report_check(), fb_rf(), fb_token(), li_auth(), li_profile(), queryGA(), slackSend()

Examples

Run this code
if (FALSE) {
api_key <- get_credentials()$openai$secret_key
# Open question:
gpt_ask("Can you write an R function to plot a dummy histogram?", api_key)

##### The following examples return dataframes:
# Classify each element based on categories:
gpt_classify(1:10, c("odd", "even"))

# Add all tags that apply to each element based on tags:
gpt_tag(
  c("I love chocolate", "I hate chocolate", "I like Coke"),
  c("food", "positive", "negative", "beverage")
)

# Extract specific information:
gpt_extract(
  c("My mail is 123@test.com", "30 Main Street, Brooklyn, NY, USA", "+82 2-312-3456", "$1.5M"),
  c("email", "full state name", "country of phone number", "amount as number")
)

# Format values
gpt_format(
  c("March 27th, 2021", "12-25-2023 3:45PM", "01.01.2000", "29 Feb 92"),
  format = "ISO Date getting rid of timestamps"
)

# Convert units
gpt_convert(c("50C", "300K"), "Fahrenheit")

# Create a table with data
gpt_table("5 random people's address in South America, email, phone, age between 18-30")

# Translate text to any language
gpt_translate(
  rep("I love you with all my heart", 5),
  language = c("spanish", "chinese", "japanese", "russian", "german")
)

# Now let's read the historical prompts and replies from current session
gpt_history()
}

Run the code above in your browser using DataLab