Skip to content

Commit

Permalink
Passes checks, tests and styler updates
Browse files Browse the repository at this point in the history
  • Loading branch information
edgararuiz committed Sep 21, 2024
1 parent 0e486d8 commit bf59cae
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 40 deletions.
26 changes: 13 additions & 13 deletions R/llm-sentiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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": "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#> <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}\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",
"supporting": [],
"filters": [
"rmarkdown/pagebreak.lua"
Expand Down
6 changes: 3 additions & 3 deletions man/llm_sentiment.Rd

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

42 changes: 20 additions & 22 deletions reference/llm_sentiment.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
```


0 comments on commit bf59cae

Please sign in to comment.