Skip to contents

llm_generate

Usage

# S3 method for class 'character'
llm_generate(
  source,
  prompt = "",
  model = "gpt-3.5-turbo",
  model_provider = NA,
  repair = FALSE,
  progress = TRUE,
  temperature = 1,
  top_p = NULL,
  top_k = NULL,
  anthropic_version = "2023-06-01",
  presence_penalty = 0,
  frequency_penalty = 0,
  max_tokens = 4096,
  max_completion_tokens = NULL,
  logit_bias = NULL,
  logprobs = FALSE,
  top_logprobs = NULL,
  seed = NULL,
  stop = NULL,
  user = NULL,
  is_reasoning_model = NULL,
  reasoning_effort = NULL,
  openai_api_key = Sys.getenv("OPENAI_API_KEY"),
  openai_organization = NULL,
  anthropic_api_key = Sys.getenv("ANTHROPIC_API_KEY"),
  google_api_key = Sys.getenv("GOOGLE_API_KEY"),
  parentInfo = NULL
)

Arguments

source

required; A source dataframe or llm-completion object.

prompt

required; A string (Or vector of Strings for handling multiple operations at the same time) of prompts to be sent to the AI model.

model

required; a character vector.

repair

optional; A boolean to repair NA's in the output column and keep values already present in the output column if the output column has already been created. False overrides the data already in an output column if it exists. Useful to continue a computation if you have been rate limited. Defaults to FALSE.

progress

optional; a length one logical vector. Defaults to TRUE. Determines whether to show a progress bar in the console. Not available when using repair mode.

temperature

optional; defaults to 1; a length one numeric vector with the value between 0 (More analytical) and 2 (More creative). 0-2 for OpenAI and Google models, 0-1 for Anthropic models.

top_p

optional; defaults to NULL; a numeric vector with the value between 0 and 1.

top_k

optional; a numeric vector with the integer value greater than 0. Only sample from the top_k options for each subsequent token. Not recommended, for most use cases use temperature instead.

anthropic_version

required; defaults to 2023-06-01; a length one character vector. Specifies the version of the Anthropic's models.

presence_penalty

optional; defaults to 0; a numeric vector with a value between -2 and 2.

frequency_penalty

optional; defaults to 0; a numeric vector with a value between -2 and 2.

max_tokens

optional; defaults to (4096 - prompt tokens); a numeric vector with the integer value greater than 0.

max_completion_tokens

optional; defaults to NULL; used in OpenAI reasoning models; a numeric vector with the integer value greater than 0. Specifies the maximum number of tokens to generate for each completion. This includes tokens generated as part of the reasoning process and are not returned to the user.

logit_bias

optional; defaults to NULL; a JSON object that maps tokens (as specified by their toekn ID in the tokenizer) to an associated bias value. -100 to 100.

logprobs

optional; defaults to FALSE. If TRUE, the API will return log probabilities for each token.

top_logprobs

optional; An integer between 0 and 20. Specifies the number of most likely tokens to return at each token position. The API will return log probabilities for the top top_logprobs tokens at each position. The logprobs must be set to TRUE to use this parameter.

seed

optional; defaults to NULL. An integer that allows for reproducible results when using the same seed. (BETA)

stop

optional; Defaults to NULL. A vector of strings (Up to length 4) of sequences where the API will stop generating further tokens.

user

optional; defaults to NULL. A string that specifies the user ID to associate with the completion.

is_reasoning_model

optional; defaults to NULL. A vector of booleans that specifies if the model is a reasoning model. If TRUE, the model will be treated as a reasoning model. Reasoning models use different model parameters and are optimized for reasoning tasks. OpenAI only.

reasoning_effort

optional; defauls to medium. A vector of strings that specifies the effort level for the reasoning model. OpenAI only. Must be one of c(low, medium, high).

openai_api_key

optional; defaults to Sys.getenv("OPENAI_API_KEY") (i.e., the value is retrieved from the .Renviron file); a length one character vector. Specifies OpenAI API key. Must obtain API Key from OpenAI.

openai_organization

optional; defaults to NULL; a length one character vector. Specifies OpenAI organization.

anthropic_api_key

optional; defaults to Sys.getenv("ANTHROPIC_API_KEY") (i.e., the value is retrieved from the .Renviron file); a length one character vector. Specifies Anthropic API key.

google_api_key

optional; defaults to Sys.getenv("GOOGLE_API_KEY") (i.e., the value is retrieved from the .Renviron file); a length one character vector. Specifies Google API key. Must obtain API Key from Google.

input

required; A column name in the source dataframe

output

optional; A string of a column name (Or a vector of strings) to be created in the source dataframe storing the output of the models. Defaults to output.

return_invisible

optional; A boolean to return just the output (TRUE) or an llm-completion object containing model metadata (FALSE). Defaults to FALSE.

iterations

optional; An integer. Number of completions to generate for each prompt Defaults to 1.

Value

A dataframe with the output column(s) created