diff --git a/R/llm-sentiment.R b/R/llm-sentiment.R index f8b6b6b..7491db9 100644 --- a/R/llm-sentiment.R +++ b/R/llm-sentiment.R @@ -9,30 +9,30 @@ #' a sentiment to the text. Defaults to: 'positive', 'negative', 'neutral' #' @returns `llm_sentiment` returns a `data.frame` or `tbl` object. #' `llm_vec_sentiment` returns a vector that is the same length as `x`. -#' -#'@examples -#'\dontrun{ +#' +#' @examples +#' \dontrun{ #' library(mall) -#' -#' llm_use("ollama", "llama3.1", seed = 100, .silent = TRUE) -#' +#' +#' 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_sentiment(reviews, review) -#' +#' #' # Pass custom sentiment options #' llm_sentiment(reviews, review, c("positive", "negative")) -#' -#' # Specify values to return per sentiment +#' +#' # Specify values to return per sentiment #' llm_sentiment(reviews, review, c("positive" ~ 1, "negative" ~ 0)) -#' +#' #' # For character vectors, instead of a data frame, use this function #' llm_vec_sentiment(c("I am happy", "I am sad")) -#'} +#' } #' @export llm_sentiment <- function(.data, col, diff --git a/_freeze/reference/llm_sentiment/execute-results/html.json b/_freeze/reference/llm_sentiment/execute-results/html.json index d81287b..8ab9b76 100644 --- a/_freeze/reference/llm_sentiment/execute-results/html.json +++ b/_freeze/reference/llm_sentiment/execute-results/html.json @@ -1,8 +1,8 @@ { - "hash": "08d9e7598213e4cfca083d11aaf47bed", + "hash": "e934c7f880a22b53e4d67f4e0568c0f7", "result": { "engine": "knitr", - "markdown": "---\ntitle: \"Sentiment analysis\"\nexecute:\n eval: true\n freeze: true\n---\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#> \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#> \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#> \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}\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#> \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#> \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#> \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", "supporting": [], "filters": [ "rmarkdown/pagebreak.lua" diff --git a/man/llm_sentiment.Rd b/man/llm_sentiment.Rd index f96da26..ea0418e 100644 --- a/man/llm_sentiment.Rd +++ b/man/llm_sentiment.Rd @@ -52,20 +52,20 @@ from the provided text \dontrun{ library(mall) -llm_use("ollama", "llama3.1", seed = 100, .silent = TRUE) +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_sentiment(reviews, review) # Pass custom sentiment options llm_sentiment(reviews, review, c("positive", "negative")) -# Specify values to return per sentiment +# Specify values to return per sentiment llm_sentiment(reviews, review, c("positive" ~ 1, "negative" ~ 0)) # For character vectors, instead of a data frame, use this function diff --git a/reference/llm_sentiment.qmd b/reference/llm_sentiment.qmd index 7e0e4f0..4448968 100644 --- a/reference/llm_sentiment.qmd +++ b/reference/llm_sentiment.qmd @@ -56,28 +56,26 @@ llm_vec_sentiment( ## 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_sentiment(reviews, review) - -# Pass custom sentiment options -llm_sentiment(reviews, review, c("positive", "negative")) - -# Specify values to return per sentiment -llm_sentiment(reviews, review, c("positive" ~ 1, "negative" ~ 0)) - -# For character vectors, instead of a data frame, use this function -llm_vec_sentiment(c("I am happy", "I am sad")) - +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_sentiment(reviews, review) + +# Pass custom sentiment options +llm_sentiment(reviews, review, c("positive", "negative")) + +# Specify values to return per sentiment +llm_sentiment(reviews, review, c("positive" ~ 1, "negative" ~ 0)) + +# For character vectors, instead of a data frame, use this function +llm_vec_sentiment(c("I am happy", "I am sad")) ```