diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 960d062..58ffbec 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -2,9 +2,9 @@ # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: - branches: main + branches: temp pull_request: - branches: main + branches: temp name: R-CMD-check.yaml diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml index cf09f1b..0a6aa3d 100644 --- a/.github/workflows/test-coverage.yaml +++ b/.github/workflows/test-coverage.yaml @@ -2,9 +2,9 @@ # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: - branches: main + branches: temp pull_request: - branches: main + branches: temp name: test-coverage.yaml diff --git a/.gitignore b/.gitignore index 5099731..c825b24 100644 --- a/.gitignore +++ b/.gitignore @@ -49,3 +49,4 @@ rsconnect/ docs/ +python/mall/src/ diff --git a/README.md b/README.md index 24dddc1..6fd398a 100644 --- a/README.md +++ b/README.md @@ -1,407 +1,13 @@ - - - # mall - - - + -[![R-CMD-check](https://github.com/edgararuiz/mall/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/edgararuiz/mall/actions/workflows/R-CMD-check.yaml) -[![Codecov test -coverage](https://codecov.io/gh/edgararuiz/mall/branch/main/graph/badge.svg)](https://app.codecov.io/gh/edgararuiz/mall?branch=main) -[![Lifecycle: -experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental) - - -## Intro Run multiple LLM predictions against a data frame. The predictions are processed row-wise over a specified column. It works using a pre-determined one-shot prompt, along with the current row’s content. -The prompt that is use will depend of the type of analysis needed. -Currently, the included prompts perform the following: - -- [Sentiment analysis](#sentiment) -- [Text summarizing](#summarize) -- [Classify text](#classify) -- [Extract one, or several](#extract), specific pieces information from - the text -- [Translate text](#translate) -- [Custom prompt](#custom-prompt) - -This package is inspired by the SQL AI functions now offered by vendors -such as -[Databricks](https://docs.databricks.com/en/large-language-models/ai-functions.html) -and Snowflake. `mall` uses [Ollama](https://ollama.com/) to interact -with LLMs installed locally. That interaction takes place via the -[`ollamar`](https://hauselin.github.io/ollama-r/) package. - -## Motivation - -We want to new find ways to help data scientists use LLMs in their daily -work. Unlike the familiar interfaces, such as chatting and code -completion, this interface runs your text data directly against the LLM. -The LLM’s flexibility, allows for it to adapt to the subject of your -data, and provide surprisingly accurate predictions. This saves the data -scientist the need to write and tune an NLP model. - -## Get started - -- Install `mall` from Github - - ``` r - pak::pak("edgararuiz/mall") - ``` - -### With local LLMs - -- Install Ollama in your machine. The `ollamar` package’s website - provides this [Installation - guide](https://hauselin.github.io/ollama-r/#installation) - -- Download an LLM model. For example, I have been developing this - package using Llama 3.2 to test. To get that model you can run: - - ``` r - ollamar::pull("llama3.2") - ``` - -### With Databricks - -If you pass a table connected to **Databricks** via `odbc`, `mall` will -automatically use Databricks’ LLM instead of Ollama. *You won’t need -Ollama installed if you are using Databricks only.* - -`mall` will call the appropriate SQL AI function. For more information -see our [Databricks -article.](https://edgararuiz.github.io/mall/articles/databricks.html) - -## LLM functions - -### Sentiment - -Primarily, `mall` provides verb-like functions that expect a `tbl` as -their first argument. This allows us to use them in piped operations. - -We will start with loading a very small data set contained in `mall`. It -has 3 product reviews that we will use as the source of our examples. - -``` r -library(mall) - -data("reviews") - -reviews -#> # A tibble: 3 × 1 -#> review -#> -#> 1 This has been the best TV I've ever used. Great screen, and sound. -#> 2 I regret buying this laptop. It is too slow and the keyboard is too noisy -#> 3 Not sure how to feel about my new washing machine. Great color, but hard to f… -``` - -For the first example, we’ll asses the sentiment of each review. In -order to do this we will call `llm_sentiment()`: - -``` r -reviews |> - llm_sentiment(review) -#> # A tibble: 3 × 2 -#> review .sentiment -#> -#> 1 This has been the best TV I've ever used. Great screen, and sound. positive -#> 2 I regret buying this laptop. It is too slow and the keyboard is to… negative -#> 3 Not sure how to feel about my new washing machine. Great color, bu… neutral -``` - -The function let’s us modify the options to choose from: - -``` r -reviews |> - llm_sentiment(review, options = c("positive", "negative")) -#> # A tibble: 3 × 2 -#> review .sentiment -#> -#> 1 This has been the best TV I've ever used. Great screen, and sound. positive -#> 2 I regret buying this laptop. It is too slow and the keyboard is to… negative -#> 3 Not sure how to feel about my new washing machine. Great color, bu… negative -``` - -As mentioned before, by being pipe friendly, the results from the LLM -prediction can be used in further transformations: - -``` r -reviews |> - llm_sentiment(review, options = c("positive", "negative")) |> - filter(.sentiment == "negative") -#> # A tibble: 2 × 2 -#> review .sentiment -#> -#> 1 I regret buying this laptop. It is too slow and the keyboard is to… negative -#> 2 Not sure how to feel about my new washing machine. Great color, bu… negative -``` - -### Summarize - -There may be a need to reduce the number of words in a given text. -Usually, to make it easier to capture its intent. To do this, use -`llm_summarize()`. This function has an argument to control the maximum -number of words to output (`max_words`): - -``` r -reviews |> - llm_summarize(review, max_words = 5) -#> # A tibble: 3 × 2 -#> review .summary -#> -#> 1 This has been the best TV I've ever used. Gr… it's a great tv -#> 2 I regret buying this laptop. It is too slow … laptop purchase was a mistake -#> 3 Not sure how to feel about my new washing ma… having mixed feelings about it -``` - -To control the name of the prediction field, you can change `pred_name` -argument. This works with the other `llm_` functions as well. - -``` r -reviews |> - llm_summarize(review, max_words = 5, pred_name = "review_summary") -#> # A tibble: 3 × 2 -#> review review_summary -#> -#> 1 This has been the best TV I've ever used. Gr… it's a great tv -#> 2 I regret buying this laptop. It is too slow … laptop purchase was a mistake -#> 3 Not sure how to feel about my new washing ma… having mixed feelings about it -``` - -### Classify - -Use the LLM to categorize the text into one of the options you provide: - -``` r -reviews |> - llm_classify(review, c("appliance", "computer")) -#> # A tibble: 3 × 2 -#> review .classify -#> -#> 1 This has been the best TV I've ever used. Gr… computer -#> 2 I regret buying this laptop. It is too slow … computer -#> 3 Not sure how to feel about my new washing ma… appliance -``` - -### Extract - -One of the most interesting operations. Using natural language, we can -tell the LLM to return a specific part of the text. In the following -example, we request that the LLM return the product being referred to. -We do this by simply saying “product”. The LLM understands what we -*mean* by that word, and looks for that in the text. - -``` r -reviews |> - llm_extract(review, "product") -#> # A tibble: 3 × 2 -#> review .extract -#> -#> 1 This has been the best TV I've ever used. Gr… tv -#> 2 I regret buying this laptop. It is too slow … laptop -#> 3 Not sure how to feel about my new washing ma… washing machine -``` - -### Translate - -As the title implies, this function will translate the text into a -specified language. What is really nice, it is that you don’t need to -specify the language of the source text. Only the target language needs -to be defined. The translation accuracy will depend on the LLM - -``` r -reviews |> - llm_translate(review, "spanish") -#> # A tibble: 3 × 2 -#> review .translation -#> -#> 1 This has been the best TV I've ever used. Gr… Esta ha sido la mejor televisió… -#> 2 I regret buying this laptop. It is too slow … Me arrepiento de comprar este p… -#> 3 Not sure how to feel about my new washing ma… No estoy seguro de cómo me sien… -``` - -### Custom prompt - -It is possible to pass your own prompt to the LLM, and have `mall` run -it against each text entry. Use `llm_custom()` to access this -functionality: - -``` r -my_prompt <- paste( - "Answer a question.", - "Return only the answer, no explanation", - "Acceptable answers are 'yes', 'no'", - "Answer this about the following text, is this a happy customer?:" -) - -reviews |> - llm_custom(review, my_prompt) -#> # A tibble: 3 × 2 -#> review .pred -#> -#> 1 This has been the best TV I've ever used. Great screen, and sound. Yes -#> 2 I regret buying this laptop. It is too slow and the keyboard is too noi… No -#> 3 Not sure how to feel about my new washing machine. Great color, but har… No -``` - -## Initialize session - -Invoking an `llm_` function will automatically initialize a model -selection if you don’t have one selected yet. If there is only one -option, it will pre-select it for you. If there are more than one -available models, then `mall` will present you as menu selection so you -can select which model you wish to use. - -Calling `llm_use()` directly will let you specify the model and backend -to use. You can also setup additional arguments that will be passed down -to the function that actually runs the prediction. In the case of -Ollama, that function is -[`chat()`](https://hauselin.github.io/ollama-r/reference/chat.html). - -``` r -llm_use("ollama", "llama3.2", seed = 100, temperature = 0) -``` - -## Key considerations - -The main consideration is **cost**. Either, time cost, or money cost. - -If using this method with an LLM locally available, the cost will be a -long running time. Unless using a very specialized LLM, a given LLM is a -general model. It was fitted using a vast amount of data. So determining -a response for each row, takes longer than if using a manually created -NLP model. The default model used in Ollama is [Llama -3.2](https://ollama.com/library/llama3.2), which was fitted using 3B -parameters. - -If using an external LLM service, the consideration will need to be for -the billing costs of using such service. Keep in mind that you will be -sending a lot of data to be evaluated. - -Another consideration is the novelty of this approach. Early tests are -providing encouraging results. But you, as an user, will still need to -keep in mind that the predictions will not be infallible, so always -check the output. At this time, I think the best use for this method, is -for a quick analysis. - -## Performance - -We will briefly cover this methods performance from two perspectives: - -- How long the analysis takes to run locally - -- How well it predicts - -To do so, we will use the `data_bookReviews` data set, provided by the -`classmap` package. For this exercise, only the first 100, of the total -1,000, are going to be part of this analysis. - -``` r -library(classmap) - -data(data_bookReviews) - -data_bookReviews |> - glimpse() -#> Rows: 1,000 -#> Columns: 2 -#> $ review "i got this as both a book and an audio file. i had waited t… -#> $ sentiment 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 1, … -``` - -As per the docs, `sentiment` is a factor indicating the sentiment of the -review: negative (1) or positive (2) - -``` r -length(strsplit(paste(head(data_bookReviews$review, 100), collapse = " "), " ")[[1]]) -#> [1] 20470 -``` - -Just to get an idea of how much data we’re processing, I’m using a very, -very simple word count. So we’re analyzing a bit over 20 thousand words. - -``` r -reviews_llm <- data_bookReviews |> - head(100) |> - llm_sentiment( - col = review, - options = c("positive" ~ 2, "negative" ~ 1), - pred_name = "predicted" - ) -#> ! There were 2 predictions with invalid output, they were coerced to NA -``` - -As far as **time**, on my Apple M3 machine, it took about 1.5 minutes to -process, 100 rows, containing 20 thousand words. Setting `temp` to 0 in -`llm_use()`, made the model run faster. - -The package uses `purrr` to send each prompt individually to the LLM. -But, I did try a few different ways to speed up the process, -unsuccessfully: - -- Used `furrr` to send multiple requests at a time. This did not work - because either the LLM or Ollama processed all my requests serially. - So there was no improvement. - -- I also tried sending more than one row’s text at a time. This cause - instability in the number of results. For example sending 5 at a time, - sometimes returned 7 or 8. Even sending 2 was not stable. - -This is what the new table looks like: - -``` r -reviews_llm -#> # A tibble: 100 × 3 -#> review sentiment predicted -#> -#> 1 "i got this as both a book and an audio file… 1 1 -#> 2 "this book places too much emphasis on spend… 1 1 -#> 3 "remember the hollywood blacklist? the holly… 2 2 -#> 4 "while i appreciate what tipler was attempti… 1 1 -#> 5 "the others in the series were great, and i … 1 1 -#> 6 "a few good things, but she's lost her edge … 1 1 -#> 7 "words cannot describe how ripped off and di… 1 1 -#> 8 "1. the persective of most writers is shaped… 1 NA -#> 9 "i have been a huge fan of michael crichton … 1 1 -#> 10 "i saw dr. polk on c-span a month or two ago… 2 2 -#> # ℹ 90 more rows -``` - -I used `yardstick` to see how well the model performed. Of course, the -accuracy will not be of the “truth”, but rather the package’s results -recorded in `sentiment`. - -``` r -library(forcats) - -reviews_llm |> - mutate(predicted = as.factor(predicted)) |> - yardstick::accuracy(sentiment, predicted) -#> # A tibble: 1 × 3 -#> .metric .estimator .estimate -#> -#> 1 accuracy binary 0.980 -``` - -## Vector functions - -`mall` includes functions that expect a vector, instead of a table, to -run the predictions. This should make it easier to test things, such as -custom prompts or results of specific text. Each `llm_` function has a -corresponding `llm_vec_` function: +`mall` is now available in both R and Python. -``` r -llm_vec_sentiment("I am happy") -#> [1] "positive" -``` +To find out how to install and use, or just to learn more about it, please +visit the official website: https://edgararuiz.github.io/mall/ -``` r -llm_vec_translate("Este es el mejor dia!", "english") -#> [1] "It's the best day!" -``` diff --git a/_freeze/index/execute-results/html.json b/_freeze/index/execute-results/html.json new file mode 100644 index 0000000..18fd744 --- /dev/null +++ b/_freeze/index/execute-results/html.json @@ -0,0 +1,15 @@ +{ + "hash": "31f1711bc2fee6106b9122901fbc16c3", + "result": { + "engine": "knitr", + "markdown": "---\nformat:\n html:\n toc: true\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n\n\n\nRun multiple LLM predictions against a data frame. The predictions are processed \nrow-wise over a specified column. It works using a pre-determined one-shot prompt,\nalong with the current row's content. The prompt that is use will depend of the\ntype of analysis needed. \n\nCurrently, the included prompts perform the following: \n\n- [Sentiment analysis](#sentiment)\n- [Text summarizing](#summarize)\n- [Classify text](#classify)\n- [Extract one, or several](#extract), specific pieces information from the text\n- [Translate text](#translate)\n- [Custom prompt](#custom-prompt)\n\nThis package is inspired by the SQL AI functions now offered by vendors such as\n[Databricks](https://docs.databricks.com/en/large-language-models/ai-functions.html) \nand Snowflake. `mall` uses [Ollama](https://ollama.com/) to interact with LLMs \ninstalled locally. \n\n\nFor R, that interaction takes place via the \n[`ollamar`](https://hauselin.github.io/ollama-r/) package. The functions are \ndesigned to easily work with piped commands, such as `dplyr`. \n\n```r\nreviews |>\n llm_sentiment(review)\n```\n\n\nFor Python, `mall` includes an extension to [Polars](https://pola.rs/). To\ninteract with Ollama, it uses the official [Python library](https://github.com/ollama/ollama-python).\n\n```python\nreviews.llm.sentiment(\"review\")\n```\n\n## Motivation\n\nWe want to new find ways to help data scientists use LLMs in their daily work. \nUnlike the familiar interfaces, such as chatting and code completion, this interface\nruns your text data directly against the LLM. \n\nThe LLM's flexibility, allows for it to adapt to the subject of your data, and \nprovide surprisingly accurate predictions. This saves the data scientist the\nneed to write and tune an NLP model. \n\nIn recent times, the capabilities of LLMs that can run locally in your computer \nhave increased dramatically. This means that these sort of analysis can run\nin your machine with good accuracy. Additionally, it makes it possible to take\nadvantage of LLM's at your institution, since the data will not leave the\ncorporate network. \n\n## Get started\n\n- Install `mall` from Github\n\n \n::: {.panel-tabset group=\"language\"}\n## R\n```r\npak::pak(\"edgararuiz/mall/r@python\")\n```\n\n## python\n```python\npip install \"mall @ git+https://git@github.com/edgararuiz/mall.git@python#subdirectory=python\"\n```\n:::\n \n\n### With local LLMs\n\n- [Download Ollama from the official website](https://ollama.com/download)\n\n- Install and start Ollama in your computer\n\n\n::: {.panel-tabset group=\"language\"}\n## R\n- Install Ollama in your machine. The `ollamar` package's website provides this\n[Installation guide](https://hauselin.github.io/ollama-r/#installation)\n\n- Download an LLM model. For example, I have been developing this package using\nLlama 3.2 to test. To get that model you can run: \n ```r\n ollamar::pull(\"llama3.2\")\n ```\n \n## python\n\n- Install the official Ollama library\n ```python\n pip install ollama\n ```\n\n- Download an LLM model. For example, I have been developing this package using\nLlama 3.2 to test. To get that model you can run: \n ```python\n import ollama\n ollama.pull('llama3.2')\n ```\n:::\n\n\n \n### With Databricks (R only)\n\nIf you pass a table connected to **Databricks** via `odbc`, `mall` will \nautomatically use Databricks' LLM instead of Ollama. *You won't need Ollama \ninstalled if you are using Databricks only.*\n\n`mall` will call the appropriate SQL AI function. For more information see our \n[Databricks article.](https://edgararuiz.github.io/mall/articles/databricks.html) \n\n## LLM functions\n\n### Sentiment\n\nPrimarily, `mall` provides verb-like functions that expect a data frame as \ntheir first argument. \n\nWe will start with loading a very small data set contained in `mall`. It has\n3 product reviews that we will use as the source of our examples.\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(mall)\n\ndata(\"reviews\")\n\nreviews\n#> # A tibble: 3 × 1\n#> review \n#> \n#> 1 This has been the best TV I've ever used. Great screen, and sound. \n#> 2 I regret buying this laptop. It is too slow and the keyboard is too noisy \n#> 3 Not sure how to feel about my new washing machine. Great color, but hard to f…\n```\n:::\n\n\n\n## python\n\n\n\n::: {.cell}\n\n```{.python .cell-code}\nimport mall \nimport polars as pl\n\ndata = mall.MallData\n\nreviews = data.reviews\nreviews \n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\nshape: (3, 1)
review
str
"This has been the best TV I've…
"I regret buying this laptop. I…
"Not sure how to feel about my …
\n```\n\n:::\n:::\n\n\n:::\n\n\n\n\n\n\n\nFor the first example, we'll asses the sentiment of each review:\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n\nreviews |>\n llm_sentiment(review)\n#> # A tibble: 3 × 2\n#> review .sentiment\n#> \n#> 1 This has been the best TV I've ever used. Great screen, and sound. positive \n#> 2 I regret buying this laptop. It is too slow and the keyboard is to… negative \n#> 3 Not sure how to feel about my new washing machine. Great color, bu… neutral\n```\n:::\n\n\n\n## python \n\n\n\n::: {.cell}\n\n```{.python .cell-code}\n\nreviews.llm.sentiment(\"review\")\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\nshape: (3, 2)
reviewsentiment
strstr
"This has been the best TV I've…"positive"
"I regret buying this laptop. I…"negative"
"Not sure how to feel about my …"neutral"
\n```\n\n:::\n:::\n\n\n\n:::\n\nWe can also provide custom sentiment labels. Use the `options` argument to control\nthat:\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n\nreviews |>\n llm_sentiment(review, options = c(\"positive\", \"negative\"))\n#> # A tibble: 3 × 2\n#> review .sentiment\n#> \n#> 1 This has been the best TV I've ever used. Great screen, and sound. positive \n#> 2 I regret buying this laptop. It is too slow and the keyboard is to… negative \n#> 3 Not sure how to feel about my new washing machine. Great color, bu… negative\n```\n:::\n\n\n\n## python \n\n\n\n::: {.cell}\n\n```{.python .cell-code}\n\nreviews.llm.sentiment(\"review\", options=[\"positive\", \"negative\"])\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\nshape: (3, 2)
reviewsentiment
strstr
"This has been the best TV I've…"positive"
"I regret buying this laptop. I…"negative"
"Not sure how to feel about my …"negative"
\n```\n\n:::\n:::\n\n\n\n:::\n\nAs mentioned before, these functions are create to play well with the rest of \nthe analysis\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n\nreviews |>\n llm_sentiment(review, options = c(\"positive\", \"negative\")) |>\n filter(.sentiment == \"negative\")\n#> # A tibble: 2 × 2\n#> review .sentiment\n#> \n#> 1 I regret buying this laptop. It is too slow and the keyboard is to… negative \n#> 2 Not sure how to feel about my new washing machine. Great color, bu… negative\n```\n:::\n\n\n\n## python\n\n\n\n::: {.cell}\n\n```{.python .cell-code}\n\nx = reviews.llm.sentiment(\"review\", options=[\"positive\", \"negative\"])\n\nx.filter(pl.col(\"sentiment\") == \"negative\")\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\nshape: (2, 2)
reviewsentiment
strstr
"I regret buying this laptop. I…"negative"
"Not sure how to feel about my …"negative"
\n```\n\n:::\n:::\n\n\n\n:::\n\n### Summarize\n\nThere may be a need to reduce the number of words in a given text. Typically to \nmake it easier to understand its intent. The function has an argument to \ncontrol the maximum number of words to output \n(`max_words`):\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n\n\nreviews |>\n llm_summarize(review, max_words = 5)\n#> # A tibble: 3 × 2\n#> review .summary \n#> \n#> 1 This has been the best TV I've ever used. Gr… it's a great tv \n#> 2 I regret buying this laptop. It is too slow … laptop purchase was a mistake \n#> 3 Not sure how to feel about my new washing ma… having mixed feelings about it\n```\n:::\n\n\n\n## python \n\n\n\n::: {.cell}\n\n```{.python .cell-code}\n\n\nreviews.llm.summarize(\"review\", 5)\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\nshape: (3, 2)
reviewsummary
strstr
"This has been the best TV I've…"it's a great tv"
"I regret buying this laptop. I…"laptop not worth the money"
"Not sure how to feel about my …"feeling uncertain about new pu…
\n```\n\n:::\n:::\n\n\n\n:::\n\nTo control the name of the prediction field, you can change `pred_name` argument.\n**This works with the other `llm` functions as well.**\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n\nreviews |>\n llm_summarize(review, max_words = 5, pred_name = \"review_summary\")\n#> # A tibble: 3 × 2\n#> review review_summary \n#> \n#> 1 This has been the best TV I've ever used. Gr… it's a great tv \n#> 2 I regret buying this laptop. It is too slow … laptop purchase was a mistake \n#> 3 Not sure how to feel about my new washing ma… having mixed feelings about it\n```\n:::\n\n\n\n## python \n\n\n\n::: {.cell}\n\n```{.python .cell-code}\n\nreviews.llm.summarize(\"review\", max_words = 5, pred_name = \"review_summary\")\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\nshape: (3, 2)
reviewreview_summary
strstr
"This has been the best TV I've…"it's a great tv"
"I regret buying this laptop. I…"laptop not worth the money"
"Not sure how to feel about my …"feeling uncertain about new pu…
\n```\n\n:::\n:::\n\n\n\n:::\n\n### Classify\n\nUse the LLM to categorize the text into one of the options you provide: \n\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nreviews |>\n llm_classify(review, c(\"appliance\", \"computer\"))\n#> # A tibble: 3 × 2\n#> review .classify\n#> \n#> 1 This has been the best TV I've ever used. Gr… computer \n#> 2 I regret buying this laptop. It is too slow … computer \n#> 3 Not sure how to feel about my new washing ma… appliance\n```\n:::\n\n\n\n\n## python \n\n\n\n::: {.cell}\n\n```{.python .cell-code}\n\nreviews.llm.classify(\"review\", [\"computer\", \"appliance\"])\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\nshape: (3, 2)
reviewclassify
strstr
"This has been the best TV I've…"appliance"
"I regret buying this laptop. I…"appliance"
"Not sure how to feel about my …"appliance"
\n```\n\n:::\n:::\n\n\n\n:::\n\n### Extract \n\nOne of the most interesting use cases Using natural language, we can tell the \nLLM to return a specific part of the text. In the following example, we request\nthat the LLM return the product being referred to. We do this by simply saying \n\"product\". The LLM understands what we *mean* by that word, and looks for that\nin the text.\n\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nreviews |>\n llm_extract(review, \"product\")\n#> # A tibble: 3 × 2\n#> review .extract \n#> \n#> 1 This has been the best TV I've ever used. Gr… tv \n#> 2 I regret buying this laptop. It is too slow … laptop \n#> 3 Not sure how to feel about my new washing ma… washing machine\n```\n:::\n\n\n\n\n## python \n\n\n\n::: {.cell}\n\n```{.python .cell-code}\n\nreviews.llm.extract(\"review\", \"product\")\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\nshape: (3, 2)
reviewextract
strstr
"This has been the best TV I've…"tv"
"I regret buying this laptop. I…"laptop"
"Not sure how to feel about my …"washing machine"
\n```\n\n:::\n:::\n\n\n\n:::\n\n\n### Translate\n\nAs the title implies, this function will translate the text into a specified \nlanguage. What is really nice, it is that you don't need to specify the language\nof the source text. Only the target language needs to be defined. The translation\naccuracy will depend on the LLM\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n\n\nreviews |>\n llm_translate(review, \"spanish\")\n#> # A tibble: 3 × 2\n#> review .translation \n#> \n#> 1 This has been the best TV I've ever used. Gr… Esta ha sido la mejor televisió…\n#> 2 I regret buying this laptop. It is too slow … Me arrepiento de comprar este p…\n#> 3 Not sure how to feel about my new washing ma… No estoy seguro de cómo me sien…\n```\n:::\n\n\n\n\n## python \n\n\n\n::: {.cell}\n\n```{.python .cell-code}\n\n\nreviews.llm.translate(\"review\", \"spanish\")\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\nshape: (3, 2)
reviewtranslation
strstr
"This has been the best TV I've…"Esta ha sido la mejor TV que h…
"I regret buying this laptop. I…"Lo lamento comprar este portát…
"Not sure how to feel about my …"No estoy seguro de cómo sentir…
\n```\n\n:::\n:::\n\n\n\n:::\n\n### Custom prompt\n\nIt is possible to pass your own prompt to the LLM, and have `mall` run it \nagainst each text entry:\n\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nmy_prompt <- paste(\n \"Answer a question.\",\n \"Return only the answer, no explanation\",\n \"Acceptable answers are 'yes', 'no'\",\n \"Answer this about the following text, is this a happy customer?:\"\n)\n\nreviews |>\n llm_custom(review, my_prompt)\n#> # A tibble: 3 × 2\n#> review .pred\n#> \n#> 1 This has been the best TV I've ever used. Great screen, and sound. Yes \n#> 2 I regret buying this laptop. It is too slow and the keyboard is too noi… No \n#> 3 Not sure how to feel about my new washing machine. Great color, but har… No\n```\n:::\n\n\n\n\n## python \n\n\n\n::: {.cell}\n\n```{.python .cell-code}\n\nmy_prompt = \"Answer a question.\" \\\n + \"Return only the answer, no explanation\" \\\n + \"Acceptable answers are 'yes', 'no'\" \\\n + \"Answer this about the following text, is this a happy customer?:\"\n\n\nreviews.llm.custom(\"review\", prompt = my_prompt)\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\nshape: (3, 2)
reviewcustom
strstr
"This has been the best TV I've…"Yes"
"I regret buying this laptop. I…"No"
"Not sure how to feel about my …"No"
\n```\n\n:::\n:::\n\n\n\n:::\n\n## Model selection and settings\n\nYou can set the model and its options to use when calling the LLM. In this case,\nwe refer to options as model specific things that can be set, such as seed or\ntemperature. \n\n::: {.panel-tabset group=\"language\"}\n## R\n\nInvoking an `llm` function will automatically initialize a model selection\nif you don't have one selected yet. If there is only one option, it will \npre-select it for you. If there are more than one available models, then `mall`\nwill present you as menu selection so you can select which model you wish to \nuse.\n\nCalling `llm_use()` directly will let you specify the model and backend to use.\nYou can also setup additional arguments that will be passed down to the \nfunction that actually runs the prediction. In the case of Ollama, that function\nis [`chat()`](https://hauselin.github.io/ollama-r/reference/chat.html). \n\nThe model to use, and other options can be set for the current R session\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nllm_use(\"ollama\", \"llama3.2\", seed = 100, temperature = 0)\n```\n:::\n\n\n\n\n## python \n\nThe model and options to be used will be defined at the Polars data frame \nobject level. If not passed, the default model will be **llama3.2**.\n\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.use(options = dict(seed = 100))\n```\n:::\n\n\n\n:::\n\n## Key considerations\n\nThe main consideration is **cost**. Either, time cost, or money cost.\n\nIf using this method with an LLM locally available, the cost will be a long \nrunning time. Unless using a very specialized LLM, a given LLM is a general model. \nIt was fitted using a vast amount of data. So determining a response for each \nrow, takes longer than if using a manually created NLP model. The default model\nused in Ollama is [Llama 3.2](https://ollama.com/library/llama3.2), \nwhich was fitted using 3B parameters. \n\nIf using an external LLM service, the consideration will need to be for the \nbilling costs of using such service. Keep in mind that you will be sending a lot\nof data to be evaluated. \n\nAnother consideration is the novelty of this approach. Early tests are \nproviding encouraging results. But you, as an user, will still need to keep\nin mind that the predictions will not be infallible, so always check the output.\nAt this time, I think the best use for this method, is for a quick analysis.\n\n## Performance\n\nWe will briefly cover this methods performance from two perspectives: \n\n- How long the analysis takes to run locally \n\n- How well it predicts \n\nTo do so, we will use the `data_bookReviews` data set, provided by the `classmap`\npackage. For this exercise, only the first 100, of the total 1,000, are going\nto be part of this analysis.\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(classmap)\n\ndata(data_bookReviews)\n\ndata_bookReviews |>\n glimpse()\n#> Rows: 1,000\n#> Columns: 2\n#> $ review \"i got this as both a book and an audio file. i had waited t…\n#> $ sentiment 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 1, …\n```\n:::\n\n\nAs per the docs, `sentiment` is a factor indicating the sentiment of the review:\nnegative (1) or positive (2)\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlength(strsplit(paste(head(data_bookReviews$review, 100), collapse = \" \"), \" \")[[1]])\n#> [1] 20470\n```\n:::\n\n\n\nJust to get an idea of how much data we're processing, I'm using a very, very \nsimple word count. So we're analyzing a bit over 20 thousand words.\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nreviews_llm <- data_bookReviews |>\n head(100) |> \n llm_sentiment(\n col = review,\n options = c(\"positive\" ~ 2, \"negative\" ~ 1),\n pred_name = \"predicted\"\n )\n#> ! There were 2 predictions with invalid output, they were coerced to NA\n```\n:::\n\n\n\nAs far as **time**, on my Apple M3 machine, it took about 1.5 minutes to process,\n100 rows, containing 20 thousand words. Setting `temp` to 0 in `llm_use()`, \nmade the model run faster.\n\nThe package uses `purrr` to send each prompt individually to the LLM. But, I did\ntry a few different ways to speed up the process, unsuccessfully:\n\n- Used `furrr` to send multiple requests at a time. This did not work because \neither the LLM or Ollama processed all my requests serially. So there was\nno improvement.\n\n- I also tried sending more than one row's text at a time. This cause instability\nin the number of results. For example sending 5 at a time, sometimes returned 7\nor 8. Even sending 2 was not stable. \n\nThis is what the new table looks like:\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nreviews_llm\n#> # A tibble: 100 × 3\n#> review sentiment predicted\n#> \n#> 1 \"i got this as both a book and an audio file… 1 1\n#> 2 \"this book places too much emphasis on spend… 1 1\n#> 3 \"remember the hollywood blacklist? the holly… 2 2\n#> 4 \"while i appreciate what tipler was attempti… 1 1\n#> 5 \"the others in the series were great, and i … 1 1\n#> 6 \"a few good things, but she's lost her edge … 1 1\n#> 7 \"words cannot describe how ripped off and di… 1 1\n#> 8 \"1. the persective of most writers is shaped… 1 NA\n#> 9 \"i have been a huge fan of michael crichton … 1 1\n#> 10 \"i saw dr. polk on c-span a month or two ago… 2 2\n#> # ℹ 90 more rows\n```\n:::\n\n\n\nI used `yardstick` to see how well the model performed. Of course, the accuracy\nwill not be of the \"truth\", but rather the package's results recorded in \n`sentiment`.\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(forcats)\n\nreviews_llm |>\n mutate(predicted = as.factor(predicted)) |>\n yardstick::accuracy(sentiment, predicted)\n#> # A tibble: 1 × 3\n#> .metric .estimator .estimate\n#> \n#> 1 accuracy binary 0.980\n```\n:::\n\n\n\n## Vector functions (R only)\n\n`mall` includes functions that expect a vector, instead of a table, to run the\npredictions. This should make it easier to test things, such as custom prompts\nor results of specific text. Each `llm_` function has a corresponding `llm_vec_`\nfunction:\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nllm_vec_sentiment(\"I am happy\")\n#> [1] \"positive\"\n```\n:::\n\n::: {.cell}\n\n```{.r .cell-code}\nllm_vec_translate(\"Este es el mejor dia!\", \"english\")\n#> [1] \"It's the best day!\"\n```\n:::\n", + "supporting": [], + "filters": [ + "rmarkdown/pagebreak.lua" + ], + "includes": {}, + "engineDependencies": {}, + "preserve": {}, + "postProcess": true + } +} \ No newline at end of file diff --git a/_freeze/reference/llm_classify/execute-results/html.json b/_freeze/reference/llm_classify/execute-results/html.json index 82d68b1..abcbed0 100644 --- a/_freeze/reference/llm_classify/execute-results/html.json +++ b/_freeze/reference/llm_classify/execute-results/html.json @@ -2,7 +2,7 @@ "hash": "2654553ad72a6ca1b62748f913913568", "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## Examples\n\n\n::: {.cell}\n\n```{.r .cell-code}\n \nlibrary(mall) \n \ndata(\"reviews\") \n \nllm_use(\"ollama\", \"llama3.2\", seed = 100, .silent = TRUE) \n \nllm_classify(reviews, review, c(\"appliance\", \"computer\")) \n#> # A tibble: 3 × 2\n#> review .classify\n#> \n#> 1 This has been the best TV I've ever used. Gr… computer \n#> 2 I regret buying this laptop. It is too slow … computer \n#> 3 Not sure how to feel about my new washing ma… 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#> \n#> 1 This has been the best TV I've ever used. Gr… computer \n#> 2 I regret buying this laptop. It is too slow … computer \n#> 3 Not sure how to feel about my new washing ma… 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#> \n#> 1 This has been the best TV I've ever used. Great screen, and sound. 1\n#> 2 I regret buying this laptop. It is too slow and the keyboard is too… 2\n#> 3 Not sure how to feel about my new washing machine. Great color, but… 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\" \"urgent\"\n \n# To preview the first call that will be made to the downstream R 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.2\", seed = 100)\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\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\n::: {.cell}\n\n```{.r .cell-code}\n \nlibrary(mall) \n \ndata(\"reviews\") \n \nllm_use(\"ollama\", \"llama3.2\", seed = 100, .silent = TRUE) \n \nllm_classify(reviews, review, c(\"appliance\", \"computer\")) \n#> # A tibble: 3 × 2\n#> review .classify\n#> \n#> 1 This has been the best TV I've ever used. Gr… computer \n#> 2 I regret buying this laptop. It is too slow … computer \n#> 3 Not sure how to feel about my new washing ma… 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#> \n#> 1 This has been the best TV I've ever used. Gr… computer \n#> 2 I regret buying this laptop. It is too slow … computer \n#> 3 Not sure how to feel about my new washing ma… 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#> \n#> 1 This has been the best TV I've ever used. Great screen, and sound. 1\n#> 2 I regret buying this laptop. It is too slow and the keyboard is too… 2\n#> 3 Not sure how to feel about my new washing machine. Great color, but… 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\" \"urgent\"\n \n# To preview the first call that will be made to the downstream R 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.2\", seed = 100)\n```\n:::\n", "supporting": [], "filters": [ "rmarkdown/pagebreak.lua" diff --git a/_freeze/reference/llm_custom/execute-results/html.json b/_freeze/reference/llm_custom/execute-results/html.json index e746465..c85f7b9 100644 --- a/_freeze/reference/llm_custom/execute-results/html.json +++ b/_freeze/reference/llm_custom/execute-results/html.json @@ -2,7 +2,7 @@ "hash": "9f9fb9cfdaebdc5ea55c78df85881f4f", "result": { "engine": "knitr", - "markdown": "---\ntitle: \"Send a custom prompt to the LLM\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n[R/llm-custom.R](https://github.com/edgararuiz/mall/blob/main/R/llm-custom.R)\n\n## llm_custom\n\n## Description\n Use a Large Language Model (LLM) to process the provided text using the instructions from `prompt` \n\n\n## Usage\n```r\n \nllm_custom(.data, col, prompt = \"\", pred_name = \".pred\", valid_resps = \"\") \n \nllm_vec_custom(x, prompt = \"\", valid_resps = NULL) \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| prompt | The prompt to append to each record sent to the LLM |\n| pred_name | A character vector with the name of the new column where the prediction will be placed |\n| valid_resps | If the response from the LLM is not open, but deterministic, provide the options in a vector. This function will set to `NA` any response not in the options |\n| x | A vector that contains the text to be analyzed |\n\n\n\n## Value\n `llm_custom` returns a `data.frame` or `tbl` object. `llm_vec_custom` 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 \ndata(\"reviews\") \n \nllm_use(\"ollama\", \"llama3.2\", seed = 100, .silent = TRUE) \n \nmy_prompt <- paste( \n \"Answer a question.\", \n \"Return only the answer, no explanation\", \n \"Acceptable answers are 'yes', 'no'\", \n \"Answer this about the following text, is this a happy customer?:\" \n) \n \nreviews |> \n llm_custom(review, my_prompt) \n#> # A tibble: 3 × 2\n#> review .pred\n#> \n#> 1 This has been the best TV I've ever used. Great screen, and sound. Yes \n#> 2 I regret buying this laptop. It is too slow and the keyboard is too noi… No \n#> 3 Not sure how to feel about my new washing machine. Great color, but har… No\n```\n:::\n", + "markdown": "---\ntitle: \"Send a custom prompt to the LLM\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n\n[R/llm-custom.R](https://github.com/edgararuiz/mall/blob/main/R/llm-custom.R)\n\n## llm_custom\n\n## Description\n Use a Large Language Model (LLM) to process the provided text using the instructions from `prompt` \n\n\n## Usage\n```r\n \nllm_custom(.data, col, prompt = \"\", pred_name = \".pred\", valid_resps = \"\") \n \nllm_vec_custom(x, prompt = \"\", valid_resps = NULL) \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| prompt | The prompt to append to each record sent to the LLM |\n| pred_name | A character vector with the name of the new column where the prediction will be placed |\n| valid_resps | If the response from the LLM is not open, but deterministic, provide the options in a vector. This function will set to `NA` any response not in the options |\n| x | A vector that contains the text to be analyzed |\n\n\n\n## Value\n `llm_custom` returns a `data.frame` or `tbl` object. `llm_vec_custom` returns a vector that is the same length as `x`. \n\n\n## Examples\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n \nlibrary(mall) \n \ndata(\"reviews\") \n \nllm_use(\"ollama\", \"llama3.2\", seed = 100, .silent = TRUE) \n \nmy_prompt <- paste( \n \"Answer a question.\", \n \"Return only the answer, no explanation\", \n \"Acceptable answers are 'yes', 'no'\", \n \"Answer this about the following text, is this a happy customer?:\" \n) \n \nreviews |> \n llm_custom(review, my_prompt) \n#> # A tibble: 3 × 2\n#> review .pred\n#> \n#> 1 This has been the best TV I've ever used. Great screen, and sound. Yes \n#> 2 I regret buying this laptop. It is too slow and the keyboard is too noi… No \n#> 3 Not sure how to feel about my new washing machine. Great color, but har… No\n```\n:::\n", "supporting": [], "filters": [ "rmarkdown/pagebreak.lua" diff --git a/_freeze/reference/llm_extract/execute-results/html.json b/_freeze/reference/llm_extract/execute-results/html.json index 5ff0d5e..85df9d0 100644 --- a/_freeze/reference/llm_extract/execute-results/html.json +++ b/_freeze/reference/llm_extract/execute-results/html.json @@ -2,7 +2,7 @@ "hash": "fa18360fb7c78438dcd9a82a136ef52a", "result": { "engine": "knitr", - "markdown": "---\ntitle: \"Extract entities from text\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n[R/llm-extract.R](https://github.com/edgararuiz/mall/blob/main/R/llm-extract.R)\n\n## llm_extract\n\n## Description\n Use a Large Language Model (LLM) to extract specific entity, or entities, from the provided text \n\n\n## Usage\n```r\n \nllm_extract( \n .data, \n col, \n labels, \n expand_cols = FALSE, \n additional_prompt = \"\", \n pred_name = \".extract\" \n) \n \nllm_vec_extract(x, labels = c(), 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 vector with the entities to extract from the text |\n| expand_cols | If multiple `labels` are passed, this is a flag that tells the function to create a new column per item in `labels`. If `labels` is a named vector, this function will use those names as the new column names, if not, the function will use a sanitized version of the content as the name. |\n| additional_prompt | Inserts this text into the prompt sent to the LLM |\n| pred_name | A character vector with the name of the new column where the prediction will be placed |\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_extract` returns a `data.frame` or `tbl` object. `llm_vec_extract` 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 \ndata(\"reviews\") \n \nllm_use(\"ollama\", \"llama3.2\", seed = 100, .silent = TRUE) \n \n# Use 'labels' to let the function know what to extract \nllm_extract(reviews, review, labels = \"product\") \n#> # A tibble: 3 × 2\n#> review .extract \n#> \n#> 1 This has been the best TV I've ever used. Gr… tv \n#> 2 I regret buying this laptop. It is too slow … laptop \n#> 3 Not sure how to feel about my new washing ma… washing machine\n \n# Use 'pred_name' to customize the new column's name \nllm_extract(reviews, review, \"product\", pred_name = \"prod\") \n#> # A tibble: 3 × 2\n#> review prod \n#> \n#> 1 This has been the best TV I've ever used. Gr… tv \n#> 2 I regret buying this laptop. It is too slow … laptop \n#> 3 Not sure how to feel about my new washing ma… washing machine\n \n# Pass a vector to request multiple things, the results will be pipe delimeted \n# in a single column \nllm_extract(reviews, review, c(\"product\", \"feelings\")) \n#> # A tibble: 3 × 2\n#> review .extract \n#> \n#> 1 This has been the best TV I've ever used. Gr… tv | great \n#> 2 I regret buying this laptop. It is too slow … laptop|frustration \n#> 3 Not sure how to feel about my new washing ma… washing machine | confusion\n \n# To get multiple columns, use 'expand_cols' \nllm_extract(reviews, review, c(\"product\", \"feelings\"), expand_cols = TRUE) \n#> # A tibble: 3 × 3\n#> review product feelings \n#> \n#> 1 This has been the best TV I've ever used. Gr… \"tv \" \" great\" \n#> 2 I regret buying this laptop. It is too slow … \"laptop\" \"frustration\"\n#> 3 Not sure how to feel about my new washing ma… \"washing machine \" \" confusion\"\n \n# Pass a named vector to set the resulting column names \nllm_extract( \n .data = reviews, \n col = review, \n labels = c(prod = \"product\", feels = \"feelings\"), \n expand_cols = TRUE \n) \n#> # A tibble: 3 × 3\n#> review prod feels \n#> \n#> 1 This has been the best TV I've ever used. Gr… \"tv \" \" great\" \n#> 2 I regret buying this laptop. It is too slow … \"laptop\" \"frustration\"\n#> 3 Not sure how to feel about my new washing ma… \"washing machine \" \" confusion\"\n \n# For character vectors, instead of a data frame, use this function \nllm_vec_extract(\"bob smith, 123 3rd street\", c(\"name\", \"address\")) \n#> [1] \"bob smith | 123 3rd street\"\n \n# To preview the first call that will be made to the downstream R function \nllm_vec_extract( \n \"bob smith, 123 3rd street\", \n c(\"name\", \"address\"), \n preview = TRUE \n) \n#> ollamar::chat(messages = list(list(role = \"user\", content = \"You are a helpful text extraction engine. Extract the name, address being referred to on the text. I expect 2 items exactly. No capitalization. No explanations. Return the response exclusively in a pipe separated list, and no headers. The answer is based on the following text:\\nbob smith, 123 3rd street\")), \n#> output = \"text\", model = \"llama3.2\", seed = 100)\n```\n:::\n", + "markdown": "---\ntitle: \"Extract entities from text\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n\n[R/llm-extract.R](https://github.com/edgararuiz/mall/blob/main/R/llm-extract.R)\n\n## llm_extract\n\n## Description\n Use a Large Language Model (LLM) to extract specific entity, or entities, from the provided text \n\n\n## Usage\n```r\n \nllm_extract( \n .data, \n col, \n labels, \n expand_cols = FALSE, \n additional_prompt = \"\", \n pred_name = \".extract\" \n) \n \nllm_vec_extract(x, labels = c(), 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 vector with the entities to extract from the text |\n| expand_cols | If multiple `labels` are passed, this is a flag that tells the function to create a new column per item in `labels`. If `labels` is a named vector, this function will use those names as the new column names, if not, the function will use a sanitized version of the content as the name. |\n| additional_prompt | Inserts this text into the prompt sent to the LLM |\n| pred_name | A character vector with the name of the new column where the prediction will be placed |\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_extract` returns a `data.frame` or `tbl` object. `llm_vec_extract` returns a vector that is the same length as `x`. \n\n\n## Examples\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n \nlibrary(mall) \n \ndata(\"reviews\") \n \nllm_use(\"ollama\", \"llama3.2\", seed = 100, .silent = TRUE) \n \n# Use 'labels' to let the function know what to extract \nllm_extract(reviews, review, labels = \"product\") \n#> # A tibble: 3 × 2\n#> review .extract \n#> \n#> 1 This has been the best TV I've ever used. Gr… tv \n#> 2 I regret buying this laptop. It is too slow … laptop \n#> 3 Not sure how to feel about my new washing ma… washing machine\n \n# Use 'pred_name' to customize the new column's name \nllm_extract(reviews, review, \"product\", pred_name = \"prod\") \n#> # A tibble: 3 × 2\n#> review prod \n#> \n#> 1 This has been the best TV I've ever used. Gr… tv \n#> 2 I regret buying this laptop. It is too slow … laptop \n#> 3 Not sure how to feel about my new washing ma… washing machine\n \n# Pass a vector to request multiple things, the results will be pipe delimeted \n# in a single column \nllm_extract(reviews, review, c(\"product\", \"feelings\")) \n#> # A tibble: 3 × 2\n#> review .extract \n#> \n#> 1 This has been the best TV I've ever used. Gr… tv | great \n#> 2 I regret buying this laptop. It is too slow … laptop|frustration \n#> 3 Not sure how to feel about my new washing ma… washing machine | confusion\n \n# To get multiple columns, use 'expand_cols' \nllm_extract(reviews, review, c(\"product\", \"feelings\"), expand_cols = TRUE) \n#> # A tibble: 3 × 3\n#> review product feelings \n#> \n#> 1 This has been the best TV I've ever used. Gr… \"tv \" \" great\" \n#> 2 I regret buying this laptop. It is too slow … \"laptop\" \"frustration\"\n#> 3 Not sure how to feel about my new washing ma… \"washing machine \" \" confusion\"\n \n# Pass a named vector to set the resulting column names \nllm_extract( \n .data = reviews, \n col = review, \n labels = c(prod = \"product\", feels = \"feelings\"), \n expand_cols = TRUE \n) \n#> # A tibble: 3 × 3\n#> review prod feels \n#> \n#> 1 This has been the best TV I've ever used. Gr… \"tv \" \" great\" \n#> 2 I regret buying this laptop. It is too slow … \"laptop\" \"frustration\"\n#> 3 Not sure how to feel about my new washing ma… \"washing machine \" \" confusion\"\n \n# For character vectors, instead of a data frame, use this function \nllm_vec_extract(\"bob smith, 123 3rd street\", c(\"name\", \"address\")) \n#> [1] \"bob smith | 123 3rd street\"\n \n# To preview the first call that will be made to the downstream R function \nllm_vec_extract( \n \"bob smith, 123 3rd street\", \n c(\"name\", \"address\"), \n preview = TRUE \n) \n#> ollamar::chat(messages = list(list(role = \"user\", content = \"You are a helpful text extraction engine. Extract the name, address being referred to on the text. I expect 2 items exactly. No capitalization. No explanations. Return the response exclusively in a pipe separated list, and no headers. The answer is based on the following text:\\nbob smith, 123 3rd street\")), \n#> output = \"text\", model = \"llama3.2\", seed = 100)\n```\n:::\n", "supporting": [], "filters": [ "rmarkdown/pagebreak.lua" diff --git a/_freeze/reference/llm_sentiment/execute-results/html.json b/_freeze/reference/llm_sentiment/execute-results/html.json index bff4d77..5247b0c 100644 --- a/_freeze/reference/llm_sentiment/execute-results/html.json +++ b/_freeze/reference/llm_sentiment/execute-results/html.json @@ -2,7 +2,7 @@ "hash": "ce5d3cf8515ce8aee247eeb4715bcbc0", "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}\n \nlibrary(mall) \n \ndata(\"reviews\") \n \nllm_use(\"ollama\", \"llama3.2\", seed = 100, .silent = TRUE) \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 used. Great screen, and sound. positive \n#> 2 I regret buying this laptop. It is too slow and the keyboard is to… negative \n#> 3 Not sure how to feel about my new washing machine. Great color, bu… 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#> \n#> 1 This has been the best TV I've ever used. Great screen, and … positive \n#> 2 I regret buying this laptop. It is too slow and the keyboard… negative \n#> 3 Not sure how to feel about my new washing machine. Great col… 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 used. Great screen, and sound. positive \n#> 2 I regret buying this laptop. It is too slow and the keyboard is to… negative \n#> 3 Not sure how to feel about my new washing machine. Great color, bu… 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 used. Great screen, and sound. 1\n#> 2 I regret buying this laptop. It is too slow and the keyboard is to… 0\n#> 3 Not sure how to feel about my new washing machine. Great color, bu… 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# To preview the first call that will be made to the downstream R 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.2\", seed = 100)\n```\n:::\n", + "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\n::: {.cell}\n\n```{.r .cell-code}\n \nlibrary(mall) \n \ndata(\"reviews\") \n \nllm_use(\"ollama\", \"llama3.2\", seed = 100, .silent = TRUE) \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 used. Great screen, and sound. positive \n#> 2 I regret buying this laptop. It is too slow and the keyboard is to… negative \n#> 3 Not sure how to feel about my new washing machine. Great color, bu… 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#> \n#> 1 This has been the best TV I've ever used. Great screen, and … positive \n#> 2 I regret buying this laptop. It is too slow and the keyboard… negative \n#> 3 Not sure how to feel about my new washing machine. Great col… 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 used. Great screen, and sound. positive \n#> 2 I regret buying this laptop. It is too slow and the keyboard is to… negative \n#> 3 Not sure how to feel about my new washing machine. Great color, bu… 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 used. Great screen, and sound. 1\n#> 2 I regret buying this laptop. It is too slow and the keyboard is to… 0\n#> 3 Not sure how to feel about my new washing machine. Great color, bu… 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# To preview the first call that will be made to the downstream R 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.2\", seed = 100)\n```\n:::\n", "supporting": [], "filters": [ "rmarkdown/pagebreak.lua" diff --git a/_freeze/reference/llm_summarize/execute-results/html.json b/_freeze/reference/llm_summarize/execute-results/html.json index 97d568e..78fa764 100644 --- a/_freeze/reference/llm_summarize/execute-results/html.json +++ b/_freeze/reference/llm_summarize/execute-results/html.json @@ -2,7 +2,7 @@ "hash": "7dcf1326b18f3451fa4dc840a052e68e", "result": { "engine": "knitr", - "markdown": "---\ntitle: \"Summarize text\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n[R/llm-summarize.R](https://github.com/edgararuiz/mall/blob/main/R/llm-summarize.R)\n\n## llm_summarize\n\n## Description\n Use a Large Language Model (LLM) to summarize text \n\n\n## Usage\n```r\n \nllm_summarize( \n .data, \n col, \n max_words = 10, \n pred_name = \".summary\", \n additional_prompt = \"\" \n) \n \nllm_vec_summarize(x, max_words = 10, 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| max_words | The maximum number of words that the LLM should use in the summary. Defaults to 10. |\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_summarize` returns a `data.frame` or `tbl` object. `llm_vec_summarize` 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 \ndata(\"reviews\") \n \nllm_use(\"ollama\", \"llama3.2\", seed = 100, .silent = TRUE) \n \n# Use max_words to set the maximum number of words to use for the summary \nllm_summarize(reviews, review, max_words = 5) \n#> # A tibble: 3 × 2\n#> review .summary \n#> \n#> 1 This has been the best TV I've ever used. Gr… it's a great tv \n#> 2 I regret buying this laptop. It is too slow … laptop purchase was a mistake \n#> 3 Not sure how to feel about my new washing ma… having mixed feelings about it\n \n# Use 'pred_name' to customize the new column's name \nllm_summarize(reviews, review, 5, pred_name = \"review_summary\") \n#> # A tibble: 3 × 2\n#> review review_summary \n#> \n#> 1 This has been the best TV I've ever used. Gr… it's a great tv \n#> 2 I regret buying this laptop. It is too slow … laptop purchase was a mistake \n#> 3 Not sure how to feel about my new washing ma… having mixed feelings about it\n \n# For character vectors, instead of a data frame, use this function \nllm_vec_summarize( \n \"This has been the best TV I've ever used. Great screen, and sound.\", \n max_words = 5 \n) \n#> [1] \"it's a great tv\"\n \n# To preview the first call that will be made to the downstream R function \nllm_vec_summarize( \n \"This has been the best TV I've ever used. Great screen, and sound.\", \n max_words = 5, \n preview = TRUE \n) \n#> ollamar::chat(messages = list(list(role = \"user\", content = \"You are a helpful summarization engine. Your answer will contain no no capitalization and no explanations. Return no more than 5 words. The answer is the summary of the following text:\\nThis has been the best TV I've ever used. Great screen, and sound.\")), \n#> output = \"text\", model = \"llama3.2\", seed = 100)\n```\n:::\n", + "markdown": "---\ntitle: \"Summarize text\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n\n[R/llm-summarize.R](https://github.com/edgararuiz/mall/blob/main/R/llm-summarize.R)\n\n## llm_summarize\n\n## Description\n Use a Large Language Model (LLM) to summarize text \n\n\n## Usage\n```r\n \nllm_summarize( \n .data, \n col, \n max_words = 10, \n pred_name = \".summary\", \n additional_prompt = \"\" \n) \n \nllm_vec_summarize(x, max_words = 10, 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| max_words | The maximum number of words that the LLM should use in the summary. Defaults to 10. |\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_summarize` returns a `data.frame` or `tbl` object. `llm_vec_summarize` returns a vector that is the same length as `x`. \n\n\n## Examples\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n \nlibrary(mall) \n \ndata(\"reviews\") \n \nllm_use(\"ollama\", \"llama3.2\", seed = 100, .silent = TRUE) \n \n# Use max_words to set the maximum number of words to use for the summary \nllm_summarize(reviews, review, max_words = 5) \n#> # A tibble: 3 × 2\n#> review .summary \n#> \n#> 1 This has been the best TV I've ever used. Gr… it's a great tv \n#> 2 I regret buying this laptop. It is too slow … laptop purchase was a mistake \n#> 3 Not sure how to feel about my new washing ma… having mixed feelings about it\n \n# Use 'pred_name' to customize the new column's name \nllm_summarize(reviews, review, 5, pred_name = \"review_summary\") \n#> # A tibble: 3 × 2\n#> review review_summary \n#> \n#> 1 This has been the best TV I've ever used. Gr… it's a great tv \n#> 2 I regret buying this laptop. It is too slow … laptop purchase was a mistake \n#> 3 Not sure how to feel about my new washing ma… having mixed feelings about it\n \n# For character vectors, instead of a data frame, use this function \nllm_vec_summarize( \n \"This has been the best TV I've ever used. Great screen, and sound.\", \n max_words = 5 \n) \n#> [1] \"it's a great tv\"\n \n# To preview the first call that will be made to the downstream R function \nllm_vec_summarize( \n \"This has been the best TV I've ever used. Great screen, and sound.\", \n max_words = 5, \n preview = TRUE \n) \n#> ollamar::chat(messages = list(list(role = \"user\", content = \"You are a helpful summarization engine. Your answer will contain no no capitalization and no explanations. Return no more than 5 words. The answer is the summary of the following text:\\nThis has been the best TV I've ever used. Great screen, and sound.\")), \n#> output = \"text\", model = \"llama3.2\", seed = 100)\n```\n:::\n", "supporting": [], "filters": [ "rmarkdown/pagebreak.lua" diff --git a/_freeze/reference/llm_translate/execute-results/html.json b/_freeze/reference/llm_translate/execute-results/html.json index fd5b557..c004058 100644 --- a/_freeze/reference/llm_translate/execute-results/html.json +++ b/_freeze/reference/llm_translate/execute-results/html.json @@ -2,7 +2,7 @@ "hash": "446270788110e4132cda33c384ad9125", "result": { "engine": "knitr", - "markdown": "---\ntitle: \"Translates text to a specific language\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n[R/llm-translate.R](https://github.com/edgararuiz/mall/blob/main/R/llm-translate.R)\n\n## llm_translate\n\n## Description\n Use a Large Language Model (LLM) to translate a text to a specific language \n\n\n## Usage\n```r\n \nllm_translate( \n .data, \n col, \n language, \n pred_name = \".translation\", \n additional_prompt = \"\" \n) \n \nllm_vec_translate(x, language, 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| language | Target language to translate the text to |\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_translate` returns a `data.frame` or `tbl` object. `llm_vec_translate` 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 \ndata(\"reviews\") \n \nllm_use(\"ollama\", \"llama3.2\", seed = 100, .silent = TRUE) \n \n# Pass the desired language to translate to \nllm_translate(reviews, review, \"spanish\") \n#> # A tibble: 3 × 2\n#> review .translation \n#> \n#> 1 This has been the best TV I've ever used. Gr… Esta ha sido la mejor televisió…\n#> 2 I regret buying this laptop. It is too slow … Me arrepiento de comprar este p…\n#> 3 Not sure how to feel about my new washing ma… No estoy seguro de cómo me sien…\n```\n:::\n", + "markdown": "---\ntitle: \"Translates text to a specific language\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n\n[R/llm-translate.R](https://github.com/edgararuiz/mall/blob/main/R/llm-translate.R)\n\n## llm_translate\n\n## Description\n Use a Large Language Model (LLM) to translate a text to a specific language \n\n\n## Usage\n```r\n \nllm_translate( \n .data, \n col, \n language, \n pred_name = \".translation\", \n additional_prompt = \"\" \n) \n \nllm_vec_translate(x, language, 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| language | Target language to translate the text to |\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_translate` returns a `data.frame` or `tbl` object. `llm_vec_translate` returns a vector that is the same length as `x`. \n\n\n## Examples\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n \nlibrary(mall) \n \ndata(\"reviews\") \n \nllm_use(\"ollama\", \"llama3.2\", seed = 100, .silent = TRUE) \n \n# Pass the desired language to translate to \nllm_translate(reviews, review, \"spanish\") \n#> # A tibble: 3 × 2\n#> review .translation \n#> \n#> 1 This has been the best TV I've ever used. Gr… Esta ha sido la mejor televisió…\n#> 2 I regret buying this laptop. It is too slow … Me arrepiento de comprar este p…\n#> 3 Not sure how to feel about my new washing ma… No estoy seguro de cómo me sien…\n```\n:::\n", "supporting": [], "filters": [ "rmarkdown/pagebreak.lua" diff --git a/_freeze/reference/llm_use/execute-results/html.json b/_freeze/reference/llm_use/execute-results/html.json index 71920e3..809195a 100644 --- a/_freeze/reference/llm_use/execute-results/html.json +++ b/_freeze/reference/llm_use/execute-results/html.json @@ -2,7 +2,7 @@ "hash": "84eedf7eec066709f406e09aee9d91c6", "result": { "engine": "knitr", - "markdown": "---\ntitle: \"Specify the model to use\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n[R/llm-use.R](https://github.com/edgararuiz/mall/blob/main/R/llm-use.R)\n\n## llm_use\n\n## Description\n Allows us to specify the back-end provider, model to use during the current R session \n\n\n## Usage\n```r\n \nllm_use( \n backend = NULL, \n model = NULL, \n ..., \n .silent = FALSE, \n .cache = NULL, \n .force = FALSE \n) \n```\n\n## Arguments\n|Arguments|Description|\n|---|---|\n| backend | The name of an supported back-end provider. Currently only 'ollama' is supported. |\n| model | The name of model supported by the back-end provider |\n| ... | Additional arguments that this function will pass down to the integrating function. In the case of Ollama, it will pass those arguments to `ollamar::chat()`. |\n| .silent | Avoids console output |\n| .cache | The path to save model results, so they can be re-used if the same operation is ran again. To turn off, set this argument to an empty character: `\"\"`. 'It defaults to '_mall_cache'. If this argument is left `NULL` when calling this function, no changes to the path will be made. |\n| .force | Flag that tell the function to reset all of the settings in the R session |\n\n\n\n## Value\n A `mall_session` object \n\n\n## Examples\n\n\n::: {.cell}\n\n```{.r .cell-code}\n \nlibrary(mall) \n \nllm_use(\"ollama\", \"llama3.2\") \n#> \n#> ── mall session object\n#> Backend: ollama\n#> LLM session: model:llama3.2\n#> R session: cache_folder:_mall_cache\n \n# Additional arguments will be passed 'as-is' to the \n# downstream R function in this example, to ollama::chat() \nllm_use(\"ollama\", \"llama3.2\", seed = 100, temp = 0.1) \n#> \n#> ── mall session object \n#> Backend: ollamaLLM session: model:llama3.2\n#> seed:100\n#> temp:0.1\n#> R session: cache_folder:_mall_cache\n \n# During the R session, you can change any argument \n# individually and it will retain all of previous \n# arguments used \nllm_use(temp = 0.3) \n#> \n#> ── mall session object \n#> Backend: ollamaLLM session: model:llama3.2\n#> seed:100\n#> temp:0.3\n#> R session: cache_folder:_mall_cache\n \n# Use .cache to modify the target folder for caching \nllm_use(.cache = \"_my_cache\") \n#> \n#> ── mall session object \n#> Backend: ollamaLLM session: model:llama3.2\n#> seed:100\n#> temp:0.3\n#> R session: cache_folder:_my_cache\n \n# Leave .cache empty to turn off this functionality \nllm_use(.cache = \"\") \n#> \n#> ── mall session object \n#> Backend: ollamaLLM session: model:llama3.2\n#> seed:100\n#> temp:0.3\n \n# Use .silent to avoid the print out \nllm_use(.silent = TRUE) \n \n```\n:::\n", + "markdown": "---\ntitle: \"Specify the model to use\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n\n[R/llm-use.R](https://github.com/edgararuiz/mall/blob/main/R/llm-use.R)\n\n## llm_use\n\n## Description\n Allows us to specify the back-end provider, model to use during the current R session \n\n\n## Usage\n```r\n \nllm_use( \n backend = NULL, \n model = NULL, \n ..., \n .silent = FALSE, \n .cache = NULL, \n .force = FALSE \n) \n```\n\n## Arguments\n|Arguments|Description|\n|---|---|\n| backend | The name of an supported back-end provider. Currently only 'ollama' is supported. |\n| model | The name of model supported by the back-end provider |\n| ... | Additional arguments that this function will pass down to the integrating function. In the case of Ollama, it will pass those arguments to `ollamar::chat()`. |\n| .silent | Avoids console output |\n| .cache | The path to save model results, so they can be re-used if the same operation is ran again. To turn off, set this argument to an empty character: `\"\"`. 'It defaults to '_mall_cache'. If this argument is left `NULL` when calling this function, no changes to the path will be made. |\n| .force | Flag that tell the function to reset all of the settings in the R session |\n\n\n\n## Value\n A `mall_session` object \n\n\n## Examples\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n \nlibrary(mall) \n \nllm_use(\"ollama\", \"llama3.2\") \n#> \n#> ── mall session object\n#> Backend: ollama\n#> LLM session: model:llama3.2\n#> R session: cache_folder:_mall_cache\n \n# Additional arguments will be passed 'as-is' to the \n# downstream R function in this example, to ollama::chat() \nllm_use(\"ollama\", \"llama3.2\", seed = 100, temp = 0.1) \n#> \n#> ── mall session object \n#> Backend: ollamaLLM session: model:llama3.2\n#> seed:100\n#> temp:0.1\n#> R session: cache_folder:_mall_cache\n \n# During the R session, you can change any argument \n# individually and it will retain all of previous \n# arguments used \nllm_use(temp = 0.3) \n#> \n#> ── mall session object \n#> Backend: ollamaLLM session: model:llama3.2\n#> seed:100\n#> temp:0.3\n#> R session: cache_folder:_mall_cache\n \n# Use .cache to modify the target folder for caching \nllm_use(.cache = \"_my_cache\") \n#> \n#> ── mall session object \n#> Backend: ollamaLLM session: model:llama3.2\n#> seed:100\n#> temp:0.3\n#> R session: cache_folder:_my_cache\n \n# Leave .cache empty to turn off this functionality \nllm_use(.cache = \"\") \n#> \n#> ── mall session object \n#> Backend: ollamaLLM session: model:llama3.2\n#> seed:100\n#> temp:0.3\n \n# Use .silent to avoid the print out \nllm_use(.silent = TRUE) \n \n```\n:::\n", "supporting": [], "filters": [ "rmarkdown/pagebreak.lua" diff --git a/_freeze/reference/m_backend_submit/execute-results/html.json b/_freeze/reference/m_backend_submit/execute-results/html.json index 80e9b0d..cda5d01 100644 --- a/_freeze/reference/m_backend_submit/execute-results/html.json +++ b/_freeze/reference/m_backend_submit/execute-results/html.json @@ -2,7 +2,7 @@ "hash": "ae407edd991c3bb6d06e7b77c3db287a", "result": { "engine": "knitr", - "markdown": "---\ntitle: \"Functions to integrate different back-ends\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n[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)\n\n## m_backend_prompt\n\n## Description\n Functions to integrate different back-ends \n\n\n## Usage\n```r\n \nm_backend_prompt(backend, additional) \n \nm_backend_submit(backend, x, prompt, preview = FALSE) \n```\n\n## Arguments\n|Arguments|Description|\n|---|---|\n| backend | An `mall_session` object |\n| additional | Additional text to insert to the `base_prompt` |\n| x | The body of the text to be submitted to the LLM |\n| prompt | The additional information to add to the submission |\n| preview | If `TRUE`, it will display the resulting R call of the first text in `x` |\n\n\n\n## Value\n `m_backend_submit` does not return an object. `m_backend_prompt` returns a list of functions that contain the base prompts. \n\n\n\n\n", + "markdown": "---\ntitle: \"Functions to integrate different back-ends\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n\n[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)\n\n## m_backend_prompt\n\n## Description\n Functions to integrate different back-ends \n\n\n## Usage\n```r\n \nm_backend_prompt(backend, additional) \n \nm_backend_submit(backend, x, prompt, preview = FALSE) \n```\n\n## Arguments\n|Arguments|Description|\n|---|---|\n| backend | An `mall_session` object |\n| additional | Additional text to insert to the `base_prompt` |\n| x | The body of the text to be submitted to the LLM |\n| prompt | The additional information to add to the submission |\n| preview | If `TRUE`, it will display the resulting R call of the first text in `x` |\n\n\n\n## Value\n `m_backend_submit` does not return an object. `m_backend_prompt` returns a list of functions that contain the base prompts. \n\n\n\n\n", "supporting": [], "filters": [ "rmarkdown/pagebreak.lua" diff --git a/_freeze/reference/reviews/execute-results/html.json b/_freeze/reference/reviews/execute-results/html.json index 96864e7..867c255 100644 --- a/_freeze/reference/reviews/execute-results/html.json +++ b/_freeze/reference/reviews/execute-results/html.json @@ -2,7 +2,7 @@ "hash": "e141f1285e2ad9e09a5a074db4ed673f", "result": { "engine": "knitr", - "markdown": "---\ntitle: \"Mini reviews data set\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n[R/data-reviews.R](https://github.com/edgararuiz/mall/blob/main/R/data-reviews.R)\n\n## reviews\n\n## Description\n Mini reviews data set \n\n## Format\n A data frame that contains 3 records. The records are of fictitious product reviews. \n\n## Usage\n```r\n \nreviews \n```\n\n\n\n\n\n\n## Examples\n\n\n::: {.cell}\n\n```{.r .cell-code}\n \nlibrary(mall) \ndata(reviews) \nreviews \n#> # A tibble: 3 × 1\n#> review \n#> \n#> 1 This has been the best TV I've ever used. Great screen, and sound. \n#> 2 I regret buying this laptop. It is too slow and the keyboard is too noisy \n#> 3 Not sure how to feel about my new washing machine. Great color, but hard to f…\n```\n:::\n", + "markdown": "---\ntitle: \"Mini reviews data set\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n\n[R/data-reviews.R](https://github.com/edgararuiz/mall/blob/main/R/data-reviews.R)\n\n## reviews\n\n## Description\n Mini reviews data set \n\n## Format\n A data frame that contains 3 records. The records are of fictitious product reviews. \n\n## Usage\n```r\n \nreviews \n```\n\n\n\n\n\n\n## Examples\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n \nlibrary(mall) \ndata(reviews) \nreviews \n#> # A tibble: 3 × 1\n#> review \n#> \n#> 1 This has been the best TV I've ever used. Great screen, and sound. \n#> 2 I regret buying this laptop. It is too slow and the keyboard is too noisy \n#> 3 Not sure how to feel about my new washing machine. Great color, but hard to f…\n```\n:::\n", "supporting": [], "filters": [ "rmarkdown/pagebreak.lua" diff --git a/_pkgdown.yml b/_pkgdown.yml deleted file mode 100644 index 3af800a..0000000 --- a/_pkgdown.yml +++ /dev/null @@ -1,13 +0,0 @@ -url: https://edgararuiz.github.io/mall/ -template: - bootstrap: 5 - light-switch: true -navbar: - structure: - right: [search, github, lightswitch] -repo: - url: - home: https://github.com/edgararuiz/mall - source: https://github.com/edgararuiz/mall/blob/HEAD/ - issue: https://github.com/edgararuiz/mall/issues/ - user: https://github.com/ diff --git a/_quarto.yml b/_quarto.yml index 7ed0039..f5d5837 100644 --- a/_quarto.yml +++ b/_quarto.yml @@ -8,9 +8,9 @@ execute: website: title: mall - favicon: "man/figures/favicon/apple-touch-icon.png" + favicon: "figures/favicon/apple-touch-icon.png" navbar: - logo: "man/figures/favicon/apple-touch-icon.png" + logo: "figures/favicon/apple-touch-icon.png" left: - sidebar:articles - href: reference/index.qmd @@ -27,12 +27,13 @@ website: contents: - text: "Caching" href: articles/caching.qmd - - text: "Databricks" + - text: "Databricks (R only)" href: articles/databricks.qmd format: html: toc: true + toc-expand: true code-copy: true code-overflow: wrap code-toos: true @@ -44,4 +45,18 @@ format: knitr: opts_chunk: collapse: true - comment: "#>" \ No newline at end of file + comment: "#>" + +quartodoc: + package: mall + options: null + style: pkgdown + dir: reference + out_index: _api_index.qmd + dynamic: true + sections: + - title: mall + desc: '' + contents: + - name: MallFrame + diff --git a/man/figures/favicon/apple-touch-icon-120x120.png b/figures/favicon/apple-touch-icon-120x120.png similarity index 100% rename from man/figures/favicon/apple-touch-icon-120x120.png rename to figures/favicon/apple-touch-icon-120x120.png diff --git a/man/figures/favicon/apple-touch-icon-152x152.png b/figures/favicon/apple-touch-icon-152x152.png similarity index 100% rename from man/figures/favicon/apple-touch-icon-152x152.png rename to figures/favicon/apple-touch-icon-152x152.png diff --git a/man/figures/favicon/apple-touch-icon-180x180.png b/figures/favicon/apple-touch-icon-180x180.png similarity index 100% rename from man/figures/favicon/apple-touch-icon-180x180.png rename to figures/favicon/apple-touch-icon-180x180.png diff --git a/man/figures/favicon/apple-touch-icon-60x60.png b/figures/favicon/apple-touch-icon-60x60.png similarity index 100% rename from man/figures/favicon/apple-touch-icon-60x60.png rename to figures/favicon/apple-touch-icon-60x60.png diff --git a/man/figures/favicon/apple-touch-icon-76x76.png b/figures/favicon/apple-touch-icon-76x76.png similarity index 100% rename from man/figures/favicon/apple-touch-icon-76x76.png rename to figures/favicon/apple-touch-icon-76x76.png diff --git a/man/figures/favicon/apple-touch-icon.png b/figures/favicon/apple-touch-icon.png similarity index 100% rename from man/figures/favicon/apple-touch-icon.png rename to figures/favicon/apple-touch-icon.png diff --git a/man/figures/favicon/favicon-16x16.png b/figures/favicon/favicon-16x16.png similarity index 100% rename from man/figures/favicon/favicon-16x16.png rename to figures/favicon/favicon-16x16.png diff --git a/man/figures/favicon/favicon-32x32.png b/figures/favicon/favicon-32x32.png similarity index 100% rename from man/figures/favicon/favicon-32x32.png rename to figures/favicon/favicon-32x32.png diff --git a/man/figures/favicon/favicon.ico b/figures/favicon/favicon.ico similarity index 100% rename from man/figures/favicon/favicon.ico rename to figures/favicon/favicon.ico diff --git a/man/figures/logo.png b/figures/logo.png similarity index 100% rename from man/figures/logo.png rename to figures/logo.png diff --git a/man/figures/mall.png b/figures/mall.png similarity index 100% rename from man/figures/mall.png rename to figures/mall.png diff --git a/index.qmd b/index.qmd index 336d2c4..f2d0f9b 100644 --- a/index.qmd +++ b/index.qmd @@ -2,6 +2,559 @@ format: html: toc: true +execute: + eval: true + freeze: true --- -{{< include README.md >}} + +```{r} +#| include: false + + +library(dplyr) +library(dbplyr) +library(tictoc) +library(DBI) +source("utils/knitr-print.R") +mall::llm_use("ollama", "llama3.2", seed = 100, .cache = "_readme_cache") +``` + + + + +Run multiple LLM predictions against a data frame. The predictions are processed +row-wise over a specified column. It works using a pre-determined one-shot prompt, +along with the current row's content. `mall` has been implemented for both R +and Python. The prompt that is use will depend of the type of analysis needed. + +Currently, the included prompts perform the following: + +- [Sentiment analysis](#sentiment) +- [Text summarizing](#summarize) +- [Classify text](#classify) +- [Extract one, or several](#extract), specific pieces information from the text +- [Translate text](#translate) +- [Custom prompt](#custom-prompt) + +This package is inspired by the SQL AI functions now offered by vendors such as +[Databricks](https://docs.databricks.com/en/large-language-models/ai-functions.html) +and Snowflake. `mall` uses [Ollama](https://ollama.com/) to interact with LLMs +installed locally. + + +For R, that interaction takes place via the +[`ollamar`](https://hauselin.github.io/ollama-r/) package. The functions are +designed to easily work with piped commands, such as `dplyr`. + +```r +reviews |> + llm_sentiment(review) +``` + + +For Python, `mall` includes an extension to [Polars](https://pola.rs/). To +interact with Ollama, it uses the official [Python library](https://github.com/ollama/ollama-python). + +```python +reviews.llm.sentiment("review") +``` + +## Motivation + +We want to new find ways to help data scientists use LLMs in their daily work. +Unlike the familiar interfaces, such as chatting and code completion, this interface +runs your text data directly against the LLM. + +The LLM's flexibility, allows for it to adapt to the subject of your data, and +provide surprisingly accurate predictions. This saves the data scientist the +need to write and tune an NLP model. + +In recent times, the capabilities of LLMs that can run locally in your computer +have increased dramatically. This means that these sort of analysis can run +in your machine with good accuracy. Additionally, it makes it possible to take +advantage of LLM's at your institution, since the data will not leave the +corporate network. + +## Get started + +- Install `mall` from Github + + +::: {.panel-tabset group="language"} +## R +```r +pak::pak("edgararuiz/mall/r") +``` + +## python +```python +pip install "mall @ git+https://git@github.com/edgararuiz/mall.git#subdirectory=python" +``` +::: + + +### With local LLMs + +- [Download Ollama from the official website](https://ollama.com/download) + +- Install and start Ollama in your computer + + +::: {.panel-tabset group="language"} +## R +- Install Ollama in your machine. The `ollamar` package's website provides this +[Installation guide](https://hauselin.github.io/ollama-r/#installation) + +- Download an LLM model. For example, I have been developing this package using +Llama 3.2 to test. To get that model you can run: + ```r + ollamar::pull("llama3.2") + ``` + +## python + +- Install the official Ollama library + ```python + pip install ollama + ``` + +- Download an LLM model. For example, I have been developing this package using +Llama 3.2 to test. To get that model you can run: + ```python + import ollama + ollama.pull('llama3.2') + ``` +::: + + + +### With Databricks (R only) + +If you pass a table connected to **Databricks** via `odbc`, `mall` will +automatically use Databricks' LLM instead of Ollama. *You won't need Ollama +installed if you are using Databricks only.* + +`mall` will call the appropriate SQL AI function. For more information see our +[Databricks article.](https://edgararuiz.github.io/mall/articles/databricks.html) + +## LLM functions + +### Sentiment + +Primarily, `mall` provides verb-like functions that expect a data frame as +their first argument. + +We will start with loading a very small data set contained in `mall`. It has +3 product reviews that we will use as the source of our examples. + +::: {.panel-tabset group="language"} +## R + +```{r} +library(mall) + +data("reviews") + +reviews +``` + +## python + +```{python} +#| eval: true +import mall +import polars as pl + +data = mall.MallData + +reviews = data.reviews +reviews +``` +::: + +```{python} +#| include: false + + +reviews.llm.use(options = dict(seed = 100), _cache = "_readme_cache") +``` + +For the first example, we'll asses the sentiment of each review: + +::: {.panel-tabset group="language"} +## R + +```{r} + +reviews |> + llm_sentiment(review) +``` + +## python + +```{python} + +reviews.llm.sentiment("review") +``` + +::: + +We can also provide custom sentiment labels. Use the `options` argument to control +that: + +::: {.panel-tabset group="language"} +## R + +```{r} + +reviews |> + llm_sentiment(review, options = c("positive", "negative")) +``` + +## python + +```{python} + +reviews.llm.sentiment("review", options=["positive", "negative"]) +``` + +::: + +As mentioned before, these functions are create to play well with the rest of +the analysis + +::: {.panel-tabset group="language"} +## R + +```{r} + +reviews |> + llm_sentiment(review, options = c("positive", "negative")) |> + filter(.sentiment == "negative") +``` + +## python + +```{python} + +x = reviews.llm.sentiment("review", options=["positive", "negative"]) + +x.filter(pl.col("sentiment") == "negative") + +``` + +::: + +### Summarize + +There may be a need to reduce the number of words in a given text. Typically to +make it easier to understand its intent. The function has an argument to +control the maximum number of words to output +(`max_words`): + +::: {.panel-tabset group="language"} +## R + +```{r} + + +reviews |> + llm_summarize(review, max_words = 5) +``` + +## python + +```{python} + + +reviews.llm.summarize("review", 5) +``` + +::: + +To control the name of the prediction field, you can change `pred_name` argument. +**This works with the other `llm` functions as well.** + +::: {.panel-tabset group="language"} +## R + +```{r} + +reviews |> + llm_summarize(review, max_words = 5, pred_name = "review_summary") +``` + +## python + +```{python} + +reviews.llm.summarize("review", max_words = 5, pred_name = "review_summary") +``` + +::: + +### Classify + +Use the LLM to categorize the text into one of the options you provide: + + +::: {.panel-tabset group="language"} +## R + +```{r} +reviews |> + llm_classify(review, c("appliance", "computer")) +``` + + +## python + +```{python} + +reviews.llm.classify("review", ["computer", "appliance"]) +``` + +::: + +### Extract + +One of the most interesting use cases Using natural language, we can tell the +LLM to return a specific part of the text. In the following example, we request +that the LLM return the product being referred to. We do this by simply saying +"product". The LLM understands what we *mean* by that word, and looks for that +in the text. + + +::: {.panel-tabset group="language"} +## R + +```{r} +reviews |> + llm_extract(review, "product") +``` + + +## python + +```{python} + +reviews.llm.extract("review", "product") +``` + +::: + + +### Translate + +As the title implies, this function will translate the text into a specified +language. What is really nice, it is that you don't need to specify the language +of the source text. Only the target language needs to be defined. The translation +accuracy will depend on the LLM + +::: {.panel-tabset group="language"} +## R + +```{r} + + +reviews |> + llm_translate(review, "spanish") +``` + + +## python + +```{python} + + +reviews.llm.translate("review", "spanish") +``` + +::: + +### Custom prompt + +It is possible to pass your own prompt to the LLM, and have `mall` run it +against each text entry: + + +::: {.panel-tabset group="language"} +## R + +```{r} +my_prompt <- paste( + "Answer a question.", + "Return only the answer, no explanation", + "Acceptable answers are 'yes', 'no'", + "Answer this about the following text, is this a happy customer?:" +) + +reviews |> + llm_custom(review, my_prompt) +``` + + +## python + +```{python} + +my_prompt = "Answer a question." \ + + "Return only the answer, no explanation" \ + + "Acceptable answers are 'yes', 'no'" \ + + "Answer this about the following text, is this a happy customer?:" + + +reviews.llm.custom("review", prompt = my_prompt) +``` + +::: + +## Model selection and settings + +You can set the model and its options to use when calling the LLM. In this case, +we refer to options as model specific things that can be set, such as seed or +temperature. + +::: {.panel-tabset group="language"} +## R + +Invoking an `llm` function will automatically initialize a model selection +if you don't have one selected yet. If there is only one option, it will +pre-select it for you. If there are more than one available models, then `mall` +will present you as menu selection so you can select which model you wish to +use. + +Calling `llm_use()` directly will let you specify the model and backend to use. +You can also setup additional arguments that will be passed down to the +function that actually runs the prediction. In the case of Ollama, that function +is [`chat()`](https://hauselin.github.io/ollama-r/reference/chat.html). + +The model to use, and other options can be set for the current R session + +```{r} +#| eval: false +llm_use("ollama", "llama3.2", seed = 100, temperature = 0) +``` + + +## python + +The model and options to be used will be defined at the Polars data frame +object level. If not passed, the default model will be **llama3.2**. + +```{python} +#| eval: false +reviews.llm.use(options = dict(seed = 100)) +``` + +::: + +## Key considerations + +The main consideration is **cost**. Either, time cost, or money cost. + +If using this method with an LLM locally available, the cost will be a long +running time. Unless using a very specialized LLM, a given LLM is a general model. +It was fitted using a vast amount of data. So determining a response for each +row, takes longer than if using a manually created NLP model. The default model +used in Ollama is [Llama 3.2](https://ollama.com/library/llama3.2), +which was fitted using 3B parameters. + +If using an external LLM service, the consideration will need to be for the +billing costs of using such service. Keep in mind that you will be sending a lot +of data to be evaluated. + +Another consideration is the novelty of this approach. Early tests are +providing encouraging results. But you, as an user, will still need to keep +in mind that the predictions will not be infallible, so always check the output. +At this time, I think the best use for this method, is for a quick analysis. + +## Performance + +We will briefly cover this methods performance from two perspectives: + +- How long the analysis takes to run locally + +- How well it predicts + +To do so, we will use the `data_bookReviews` data set, provided by the `classmap` +package. For this exercise, only the first 100, of the total 1,000, are going +to be part of this analysis. + +```{r} +library(classmap) + +data(data_bookReviews) + +data_bookReviews |> + glimpse() +``` +As per the docs, `sentiment` is a factor indicating the sentiment of the review: +negative (1) or positive (2) + +```{r} +length(strsplit(paste(head(data_bookReviews$review, 100), collapse = " "), " ")[[1]]) +``` + +Just to get an idea of how much data we're processing, I'm using a very, very +simple word count. So we're analyzing a bit over 20 thousand words. + +```{r} +reviews_llm <- data_bookReviews |> + head(100) |> + llm_sentiment( + col = review, + options = c("positive" ~ 2, "negative" ~ 1), + pred_name = "predicted" + ) +``` + +As far as **time**, on my Apple M3 machine, it took about 1.5 minutes to process, +100 rows, containing 20 thousand words. Setting `temp` to 0 in `llm_use()`, +made the model run faster. + +The package uses `purrr` to send each prompt individually to the LLM. But, I did +try a few different ways to speed up the process, unsuccessfully: + +- Used `furrr` to send multiple requests at a time. This did not work because +either the LLM or Ollama processed all my requests serially. So there was +no improvement. + +- I also tried sending more than one row's text at a time. This cause instability +in the number of results. For example sending 5 at a time, sometimes returned 7 +or 8. Even sending 2 was not stable. + +This is what the new table looks like: + +```{r} +reviews_llm +``` + +I used `yardstick` to see how well the model performed. Of course, the accuracy +will not be of the "truth", but rather the package's results recorded in +`sentiment`. + +```{r} +library(forcats) + +reviews_llm |> + mutate(predicted = as.factor(predicted)) |> + yardstick::accuracy(sentiment, predicted) +``` + +## Vector functions (R only) + +`mall` includes functions that expect a vector, instead of a table, to run the +predictions. This should make it easier to test things, such as custom prompts +or results of specific text. Each `llm_` function has a corresponding `llm_vec_` +function: + +```{r} +llm_vec_sentiment("I am happy") +``` + +```{r} +llm_vec_translate("Este es el mejor dia!", "english") +``` + diff --git a/objects.json b/objects.json new file mode 100644 index 0000000..9d36587 --- /dev/null +++ b/objects.json @@ -0,0 +1 @@ +{"project": "mall", "version": "0.0.9999", "count": 4, "items": [{"name": "mall.MallFrame.translate", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.translate", "dispname": "-"}, {"name": "mall.polars.MallFrame.translate", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.translate", "dispname": "mall.MallFrame.translate"}, {"name": "mall.MallFrame", "domain": "py", "role": "class", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame", "dispname": "-"}, {"name": "mall.polars.MallFrame", "domain": "py", "role": "class", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame", "dispname": "mall.MallFrame"}]} \ No newline at end of file diff --git a/python/MANIFEST.in b/python/MANIFEST.in new file mode 100644 index 0000000..c08dce5 --- /dev/null +++ b/python/MANIFEST.in @@ -0,0 +1 @@ +graft mall diff --git a/python/README.md b/python/README.md new file mode 100644 index 0000000..18194a4 --- /dev/null +++ b/python/README.md @@ -0,0 +1,100 @@ +# mall + +## Intro + +Run multiple LLM predictions against a data frame. The predictions are +processed row-wise over a specified column. It works using a +pre-determined one-shot prompt, along with the current row’s content. + +## Install + +To install from Github, use: + +``` python +pip install "mall @ git+https://git@github.com/edgararuiz/mall.git@python#subdirectory=python" +``` + +## Examples + +``` python +import mall +import polars as pl + +reviews = pl.DataFrame( + data=[ + "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" + ], + schema=[("review", pl.String)], +) +``` + +## Sentiment + + +``` python +reviews.llm.sentiment("review") +``` + +shape: (3, 2) + +| review | sentiment | +|----------------------------------|------------| +| str | str | +| "This has been the best TV I've… | "positive" | +| "I regret buying this laptop. I… | "negative" | +| "Not sure how to feel about my … | "neutral" | + +## Summarize + +``` python +reviews.llm.summarize("review", 5) +``` + +shape: (3, 2) + +| review | summary | +|----------------------------------|----------------------------------| +| str | str | +| "This has been the best TV I've… | "it's a great tv" | +| "I regret buying this laptop. I… | "laptop not worth the money" | +| "Not sure how to feel about my … | "feeling uncertain about new pu… | + +## Translate (as in ‘English to French’) + +``` python +reviews.llm.translate("review", "spanish") +``` + +shape: (3, 2) + +| review | translation | +|----------------------------------|----------------------------------| +| str | str | +| "This has been the best TV I've… | "Esta ha sido la mejor TV que h… | +| "I regret buying this laptop. I… | "Lo lamento comprar este portát… | +| "Not sure how to feel about my … | "No estoy seguro de cómo sentir… | + +## Classify + +``` python +reviews.llm.classify("review", ["computer", "appliance"]) +``` + +shape: (3, 2) + +| review | classify | +|----------------------------------|-------------| +| str | str | +| "This has been the best TV I've… | "appliance" | +| "I regret buying this laptop. I… | "appliance" | +| "Not sure how to feel about my … | "appliance" | + +## LLM session setup + +``` python +reviews.llm.use(options = dict(seed = 100)) +``` + + {'backend': 'ollama', 'model': 'llama3.2', 'options': {'seed': 100}} diff --git a/python/README.qmd b/python/README.qmd new file mode 100644 index 0000000..862f56a --- /dev/null +++ b/python/README.qmd @@ -0,0 +1,71 @@ +--- +format: gfm +--- + +# mall + +## Intro + +Run multiple LLM predictions against a data frame. The predictions are processed row-wise over a specified column. It works using a pre-determined one-shot prompt, along with the current row’s content. + +## Install + +To install from Github, use: + +```python +pip install "mall @ git+https://git@github.com/edgararuiz/mall.git@python#subdirectory=python" +``` + +## Examples + +```{python} +#| include: false +import polars as pl +from polars.dataframe._html import HTMLFormatter +html_formatter = get_ipython().display_formatter.formatters['text/html'] +html_formatter.for_type(pl.DataFrame, lambda df: "\n".join(HTMLFormatter(df).render())) +``` + + +```{python} +import mall +import polars as pl +data = mall.MallData +reviews = data.reviews +``` + +```{python} +#| include: false +reviews.llm.use(options = dict(seed = 100)) +``` + + +## Sentiment + +```{python} +reviews.llm.sentiment("review") +``` + +## Summarize + +```{python} +reviews.llm.summarize("review", 5) +``` + +## Translate (as in 'English to French') + +```{python} +reviews.llm.translate("review", "spanish") +``` + +## Classify + +```{python} +reviews.llm.classify("review", ["computer", "appliance"]) +``` + +## LLM session setup + +```{python} +reviews.llm.use(options = dict(seed = 100)) +``` diff --git a/python/mall/__init__.py b/python/mall/__init__.py new file mode 100644 index 0000000..e623439 --- /dev/null +++ b/python/mall/__init__.py @@ -0,0 +1,4 @@ +__all__ = ["MallFrame", "MallData"] + +from mall.polars import MallFrame +from mall.data import MallData diff --git a/python/mall/data.py b/python/mall/data.py new file mode 100644 index 0000000..73b7b23 --- /dev/null +++ b/python/mall/data.py @@ -0,0 +1,12 @@ +import polars as pl + + +class MallData: + reviews = pl.DataFrame( + data=[ + "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", + ], + schema=[("review", pl.String)], + ) diff --git a/python/mall/llm.py b/python/mall/llm.py new file mode 100644 index 0000000..dc89ee3 --- /dev/null +++ b/python/mall/llm.py @@ -0,0 +1,84 @@ +import ollama +import json +import hashlib +import os + + +def build_msg(x, msg): + out = [] + for msgs in msg: + out.append({"role": msgs["role"], "content": msgs["content"].format(x)}) + return out + + +def llm_call(x, msg, use, preview=False, valid_resps=""): + + call = dict( + model=use.get("model"), + messages=build_msg(x, msg), + options=use.get("options"), + ) + + if preview: + print(call) + + cache = "" + if use.get("_cache") != "": + hash_call = build_hash(call) + cache = cache_check(hash_call, use) + + if cache == "": + resp = ollama.chat( + model=use.get("model"), + messages=build_msg(x, msg), + options=use.get("options"), + ) + out = resp["message"]["content"] + else: + out = cache + + if use.get("_cache") != "": + if cache == "": + cache_record(hash_call, use, call, out) + + if isinstance(valid_resps, list): + if out not in valid_resps: + out = None + return out + + +def build_hash(x): + if isinstance(x, dict): + x = json.dumps(x) + x_sha = hashlib.sha1(x.encode("utf-8")) + x_digest = x_sha.hexdigest() + return x_digest + + +def cache_check(hash_call, use): + file_path = cache_path(hash_call, use) + if os.path.isfile(file_path): + file_connection = open(file_path) + file_read = file_connection.read() + file_parse = json.loads(file_read) + out = file_parse.get("response") + else: + out = "" + return out + + +def cache_record(hash_call, use, call, response): + file_path = cache_path(hash_call, use) + file_folder = os.path.dirname(file_path) + if not os.path.isdir(file_folder): + os.makedirs(file_folder) + contents = dict(request=call, response=response) + json_contents = json.dumps(contents) + with open(file_path, "w") as file: + file.write(json_contents) + + +def cache_path(hash_call, use): + sub_folder = hash_call[0:2] + file_path = use.get("_cache") + "/" + sub_folder + "/" + hash_call + ".json" + return file_path diff --git a/python/mall/polars.py b/python/mall/polars.py new file mode 100644 index 0000000..e7aff5b --- /dev/null +++ b/python/mall/polars.py @@ -0,0 +1,156 @@ +import polars as pl +from mall.prompt import sentiment, summarize, translate, classify, extract, custom +from mall.llm import llm_call + + +@pl.api.register_dataframe_namespace("llm") +class MallFrame: + """Extension to Polars that add ability to use + an LLM to run batch predictions over a data frame + """ + + def __init__(self, df: pl.DataFrame) -> None: + self._df = df + self._use = dict( + backend = "ollama", + model = "llama3.2", + _cache = "_mall_cache" + ) + + def use(self, backend="", model="", _cache = "_mall_cache", **kwargs): + if backend != "": + self._use.update(dict(backend = backend)) + if model != "": + self._use.update(dict(model = model)) + self._use.update(dict(_cache = _cache)) + self._use.update(dict(kwargs)) + return self._use + + def sentiment( + self, + col, + options=["positive", "negative", "neutral"], + additional="", + pred_name="sentiment", + ) -> list[pl.DataFrame]: + df = map_call( + df=self._df, + col=col, + msg=sentiment(options, additional=additional), + pred_name=pred_name, + use=self._use, + valid_resps=options, + ) + return df + + def summarize( + self, + col, + max_words=10, + additional="", + pred_name="summary", + ) -> list[pl.DataFrame]: + df = map_call( + df=self._df, + col=col, + msg=summarize(max_words, additional=additional), + pred_name=pred_name, + use=self._use, + ) + return df + + def translate( + self, + col, + language="", + additional="", + pred_name="translation", + ) -> list[pl.DataFrame]: + """Translate text into another language. + + Parameters + ------ + col: str + The name of the text field to process + + language: str + The target language to translate to. For example 'French'. + + pred_name: str + A character vector with the name of the new column where the + prediction will be placed + + additional: str + Inserts this text into the prompt sent to the LLM + """ + df = map_call( + df=self._df, + col=col, + msg=translate(language, additional=additional), + pred_name=pred_name, + use=self._use, + ) + return df + + def classify( + self, + col, + labels="", + additional="", + pred_name="classify", + ) -> list[pl.DataFrame]: + df = map_call( + df=self._df, + col=col, + msg=classify(labels, additional=additional), + pred_name=pred_name, + use=self._use, + valid_resps=labels, + ) + return df + + def extract( + self, + col, + labels="", + additional="", + pred_name="extract", + ) -> list[pl.DataFrame]: + df = map_call( + df=self._df, + col=col, + msg=extract(labels, additional=additional), + pred_name=pred_name, + use=self._use, + valid_resps=labels, + ) + return df + + def custom( + self, + col, + prompt="", + valid_resps="", + pred_name="custom", + ) -> list[pl.DataFrame]: + df = map_call( + df=self._df, + col=col, + msg=custom(prompt), + pred_name=pred_name, + use=self._use, + valid_resps=valid_resps, + ) + return df + + +def map_call(df, col, msg, pred_name, use, valid_resps=""): + df = df.with_columns( + pl.col(col) + .map_elements( + lambda x: llm_call(x, msg, use, False, valid_resps), + return_dtype=pl.String, + ) + .alias(pred_name) + ) + return df diff --git a/python/mall/prompt.py b/python/mall/prompt.py new file mode 100644 index 0000000..cd7510a --- /dev/null +++ b/python/mall/prompt.py @@ -0,0 +1,122 @@ +def process_labels(x, if_list="", if_dict=""): + if isinstance(x, list): + out = "" + for i in x: + out += " " + i + out = out.strip() + out = out.replace(" ", ", ") + out = if_list.replace("{values}", out) + if isinstance(x, dict): + out = "" + for i in x: + new = if_dict + new = new.replace("{key}", i) + new = new.replace("{value}", x.get(i)) + out += " " + new + return out + + +def sentiment(options, additional=""): + new_options = process_labels( + options, + "Return only one of the following answers: {values}", + "- If the text is {key}, return {value}", + ) + msg = [ + { + "role": "user", + "content": "You are a helpful sentiment engine. " + + f"{new_options}. " + + "No capitalization. No explanations. " + + f"{additional} " + + "The answer is based on the following text:\n{}", + } + ] + return msg + + +def summarize(max_words, additional=""): + msg = [ + { + "role": "user", + "content": "You are a helpful summarization engine." + + "Your answer will contain no no capitalization and no explanations." + + f"Return no more than " + + str(max_words) + + " words." + + f"{additional}" + + "The answer is the summary of the following text:\n{}", + } + ] + return msg + + +def translate(language, additional=""): + msg = [ + { + "role": "user", + "content": "You are a helpful translation engine." + + "You will return only the translation text, no explanations." + + f"The target language to translate to is: {language}." + + f"{additional}" + + "The answer is the translation of the following text:\n{}", + } + ] + return msg + + +def classify(labels, additional=""): + new_labels = process_labels( + labels, + "Determine if the text refers to one of the following:{values}", + "- If the text is {key}, return {value}", + ) + msg = [ + { + "role": "user", + "content": "You are a helpful classification engine. " + + f"{new_labels}. " + + "No capitalization. No explanations. " + + f"{additional} " + + "The answer is based on the following text:\n{}", + } + ] + return msg + + +def extract(labels, additional=""): + col_labels = "" + if isinstance(labels, list): + no_labels = len(labels) + plural = "s" + text_multi = ( + "Return the response in a simple list, pipe separated, and no headers. " + ) + for label in labels: + col_labels += label + " " + col_labels = col_labels.rstrip() + col_labels = col_labels.replace(" ", ", ") + else: + no_labels = 1 + plural = "" + text_multi = "" + col_labels = labels + + msg = [ + { + "role": "user", + "content": "You are a helpful text extraction engine." + + f"Extract the {col_labels} being referred to on the text." + + f"I expect {no_labels} item{plural} exactly." + + "No capitalization. No explanations." + + f"{text_multi}" + + f"{additional}" + + "The answer is based on the following text:\n{}", + } + ] + return msg + + +def custom(prompt): + msg = [{"role": "user", "content": f"{prompt}" + ": \n{}"}] + return msg diff --git a/python/pyproject.toml b/python/pyproject.toml new file mode 100644 index 0000000..277be4c --- /dev/null +++ b/python/pyproject.toml @@ -0,0 +1,14 @@ +[project] +name = "mall" +version = "0.1.0" +description = "Add your description here" +readme = "README.md" +requires-python = ">=3.12" +dependencies = [ + "ollama>=0.3.3", + "polars>=1.9.0", +] + +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" diff --git a/python/uv.lock b/python/uv.lock new file mode 100644 index 0000000..b4ad052 --- /dev/null +++ b/python/uv.lock @@ -0,0 +1,120 @@ +version = 1 +requires-python = ">=3.12" + +[[package]] +name = "anyio" +version = "4.6.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "idna" }, + { name = "sniffio" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/78/49/f3f17ec11c4a91fe79275c426658e509b07547f874b14c1a526d86a83fc8/anyio-4.6.0.tar.gz", hash = "sha256:137b4559cbb034c477165047febb6ff83f390fc3b20bf181c1fc0a728cb8beeb", size = 170983 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/9e/ef/7a4f225581a0d7886ea28359179cb861d7fbcdefad29663fc1167b86f69f/anyio-4.6.0-py3-none-any.whl", hash = "sha256:c7d2e9d63e31599eeb636c8c5c03a7e108d73b345f064f1c19fdc87b79036a9a", size = 89631 }, +] + +[[package]] +name = "certifi" +version = "2024.8.30" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/b0/ee/9b19140fe824b367c04c5e1b369942dd754c4c5462d5674002f75c4dedc1/certifi-2024.8.30.tar.gz", hash = "sha256:bec941d2aa8195e248a60b31ff9f0558284cf01a52591ceda73ea9afffd69fd9", size = 168507 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/12/90/3c9ff0512038035f59d279fddeb79f5f1eccd8859f06d6163c58798b9487/certifi-2024.8.30-py3-none-any.whl", hash = "sha256:922820b53db7a7257ffbda3f597266d435245903d80737e34f8a45ff3e3230d8", size = 167321 }, +] + +[[package]] +name = "h11" +version = "0.14.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f5/38/3af3d3633a34a3316095b39c8e8fb4853a28a536e55d347bd8d8e9a14b03/h11-0.14.0.tar.gz", hash = "sha256:8f19fbbe99e72420ff35c00b27a34cb9937e902a8b810e2c88300c6f0a3b699d", size = 100418 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/95/04/ff642e65ad6b90db43e668d70ffb6736436c7ce41fcc549f4e9472234127/h11-0.14.0-py3-none-any.whl", hash = "sha256:e3fe4ac4b851c468cc8363d500db52c2ead036020723024a109d37346efaa761", size = 58259 }, +] + +[[package]] +name = "httpcore" +version = "1.0.6" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "certifi" }, + { name = "h11" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/b6/44/ed0fa6a17845fb033bd885c03e842f08c1b9406c86a2e60ac1ae1b9206a6/httpcore-1.0.6.tar.gz", hash = "sha256:73f6dbd6eb8c21bbf7ef8efad555481853f5f6acdeaff1edb0694289269ee17f", size = 85180 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/06/89/b161908e2f51be56568184aeb4a880fd287178d176fd1c860d2217f41106/httpcore-1.0.6-py3-none-any.whl", hash = "sha256:27b59625743b85577a8c0e10e55b50b5368a4f2cfe8cc7bcfa9cf00829c2682f", size = 78011 }, +] + +[[package]] +name = "httpx" +version = "0.27.2" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "anyio" }, + { name = "certifi" }, + { name = "httpcore" }, + { name = "idna" }, + { name = "sniffio" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/78/82/08f8c936781f67d9e6b9eeb8a0c8b4e406136ea4c3d1f89a5db71d42e0e6/httpx-0.27.2.tar.gz", hash = "sha256:f7c2be1d2f3c3c3160d441802406b206c2b76f5947b11115e6df10c6c65e66c2", size = 144189 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/56/95/9377bcb415797e44274b51d46e3249eba641711cf3348050f76ee7b15ffc/httpx-0.27.2-py3-none-any.whl", hash = "sha256:7bb2708e112d8fdd7829cd4243970f0c223274051cb35ee80c03301ee29a3df0", size = 76395 }, +] + +[[package]] +name = "idna" +version = "3.10" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f1/70/7703c29685631f5a7590aa73f1f1d3fa9a380e654b86af429e0934a32f7d/idna-3.10.tar.gz", hash = "sha256:12f65c9b470abda6dc35cf8e63cc574b1c52b11df2c86030af0ac09b01b13ea9", size = 190490 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/76/c6/c88e154df9c4e1a2a66ccf0005a88dfb2650c1dffb6f5ce603dfbd452ce3/idna-3.10-py3-none-any.whl", hash = "sha256:946d195a0d259cbba61165e88e65941f16e9b36ea6ddb97f00452bae8b1287d3", size = 70442 }, +] + +[[package]] +name = "mall" +version = "0.1.0" +source = { editable = "." } +dependencies = [ + { name = "ollama" }, + { name = "polars" }, +] + +[package.metadata] +requires-dist = [ + { name = "ollama", specifier = ">=0.3.3" }, + { name = "polars", specifier = ">=1.9.0" }, +] + +[[package]] +name = "ollama" +version = "0.3.3" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "httpx" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/a6/8e/60a9b065eb796ef3996451cbe2d8044f6b030696166693b9805ae33b8b4c/ollama-0.3.3.tar.gz", hash = "sha256:f90a6d61803117f40b0e8ff17465cab5e1eb24758a473cfe8101aff38bc13b51", size = 10390 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/6a/ca/d22905ac3f768523f778189d38c9c6cd9edf4fa9dd09cb5a3fc57b184f90/ollama-0.3.3-py3-none-any.whl", hash = "sha256:ca6242ce78ab34758082b7392df3f9f6c2cb1d070a9dede1a4c545c929e16dba", size = 10267 }, +] + +[[package]] +name = "polars" +version = "1.9.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/53/09/c2fb0b231d551e0c8e68097d08577712bdff1ba91346cda8228e769602f5/polars-1.9.0.tar.gz", hash = "sha256:8e1206ef876f61c1d50a81e102611ea92ee34631cb135b46ad314bfefd3cb122", size = 4027431 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/64/cc/3d0292048d8f9045a03510aeecda2e6ed9df451ae8853274946ff841f98b/polars-1.9.0-cp38-abi3-macosx_10_12_x86_64.whl", hash = "sha256:a471d2ce96f6fa5dd0ef16bcdb227f3dbe3af8acb776ca52f9e64ef40c7489a0", size = 31870933 }, + { url = "https://files.pythonhosted.org/packages/ee/be/15af97f4d8b775630da16a8bf0141507d9c0ae5f2637b9a27ed337b3b1ba/polars-1.9.0-cp38-abi3-macosx_11_0_arm64.whl", hash = "sha256:94b12d731cd200d2c50b13fc070d6353f708e632bca6529c5a72aa6a69e5285d", size = 28171055 }, + { url = "https://files.pythonhosted.org/packages/bb/57/b286b317f061d8f17bab4726a27e7b185fbf3d3db65cf689074256ea34a9/polars-1.9.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f85f132732aa63c6f3b502b0fdfc3ba9f0b78cc6330059b5a2d6f9fd78508acb", size = 33063367 }, + { url = "https://files.pythonhosted.org/packages/e5/25/bf5d43dcb538bf6573b15f3d5995a52be61b8fbce0cd737e72c4d25eef88/polars-1.9.0-cp38-abi3-manylinux_2_24_aarch64.whl", hash = "sha256:f753c8941a3b3249d59262d68a856714a96a7d4e16977aefbb196be0c192e151", size = 29764698 }, + { url = "https://files.pythonhosted.org/packages/a6/cf/f9170a3ac20e0efb9d3c1cdacc677e35b711ffd5ec48a6d5f3da7b7d8663/polars-1.9.0-cp38-abi3-win_amd64.whl", hash = "sha256:95de07066cd797dd940fa2783708a7bef93c827a57be0f4dfad3575a6144212b", size = 32819142 }, +] + +[[package]] +name = "sniffio" +version = "1.3.1" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/a2/87/a6771e1546d97e7e041b6ae58d80074f81b7d5121207425c964ddf5cfdbd/sniffio-1.3.1.tar.gz", hash = "sha256:f4324edc670a0f49750a81b895f35c3adb843cca46f0530f79fc1babb23789dc", size = 20372 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/e9/44/75a9c9421471a6c4805dbf2356f7c181a29c1879239abab1ea2cc8f38b40/sniffio-1.3.1-py3-none-any.whl", hash = "sha256:2f6da418d1f1e0fddd844478f41680e794e6051915791a034ff65e5f100525a2", size = 10235 }, +] diff --git a/r/.Rbuildignore b/r/.Rbuildignore new file mode 100644 index 0000000..91114bf --- /dev/null +++ b/r/.Rbuildignore @@ -0,0 +1,2 @@ +^.*\.Rproj$ +^\.Rproj\.user$ diff --git a/DESCRIPTION b/r/DESCRIPTION similarity index 100% rename from DESCRIPTION rename to r/DESCRIPTION diff --git a/LICENSE b/r/LICENSE similarity index 100% rename from LICENSE rename to r/LICENSE diff --git a/LICENSE.md b/r/LICENSE.md similarity index 100% rename from LICENSE.md rename to r/LICENSE.md diff --git a/NAMESPACE b/r/NAMESPACE similarity index 100% rename from NAMESPACE rename to r/NAMESPACE diff --git a/R/data-reviews.R b/r/R/data-reviews.R similarity index 100% rename from R/data-reviews.R rename to r/R/data-reviews.R diff --git a/R/import-standalone-purrr.R b/r/R/import-standalone-purrr.R similarity index 100% rename from R/import-standalone-purrr.R rename to r/R/import-standalone-purrr.R diff --git a/R/llm-classify.R b/r/R/llm-classify.R similarity index 100% rename from R/llm-classify.R rename to r/R/llm-classify.R diff --git a/R/llm-custom.R b/r/R/llm-custom.R similarity index 100% rename from R/llm-custom.R rename to r/R/llm-custom.R diff --git a/R/llm-extract.R b/r/R/llm-extract.R similarity index 100% rename from R/llm-extract.R rename to r/R/llm-extract.R diff --git a/R/llm-sentiment.R b/r/R/llm-sentiment.R similarity index 100% rename from R/llm-sentiment.R rename to r/R/llm-sentiment.R diff --git a/R/llm-summarize.R b/r/R/llm-summarize.R similarity index 100% rename from R/llm-summarize.R rename to r/R/llm-summarize.R diff --git a/R/llm-translate.R b/r/R/llm-translate.R similarity index 100% rename from R/llm-translate.R rename to r/R/llm-translate.R diff --git a/R/llm-use.R b/r/R/llm-use.R similarity index 100% rename from R/llm-use.R rename to r/R/llm-use.R diff --git a/R/m-backend-prompt.R b/r/R/m-backend-prompt.R similarity index 100% rename from R/m-backend-prompt.R rename to r/R/m-backend-prompt.R diff --git a/R/m-backend-submit.R b/r/R/m-backend-submit.R similarity index 100% rename from R/m-backend-submit.R rename to r/R/m-backend-submit.R diff --git a/R/m-cache.R b/r/R/m-cache.R similarity index 100% rename from R/m-cache.R rename to r/R/m-cache.R diff --git a/R/m-defaults.R b/r/R/m-defaults.R similarity index 100% rename from R/m-defaults.R rename to r/R/m-defaults.R diff --git a/R/m-vec-prompt.R b/r/R/m-vec-prompt.R similarity index 100% rename from R/m-vec-prompt.R rename to r/R/m-vec-prompt.R diff --git a/R/mall.R b/r/R/mall.R similarity index 100% rename from R/mall.R rename to r/R/mall.R diff --git a/R/utils.R b/r/R/utils.R similarity index 100% rename from R/utils.R rename to r/R/utils.R diff --git a/README.Rmd b/r/README.Rmd similarity index 100% rename from README.Rmd rename to r/README.Rmd diff --git a/r/README.md b/r/README.md new file mode 100644 index 0000000..24dddc1 --- /dev/null +++ b/r/README.md @@ -0,0 +1,407 @@ + + + +# mall + + + + + +[![R-CMD-check](https://github.com/edgararuiz/mall/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/edgararuiz/mall/actions/workflows/R-CMD-check.yaml) +[![Codecov test +coverage](https://codecov.io/gh/edgararuiz/mall/branch/main/graph/badge.svg)](https://app.codecov.io/gh/edgararuiz/mall?branch=main) +[![Lifecycle: +experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental) + + +## Intro + +Run multiple LLM predictions against a data frame. The predictions are +processed row-wise over a specified column. It works using a +pre-determined one-shot prompt, along with the current row’s content. +The prompt that is use will depend of the type of analysis needed. +Currently, the included prompts perform the following: + +- [Sentiment analysis](#sentiment) +- [Text summarizing](#summarize) +- [Classify text](#classify) +- [Extract one, or several](#extract), specific pieces information from + the text +- [Translate text](#translate) +- [Custom prompt](#custom-prompt) + +This package is inspired by the SQL AI functions now offered by vendors +such as +[Databricks](https://docs.databricks.com/en/large-language-models/ai-functions.html) +and Snowflake. `mall` uses [Ollama](https://ollama.com/) to interact +with LLMs installed locally. That interaction takes place via the +[`ollamar`](https://hauselin.github.io/ollama-r/) package. + +## Motivation + +We want to new find ways to help data scientists use LLMs in their daily +work. Unlike the familiar interfaces, such as chatting and code +completion, this interface runs your text data directly against the LLM. +The LLM’s flexibility, allows for it to adapt to the subject of your +data, and provide surprisingly accurate predictions. This saves the data +scientist the need to write and tune an NLP model. + +## Get started + +- Install `mall` from Github + + ``` r + pak::pak("edgararuiz/mall") + ``` + +### With local LLMs + +- Install Ollama in your machine. The `ollamar` package’s website + provides this [Installation + guide](https://hauselin.github.io/ollama-r/#installation) + +- Download an LLM model. For example, I have been developing this + package using Llama 3.2 to test. To get that model you can run: + + ``` r + ollamar::pull("llama3.2") + ``` + +### With Databricks + +If you pass a table connected to **Databricks** via `odbc`, `mall` will +automatically use Databricks’ LLM instead of Ollama. *You won’t need +Ollama installed if you are using Databricks only.* + +`mall` will call the appropriate SQL AI function. For more information +see our [Databricks +article.](https://edgararuiz.github.io/mall/articles/databricks.html) + +## LLM functions + +### Sentiment + +Primarily, `mall` provides verb-like functions that expect a `tbl` as +their first argument. This allows us to use them in piped operations. + +We will start with loading a very small data set contained in `mall`. It +has 3 product reviews that we will use as the source of our examples. + +``` r +library(mall) + +data("reviews") + +reviews +#> # A tibble: 3 × 1 +#> review +#> +#> 1 This has been the best TV I've ever used. Great screen, and sound. +#> 2 I regret buying this laptop. It is too slow and the keyboard is too noisy +#> 3 Not sure how to feel about my new washing machine. Great color, but hard to f… +``` + +For the first example, we’ll asses the sentiment of each review. In +order to do this we will call `llm_sentiment()`: + +``` r +reviews |> + llm_sentiment(review) +#> # A tibble: 3 × 2 +#> review .sentiment +#> +#> 1 This has been the best TV I've ever used. Great screen, and sound. positive +#> 2 I regret buying this laptop. It is too slow and the keyboard is to… negative +#> 3 Not sure how to feel about my new washing machine. Great color, bu… neutral +``` + +The function let’s us modify the options to choose from: + +``` r +reviews |> + llm_sentiment(review, options = c("positive", "negative")) +#> # A tibble: 3 × 2 +#> review .sentiment +#> +#> 1 This has been the best TV I've ever used. Great screen, and sound. positive +#> 2 I regret buying this laptop. It is too slow and the keyboard is to… negative +#> 3 Not sure how to feel about my new washing machine. Great color, bu… negative +``` + +As mentioned before, by being pipe friendly, the results from the LLM +prediction can be used in further transformations: + +``` r +reviews |> + llm_sentiment(review, options = c("positive", "negative")) |> + filter(.sentiment == "negative") +#> # A tibble: 2 × 2 +#> review .sentiment +#> +#> 1 I regret buying this laptop. It is too slow and the keyboard is to… negative +#> 2 Not sure how to feel about my new washing machine. Great color, bu… negative +``` + +### Summarize + +There may be a need to reduce the number of words in a given text. +Usually, to make it easier to capture its intent. To do this, use +`llm_summarize()`. This function has an argument to control the maximum +number of words to output (`max_words`): + +``` r +reviews |> + llm_summarize(review, max_words = 5) +#> # A tibble: 3 × 2 +#> review .summary +#> +#> 1 This has been the best TV I've ever used. Gr… it's a great tv +#> 2 I regret buying this laptop. It is too slow … laptop purchase was a mistake +#> 3 Not sure how to feel about my new washing ma… having mixed feelings about it +``` + +To control the name of the prediction field, you can change `pred_name` +argument. This works with the other `llm_` functions as well. + +``` r +reviews |> + llm_summarize(review, max_words = 5, pred_name = "review_summary") +#> # A tibble: 3 × 2 +#> review review_summary +#> +#> 1 This has been the best TV I've ever used. Gr… it's a great tv +#> 2 I regret buying this laptop. It is too slow … laptop purchase was a mistake +#> 3 Not sure how to feel about my new washing ma… having mixed feelings about it +``` + +### Classify + +Use the LLM to categorize the text into one of the options you provide: + +``` r +reviews |> + llm_classify(review, c("appliance", "computer")) +#> # A tibble: 3 × 2 +#> review .classify +#> +#> 1 This has been the best TV I've ever used. Gr… computer +#> 2 I regret buying this laptop. It is too slow … computer +#> 3 Not sure how to feel about my new washing ma… appliance +``` + +### Extract + +One of the most interesting operations. Using natural language, we can +tell the LLM to return a specific part of the text. In the following +example, we request that the LLM return the product being referred to. +We do this by simply saying “product”. The LLM understands what we +*mean* by that word, and looks for that in the text. + +``` r +reviews |> + llm_extract(review, "product") +#> # A tibble: 3 × 2 +#> review .extract +#> +#> 1 This has been the best TV I've ever used. Gr… tv +#> 2 I regret buying this laptop. It is too slow … laptop +#> 3 Not sure how to feel about my new washing ma… washing machine +``` + +### Translate + +As the title implies, this function will translate the text into a +specified language. What is really nice, it is that you don’t need to +specify the language of the source text. Only the target language needs +to be defined. The translation accuracy will depend on the LLM + +``` r +reviews |> + llm_translate(review, "spanish") +#> # A tibble: 3 × 2 +#> review .translation +#> +#> 1 This has been the best TV I've ever used. Gr… Esta ha sido la mejor televisió… +#> 2 I regret buying this laptop. It is too slow … Me arrepiento de comprar este p… +#> 3 Not sure how to feel about my new washing ma… No estoy seguro de cómo me sien… +``` + +### Custom prompt + +It is possible to pass your own prompt to the LLM, and have `mall` run +it against each text entry. Use `llm_custom()` to access this +functionality: + +``` r +my_prompt <- paste( + "Answer a question.", + "Return only the answer, no explanation", + "Acceptable answers are 'yes', 'no'", + "Answer this about the following text, is this a happy customer?:" +) + +reviews |> + llm_custom(review, my_prompt) +#> # A tibble: 3 × 2 +#> review .pred +#> +#> 1 This has been the best TV I've ever used. Great screen, and sound. Yes +#> 2 I regret buying this laptop. It is too slow and the keyboard is too noi… No +#> 3 Not sure how to feel about my new washing machine. Great color, but har… No +``` + +## Initialize session + +Invoking an `llm_` function will automatically initialize a model +selection if you don’t have one selected yet. If there is only one +option, it will pre-select it for you. If there are more than one +available models, then `mall` will present you as menu selection so you +can select which model you wish to use. + +Calling `llm_use()` directly will let you specify the model and backend +to use. You can also setup additional arguments that will be passed down +to the function that actually runs the prediction. In the case of +Ollama, that function is +[`chat()`](https://hauselin.github.io/ollama-r/reference/chat.html). + +``` r +llm_use("ollama", "llama3.2", seed = 100, temperature = 0) +``` + +## Key considerations + +The main consideration is **cost**. Either, time cost, or money cost. + +If using this method with an LLM locally available, the cost will be a +long running time. Unless using a very specialized LLM, a given LLM is a +general model. It was fitted using a vast amount of data. So determining +a response for each row, takes longer than if using a manually created +NLP model. The default model used in Ollama is [Llama +3.2](https://ollama.com/library/llama3.2), which was fitted using 3B +parameters. + +If using an external LLM service, the consideration will need to be for +the billing costs of using such service. Keep in mind that you will be +sending a lot of data to be evaluated. + +Another consideration is the novelty of this approach. Early tests are +providing encouraging results. But you, as an user, will still need to +keep in mind that the predictions will not be infallible, so always +check the output. At this time, I think the best use for this method, is +for a quick analysis. + +## Performance + +We will briefly cover this methods performance from two perspectives: + +- How long the analysis takes to run locally + +- How well it predicts + +To do so, we will use the `data_bookReviews` data set, provided by the +`classmap` package. For this exercise, only the first 100, of the total +1,000, are going to be part of this analysis. + +``` r +library(classmap) + +data(data_bookReviews) + +data_bookReviews |> + glimpse() +#> Rows: 1,000 +#> Columns: 2 +#> $ review "i got this as both a book and an audio file. i had waited t… +#> $ sentiment 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 1, … +``` + +As per the docs, `sentiment` is a factor indicating the sentiment of the +review: negative (1) or positive (2) + +``` r +length(strsplit(paste(head(data_bookReviews$review, 100), collapse = " "), " ")[[1]]) +#> [1] 20470 +``` + +Just to get an idea of how much data we’re processing, I’m using a very, +very simple word count. So we’re analyzing a bit over 20 thousand words. + +``` r +reviews_llm <- data_bookReviews |> + head(100) |> + llm_sentiment( + col = review, + options = c("positive" ~ 2, "negative" ~ 1), + pred_name = "predicted" + ) +#> ! There were 2 predictions with invalid output, they were coerced to NA +``` + +As far as **time**, on my Apple M3 machine, it took about 1.5 minutes to +process, 100 rows, containing 20 thousand words. Setting `temp` to 0 in +`llm_use()`, made the model run faster. + +The package uses `purrr` to send each prompt individually to the LLM. +But, I did try a few different ways to speed up the process, +unsuccessfully: + +- Used `furrr` to send multiple requests at a time. This did not work + because either the LLM or Ollama processed all my requests serially. + So there was no improvement. + +- I also tried sending more than one row’s text at a time. This cause + instability in the number of results. For example sending 5 at a time, + sometimes returned 7 or 8. Even sending 2 was not stable. + +This is what the new table looks like: + +``` r +reviews_llm +#> # A tibble: 100 × 3 +#> review sentiment predicted +#> +#> 1 "i got this as both a book and an audio file… 1 1 +#> 2 "this book places too much emphasis on spend… 1 1 +#> 3 "remember the hollywood blacklist? the holly… 2 2 +#> 4 "while i appreciate what tipler was attempti… 1 1 +#> 5 "the others in the series were great, and i … 1 1 +#> 6 "a few good things, but she's lost her edge … 1 1 +#> 7 "words cannot describe how ripped off and di… 1 1 +#> 8 "1. the persective of most writers is shaped… 1 NA +#> 9 "i have been a huge fan of michael crichton … 1 1 +#> 10 "i saw dr. polk on c-span a month or two ago… 2 2 +#> # ℹ 90 more rows +``` + +I used `yardstick` to see how well the model performed. Of course, the +accuracy will not be of the “truth”, but rather the package’s results +recorded in `sentiment`. + +``` r +library(forcats) + +reviews_llm |> + mutate(predicted = as.factor(predicted)) |> + yardstick::accuracy(sentiment, predicted) +#> # A tibble: 1 × 3 +#> .metric .estimator .estimate +#> +#> 1 accuracy binary 0.980 +``` + +## Vector functions + +`mall` includes functions that expect a vector, instead of a table, to +run the predictions. This should make it easier to test things, such as +custom prompts or results of specific text. Each `llm_` function has a +corresponding `llm_vec_` function: + +``` r +llm_vec_sentiment("I am happy") +#> [1] "positive" +``` + +``` r +llm_vec_translate("Este es el mejor dia!", "english") +#> [1] "It's the best day!" +``` diff --git a/codecov.yml b/r/codecov.yml similarity index 100% rename from codecov.yml rename to r/codecov.yml diff --git a/data/reviews.rda b/r/data/reviews.rda similarity index 100% rename from data/reviews.rda rename to r/data/reviews.rda diff --git a/mall.Rproj b/r/mall.Rproj similarity index 100% rename from mall.Rproj rename to r/mall.Rproj diff --git a/man/llm_classify.Rd b/r/man/llm_classify.Rd similarity index 100% rename from man/llm_classify.Rd rename to r/man/llm_classify.Rd diff --git a/man/llm_custom.Rd b/r/man/llm_custom.Rd similarity index 100% rename from man/llm_custom.Rd rename to r/man/llm_custom.Rd diff --git a/man/llm_extract.Rd b/r/man/llm_extract.Rd similarity index 100% rename from man/llm_extract.Rd rename to r/man/llm_extract.Rd diff --git a/man/llm_sentiment.Rd b/r/man/llm_sentiment.Rd similarity index 100% rename from man/llm_sentiment.Rd rename to r/man/llm_sentiment.Rd diff --git a/man/llm_summarize.Rd b/r/man/llm_summarize.Rd similarity index 100% rename from man/llm_summarize.Rd rename to r/man/llm_summarize.Rd diff --git a/man/llm_translate.Rd b/r/man/llm_translate.Rd similarity index 100% rename from man/llm_translate.Rd rename to r/man/llm_translate.Rd diff --git a/man/llm_use.Rd b/r/man/llm_use.Rd similarity index 100% rename from man/llm_use.Rd rename to r/man/llm_use.Rd diff --git a/man/m_backend_submit.Rd b/r/man/m_backend_submit.Rd similarity index 100% rename from man/m_backend_submit.Rd rename to r/man/m_backend_submit.Rd diff --git a/man/reviews.Rd b/r/man/reviews.Rd similarity index 100% rename from man/reviews.Rd rename to r/man/reviews.Rd diff --git a/tests/testthat.R b/r/tests/testthat.R similarity index 100% rename from tests/testthat.R rename to r/tests/testthat.R diff --git a/tests/testthat/_snaps/llm-classify.md b/r/tests/testthat/_snaps/llm-classify.md similarity index 100% rename from tests/testthat/_snaps/llm-classify.md rename to r/tests/testthat/_snaps/llm-classify.md diff --git a/tests/testthat/_snaps/llm-custom.md b/r/tests/testthat/_snaps/llm-custom.md similarity index 100% rename from tests/testthat/_snaps/llm-custom.md rename to r/tests/testthat/_snaps/llm-custom.md diff --git a/tests/testthat/_snaps/llm-extract.md b/r/tests/testthat/_snaps/llm-extract.md similarity index 100% rename from tests/testthat/_snaps/llm-extract.md rename to r/tests/testthat/_snaps/llm-extract.md diff --git a/tests/testthat/_snaps/llm-sentiment.md b/r/tests/testthat/_snaps/llm-sentiment.md similarity index 100% rename from tests/testthat/_snaps/llm-sentiment.md rename to r/tests/testthat/_snaps/llm-sentiment.md diff --git a/tests/testthat/_snaps/llm-summarize.md b/r/tests/testthat/_snaps/llm-summarize.md similarity index 100% rename from tests/testthat/_snaps/llm-summarize.md rename to r/tests/testthat/_snaps/llm-summarize.md diff --git a/tests/testthat/_snaps/llm-translate.md b/r/tests/testthat/_snaps/llm-translate.md similarity index 100% rename from tests/testthat/_snaps/llm-translate.md rename to r/tests/testthat/_snaps/llm-translate.md diff --git a/tests/testthat/_snaps/llm-use.md b/r/tests/testthat/_snaps/llm-use.md similarity index 100% rename from tests/testthat/_snaps/llm-use.md rename to r/tests/testthat/_snaps/llm-use.md diff --git a/tests/testthat/_snaps/zzz-cache.md b/r/tests/testthat/_snaps/zzz-cache.md similarity index 100% rename from tests/testthat/_snaps/zzz-cache.md rename to r/tests/testthat/_snaps/zzz-cache.md diff --git a/tests/testthat/helper-ollama.R b/r/tests/testthat/helper-ollama.R similarity index 100% rename from tests/testthat/helper-ollama.R rename to r/tests/testthat/helper-ollama.R diff --git a/tests/testthat/test-llm-classify.R b/r/tests/testthat/test-llm-classify.R similarity index 100% rename from tests/testthat/test-llm-classify.R rename to r/tests/testthat/test-llm-classify.R diff --git a/tests/testthat/test-llm-custom.R b/r/tests/testthat/test-llm-custom.R similarity index 100% rename from tests/testthat/test-llm-custom.R rename to r/tests/testthat/test-llm-custom.R diff --git a/tests/testthat/test-llm-extract.R b/r/tests/testthat/test-llm-extract.R similarity index 100% rename from tests/testthat/test-llm-extract.R rename to r/tests/testthat/test-llm-extract.R diff --git a/tests/testthat/test-llm-sentiment.R b/r/tests/testthat/test-llm-sentiment.R similarity index 100% rename from tests/testthat/test-llm-sentiment.R rename to r/tests/testthat/test-llm-sentiment.R diff --git a/tests/testthat/test-llm-summarize.R b/r/tests/testthat/test-llm-summarize.R similarity index 100% rename from tests/testthat/test-llm-summarize.R rename to r/tests/testthat/test-llm-summarize.R diff --git a/tests/testthat/test-llm-translate.R b/r/tests/testthat/test-llm-translate.R similarity index 100% rename from tests/testthat/test-llm-translate.R rename to r/tests/testthat/test-llm-translate.R diff --git a/tests/testthat/test-llm-use.R b/r/tests/testthat/test-llm-use.R similarity index 100% rename from tests/testthat/test-llm-use.R rename to r/tests/testthat/test-llm-use.R diff --git a/tests/testthat/test-m-backend-prompt.R b/r/tests/testthat/test-m-backend-prompt.R similarity index 100% rename from tests/testthat/test-m-backend-prompt.R rename to r/tests/testthat/test-m-backend-prompt.R diff --git a/tests/testthat/test-m-backend-submit.R b/r/tests/testthat/test-m-backend-submit.R similarity index 100% rename from tests/testthat/test-m-backend-submit.R rename to r/tests/testthat/test-m-backend-submit.R diff --git a/tests/testthat/test-zzz-cache.R b/r/tests/testthat/test-zzz-cache.R similarity index 100% rename from tests/testthat/test-zzz-cache.R rename to r/tests/testthat/test-zzz-cache.R diff --git a/reference/MallFrame.qmd b/reference/MallFrame.qmd new file mode 100644 index 0000000..bcd2cba --- /dev/null +++ b/reference/MallFrame.qmd @@ -0,0 +1,27 @@ +# MallFrame { #mall.MallFrame } + +`MallFrame(self, df)` + +Extension to Polars that add ability to use +an LLM to run batch predictions over a data frame + +## Methods + +| Name | Description | +| --- | --- | +| [translate](#mall.MallFrame.translate) | Translate text into another language. | + +### translate { #mall.MallFrame.translate } + +`MallFrame.translate(col, language='', additional='', pred_name='translation')` + +Translate text into another language. + +#### Parameters + +| Name | Type | Description | Default | +|--------------|--------|----------------------------------------------------------------------------------------|-----------------| +| `col` | | The name of the text field to process | _required_ | +| `language` | | The target language to translate to. For example 'French'. | `''` | +| `pred_name` | | A character vector with the name of the new column where the prediction will be placed | `'translation'` | +| `additional` | | Inserts this text into the prompt sent to the LLM | `''` | \ No newline at end of file diff --git a/reference/_api_index.qmd b/reference/_api_index.qmd new file mode 100644 index 0000000..901fdf5 --- /dev/null +++ b/reference/_api_index.qmd @@ -0,0 +1,9 @@ +# Function reference {.doc .doc-index} + +## mall + + + +| | | +| --- | --- | +| [MallFrame](MallFrame.qmd#mall.MallFrame) | Extension to Polars that add ability to use | \ No newline at end of file diff --git a/reference/index.qmd b/reference/index.qmd index 0c8d4bc..49219cd 100644 --- a/reference/index.qmd +++ b/reference/index.qmd @@ -1,47 +1,15 @@ ---- -toc: false ---- - +::: {.panel-tabset group="language"} +## R +{{< include r_index.qmd >}} -# Function Reference +## python -[llm_classify()](llm_classify.html) [llm_vec_classify()](llm_classify.html) + -      Categorize data as one of options given - - -[llm_custom()](llm_custom.html) [llm_vec_custom()](llm_custom.html) - -      Send a custom prompt to the LLM - - -[llm_extract()](llm_extract.html) [llm_vec_extract()](llm_extract.html) - -      Extract entities from text - - -[llm_sentiment()](llm_sentiment.html) [llm_vec_sentiment()](llm_sentiment.html) - -      Sentiment analysis - - -[llm_summarize()](llm_summarize.html) [llm_vec_summarize()](llm_summarize.html) - -      Summarize text - - -[llm_translate()](llm_translate.html) [llm_vec_translate()](llm_translate.html) - -      Translates text to a specific language - - -[llm_use()](llm_use.html) - -      Specify the model to use - - -[reviews](reviews.html) - -      Mini reviews data set +[MallFrame](MallFrame.qmd#mall.MallFrame) +      Extension to Polars that add ability to use +an LLM to run batch predictions over a data frame +::: + \ No newline at end of file diff --git a/reference/r_index.qmd b/reference/r_index.qmd new file mode 100644 index 0000000..761cf58 --- /dev/null +++ b/reference/r_index.qmd @@ -0,0 +1,45 @@ +--- +toc: false +--- + + +[llm_classify()](llm_classify.html) [llm_vec_classify()](llm_classify.html) + +      Categorize data as one of options given + + +[llm_custom()](llm_custom.html) [llm_vec_custom()](llm_custom.html) + +      Send a custom prompt to the LLM + + +[llm_extract()](llm_extract.html) [llm_vec_extract()](llm_extract.html) + +      Extract entities from text + + +[llm_sentiment()](llm_sentiment.html) [llm_vec_sentiment()](llm_sentiment.html) + +      Sentiment analysis + + +[llm_summarize()](llm_summarize.html) [llm_vec_summarize()](llm_summarize.html) + +      Summarize text + + +[llm_translate()](llm_translate.html) [llm_vec_translate()](llm_translate.html) + +      Translates text to a specific language + + +[llm_use()](llm_use.html) + +      Specify the model to use + + +[reviews](reviews.html) + +      Mini reviews data set + + diff --git a/utils/website/README.md b/utils/website/README.md index a0c5076..6b07432 100644 --- a/utils/website/README.md +++ b/utils/website/README.md @@ -2,8 +2,8 @@ ```r devtools::install(upgrade = "never") -try(fs::dir_delete("_freeze/reference/")) -source("utils/website/build_reference.R") +#try(fs::dir_delete("_freeze/reference/")) +source(here::here("utils/website/build_reference.R")) quarto::quarto_render(as_job = FALSE) quarto::quarto_preview() ``` diff --git a/utils/website/build_reference.R b/utils/website/build_reference.R index 674d8ce..afb7fb5 100644 --- a/utils/website/build_reference.R +++ b/utils/website/build_reference.R @@ -8,9 +8,9 @@ library(cli) build_reference_index <- function(pkg = ".", folder = "reference") { if (is.character(pkg)) pkg <- pkgdown::as_pkgdown(pkg) try(dir_create(folder)) - ref_path <- path(folder, "index", ext = "qmd") + ref_path <- path(folder, "r_index", ext = "qmd") try(file_delete(ref_path)) - writeLines(reference_index(), ref_path) + writeLines(reference_index(pkg), ref_path) cli_inform(col_green(ref_path)) } @@ -29,5 +29,5 @@ build_reference <- function(pkg = ".", folder = "reference") { ) } -build_reference_index() -build_reference() +build_reference_index("r") +build_reference("r") diff --git a/utils/website/index-page.R b/utils/website/index-page.R index d080733..7af798b 100644 --- a/utils/website/index-page.R +++ b/utils/website/index-page.R @@ -17,9 +17,7 @@ reference_index <- function(pkg = ".") { "---", "toc: false", "---", - "", - "", - "# Function Reference", + "", "", res )