Skip to content

Commit

Permalink
Merge pull request #14 from edgararuiz/updates
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
edgararuiz authored Sep 23, 2024
2 parents bf59cae + 7730724 commit c9c37d2
Show file tree
Hide file tree
Showing 19 changed files with 339 additions and 431 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
7 changes: 7 additions & 0 deletions R/llm-sentiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#'
#' llm_sentiment(reviews, review)
#'
#' # Use 'pred_name' to customize the new column's name
#' llm_sentiment(reviews, review, pred_name = "review_sentiment")
#'
#' # Pass custom sentiment options
#' llm_sentiment(reviews, review, c("positive", "negative"))
#'
Expand All @@ -32,6 +35,10 @@
#'
#' # For character vectors, instead of a data frame, use this function
#' llm_vec_sentiment(c("I am happy", "I am sad"))
#'
#' #' # For character vectors, instead of a data frame, use this function
#' llm_vec_sentiment(c("I am happy", "I am sad"), preview = TRUE)
#'
#' }
#' @export
llm_sentiment <- function(.data,
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
4 changes: 2 additions & 2 deletions _freeze/reference/llm_sentiment/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"hash": "e934c7f880a22b53e4d67f4e0568c0f7",
"hash": "1bda1dfe3201dd34f5b5c04c7452c657",
"result": {
"engine": "knitr",
"markdown": "---\ntitle: \"Sentiment analysis\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n[R/llm-sentiment.R](https://github.com/edgararuiz/mall/blob/main/R/llm-sentiment.R)\n\n## llm_sentiment\n\n## Description\n Use a Large Language Model (LLM) to perform sentiment analysis from the provided text \n\n\n## Usage\n```r\n \nllm_sentiment( \n .data, \n col, \n options = c(\"positive\", \"negative\", \"neutral\"), \n pred_name = \".sentiment\", \n additional_prompt = \"\" \n) \n \nllm_vec_sentiment( \n x, \n options = c(\"positive\", \"negative\", \"neutral\"), \n additional_prompt = \"\", \n preview = FALSE \n) \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| options | A vector with the options that the LLM should use to assign a sentiment to the text. Defaults to: 'positive', 'negative', 'neutral' |\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_sentiment` returns a `data.frame` or `tbl` object. `llm_vec_sentiment` returns a vector that is the same length as `x`. \n\n\n## Examples\n\n\n::: {.cell}\n\n```{.r .cell-code}\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_sentiment(reviews, review)\n#> # A tibble: 3 × 2\n#> review .sentiment\n#> <chr> <chr> \n#> 1 This has been the best TV I've ever use… positive \n#> 2 I regret buying this laptop. It is too … negative \n#> 3 Not sure how to feel about my new washi… neutral\n\n# Pass custom sentiment options\nllm_sentiment(reviews, review, c(\"positive\", \"negative\"))\n#> # A tibble: 3 × 2\n#> review .sentiment\n#> <chr> <chr> \n#> 1 This has been the best TV I've ever use… positive \n#> 2 I regret buying this laptop. It is too … negative \n#> 3 Not sure how to feel about my new washi… negative\n\n# Specify values to return per sentiment\nllm_sentiment(reviews, review, c(\"positive\" ~ 1, \"negative\" ~ 0))\n#> # A tibble: 3 × 2\n#> review .sentiment\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 … 0\n#> 3 Not sure how to feel about my new washi… 0\n\n# For character vectors, instead of a data frame, use this function\nllm_vec_sentiment(c(\"I am happy\", \"I am sad\"))\n#> [1] \"positive\" \"negative\"\n```\n:::\n",
"markdown": "---\ntitle: \"Sentiment analysis\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n[R/llm-sentiment.R](https://github.com/edgararuiz/mall/blob/main/R/llm-sentiment.R)\n\n## llm_sentiment\n\n## Description\n Use a Large Language Model (LLM) to perform sentiment analysis from the provided text \n\n\n## Usage\n```r\n \nllm_sentiment( \n .data, \n col, \n options = c(\"positive\", \"negative\", \"neutral\"), \n pred_name = \".sentiment\", \n additional_prompt = \"\" \n) \n \nllm_vec_sentiment( \n x, \n options = c(\"positive\", \"negative\", \"neutral\"), \n additional_prompt = \"\", \n preview = FALSE \n) \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| options | A vector with the options that the LLM should use to assign a sentiment to the text. Defaults to: 'positive', 'negative', 'neutral' |\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_sentiment` returns a `data.frame` or `tbl` object. `llm_vec_sentiment` 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_sentiment(reviews, review) \n#> # A tibble: 3 × 2\n#> review .sentiment\n#> <chr> <chr> \n#> 1 This has been the best TV I've ever use… positive \n#> 2 I regret buying this laptop. It is too … negative \n#> 3 Not sure how to feel about my new washi… neutral\n \n# Use 'pred_name' to customize the new column's name \nllm_sentiment(reviews, review, pred_name = \"review_sentiment\") \n#> # A tibble: 3 × 2\n#> review review_sentiment\n#> <chr> <chr> \n#> 1 This has been the best TV I've ever use… positive \n#> 2 I regret buying this laptop. It is too … negative \n#> 3 Not sure how to feel about my new washi… neutral\n \n# Pass custom sentiment options \nllm_sentiment(reviews, review, c(\"positive\", \"negative\")) \n#> # A tibble: 3 × 2\n#> review .sentiment\n#> <chr> <chr> \n#> 1 This has been the best TV I've ever use… positive \n#> 2 I regret buying this laptop. It is too … negative \n#> 3 Not sure how to feel about my new washi… negative\n \n# Specify values to return per sentiment \nllm_sentiment(reviews, review, c(\"positive\" ~ 1, \"negative\" ~ 0)) \n#> # A tibble: 3 × 2\n#> review .sentiment\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 … 0\n#> 3 Not sure how to feel about my new washi… 0\n \n# For character vectors, instead of a data frame, use this function \nllm_vec_sentiment(c(\"I am happy\", \"I am sad\")) \n#> [1] \"positive\" \"negative\"\n \n#' # For character vectors, instead of a data frame, use this function \nllm_vec_sentiment(c(\"I am happy\", \"I am sad\"), preview = TRUE) \n#> ollamar::chat(messages = list(list(role = \"user\", content = \"You are a helpful sentiment engine. Return only one of the following answers: positive, negative, neutral. No capitalization. No explanations. The answer is based on the following text:\\nI am happy\")), \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.

7 changes: 7 additions & 0 deletions man/llm_sentiment.Rd

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

16 changes: 8 additions & 8 deletions reference/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ description: Run multiple 'Large Language Model' predictions against a table, or
---
Function(s) | Description
|---|---|
|[llm_classify() \| llm_vec_classify()](/reference/llm_classify.html)|Categorize data as one of options given|
|[llm_custom() \| llm_vec_custom()](/reference/llm_custom.html)|Send a custom prompt to the LLM|
|[llm_extract() \| llm_vec_extract()](/reference/llm_extract.html)|Extract entities from text|
|[llm_sentiment() \| llm_vec_sentiment()](/reference/llm_sentiment.html)|Sentiment analysis|
|[llm_summarize() \| llm_vec_summarize()](/reference/llm_summarize.html)|Summarize text|
|[llm_translate() \| llm_vec_translate()](/reference/llm_translate.html)|Translates text to a specific language|
|[llm_use()](/reference/llm_use.html)|Specify the model to use|
|[m_backend_prompt() \| m_backend_submit()](/reference/m_backend_submit.html)|Functions to integrate different back-ends|
|[llm_classify() \| llm_vec_classify()](reference/llm_classify.html)|Categorize data as one of options given|
|[llm_custom() \| llm_vec_custom()](reference/llm_custom.html)|Send a custom prompt to the LLM|
|[llm_extract() \| llm_vec_extract()](reference/llm_extract.html)|Extract entities from text|
|[llm_sentiment() \| llm_vec_sentiment()](reference/llm_sentiment.html)|Sentiment analysis|
|[llm_summarize() \| llm_vec_summarize()](reference/llm_summarize.html)|Summarize text|
|[llm_translate() \| llm_vec_translate()](reference/llm_translate.html)|Translates text to a specific language|
|[llm_use()](reference/llm_use.html)|Specify the model to use|
|[m_backend_prompt() \| m_backend_submit()](reference/m_backend_submit.html)|Functions to integrate different back-ends|
Loading

0 comments on commit c9c37d2

Please sign in to comment.