Skip to content

Commit

Permalink
Adds examples to classify
Browse files Browse the repository at this point in the history
  • Loading branch information
edgararuiz committed Sep 22, 2024
1 parent b7fb571 commit 7730724
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 2 deletions.
39 changes: 39 additions & 0 deletions R/llm-classify.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,45 @@
#' Applies to vector function only.
#' @returns `llm_classify` returns a `data.frame` or `tbl` object.
#' `llm_vec_classify` returns a vector that is the same length as `x`.
#' @examples
#' \dontrun{
#' library(mall)
#'
#' llm_use("ollama", "llama3.1", seed = 100, .silent = TRUE)
#'
#' reviews <- data.frame(review = c(
#' "This has been the best TV I've ever used. Great screen, and sound.",
#' "I regret buying this laptop. It is too slow and the keyboard is too noisy",
#' "Not sure how to feel about my new washing machine. Great color, but hard to figure"
#' ))
#'
#' llm_classify(reviews, review, c("appliance", "computer"))
#'
#' # Use 'pred_name' to customize the new column's name
#' llm_classify(
#' reviews,
#' review,
#' c("appliance", "computer"),
#' pred_name = "prod_type"
#' )
#'
#' # Pass custom values for each classification
#' llm_classify(reviews, review, c("appliance" ~ 1, "computer" ~ 2))
#'
#' # For character vectors, instead of a data frame, use this function
#' llm_vec_classify(
#' c("this is important!", "just whenever"),
#' c("urgent", "not urgent")
#' )
#'
#' #' # For character vectors, instead of a data frame, use this function
#' llm_vec_classify(
#' c("this is important!", "just whenever"),
#' c("urgent", "not urgent"),
#' preview = TRUE
#' )
#'
#' }
#' @export
llm_classify <- function(.data,
col,
Expand Down
4 changes: 2 additions & 2 deletions _freeze/reference/llm_classify/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"hash": "0cd7aa0f5859119d46e18cab84883e84",
"hash": "9989e0fa332b64d7df96afcee5b53cf0",
"result": {
"engine": "knitr",
"markdown": "---\ntitle: \"Categorize data as one of options given\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n[R/llm-classify.R](https://github.com/edgararuiz/mall/blob/main/R/llm-classify.R)\n\n## llm_classify\n\n## Description\n Use a Large Language Model (LLM) to classify the provided text as one of the options provided via the `labels` argument. \n\n\n## Usage\n```r\n \nllm_classify( \n .data, \n col, \n labels, \n pred_name = \".classify\", \n additional_prompt = \"\" \n) \n \nllm_vec_classify(x, labels, additional_prompt = \"\", preview = FALSE) \n```\n\n## Arguments\n|Arguments|Description|\n|---|---|\n| .data | A `data.frame` or `tbl` object that contains the text to be analyzed |\n| col | The name of the field to analyze, supports `tidy-eval` |\n| labels | A character vector with at least 2 labels to classify the text as |\n| pred_name | A character vector with the name of the new column where the prediction will be placed |\n| additional_prompt | Inserts this text into the prompt sent to the LLM |\n| x | A vector that contains the text to be analyzed |\n| 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. |\n\n\n\n## Value\n `llm_classify` returns a `data.frame` or `tbl` object. `llm_vec_classify` returns a vector that is the same length as `x`. \n\n\n\n\n",
"markdown": "---\ntitle: \"Categorize data as one of options given\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n[R/llm-classify.R](https://github.com/edgararuiz/mall/blob/main/R/llm-classify.R)\n\n## llm_classify\n\n## Description\n Use a Large Language Model (LLM) to classify the provided text as one of the options provided via the `labels` argument. \n\n\n## Usage\n```r\n \nllm_classify( \n .data, \n col, \n labels, \n pred_name = \".classify\", \n additional_prompt = \"\" \n) \n \nllm_vec_classify(x, labels, additional_prompt = \"\", preview = FALSE) \n```\n\n## Arguments\n|Arguments|Description|\n|---|---|\n| .data | A `data.frame` or `tbl` object that contains the text to be analyzed |\n| col | The name of the field to analyze, supports `tidy-eval` |\n| labels | A character vector with at least 2 labels to classify the text as |\n| pred_name | A character vector with the name of the new column where the prediction will be placed |\n| additional_prompt | Inserts this text into the prompt sent to the LLM |\n| x | A vector that contains the text to be analyzed |\n| 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. |\n\n\n\n## Value\n `llm_classify` returns a `data.frame` or `tbl` object. `llm_vec_classify` returns a vector that is the same length as `x`. \n\n\n## Examples\n\n\n::: {.cell}\n\n```{.r .cell-code}\n \nlibrary(mall) \n \nllm_use(\"ollama\", \"llama3.1\", seed = 100, .silent = TRUE) \n \nreviews <- data.frame(review = c( \n \"This has been the best TV I've ever used. Great screen, and sound.\", \n \"I regret buying this laptop. It is too slow and the keyboard is too noisy\", \n \"Not sure how to feel about my new washing machine. Great color, but hard to figure\" \n)) \n \nllm_classify(reviews, review, c(\"appliance\", \"computer\")) \n#> # A tibble: 3 × 2\n#> review .classify\n#> <chr> <chr> \n#> 1 This has been the best TV I've ever use… appliance\n#> 2 I regret buying this laptop. It is too … computer \n#> 3 Not sure how to feel about my new washi… appliance\n \n# Use 'pred_name' to customize the new column's name \nllm_classify( \n reviews, \n review, \n c(\"appliance\", \"computer\"), \n pred_name = \"prod_type\" \n ) \n#> # A tibble: 3 × 2\n#> review prod_type\n#> <chr> <chr> \n#> 1 This has been the best TV I've ever use… appliance\n#> 2 I regret buying this laptop. It is too … computer \n#> 3 Not sure how to feel about my new washi… appliance\n \n# Pass custom values for each classification \nllm_classify(reviews, review, c(\"appliance\" ~ 1, \"computer\" ~ 2)) \n#> # A tibble: 3 × 2\n#> review .classify\n#> <chr> <dbl>\n#> 1 This has been the best TV I've ever use… 1\n#> 2 I regret buying this laptop. It is too … 2\n#> 3 Not sure how to feel about my new washi… 1\n \n# For character vectors, instead of a data frame, use this function \nllm_vec_classify( \n c(\"this is important!\", \"just whenever\"), \n c(\"urgent\", \"not urgent\") \n ) \n#> [1] \"urgent\" \"not urgent\"\n \n#' # For character vectors, instead of a data frame, use this function \nllm_vec_classify( \n c(\"this is important!\", \"just whenever\"), \n c(\"urgent\", \"not urgent\"), \n preview = TRUE \n ) \n#> ollamar::chat(messages = list(list(role = \"user\", content = \"You are a helpful classification engine. Determine if the text refers to one of the following: urgent, not urgent. No capitalization. No explanations. The answer is based on the following text:\\nthis is important!\")), \n#> output = \"text\", model = \"llama3.1\", seed = 100)\n```\n:::\n",
"supporting": [],
"filters": [
"rmarkdown/pagebreak.lua"
Expand Down
40 changes: 40 additions & 0 deletions man/llm_classify.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions reference/llm_classify.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,46 @@ llm_vec_classify(x, labels, additional_prompt = "", preview = FALSE)
`llm_classify` returns a `data.frame` or `tbl` object. `llm_vec_classify` returns a vector that is the same length as `x`.


## Examples
```{r}
library(mall)
llm_use("ollama", "llama3.1", seed = 100, .silent = TRUE)
reviews <- data.frame(review = c(
"This has been the best TV I've ever used. Great screen, and sound.",
"I regret buying this laptop. It is too slow and the keyboard is too noisy",
"Not sure how to feel about my new washing machine. Great color, but hard to figure"
))
llm_classify(reviews, review, c("appliance", "computer"))
# Use 'pred_name' to customize the new column's name
llm_classify(
reviews,
review,
c("appliance", "computer"),
pred_name = "prod_type"
)
# Pass custom values for each classification
llm_classify(reviews, review, c("appliance" ~ 1, "computer" ~ 2))
# For character vectors, instead of a data frame, use this function
llm_vec_classify(
c("this is important!", "just whenever"),
c("urgent", "not urgent")
)
#' # For character vectors, instead of a data frame, use this function
llm_vec_classify(
c("this is important!", "just whenever"),
c("urgent", "not urgent"),
preview = TRUE
)
```


0 comments on commit 7730724

Please sign in to comment.