Skip to content

Commit

Permalink
Enables examples run, updates template to freeze results, adds exampl…
Browse files Browse the repository at this point in the history
…e to sentiment, updates freeze
  • Loading branch information
edgararuiz committed Sep 21, 2024
1 parent e5a4f02 commit 5fdd672
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ po/*~
rsconnect/

/.quarto/

docs/

14 changes: 14 additions & 0 deletions R/llm-sentiment.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ llm_sentiment.data.frame <- function(.data,
globalVariables("ai_analyze_sentiment")

#' @rdname llm_sentiment
#'@examples
#'\dontrun{
#'library(mall)
#'
#'llm_use("ollama", "llama3.1", seed = 100, .silent = TRUE)
#'
#'llm_vec_sentiment("I am happy")
#'
#'# Specify values to return per sentiment
#'llm_vec_sentiment("I am happy", c("positive" ~ 1, "negative" ~ 0))
#'
#'llm_vec_sentiment("I am sad", c("positive" ~ 1, "negative" ~ 0))
#'
#'}
#' @export
llm_vec_sentiment <- function(x,
options = c("positive", "negative", "neutral"),
Expand Down
15 changes: 15 additions & 0 deletions _freeze/reference/llm_sentiment/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"hash": "ffb23648b4f6280b3e4c6a87d85973f4",
"result": {
"engine": "knitr",
"markdown": "---\ntitle: \"Sentiment analysis\"\nexecute:\n eval: true\n freeze: true\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 \nllm_vec_sentiment(\"I am happy\") \n#> [1] \"positive\"\n \n# Specify values to return per sentiment \nllm_vec_sentiment(\"I am happy\", c(\"positive\" ~ 1, \"negative\" ~ 0)) \n#> [1] 1\n \nllm_vec_sentiment(\"I am sad\", c(\"positive\" ~ 1, \"negative\" ~ 0)) \n#> [1] 0\n```\n:::\n",
"supporting": [],
"filters": [
"rmarkdown/pagebreak.lua"
],
"includes": {},
"engineDependencies": {},
"preserve": {},
"postProcess": true
}
}
15 changes: 15 additions & 0 deletions man/llm_sentiment.Rd

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

3 changes: 3 additions & 0 deletions reference/llm_classify.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
title: "Categorize data as one of options given"
execute:
eval: true
freeze: true
---

[R/llm-classify.R](https://github.com/edgararuiz/mall/blob/main/R/llm-classify.R)
Expand Down
3 changes: 3 additions & 0 deletions reference/llm_custom.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
title: "Send a custom prompt to the LLM"
execute:
eval: true
freeze: true
---

[R/llm-custom.R](https://github.com/edgararuiz/mall/blob/main/R/llm-custom.R)
Expand Down
3 changes: 3 additions & 0 deletions reference/llm_extract.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
title: "Extract entities from text"
execute:
eval: true
freeze: true
---

[R/llm-extract.R](https://github.com/edgararuiz/mall/blob/main/R/llm-extract.R)
Expand Down
19 changes: 19 additions & 0 deletions reference/llm_sentiment.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
title: "Sentiment analysis"
execute:
eval: true
freeze: true
---

[R/llm-sentiment.R](https://github.com/edgararuiz/mall/blob/main/R/llm-sentiment.R)
Expand Down Expand Up @@ -46,5 +49,21 @@ llm_vec_sentiment(
`llm_sentiment` returns a `data.frame` or `tbl` object. `llm_vec_sentiment` returns a vector that is the same length as `x`.


## Examples
```{r}
library(mall)
llm_use("ollama", "llama3.1", seed = 100, .silent = TRUE)
llm_vec_sentiment("I am happy")
# Specify values to return per sentiment
llm_vec_sentiment("I am happy", c("positive" ~ 1, "negative" ~ 0))
llm_vec_sentiment("I am sad", c("positive" ~ 1, "negative" ~ 0))
```


3 changes: 3 additions & 0 deletions reference/llm_summarize.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
title: "Summarize text"
execute:
eval: true
freeze: true
---

[R/llm-summarize.R](https://github.com/edgararuiz/mall/blob/main/R/llm-summarize.R)
Expand Down
3 changes: 3 additions & 0 deletions reference/llm_translate.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
title: "Translates text to a specific language"
execute:
eval: true
freeze: true
---

[R/llm-translate.R](https://github.com/edgararuiz/mall/blob/main/R/llm-translate.R)
Expand Down
3 changes: 3 additions & 0 deletions reference/llm_use.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
title: "Specify the model to use"
execute:
eval: true
freeze: true
---

[R/llm-use.R](https://github.com/edgararuiz/mall/blob/main/R/llm-use.R)
Expand Down
3 changes: 3 additions & 0 deletions reference/m_backend_submit.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
title: "Functions to integrate different back-ends"
execute:
eval: true
freeze: true
---

[R/m-backend-prompt.R, R/m-backend-submit.R](https://github.com/edgararuiz/mall/blob/main/R/m-backend-prompt.R, R/m-backend-submit.R)
Expand Down
1 change: 1 addition & 0 deletions utils/qmd-reference/reference.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
source("utils/qmd-reference/utils-reference-index.R")
source("utils/qmd-reference/utils-reference-list.R")
source("utils/qmd-reference/utils-reference-pages.R")
source("utils/qmd-reference/utils-context.R")
source("utils/qmd-reference/pkgdown.R")
library(pkgdown)
library(magrittr)
Expand Down
3 changes: 3 additions & 0 deletions utils/qmd-reference/reference.qmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
title: "{{{{notitle.title}}}}"
execute:
eval: true
freeze: true
---

[{{{{notitle.source}}}}](https://github.com/edgararuiz/mall/blob/main/{{{{notitle.source}}}})
Expand Down
64 changes: 64 additions & 0 deletions utils/qmd-reference/utils-context.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
ecodown_context <- new.env(parent = emptyenv())

ecodown_context_set <- function(id, vals = list()) {
ecodown_context[[id]] <- vals
}

ecodown_context_get <- function(id) {
if (id == "") {
return(NULL)
}
ecodown_context[[id]]
}

get_verbosity <- function() {
x <- ecodown_context_get("verbosity")
if (is.null(x)) x <- "verbose"
x[[1]]
}

set_verbosity <- function(x) {
ecodown_context_set("verbosity", x[[1]])
invisible()
}

is_summary <- function() {
get_verbosity() == "summary"
}

get_clone_header <- function() {
x <- ecodown_context_get("clone_header")
if (is.null(x)) x <- 0
x[[1]]
}

clone_header <- function() {
get_clone_header() == 0
}

set_clone_header <- function(x = 1) {
ecodown_context_set("clone_header", x)
}

get_package_header <- function() {
x <- ecodown_context_get("package_header")
if (is.null(x)) x <- 0
x[[1]]
}

package_header <- function() {
get_package_header() == 0
}

set_package_header <- function(x = 1) {
ecodown_context_set("package_header", x)
}

set_package_name <- function(x) {
ecodown_context_set("package_name", x)
}

get_package_name <- function() {
ecodown_context_get("package_name")
}

6 changes: 4 additions & 2 deletions utils/qmd-reference/utils-reference-pages.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ reference_to_qmd <- function(file_in, pkg, template = NULL) {

template |>
map(parse_line_tag, con) %>%
discard(is.null) %>%
list_flatten() %>%
list_c() %>%
as.character()
}
Expand Down Expand Up @@ -119,10 +121,10 @@ reference_convert <- function(x, output = "qmd") {
} else {
out <- list()
if (!is.null(curr$code_run)) {
out <- c(out, "```{r, eval=ecodown::examples_run()}", curr$code_run, "```")
out <- c(out, "```{r}", curr$code_run, "```")
}
if (!is.null(curr$code_dont_run)) {
out <- c(out, "```{r, eval=ecodown::examples_not_run()}", curr$code_dont_run, "```")
out <- c(out, "```{r}", curr$code_dont_run, "```")
}
}
}
Expand Down

0 comments on commit 5fdd672

Please sign in to comment.