Send a custom prompt to the LLM

R/llm-custom.R

llm_custom

Description

Use a Large Language Model (LLM) to process the provided text using the instructions from prompt

Usage

llm_custom(.data, col, prompt = "", pred_name = ".pred", valid_resps = "")

llm_vec_custom(x, prompt = "", valid_resps = NULL, preview = FALSE)

Arguments

Arguments Description
.data A data.frame or tbl object that contains the text to be analyzed
col The name of the field to analyze, supports tidy-eval
prompt The prompt to append to each record sent to the LLM
pred_name A character vector with the name of the new column where the prediction will be placed
valid_resps If the response from the LLM is not open, but deterministic, provide the options in a vector. This function will set to NA any response not in the options
x A vector that contains the text to be analyzed
preview It returns the R call that would have been used to run the prediction. It only returns the first record in x. Defaults to FALSE Applies to vector function only.

Value

llm_custom returns a data.frame or tbl object. llm_vec_custom returns a vector that is the same length as x.

Examples

library(mall)

data("reviews")

llm_use("ollama", "llama3.2", seed = 100, .silent = TRUE)

my_prompt <- paste(
  "Answer a question.",
  "Return only the answer, no explanation",
  "Acceptable answers are 'yes', 'no'",
  "Answer this about the following text, is this a happy customer?:"
)

reviews |>
  llm_custom(review, my_prompt)
#> # A tibble: 3 × 2
#>   review                                                                   .pred
#>   <chr>                                                                    <chr>
#> 1 This has been the best TV I've ever used. Great screen, and sound.       Yes  
#> 2 I regret buying this laptop. It is too slow and the keyboard is too noi… No   
#> 3 Not sure how to feel about my new washing machine. Great color, but har… No

# For character vectors, instead of a data frame, use this function
llm_vec_custom(reviews$review, my_prompt)
#> [1] "Yes" "No"  "No"

# To preview the first call that will be made to the downstream R function
llm_vec_custom(reviews$review, my_prompt, preview = TRUE)
#> [[1]]
#> ollamar::chat(messages = list(list(role = "user", content = "Answer a question. Return only the answer, no explanation Acceptable answers are 'yes', 'no' Answer this about the following text, is this a happy customer?:  The answer is based on the following text: This has been the best TV I've ever used. Great screen, and sound.")), 
#>     output = "text", model = "llama3.2", seed = 100)