diff --git a/_freeze/index/execute-results/html.json b/_freeze/index/execute-results/html.json index 57f6a93..a847328 100644 --- a/_freeze/index/execute-results/html.json +++ b/_freeze/index/execute-results/html.json @@ -1,8 +1,8 @@ { - "hash": "fd815bf5a06da5a597e370f770ff1b8e", + "hash": "caaf0a8a4e7d66d2670b9f92dba16bf0", "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\n\n[![PyPi](https://img.shields.io/pypi/v/mlverse-mall)](https://pypi.org/project/mlverse-mall/) [![Python tests](https://github.com/mlverse/mall/actions/workflows/python-tests.yaml/badge.svg)](https://github.com/mlverse/mall/actions/workflows/python-tests.yaml) \\| \"CRAN [![R check](https://github.com/mlverse/mall/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/mlverse/mall/actions/workflows/R-CMD-check.yaml) \\| [![Package coverage](https://codecov.io/gh/mlverse/mall/branch/main/graph/badge.svg)](https://app.codecov.io/gh/mlverse/mall?branch=main)\n\n\n\nUse Large Language Models (LLM) to run Natural Language Processing (NLP) \noperations against your data. It takes advantage of the LLMs general language\ntraining in order to get the predictions, thus removing the need to train a new\nNLP model. `mall` is available for R and Python.\n\nIt works by running multiple LLM predictions against your data. The predictions\nare processed row-wise over a specified column. It relies on the \"one-shot\" \nprompt technique to instruct the LLM on a particular NLP operation to perform. \nThe package includes prompts to perform the following specific NLP operations:\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- [Verify that something is true](#verify) about the text (binary)\n\nFor other NLP operations, `mall` offers the ability for you to [write your own prompt](#custom-prompt).\n\n\n\nIn **R** The functions inside `mall` are designed to easily work with piped \ncommands, such as `dplyr`.\n\n``` r\nreviews |>\n llm_sentiment(review)\n```\n\n\n\nIn **Python**, `mall` is a library extension to [Polars](https://pola.rs/).\n\n``` python\nreviews.llm.sentiment(\"review\")\n```\n\n## Motivation\n\nWe want to new find new ways to help data scientists use LLMs in their daily work.\nUnlike the familiar interfaces, such as chatting and code completion, this \ninterface runs your text data directly against the LLM. This package is inspired\nby the SQL AI functions now offered by vendors such as [Databricks](https://docs.databricks.com/en/large-language-models/ai-functions.html) \nand Snowflake. \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 in \nyour machine with good accuracy. It also makes it possible to take \nadvantage of LLMs at your institution, since the data will not leave the \ncorporate network. Additionally, LLM management and integration platforms, such\nas [Ollama](https://ollama.com/), are now very easy to setup and use. `mall`\nuses Ollama as to interact with local LLMs.\n\nThe development version of `mall` lets you **use external LLMs such as\n[OpenAI](https://openai.com/), [Gemini](https://gemini.google.com/) and\n[Anthropic](https://www.anthropic.com/)**. In R, `mall` uses the\n[`ellmer`](https://ellmer.tidyverse.org/index.html)\npackage to integrate with the external LLM, and the \n[`chatlas`](https://posit-dev.github.io/chatlas/) package to integrate in Python.\n\n## Install `mall` {#get-started}\n\nInstall the package to get started:\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nOfficial version from CRAN:\n\n``` r\ninstall.packages(\"mall\")\n```\n\nDevelopment version from GitHub *(required for remote LLM integration)*:\n\n``` r\npak::pak(\"mlverse/mall/r\")\n```\n\n## Python\n\nOfficial version from PyPi:\n\n``` python\npip install mlverse-mall\n```\n\nDevelopment version from GitHub:\n\n``` python\npip install \"mlverse-mall @ git+https://git@github.com/mlverse/mall.git#subdirectory=python\"\n```\n:::\n\n## Setup the LLM\n\nChoose one of the two following options to setup LLM connectivity:\n\n### Local LLMs, via Ollama {#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::: {.panel-tabset group=\"language\"}\n## R\n\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\n ``` r\n ollamar::pull(\"llama3.2\")\n ```\n\n## Python\n\n- Install the official Ollama library\n\n ``` python\n pip install ollama\n ```\n\n- Download an LLM model. For example, I have been developing this package\nusing Llama 3.2 to test. To get that model you can run:\n\n ``` python\n import ollama\n ollama.pull('llama3.2')\n ```\n:::\n\n### Remote LLMs {#remote-llms}\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n`mall` uses the `ellmer` package as the integration point to the LLM. This package supports multiple providers such as OpenAI, Anthropic, Google Gemini, etc.\n\n- Install `ellmer`\n\n ``` r\n install.packages(\"ellmer\")\n ```\n\n- Refer to `ellmer`'s documentation to find out how to setup the connections with your selected provider: \n\n- Let `mall` know which `ellmer` object to use during the R session. To do this, call `llm_use()`. Here is an example of using OpenAI:\n\n\n ::: {.cell}\n \n ```{.r .cell-code}\n library(mall)\n library(ellmer)\n chat <- chat_openai()\n #> Using model = \"gpt-4o\".\n llm_use(chat)\n #> \n #> ── mall session object \n #> Backend: ellmerLLM session: model:gpt-4oR session:\n #> cache_folder:/var/folders/y_/f_0cx_291nl0s8h26t4jg6ch0000gp/T//RtmpMrbC3S/_mall_cacheb51979c641c2\n ```\n :::\n\n\n**Set a default LLM for your R session**\n\nAs a convenience, `mall` is able to automatically establish a connection with the\nLLM at the beginning o R session. To do this you can use the `.mall_chat` option:\n\n```r\noptions(.mall_chat = ellmer::chat_openai(model = \"gpt-4o\"))\n```\n\nAdd this line to your *.Rprofile* file in order for that code to run every time\nyou start R. You can call `usethis::edit_r_profile()` to open your .Rprofile\nfile so you can add the option. \n\n## Python\n\n`mall` uses the `chatlas` package as the integration point to the LLM. This \npackage supports multiple providers such as OpenAI, Anthropic, Google Gemini, etc.\n\n- Install the `chatlas` library\n\n ``` python\n pip install chatlas\n ```\n\n- Refer to `chatlas`'s documentation to find out how to setup the connections\nwith your selected provider: \n\n- Let `mall` know which `chatlas` object to use during the Python session. \nTo do this, call `llm_use()`. Here is an example of using OpenAI:\n\n ``` python\n import mall\n from chatlas import ChatOpenAI\n\n chat = ChatOpenAI()\n\n data = mall.MallData\n reviews = data.reviews\n\n reviews.llm.use(chat)\n ```\n:::\n\n## LLM functions\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\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(mall)\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## Python\n\n\n\n\n::: {.cell}\n\n```{.python .cell-code}\nimport mall \ndata = mall.MallData\nreviews = data.reviews\n\nreviews \n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\n
review
"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"
\n```\n\n:::\n:::\n\n:::\n\n\n\n### Sentiment {#sentiment}\n\nAutomatically returns \"positive\", \"negative\", or \"neutral\" based on the text.\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\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\nFor more information and examples visit this function's [R reference page](reference/llm_sentiment.qmd)\n\n## Python\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.sentiment(\"review\")\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\n
reviewsentiment
"This has been the best TV I've ever used. Great screen, and sound.""positive"
"I regret buying this laptop. It is too slow and the keyboard is too noisy""negative"
"Not sure how to feel about my new washing machine. Great color, but hard to figure""neutral"
\n```\n\n:::\n:::\n\n\nFor more information and examples visit this function's [Python reference page](reference/MallFrame.qmd#mall.MallFrame.sentiment)\n:::\n\n### Summarize {#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 control \nthe maximum number of words to output (`max_words`):\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\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\nFor more information and examples visit this function's [R reference page](reference/llm_summarize.qmd)\n\n## Python\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.summarize(\"review\", 5)\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\n
reviewsummary
"This has been the best TV I've ever used. Great screen, and sound.""great tv with good features"
"I regret buying this laptop. It is too slow and the keyboard is too noisy""laptop purchase was a mistake"
"Not sure how to feel about my new washing machine. Great color, but hard to figure""feeling uncertain about new purchase"
\n```\n\n:::\n:::\n\n\nFor more information and examples visit this function's [Python reference page](reference/MallFrame.qmd#mall.MallFrame.summarize)\n:::\n\n### Classify {#classify}\n\nUse the LLM to categorize the text into one of the options you provide:\n\n::: {.panel-tabset group=\"language\"}\n## R\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\nFor more information and examples visit this function's [R reference page](reference/llm_classify.qmd)\n\n## Python\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.classify(\"review\", [\"computer\", \"appliance\"])\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\n
reviewclassify
"This has been the best TV I've ever used. Great screen, and sound.""appliance"
"I regret buying this laptop. It is too slow and the keyboard is too noisy""computer"
"Not sure how to feel about my new washing machine. Great color, but hard to figure""appliance"
\n```\n\n:::\n:::\n\n\nFor more information and examples visit this function's [Python reference page](reference/MallFrame.qmd#mall.MallFrame.classify)\n:::\n\n### Extract {#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::: {.panel-tabset group=\"language\"}\n## R\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\nFor more information and examples visit this function's [R reference page](reference/llm_extract.qmd)\n\n## Python\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.extract(\"review\", \"product\")\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\n
reviewextract
"This has been the best TV I've ever used. Great screen, and sound.""tv"
"I regret buying this laptop. It is too slow and the keyboard is too noisy""laptop"
"Not sure how to feel about my new washing machine. Great color, but hard to figure""washing machine"
\n```\n\n:::\n:::\n\n\nFor more information and examples visit this function's [Python reference page](reference/MallFrame.qmd#mall.MallFrame.extract)\n:::\n\n### Verify {#verify}\n\nThis functions allows you to check and see if a statement is true, based on the\nprovided text. By default, it will return a 1 for \"yes\", and 0 for \"no\". This \ncan be customized.\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\nreviews |>\n llm_verify(review, \"is the customer happy with the purchase\")\n#> # A tibble: 3 × 2\n#> review .verify\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 n… 0 \n#> 3 Not sure how to feel about my new washing machine. Great color, but h… 0\n```\n:::\n\n\nFor more information and examples visit this function's [R reference page](reference/llm_verify.qmd)\n\n## Python\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.verify(\"review\", \"is the customer happy with the purchase\")\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\n
reviewverify
"This has been the best TV I've ever used. Great screen, and sound."1
"I regret buying this laptop. It is too slow and the keyboard is too noisy"0
"Not sure how to feel about my new washing machine. Great color, but hard to figure"0
\n```\n\n:::\n:::\n\n\nFor more information and examples visit this function's [Python reference page](reference/MallFrame.qmd#mall.MallFrame.verify)\n:::\n\n### Translate {#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 \ntranslation accuracy will depend on the LLM\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\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\nFor more information and examples visit this function's [R reference page](reference/llm_translate.qmd)\n\n## Python\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.translate(\"review\", \"spanish\")\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\n
reviewtranslation
"This has been the best TV I've ever used. Great screen, and sound.""Esta ha sido la mejor televisión que he utilizado hasta ahora. Gran pantalla y sonido."
"I regret buying this laptop. It is too slow and the keyboard is too noisy""Me arrepiento de comprar este portátil. Es demasiado lento y la tecla es demasiado ruidosa."
"Not sure how to feel about my new washing machine. Great color, but hard to figure""No estoy seguro de cómo sentirme con mi nueva lavadora. Un color maravilloso, pero muy difícil de en…
\n```\n\n:::\n:::\n\n\nFor more information and examples visit this function's [Python reference page](reference/MallFrame.qmd#mall.MallFrame.translate)\n:::\n\n### Custom prompt {#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::: {.panel-tabset group=\"language\"}\n## R\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\nFor more information and examples visit this function's [R reference page](reference/llm_custom.qmd)\n\n## Python\n\n\n::: {.cell}\n\n```{.python .cell-code}\nmy_prompt = (\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.llm.custom(\"review\", prompt = my_prompt)\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\n
reviewcustom
"This has been the best TV I've ever used. Great screen, and sound.""Yes"
"I regret buying this laptop. It is too slow and the keyboard is too noisy""No"
"Not sure how to feel about my new washing machine. Great color, but hard to figure""No"
\n```\n\n:::\n:::\n\n\nFor more information and examples visit this function's [Python reference page](reference/MallFrame.qmd#mall.MallFrame.custom)\n:::\n\n## Model selection and settings\n\n#### Local LLMs via Ollama {#settings-local}\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 if \nyou don't have one selected yet. If there is only one option, it will pre-select\nit for you. If there are more than one available models, then `mall` will \npresent you as menu selection so you can select which model you wish to use.\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 function\nthat actually runs the prediction. In the case of Ollama, that function is [`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::: {.cell}\n\n```{.r .cell-code}\nllm_use(\"ollama\", \"llama3.2\", seed = 100, temperature = 0)\n```\n:::\n\n\n## Python\n\nThe model and options to be used will be defined at the Polars data frame object \nlevel. If not passed, the default model will be **llama3.2**.\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.use(\"ollama\", \"llama3.2\", options = dict(seed = 100))\n```\n:::\n\n:::\n\n#### Remote LLMs\n\nThe provider and model selection will be based on the chat object you create. \nAny model related setting, such as temperature, seed and others, should be\nset at the time of the object creation as well.\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(mall)\nlibrary(ellmer)\nchat <- chat_openai(model = \"gpt-4o\", seed = 100)\nllm_use(chat)\n```\n:::\n\n\n## Python\n\n\n::: {.cell}\n\n```{.python .cell-code}\nimport mall\nfrom chatlas import ChatOpenAI\nchat = ChatOpenAI(model = \"gpt-4o\", seed= 100)\ndata = mall.MallData\nreviews = data.reviews\nreviews.llm.use(chat)\n```\n:::\n\n:::\n\n\n## Results caching\n\nBy default `mall` caches the requests and corresponding results from a given \nLLM run. Each response is saved as individual JSON files. By default, the folder\nname is `_mall_cache`. The folder name can be customized, if needed. Also, the\ncaching can be turned off by setting the argument to empty (`\"\"`).\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\nllm_use(.cache = \"_my_cache\")\n```\n:::\n\n\nTo turn off:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nllm_use(.cache = \"\")\n```\n:::\n\n\n## Python\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.use(_cache = \"my_cache\")\n```\n:::\n\n\nTo turn off:\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.use(_cache = \"\")\n```\n:::\n\n:::\n\nFor more information see the [Caching Results](articles/caching.qmd) article.\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 \nmodel. It was fitted using a vast amount of data. So determining a response for\neach row, takes longer than if using a manually created NLP model. The default\nmodel used in Ollama is [Llama 3.2](https://ollama.com/library/llama3.2), which \nwas 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 \nlot of data to be evaluated.\n\nAnother consideration is the novelty of this approach. Early tests are providing\nencouraging results. But you, as an user, will still need to keep in mind that \nthe predictions will not be infallible, so always check the output. At this time,\nI think the best use for this method, is for a quick analysis.\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::: {.cell}\n\n```{.r .cell-code}\nllm_vec_sentiment(\"I am happy\")\n#> [1] \"positive\"\n```\n:::\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\n", + "markdown": "---\nformat:\n html:\n toc: true\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n\n\n\n\n[![PyPi](https://img.shields.io/pypi/v/mlverse-mall)](https://pypi.org/project/mlverse-mall/) [![Python tests](https://github.com/mlverse/mall/actions/workflows/python-tests.yaml/badge.svg)](https://github.com/mlverse/mall/actions/workflows/python-tests.yaml) \\| \"CRAN [![R check](https://github.com/mlverse/mall/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/mlverse/mall/actions/workflows/R-CMD-check.yaml) \\| [![Package coverage](https://codecov.io/gh/mlverse/mall/branch/main/graph/badge.svg)](https://app.codecov.io/gh/mlverse/mall?branch=main)\n\n\n\nUse Large Language Models (LLM) to run Natural Language Processing (NLP) \noperations against your data. It takes advantage of the LLMs general language\ntraining in order to get the predictions, thus removing the need to train a new\nNLP model. `mall` is available for R and Python.\n\nIt works by running multiple LLM predictions against your data. The predictions\nare processed row-wise over a specified column. It relies on the \"one-shot\" \nprompt technique to instruct the LLM on a particular NLP operation to perform. \nThe package includes prompts to perform the following specific NLP operations:\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- [Verify that something is true](#verify) about the text (binary)\n\nFor other NLP operations, `mall` offers the ability for you to [write your own prompt](#custom-prompt).\n\n\n\nIn **R** The functions inside `mall` are designed to easily work with piped \ncommands, such as `dplyr`.\n\n``` r\nreviews |>\n llm_sentiment(review)\n```\n\n\n\nIn **Python**, `mall` is a library extension to [Polars](https://pola.rs/).\n\n``` python\nreviews.llm.sentiment(\"review\")\n```\n\n## Motivation\n\nWe want to new find new ways to help data scientists use LLMs in their daily work.\nUnlike the familiar interfaces, such as chatting and code completion, this \ninterface runs your text data directly against the LLM. This package is inspired\nby the SQL AI functions now offered by vendors such as [Databricks](https://docs.databricks.com/en/large-language-models/ai-functions.html) \nand Snowflake. \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 in \nyour machine with good accuracy. It also makes it possible to take \nadvantage of LLMs at your institution, since the data will not leave the \ncorporate network. Additionally, LLM management and integration platforms, such\nas [Ollama](https://ollama.com/), are now very easy to setup and use. `mall`\nuses Ollama as to interact with local LLMs.\n\nThe development version of `mall` lets you **use external LLMs such as\n[OpenAI](https://openai.com/), [Gemini](https://gemini.google.com/) and\n[Anthropic](https://www.anthropic.com/)**. In R, `mall` uses the\n[`ellmer`](https://ellmer.tidyverse.org/index.html)\npackage to integrate with the external LLM, and the \n[`chatlas`](https://posit-dev.github.io/chatlas/) package to integrate in Python.\n\n## Install `mall` {#get-started}\n\nInstall the package to get started:\n\n::: {.panel-tabset group=\"language\"}\n## R\n\nOfficial version from CRAN:\n\n``` r\ninstall.packages(\"mall\")\n```\n\nDevelopment version from GitHub *(required for remote LLM integration)*:\n\n``` r\npak::pak(\"mlverse/mall/r\")\n```\n\n## Python\n\nOfficial version from PyPi:\n\n``` python\npip install mlverse-mall\n```\n\nDevelopment version from GitHub:\n\n``` python\npip install \"mlverse-mall @ git+https://git@github.com/mlverse/mall.git#subdirectory=python\"\n```\n:::\n\n## Setup the LLM\n\nChoose one of the two following options to setup LLM connectivity:\n\n### Local LLMs, via Ollama {#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::: {.panel-tabset group=\"language\"}\n## R\n\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\n ``` r\n ollamar::pull(\"llama3.2\")\n ```\n\n## Python\n\n- Install the official Ollama library\n\n ``` python\n pip install ollama\n ```\n\n- Download an LLM model. For example, I have been developing this package\nusing Llama 3.2 to test. To get that model you can run:\n\n ``` python\n import ollama\n ollama.pull('llama3.2')\n ```\n:::\n\n### Remote LLMs {#remote-llms}\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n`mall` uses the `ellmer` package as the integration point to the LLM. This package supports multiple providers such as OpenAI, Anthropic, Google Gemini, etc.\n\n- Install `ellmer`\n\n ``` r\n install.packages(\"ellmer\")\n ```\n\n- Refer to `ellmer`'s documentation to find out how to setup the connections with your selected provider: \n\n- Let `mall` know which `ellmer` object to use during the R session. To do this, call `llm_use()`. Here is an example of using OpenAI:\n\n\n ::: {.cell}\n \n ```{.r .cell-code}\n library(mall)\n library(ellmer)\n chat <- chat_openai()\n #> Using model = \"gpt-4.1\".\n llm_use(chat)\n #> \n #> ── mall session object \n #> Backend: ellmerLLM session: model:gpt-4.1R session:\n #> cache_folder:/var/folders/y_/f_0cx_291nl0s8h26t4jg6ch0000gp/T//RtmpHpDogo/_mall_cacheb8cf19ff4806\n ```\n :::\n\n\n**Set a default LLM for your R session**\n\nAs a convenience, `mall` is able to automatically establish a connection with the\nLLM at the beginning o R session. To do this you can use the `.mall_chat` option:\n\n```r\noptions(.mall_chat = ellmer::chat_openai(model = \"gpt-4o\"))\n```\n\nAdd this line to your *.Rprofile* file in order for that code to run every time\nyou start R. You can call `usethis::edit_r_profile()` to open your .Rprofile\nfile so you can add the option. \n\n## Python\n\n`mall` uses the `chatlas` package as the integration point to the LLM. This \npackage supports multiple providers such as OpenAI, Anthropic, Google Gemini, etc.\n\n- Install the `chatlas` library\n\n ``` python\n pip install chatlas\n ```\n\n- Refer to `chatlas`'s documentation to find out how to setup the connections\nwith your selected provider: \n\n- Let `mall` know which `chatlas` object to use during the Python session. \nTo do this, call `llm_use()`. Here is an example of using OpenAI:\n\n ``` python\n import mall\n from chatlas import ChatOpenAI\n\n chat = ChatOpenAI()\n\n data = mall.MallData\n reviews = data.reviews\n\n reviews.llm.use(chat)\n ```\n:::\n\n## LLM functions\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\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(mall)\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## Python\n\n\n\n\n::: {.cell}\n\n```{.python .cell-code}\nimport mall \ndata = mall.MallData\nreviews = data.reviews\n\nreviews \n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\n
review
"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"
\n```\n\n:::\n:::\n\n:::\n\n### Sentiment {#sentiment}\n\nAutomatically returns \"positive\", \"negative\", or \"neutral\" based on the text.\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\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\nFor more information and examples visit this function's [R reference page](reference/llm_sentiment.qmd)\n\n## Python\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.sentiment(\"review\")\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\n
reviewsentiment
"This has been the best TV I've ever used. Great screen, and sound.""positive"
"I regret buying this laptop. It is too slow and the keyboard is too noisy""negative"
"Not sure how to feel about my new washing machine. Great color, but hard to figure""negative"
\n```\n\n:::\n:::\n\n\nFor more information and examples visit this function's [Python reference page](reference/MallFrame.qmd#mall.MallFrame.sentiment)\n:::\n\n### Summarize {#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 control \nthe maximum number of words to output (`max_words`):\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\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… great tv with good features \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\nFor more information and examples visit this function's [R reference page](reference/llm_summarize.qmd)\n\n## Python\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.summarize(\"review\", 5)\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\n
reviewsummary
"This has been the best TV I've ever used. Great screen, and sound.""best tv ever purchased"
"I regret buying this laptop. It is too slow and the keyboard is too noisy""laptop not up to expectations"
"Not sure how to feel about my new washing machine. Great color, but hard to figure""uncertain about new washer"
\n```\n\n:::\n:::\n\n\nFor more information and examples visit this function's [Python reference page](reference/MallFrame.qmd#mall.MallFrame.summarize)\n:::\n\n### Classify {#classify}\n\nUse the LLM to categorize the text into one of the options you provide:\n\n::: {.panel-tabset group=\"language\"}\n## R\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\nFor more information and examples visit this function's [R reference page](reference/llm_classify.qmd)\n\n## Python\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.classify(\"review\", [\"computer\", \"appliance\"])\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\n
reviewclassify
"This has been the best TV I've ever used. Great screen, and sound.""appliance"
"I regret buying this laptop. It is too slow and the keyboard is too noisy""appliance"
"Not sure how to feel about my new washing machine. Great color, but hard to figure""appliance"
\n```\n\n:::\n:::\n\n\nFor more information and examples visit this function's [Python reference page](reference/MallFrame.qmd#mall.MallFrame.classify)\n:::\n\n### Extract {#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::: {.panel-tabset group=\"language\"}\n## R\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\nFor more information and examples visit this function's [R reference page](reference/llm_extract.qmd)\n\n## Python\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.extract(\"review\", \"product\")\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\n
reviewextract
"This has been the best TV I've ever used. Great screen, and sound.""tv"
"I regret buying this laptop. It is too slow and the keyboard is too noisy""laptop"
"Not sure how to feel about my new washing machine. Great color, but hard to figure""washing machine"
\n```\n\n:::\n:::\n\n\nFor more information and examples visit this function's [Python reference page](reference/MallFrame.qmd#mall.MallFrame.extract)\n:::\n\n### Verify {#verify}\n\nThis functions allows you to check and see if a statement is true, based on the\nprovided text. By default, it will return a 1 for \"yes\", and 0 for \"no\". This \ncan be customized.\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\nreviews |>\n llm_verify(review, \"is the customer happy with the purchase\")\n#> # A tibble: 3 × 2\n#> review .verify\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 n… 0 \n#> 3 Not sure how to feel about my new washing machine. Great color, but h… 0\n```\n:::\n\n\nFor more information and examples visit this function's [R reference page](reference/llm_verify.qmd)\n\n## Python\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.verify(\"review\", \"is the customer happy with the purchase\")\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\n
reviewverify
"This has been the best TV I've ever used. Great screen, and sound."1
"I regret buying this laptop. It is too slow and the keyboard is too noisy"0
"Not sure how to feel about my new washing machine. Great color, but hard to figure"0
\n```\n\n:::\n:::\n\n\nFor more information and examples visit this function's [Python reference page](reference/MallFrame.qmd#mall.MallFrame.verify)\n:::\n\n### Translate {#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 \ntranslation accuracy will depend on the LLM\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\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\nFor more information and examples visit this function's [R reference page](reference/llm_translate.qmd)\n\n## Python\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.translate(\"review\", \"spanish\")\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\n
reviewtranslation
"This has been the best TV I've ever used. Great screen, and sound.""Esta ha sido la mejor TV que he utilizado. Gran pantalla y sonido."
"I regret buying this laptop. It is too slow and the keyboard is too noisy""Lamento haber comprado este portátil. Está demasiado lento y la tecla es demasiado ruidosa."
"Not sure how to feel about my new washing machine. Great color, but hard to figure""No estoy seguro de cómo sentirme con mi nueva lavadora. Bonito color, pero difícil de entender."
\n```\n\n:::\n:::\n\n\nFor more information and examples visit this function's [Python reference page](reference/MallFrame.qmd#mall.MallFrame.translate)\n:::\n\n### Custom prompt {#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::: {.panel-tabset group=\"language\"}\n## R\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\nFor more information and examples visit this function's [R reference page](reference/llm_custom.qmd)\n\n## Python\n\n\n::: {.cell}\n\n```{.python .cell-code}\nmy_prompt = (\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.llm.custom(\"review\", prompt = my_prompt)\n```\n\n::: {.cell-output-display}\n\n```{=html}\n
\n
reviewcustom
"This has been the best TV I've ever used. Great screen, and sound.""Yes"
"I regret buying this laptop. It is too slow and the keyboard is too noisy""No"
"Not sure how to feel about my new washing machine. Great color, but hard to figure""No"
\n```\n\n:::\n:::\n\n\nFor more information and examples visit this function's [Python reference page](reference/MallFrame.qmd#mall.MallFrame.custom)\n:::\n\n## Model selection and settings\n\n#### Local LLMs via Ollama {#settings-local}\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 if \nyou don't have one selected yet. If there is only one option, it will pre-select\nit for you. If there are more than one available models, then `mall` will \npresent you as menu selection so you can select which model you wish to use.\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 function\nthat actually runs the prediction. In the case of Ollama, that function is [`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::: {.cell}\n\n```{.r .cell-code}\nllm_use(\"ollama\", \"llama3.2\", seed = 100, temperature = 0)\n```\n:::\n\n\n## Python\n\nThe model and options to be used will be defined at the Polars data frame object \nlevel. If not passed, the default model will be **llama3.2**.\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.use(\"ollama\", \"llama3.2\", options = dict(seed = 100))\n```\n:::\n\n:::\n\n#### Remote LLMs\n\nThe provider and model selection will be based on the chat object you create. \nAny model related setting, such as temperature, seed and others, should be\nset at the time of the object creation as well.\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(mall)\nlibrary(ellmer)\nchat <- chat_openai(model = \"gpt-4o\", seed = 100)\nllm_use(chat)\n```\n:::\n\n\n## Python\n\n\n::: {.cell}\n\n```{.python .cell-code}\nimport mall\nfrom chatlas import ChatOpenAI\nchat = ChatOpenAI(model = \"gpt-4o\", seed= 100)\ndata = mall.MallData\nreviews = data.reviews\nreviews.llm.use(chat)\n```\n:::\n\n:::\n\n\n## Results caching\n\nBy default `mall` caches the requests and corresponding results from a given \nLLM run. Each response is saved as individual JSON files. By default, the folder\nname is `_mall_cache`. The folder name can be customized, if needed. Also, the\ncaching can be turned off by setting the argument to empty (`\"\"`).\n\n::: {.panel-tabset group=\"language\"}\n## R\n\n\n::: {.cell}\n\n```{.r .cell-code}\nllm_use(.cache = \"_my_cache\")\n```\n:::\n\n\nTo turn off:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nllm_use(.cache = \"\")\n```\n:::\n\n\n## Python\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.use(_cache = \"my_cache\")\n```\n:::\n\n\nTo turn off:\n\n\n::: {.cell}\n\n```{.python .cell-code}\nreviews.llm.use(_cache = \"\")\n```\n:::\n\n:::\n\nFor more information see the [Caching Results](articles/caching.qmd) article.\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 \nmodel. It was fitted using a vast amount of data. So determining a response for\neach row, takes longer than if using a manually created NLP model. The default\nmodel used in Ollama is [Llama 3.2](https://ollama.com/library/llama3.2), which \nwas 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 \nlot of data to be evaluated.\n\nAnother consideration is the novelty of this approach. Early tests are providing\nencouraging results. But you, as an user, will still need to keep in mind that \nthe predictions will not be infallible, so always check the output. At this time,\nI think the best use for this method, is for a quick analysis.\n\n## Vector functions\n\n::: {.panel-tabset group=\"language\"}\n## R\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\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\n\n## Python \n\n`mall` is also able to process vectors contained in a `list` object. This allows\nus to avoid having to convert a list of texts without having to first convert\nthem into a single column data frame. To use, initialize a new `LLMVec` class\nobject with either an Ollama model, or a `chatlas` `Chat` object, and then\naccess the same NLP functions as the Polars extension.\n\n\n::: {.cell}\n\n```{.python .cell-code}\n# Initialize a Chat object\nfrom chatlas import ChatOllama\nchat = ChatOllama(model = \"llama3.2\")\n\n# Pass it to a new LLMVec\nfrom mall import LLMVec\nllm = LLMVec(chat) \n```\n:::\n\n\nAccess the functions via the new LLMVec object, and pass the text to be processed.\n\n\n::: {.cell}\n\n```{.python .cell-code}\nllm.sentiment([\"I am happy\", \"I am sad\"])\n#> ['positive', 'negative']\n```\n:::\n\n\n\n::: {.cell}\n\n```{.python .cell-code}\nllm.translate([\"Este es el mejor dia!\"], \"english\")\n#> [\"It's the best day!\"]\n```\n:::\n\n\n\n:::\n\n", "supporting": [], "filters": [ "rmarkdown/pagebreak.lua" diff --git a/_freeze/reference/LlmVec/execute-results/html.json b/_freeze/reference/LlmVec/execute-results/html.json new file mode 100644 index 0000000..0b493ac --- /dev/null +++ b/_freeze/reference/LlmVec/execute-results/html.json @@ -0,0 +1,12 @@ +{ + "hash": "db8aa962358a8674ed6a69098f0ff7ea", + "result": { + "engine": "jupyter", + "markdown": "---\ntitle: LLMVec\n---\n\n\n\n```python\nLLMVec(backend='', model='', _cache='_mall_cache', **kwargs)\n```\n\nClass that adds ability to use an LLM to run batch predictions\n\n\n::: {#caadb44f .cell execution_count=1}\n``` {.python .cell-code}\nfrom chatlas import ChatOllama\nfrom mall import LLMVec\n\nchat = ChatOllama(model = \"llama3.2\")\n\nllm = LLMVec(chat) \n```\n:::\n\n\n## Methods\n\n| Name | Description |\n| --- | --- |\n| [classify](#mall.LLMVec.classify) | Classify text into specific categories. |\n| [custom](#mall.LLMVec.custom) | Provide the full prompt that the LLM will process. |\n| [extract](#mall.LLMVec.extract) | Pull a specific label from the text. |\n| [sentiment](#mall.LLMVec.sentiment) | Use an LLM to run a sentiment analysis |\n| [summarize](#mall.LLMVec.summarize) | Summarize the text down to a specific number of words. |\n| [translate](#mall.LLMVec.translate) | Translate text into another language. |\n| [verify](#mall.LLMVec.verify) | Check to see if something is true about the text. |\n\n### classify { #mall.LLMVec.classify }\n\n```python\nLLMVec.classify(x, labels='', additional='')\n```\n\nClassify text into specific categories.\n\n#### Parameters {.doc-section .doc-section-parameters}\n\n| Name | Type | Description | Default |\n|------------|--------|-------------------------------------------------------------------------------------------------------------------------|------------|\n| x | list | A list of texts | _required_ |\n| labels | list | A list or a DICT object that defines the categories to classify the text as. It will return one of the provided labels. | `''` |\n| additional | str | Inserts this text into the prompt sent to the LLM | `''` |\n\n#### Examples {.doc-section .doc-section-examples}\n\n::: {#f0de90cb .cell execution_count=2}\n``` {.python .cell-code}\nllm.classify(['this is important!', 'there is no rush'], ['urgent', 'not urgent'])\n```\n\n::: {.cell-output .cell-output-display execution_count=2}\n```\n['urgent', None]\n```\n:::\n:::\n\n\n### custom { #mall.LLMVec.custom }\n\n```python\nLLMVec.custom(x, prompt='', valid_resps='')\n```\n\nProvide the full prompt that the LLM will process.\n\n#### Parameters {.doc-section .doc-section-parameters}\n\n| Name | Type | Description | Default |\n|--------|--------|----------------------------------------------------|------------|\n| x | list | A list of texts | _required_ |\n| prompt | str | The prompt to send to the LLM along with the `col` | `''` |\n\n### extract { #mall.LLMVec.extract }\n\n```python\nLLMVec.extract(x, labels='', additional='')\n```\n\nPull a specific label from the text.\n\n#### Parameters {.doc-section .doc-section-parameters}\n\n| Name | Type | Description | Default |\n|------------|--------|--------------------------------------------------------------------------------|------------|\n| x | list | A list of texts | _required_ |\n| labels | list | A list or a DICT object that defines tells the LLM what to look for and return | `''` |\n| additional | str | Inserts this text into the prompt sent to the LLM | `''` |\n\n#### Examples {.doc-section .doc-section-examples}\n\n::: {#49687301 .cell execution_count=3}\n``` {.python .cell-code}\nllm.extract(['bob smith, 123 3rd street'], labels=['name', 'address'])\n```\n\n::: {.cell-output .cell-output-display execution_count=3}\n```\n['| bob smith | 123 3rd street |']\n```\n:::\n:::\n\n\n### sentiment { #mall.LLMVec.sentiment }\n\n```python\nLLMVec.sentiment(x, options=['positive', 'negative', 'neutral'], additional='')\n```\n\nUse an LLM to run a sentiment analysis\n\n#### Parameters {.doc-section .doc-section-parameters}\n\n| Name | Type | Description | Default |\n|------------|--------------|----------------------------------------------------------------|---------------------------------------|\n| x | list | A list of texts | _required_ |\n| options | list or dict | A list of the sentiment options to use, or a named DICT object | `['positive', 'negative', 'neutral']` |\n| additional | str | Inserts this text into the prompt sent to the LLM | `''` |\n\n#### Examples {.doc-section .doc-section-examples}\n\n::: {#af1bc6cc .cell execution_count=4}\n``` {.python .cell-code}\nllm.sentiment(['I am happy', 'I am sad'])\n```\n\n::: {.cell-output .cell-output-display execution_count=4}\n```\n['positive', 'negative']\n```\n:::\n:::\n\n\n### summarize { #mall.LLMVec.summarize }\n\n```python\nLLMVec.summarize(x, max_words=10, additional='')\n```\n\nSummarize the text down to a specific number of words.\n\n#### Parameters {.doc-section .doc-section-parameters}\n\n| Name | Type | Description | Default |\n|------------|--------|---------------------------------------------------|------------|\n| x | list | A list of texts | _required_ |\n| max_words | int | Maximum number of words to use for the summary | `10` |\n| additional | str | Inserts this text into the prompt sent to the LLM | `''` |\n\n#### Examples {.doc-section .doc-section-examples}\n\n::: {#1960bc54 .cell execution_count=5}\n``` {.python .cell-code}\nllm.summarize(['This has been the best TV Ive ever used. Great screen, and sound.'], max_words = 5)\n```\n\n::: {.cell-output .cell-output-display execution_count=5}\n```\n['this tv has exceeded expectations']\n```\n:::\n:::\n\n\n### translate { #mall.LLMVec.translate }\n\n```python\nLLMVec.translate(x, language='', additional='')\n```\n\nTranslate text into another language.\n\n#### Parameters {.doc-section .doc-section-parameters}\n\n| Name | Type | Description | Default |\n|------------|--------|------------------------------------------------------------|------------|\n| x | list | A list of texts | _required_ |\n| language | str | The target language to translate to. For example 'French'. | `''` |\n| additional | str | Inserts this text into the prompt sent to the LLM | `''` |\n\n#### Examples {.doc-section .doc-section-examples}\n\n::: {#84be8518 .cell execution_count=6}\n``` {.python .cell-code}\nllm.translate(['This has been the best TV Ive ever used. Great screen, and sound.'], language = 'spanish')\n```\n\n::: {.cell-output .cell-output-display execution_count=6}\n```\n['Esto ha sido la mejor televisión que he tenido, gran pantalla y sonido.']\n```\n:::\n:::\n\n\n### verify { #mall.LLMVec.verify }\n\n```python\nLLMVec.verify(x, what='', yes_no=[1, 0], additional='')\n```\n\nCheck to see if something is true about the text.\n\n#### Parameters {.doc-section .doc-section-parameters}\n\n| Name | Type | Description | Default |\n|------------|--------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------|\n| x | list | A list of texts | _required_ |\n| what | str | The statement or question that needs to be verified against the provided text | `''` |\n| yes_no | list | A positional list of size 2, which contains the values to return if true and false. The first position will be used as the 'true' value, and the second as the 'false' value | `[1, 0]` |\n| additional | str | Inserts this text into the prompt sent to the LLM | `''` |\n\n", + "supporting": [ + "LLMVec_files" + ], + "filters": [], + "includes": {} + } +} \ No newline at end of file diff --git a/_quarto.yml b/_quarto.yml index e592821..86311e8 100644 --- a/_quarto.yml +++ b/_quarto.yml @@ -59,10 +59,15 @@ quartodoc: out_index: _api_index.qmd dynamic: true sections: - - title: mall + - title: Polars desc: '' contents: - name: MallFrame + - title: Vectors + desc: '' + contents: + - name: LLMVec + pkgsite: dir: r diff --git a/index.qmd b/index.qmd index 53737b6..b7c35b4 100644 --- a/index.qmd +++ b/index.qmd @@ -16,6 +16,8 @@ library(dbplyr) library(tictoc) library(DBI) source("site/knitr-print.R") + +reticulate::use_virtualenv("python/.venv") ``` ```{python} @@ -277,11 +279,6 @@ reviews ``` ::: -```{python} -#| include: false -reviews.llm.use(options = dict(seed = 100), _cache = "_readme_cache") -``` - ### Sentiment {#sentiment} Automatically returns "positive", "negative", or "neutral" based on the text. @@ -597,13 +594,17 @@ 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. -## Vector functions (R only) +## Vector functions + +::: {.panel-tabset group="language"} +## R `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") ``` @@ -611,3 +612,35 @@ llm_vec_sentiment("I am happy") ```{r} llm_vec_translate("Este es el mejor dia!", "english") ``` + +## Python + +`mall` is also able to process vectors contained in a `list` object. This allows +us to avoid having to convert a list of texts without having to first convert +them into a single column data frame. To use, initialize a new `LLMVec` class +object with either an Ollama model, or a `chatlas` `Chat` object, and then +access the same NLP functions as the Polars extension. + +```{python} +# Initialize a Chat object +from chatlas import ChatOllama +chat = ChatOllama(model = "llama3.2") + +# Pass it to a new LLMVec +from mall import LLMVec +llm = LLMVec(chat) +``` + +Access the functions via the new LLMVec object, and pass the text to be processed. + +```{python} +llm.sentiment(["I am happy", "I am sad"]) +``` + +```{python} +llm.translate(["Este es el mejor dia!"], "english") +``` + + +::: + diff --git a/objects.json b/objects.json index 00ec9f7..67a8a41 100644 --- a/objects.json +++ b/objects.json @@ -1 +1 @@ -{"project": "mall", "version": "0.0.9999", "count": 18, "items": [{"name": "mall.MallFrame.classify", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.classify", "dispname": "-"}, {"name": "mall.polars.MallFrame.classify", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.classify", "dispname": "mall.MallFrame.classify"}, {"name": "mall.MallFrame.custom", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.custom", "dispname": "-"}, {"name": "mall.polars.MallFrame.custom", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.custom", "dispname": "mall.MallFrame.custom"}, {"name": "mall.MallFrame.extract", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.extract", "dispname": "-"}, {"name": "mall.polars.MallFrame.extract", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.extract", "dispname": "mall.MallFrame.extract"}, {"name": "mall.MallFrame.sentiment", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.sentiment", "dispname": "-"}, {"name": "mall.polars.MallFrame.sentiment", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.sentiment", "dispname": "mall.MallFrame.sentiment"}, {"name": "mall.MallFrame.summarize", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.summarize", "dispname": "-"}, {"name": "mall.polars.MallFrame.summarize", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.summarize", "dispname": "mall.MallFrame.summarize"}, {"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.use", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.use", "dispname": "-"}, {"name": "mall.polars.MallFrame.use", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.use", "dispname": "mall.MallFrame.use"}, {"name": "mall.MallFrame.verify", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.verify", "dispname": "-"}, {"name": "mall.polars.MallFrame.verify", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.verify", "dispname": "mall.MallFrame.verify"}, {"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 +{"project": "mall", "version": "0.0.9999", "count": 34, "items": [{"name": "mall.MallFrame.classify", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.classify", "dispname": "-"}, {"name": "mall.polars.MallFrame.classify", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.classify", "dispname": "mall.MallFrame.classify"}, {"name": "mall.MallFrame.custom", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.custom", "dispname": "-"}, {"name": "mall.polars.MallFrame.custom", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.custom", "dispname": "mall.MallFrame.custom"}, {"name": "mall.MallFrame.extract", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.extract", "dispname": "-"}, {"name": "mall.polars.MallFrame.extract", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.extract", "dispname": "mall.MallFrame.extract"}, {"name": "mall.MallFrame.sentiment", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.sentiment", "dispname": "-"}, {"name": "mall.polars.MallFrame.sentiment", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.sentiment", "dispname": "mall.MallFrame.sentiment"}, {"name": "mall.MallFrame.summarize", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.summarize", "dispname": "-"}, {"name": "mall.polars.MallFrame.summarize", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.summarize", "dispname": "mall.MallFrame.summarize"}, {"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.use", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.use", "dispname": "-"}, {"name": "mall.polars.MallFrame.use", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.use", "dispname": "mall.MallFrame.use"}, {"name": "mall.MallFrame.verify", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.verify", "dispname": "-"}, {"name": "mall.polars.MallFrame.verify", "domain": "py", "role": "function", "priority": "1", "uri": "reference/MallFrame.html#mall.MallFrame.verify", "dispname": "mall.MallFrame.verify"}, {"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"}, {"name": "mall.LLMVec.classify", "domain": "py", "role": "function", "priority": "1", "uri": "reference/LLMVec.html#mall.LLMVec.classify", "dispname": "-"}, {"name": "mall.llmvec.LLMVec.classify", "domain": "py", "role": "function", "priority": "1", "uri": "reference/LLMVec.html#mall.LLMVec.classify", "dispname": "mall.LLMVec.classify"}, {"name": "mall.LLMVec.custom", "domain": "py", "role": "function", "priority": "1", "uri": "reference/LLMVec.html#mall.LLMVec.custom", "dispname": "-"}, {"name": "mall.llmvec.LLMVec.custom", "domain": "py", "role": "function", "priority": "1", "uri": "reference/LLMVec.html#mall.LLMVec.custom", "dispname": "mall.LLMVec.custom"}, {"name": "mall.LLMVec.extract", "domain": "py", "role": "function", "priority": "1", "uri": "reference/LLMVec.html#mall.LLMVec.extract", "dispname": "-"}, {"name": "mall.llmvec.LLMVec.extract", "domain": "py", "role": "function", "priority": "1", "uri": "reference/LLMVec.html#mall.LLMVec.extract", "dispname": "mall.LLMVec.extract"}, {"name": "mall.LLMVec.sentiment", "domain": "py", "role": "function", "priority": "1", "uri": "reference/LLMVec.html#mall.LLMVec.sentiment", "dispname": "-"}, {"name": "mall.llmvec.LLMVec.sentiment", "domain": "py", "role": "function", "priority": "1", "uri": "reference/LLMVec.html#mall.LLMVec.sentiment", "dispname": "mall.LLMVec.sentiment"}, {"name": "mall.LLMVec.summarize", "domain": "py", "role": "function", "priority": "1", "uri": "reference/LLMVec.html#mall.LLMVec.summarize", "dispname": "-"}, {"name": "mall.llmvec.LLMVec.summarize", "domain": "py", "role": "function", "priority": "1", "uri": "reference/LLMVec.html#mall.LLMVec.summarize", "dispname": "mall.LLMVec.summarize"}, {"name": "mall.LLMVec.translate", "domain": "py", "role": "function", "priority": "1", "uri": "reference/LLMVec.html#mall.LLMVec.translate", "dispname": "-"}, {"name": "mall.llmvec.LLMVec.translate", "domain": "py", "role": "function", "priority": "1", "uri": "reference/LLMVec.html#mall.LLMVec.translate", "dispname": "mall.LLMVec.translate"}, {"name": "mall.LLMVec.verify", "domain": "py", "role": "function", "priority": "1", "uri": "reference/LLMVec.html#mall.LLMVec.verify", "dispname": "-"}, {"name": "mall.llmvec.LLMVec.verify", "domain": "py", "role": "function", "priority": "1", "uri": "reference/LLMVec.html#mall.LLMVec.verify", "dispname": "mall.LLMVec.verify"}, {"name": "mall.LLMVec", "domain": "py", "role": "class", "priority": "1", "uri": "reference/LLMVec.html#mall.LLMVec", "dispname": "-"}, {"name": "mall.llmvec.LLMVec", "domain": "py", "role": "class", "priority": "1", "uri": "reference/LLMVec.html#mall.LLMVec", "dispname": "mall.LLMVec"}]} \ No newline at end of file diff --git a/python/mall/__init__.py b/python/mall/__init__.py index e623439..de3669a 100644 --- a/python/mall/__init__.py +++ b/python/mall/__init__.py @@ -1,4 +1,5 @@ -__all__ = ["MallFrame", "MallData"] +__all__ = ["MallFrame", "MallData", "LLMVec"] from mall.polars import MallFrame from mall.data import MallData +from mall.llmvec import LLMVec diff --git a/python/mall/llm.py b/python/mall/llm.py index de932a8..efed256 100644 --- a/python/mall/llm.py +++ b/python/mall/llm.py @@ -1,11 +1,33 @@ +from ollama import Client +from chatlas import Chat import polars as pl +import hashlib import ollama import json -import hashlib import os -def map_call(df, col, msg, pred_name, use, valid_resps="", convert=None): +def llm_use(backend="", model="", _cache="_mall_cache", **kwargs): + out = dict() + if isinstance(backend, Chat): + out.update(dict(backend="chatlas")) + out.update(dict(chat=backend)) + backend = "" + model = "" + if isinstance(backend, Client): + out.update(dict(backend="ollama-client")) + out.update(dict(client=backend)) + backend = "" + if backend != "": + out.update(dict(backend=backend)) + if model != "": + out.update(dict(model=model)) + out.update(dict(_cache=_cache)) + out.update(dict(kwargs)) + return out + + +def llm_map(df, col, msg, pred_name, use, valid_resps="", convert=None): if valid_resps == "": valid_resps = [] valid_resps = valid_output(valid_resps) @@ -38,6 +60,17 @@ def map_call(df, col, msg, pred_name, use, valid_resps="", convert=None): return df +def llm_loop(x, msg, use, valid_resps="", convert=None): + if isinstance(x, list) == False: + raise TypeError("`x` is not a list object") + out = list() + for row in x: + out.append( + llm_call(x=row, msg=msg, use=use, valid_resps=valid_resps, convert=convert) + ) + return out + + def llm_call(x, msg, use, valid_resps="", convert=None, data_type=None): backend = use.get("backend") @@ -48,7 +81,7 @@ def llm_call(x, msg, use, valid_resps="", convert=None, data_type=None): messages=build_msg(x, msg), options=use.get("options"), ) - + out = "" cache = "" if use.get("_cache") != "": diff --git a/python/mall/llmvec.py b/python/mall/llmvec.py new file mode 100644 index 0000000..04ec047 --- /dev/null +++ b/python/mall/llmvec.py @@ -0,0 +1,211 @@ +from mall.prompt import ( + sentiment, + summarize, + translate, + classify, + extract, + custom, + verify, +) + +from mall.llm import llm_use, llm_loop + + +class LLMVec: + """Class that adds ability to use an LLM to run batch predictions + + ```{python} + from chatlas import ChatOllama + from mall import LLMVec + + chat = ChatOllama(model = "llama3.2") + + llm = LLMVec(chat) + ``` + """ + def __init__(self, backend="", model="", _cache="_mall_cache", **kwargs): + self._use = llm_use(backend=backend, model=model, _cache=_cache, **kwargs) + + def sentiment( + self, x, options=["positive", "negative", "neutral"], additional="" + ) -> list: + """Use an LLM to run a sentiment analysis + + Parameters + ------ + x : list + A list of texts + + options : list or dict + A list of the sentiment options to use, or a named DICT + object + + additional : str + Inserts this text into the prompt sent to the LLM + + Examples + ------ + + ```{python} + llm.sentiment(['I am happy', 'I am sad']) + ``` + """ + return llm_loop( + x=x, + msg=sentiment(options, additional=additional), + use=self._use, + valid_resps=options, + ) + + def summarize(self, x, max_words=10, additional="") -> list: + """Summarize the text down to a specific number of words. + + Parameters + ------ + x : list + A list of texts + + max_words : int + Maximum number of words to use for the summary + + additional : str + Inserts this text into the prompt sent to the LLM + + Examples + ------ + + ```{python} + llm.summarize(['This has been the best TV Ive ever used. Great screen, and sound.'], max_words = 5) + ``` + """ + return llm_loop( + x=x, + msg=summarize(max_words, additional=additional), + use=self._use, + ) + + def translate(self, x, language="", additional="") -> list: + """Translate text into another language. + + Parameters + ------ + x : list + A list of texts + + language : str + The target language to translate to. For example 'French'. + + additional : str + Inserts this text into the prompt sent to the LLM + + + Examples + ------ + + ```{python} + llm.translate(['This has been the best TV Ive ever used. Great screen, and sound.'], language = 'spanish') + ``` + + """ + return llm_loop( + x=x, + msg=translate(language, additional=additional), + use=self._use, + ) + + def classify(self, x, labels="", additional="") -> list: + """Classify text into specific categories. + + Parameters + ------ + x : list + A list of texts + + labels : list + A list or a DICT object that defines the categories to + classify the text as. It will return one of the provided + labels. + + additional : str + Inserts this text into the prompt sent to the LLM + + Examples + ------ + + ```{python} + llm.classify(['this is important!', 'there is no rush'], ['urgent', 'not urgent']) + ``` + """ + return llm_loop( + x=x, + msg=classify(labels, additional=additional), + use=self._use, + valid_resps=labels, + ) + + def extract(self, x, labels="", additional="") -> list: + """Pull a specific label from the text. + + Parameters + ------ + x : list + A list of texts + + labels : list + A list or a DICT object that defines tells the LLM what + to look for and return + + additional : str + Inserts this text into the prompt sent to the LLM + + Examples + ------ + + ```{python} + llm.extract(['bob smith, 123 3rd street'], labels=['name', 'address']) + ``` + """ + return llm_loop(x=x, msg=extract(labels, additional=additional), use=self._use) + + def custom(self, x, prompt="", valid_resps="") -> list: + """Provide the full prompt that the LLM will process. + + Parameters + ------ + x : list + A list of texts + + prompt : str + The prompt to send to the LLM along with the `col` + + """ + return llm_loop(x=x, msg=custom(prompt), use=self._use, valid_resps=valid_resps) + + def verify(self, x, what="", yes_no=[1, 0], additional="") -> list: + """Check to see if something is true about the text. + + Parameters + ------ + x : list + A list of texts + + what : str + The statement or question that needs to be verified against the + provided text + + yes_no : list + A positional list of size 2, which contains the values to return + if true and false. The first position will be used as the 'true' + value, and the second as the 'false' value + + additional : str + Inserts this text into the prompt sent to the LLM + + """ + return llm_loop( + x=x, + msg=verify(what, additional=additional), + use=self._use, + valid_resps=yes_no, + convert=dict(yes=yes_no[0], no=yes_no[1]), + ) diff --git a/python/mall/polars.py b/python/mall/polars.py index 6202cc9..d7ad452 100644 --- a/python/mall/polars.py +++ b/python/mall/polars.py @@ -11,7 +11,7 @@ custom, verify, ) -from mall.llm import map_call +from mall.llm import llm_use, llm_map @pl.api.register_dataframe_namespace("llm") @@ -95,21 +95,7 @@ def use(self, backend="", model="", _cache="_mall_cache", **kwargs): reviews.llm.use(chat) ``` """ - if isinstance(backend, Chat): - self._use.update(dict(backend="chatlas")) - self._use.update(dict(chat=backend)) - backend = "" - model = "" - if isinstance(backend, Client): - self._use.update(dict(backend="ollama-client")) - self._use.update(dict(client=backend)) - backend = "" - 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)) + self._use = llm_use(backend=backend, model=model, _cache=_cache, **kwargs) return self._use def sentiment( @@ -161,7 +147,7 @@ def sentiment( ``` """ - df = map_call( + df = llm_map( df=self._df, col=col, msg=sentiment(options, additional=additional), @@ -208,7 +194,7 @@ def summarize( reviews.llm.summarize("review", 5, pred_name = "review_summary") ``` """ - df = map_call( + df = llm_map( df=self._df, col=col, msg=summarize(max_words, additional=additional), @@ -254,7 +240,7 @@ def translate( ``` """ - df = map_call( + df = llm_map( df=self._df, col=col, msg=translate(language, additional=additional), @@ -306,7 +292,7 @@ def classify( reviews.llm.classify("review", {"appliance" : "1", "computer" : "2"}) ``` """ - df = map_call( + df = llm_map( df=self._df, col=col, msg=classify(labels, additional=additional), @@ -390,7 +376,7 @@ def extract( for label in labels: lab_names.append(label) lab_vals.append(labels[label]) - df = map_call( + df = llm_map( df=self._df, col=col, msg=extract(lab_vals, additional=additional), @@ -442,7 +428,7 @@ def custom( reviews.llm.custom("review", prompt = my_prompt) ``` """ - df = map_call( + df = llm_map( df=self._df, col=col, msg=custom(prompt), @@ -495,7 +481,7 @@ def verify( reviews.llm.verify("review", "is the customer happy", ["y", "n"]) ``` """ - df = map_call( + df = llm_map( df=self._df, col=col, msg=verify(what, additional=additional), diff --git a/python/pyproject.toml b/python/pyproject.toml index 5807592..f4b4d2d 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -3,7 +3,7 @@ packages = ["mall"] [project] name = "mlverse-mall" -version = "0.1.0.9001" +version = "0.1.0.9003" description = "Run multiple 'Large Language Model' predictions against a table. The predictions run row-wise over a specified column." readme = "README.md" authors = [ diff --git a/python/tests/test_classify.py b/python/tests/test_classify.py index ca39450..7b0f957 100644 --- a/python/tests/test_classify.py +++ b/python/tests/test_classify.py @@ -26,3 +26,9 @@ def pull(df, col): for i in df.select(col).to_dicts(): out.append(i.get(col)) return out + +def test_classify_vec(): + from mall import LLMVec + llm = LLMVec("test", "echo") + x = llm.classify(["a"], ["a", "b"]) + assert x == ['a'] diff --git a/python/tests/test_custom.py b/python/tests/test_custom.py index 515a744..efca829 100644 --- a/python/tests/test_custom.py +++ b/python/tests/test_custom.py @@ -11,3 +11,9 @@ def test_custom_prompt(): x = df.llm.custom("x", "hello") assert x["custom"][0] == "hello: \n{}" shutil.rmtree("_test_cache", ignore_errors=True) + +def test_custom_vec(): + from mall import LLMVec + llm = LLMVec("test", "echo") + x = llm.custom(["a"], "hello") + assert x == ['a'] diff --git a/python/tests/test_extract.py b/python/tests/test_extract.py index 9d675bf..9db3f86 100644 --- a/python/tests/test_extract.py +++ b/python/tests/test_extract.py @@ -44,3 +44,9 @@ def test_extract_expand(): x = df.llm.extract("x", ["a", "b"], expand_cols=True) assert x["a"][0] == "x " shutil.rmtree("_test_cache", ignore_errors=True) + +def test_extract_vec(): + from mall import LLMVec + llm = LLMVec("test", "echo") + x = llm.extract(["a"], ["a"]) + assert x == ['a'] diff --git a/python/tests/test-llm.py b/python/tests/test_llm.py similarity index 100% rename from python/tests/test-llm.py rename to python/tests/test_llm.py diff --git a/python/tests/test_sentiment.py b/python/tests/test_sentiment.py index 22911f7..1982acf 100644 --- a/python/tests/test_sentiment.py +++ b/python/tests/test_sentiment.py @@ -48,3 +48,9 @@ def pull(df, col): for i in df.select(col).to_dicts(): out.append(i.get(col)) return out + +def test_sentiment_vec(): + from mall import LLMVec + llm = LLMVec("test", "echo") + x = llm.sentiment(["a"], ["a"]) + assert x == ['a'] diff --git a/python/tests/test_summarize.py b/python/tests/test_summarize.py index 0542283..5efc3c4 100644 --- a/python/tests/test_summarize.py +++ b/python/tests/test_summarize.py @@ -25,3 +25,9 @@ def test_summarize_max(): == "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:\n{}" ) shutil.rmtree("_test_cache", ignore_errors=True) + +def test_summarize_vec(): + from mall import LLMVec + llm = LLMVec("test", "echo") + x = llm.summarize(["a"]) + assert x == ['a'] diff --git a/python/tests/test_translate.py b/python/tests/test_translate.py index e6c85c4..88a251c 100644 --- a/python/tests/test_translate.py +++ b/python/tests/test_translate.py @@ -14,3 +14,9 @@ def test_translate_prompt(): == "You are a helpful translation engine. You will return only the translation text, no explanations. The target language to translate to is: spanish. The answer is the translation of the following text:\n{}" ) shutil.rmtree("_test_cache", ignore_errors=True) + +def test_translate_vec(): + from mall import LLMVec + llm = LLMVec("test", "echo") + x = llm.translate(["a"], "spanish") + assert x == ['a'] diff --git a/python/tests/test_verify.py b/python/tests/test_verify.py index f6df6fe..9a47cb1 100644 --- a/python/tests/test_verify.py +++ b/python/tests/test_verify.py @@ -26,3 +26,10 @@ def pull(df, col): for i in df.select(col).to_dicts(): out.append(i.get(col)) return out + +def test_verify_vec(): + from mall import LLMVec + llm = LLMVec("test", "echo") + x = llm.verify(["a"], "this is the verify test", ["a", "b"]) + assert x == ['a'] + diff --git a/r/DESCRIPTION b/r/DESCRIPTION index 8fc43f6..30007d4 100644 --- a/r/DESCRIPTION +++ b/r/DESCRIPTION @@ -17,7 +17,7 @@ RoxygenNote: 7.3.2 Imports: cli, dplyr, - ellmer, + ellmer (>= 0.3.0), fs, glue, jsonlite, diff --git a/r/NAMESPACE b/r/NAMESPACE index 6b0e63f..3903ec9 100644 --- a/r/NAMESPACE +++ b/r/NAMESPACE @@ -34,7 +34,6 @@ export(llm_verify) export(m_backend_prompt) export(m_backend_submit) import(cli) -import(ellmer) import(fs) import(glue) import(rlang) @@ -43,6 +42,7 @@ importFrom(dplyr,mutate) importFrom(dplyr,pull) importFrom(dplyr,sql) importFrom(dplyr,tibble) +importFrom(ellmer,parallel_chat_text) importFrom(jsonlite,fromJSON) importFrom(jsonlite,read_json) importFrom(jsonlite,write_json) diff --git a/r/R/m-backend-submit.R b/r/R/m-backend-submit.R index 4d5bd5d..83d67de 100644 --- a/r/R/m-backend-submit.R +++ b/r/R/m-backend-submit.R @@ -127,6 +127,10 @@ m_ellmer_chat <- function(...) { temp_ellmer$chat(...) } +dummy_func <- function(x, y) { + parallel_chat_text(x, y) +} + # ------------------------------ Simulate -------------------------------------- #' @export diff --git a/r/R/mall.R b/r/R/mall.R index befecfe..6c1168a 100644 --- a/r/R/mall.R +++ b/r/R/mall.R @@ -1,8 +1,8 @@ #' @importFrom ollamar chat test_connection list_models #' @importFrom dplyr mutate tibble bind_cols pull sql -#' @importFrom utils menu head #' @importFrom jsonlite fromJSON read_json write_json -#' @import ellmer +#' @importFrom ellmer parallel_chat_text +#' @importFrom utils menu head #' @import rlang #' @import glue #' @import cli diff --git a/reference/LlmVec.qmd b/reference/LlmVec.qmd new file mode 100644 index 0000000..7318184 --- /dev/null +++ b/reference/LlmVec.qmd @@ -0,0 +1,170 @@ +# LLMVec { #mall.LLMVec } + +```python +LLMVec(backend='', model='', _cache='_mall_cache', **kwargs) +``` + +Class that adds ability to use an LLM to run batch predictions + +```{python} +from chatlas import ChatOllama +from mall import LLMVec + +chat = ChatOllama(model = "llama3.2") + +llm = LLMVec(chat) +``` + +## Methods + +| Name | Description | +| --- | --- | +| [classify](#mall.LLMVec.classify) | Classify text into specific categories. | +| [custom](#mall.LLMVec.custom) | Provide the full prompt that the LLM will process. | +| [extract](#mall.LLMVec.extract) | Pull a specific label from the text. | +| [sentiment](#mall.LLMVec.sentiment) | Use an LLM to run a sentiment analysis | +| [summarize](#mall.LLMVec.summarize) | Summarize the text down to a specific number of words. | +| [translate](#mall.LLMVec.translate) | Translate text into another language. | +| [verify](#mall.LLMVec.verify) | Check to see if something is true about the text. | + +### classify { #mall.LLMVec.classify } + +```python +LLMVec.classify(x, labels='', additional='') +``` + +Classify text into specific categories. + +#### Parameters {.doc-section .doc-section-parameters} + +| Name | Type | Description | Default | +|------------|--------|-------------------------------------------------------------------------------------------------------------------------|------------| +| x | list | A list of texts | _required_ | +| labels | list | A list or a DICT object that defines the categories to classify the text as. It will return one of the provided labels. | `''` | +| additional | str | Inserts this text into the prompt sent to the LLM | `''` | + +#### Examples {.doc-section .doc-section-examples} + +```{python} +llm.classify(['this is important!', 'there is no rush'], ['urgent', 'not urgent']) +``` + +### custom { #mall.LLMVec.custom } + +```python +LLMVec.custom(x, prompt='', valid_resps='') +``` + +Provide the full prompt that the LLM will process. + +#### Parameters {.doc-section .doc-section-parameters} + +| Name | Type | Description | Default | +|--------|--------|----------------------------------------------------|------------| +| x | list | A list of texts | _required_ | +| prompt | str | The prompt to send to the LLM along with the `col` | `''` | + +### extract { #mall.LLMVec.extract } + +```python +LLMVec.extract(x, labels='', additional='') +``` + +Pull a specific label from the text. + +#### Parameters {.doc-section .doc-section-parameters} + +| Name | Type | Description | Default | +|------------|--------|--------------------------------------------------------------------------------|------------| +| x | list | A list of texts | _required_ | +| labels | list | A list or a DICT object that defines tells the LLM what to look for and return | `''` | +| additional | str | Inserts this text into the prompt sent to the LLM | `''` | + +#### Examples {.doc-section .doc-section-examples} + +```{python} +llm.extract(['bob smith, 123 3rd street'], labels=['name', 'address']) +``` + +### sentiment { #mall.LLMVec.sentiment } + +```python +LLMVec.sentiment(x, options=['positive', 'negative', 'neutral'], additional='') +``` + +Use an LLM to run a sentiment analysis + +#### Parameters {.doc-section .doc-section-parameters} + +| Name | Type | Description | Default | +|------------|--------------|----------------------------------------------------------------|---------------------------------------| +| x | list | A list of texts | _required_ | +| options | list or dict | A list of the sentiment options to use, or a named DICT object | `['positive', 'negative', 'neutral']` | +| additional | str | Inserts this text into the prompt sent to the LLM | `''` | + +#### Examples {.doc-section .doc-section-examples} + +```{python} +llm.sentiment(['I am happy', 'I am sad']) +``` + +### summarize { #mall.LLMVec.summarize } + +```python +LLMVec.summarize(x, max_words=10, additional='') +``` + +Summarize the text down to a specific number of words. + +#### Parameters {.doc-section .doc-section-parameters} + +| Name | Type | Description | Default | +|------------|--------|---------------------------------------------------|------------| +| x | list | A list of texts | _required_ | +| max_words | int | Maximum number of words to use for the summary | `10` | +| additional | str | Inserts this text into the prompt sent to the LLM | `''` | + +#### Examples {.doc-section .doc-section-examples} + +```{python} +llm.summarize(['This has been the best TV Ive ever used. Great screen, and sound.'], max_words = 5) +``` + +### translate { #mall.LLMVec.translate } + +```python +LLMVec.translate(x, language='', additional='') +``` + +Translate text into another language. + +#### Parameters {.doc-section .doc-section-parameters} + +| Name | Type | Description | Default | +|------------|--------|------------------------------------------------------------|------------| +| x | list | A list of texts | _required_ | +| language | str | The target language to translate to. For example 'French'. | `''` | +| additional | str | Inserts this text into the prompt sent to the LLM | `''` | + +#### Examples {.doc-section .doc-section-examples} + +```{python} +llm.translate(['This has been the best TV Ive ever used. Great screen, and sound.'], language = 'spanish') +``` + +### verify { #mall.LLMVec.verify } + +```python +LLMVec.verify(x, what='', yes_no=[1, 0], additional='') +``` + +Check to see if something is true about the text. + +#### Parameters {.doc-section .doc-section-parameters} + +| Name | Type | Description | Default | +|------------|--------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------| +| x | list | A list of texts | _required_ | +| what | str | The statement or question that needs to be verified against the provided text | `''` | +| yes_no | list | A positional list of size 2, which contains the values to return if true and false. The first position will be used as the 'true' value, and the second as the 'false' value | `[1, 0]` | +| additional | str | Inserts this text into the prompt sent to the LLM | `''` | \ No newline at end of file diff --git a/reference/MallFrame.qmd b/reference/MallFrame.qmd index 05031ff..d01ae23 100644 --- a/reference/MallFrame.qmd +++ b/reference/MallFrame.qmd @@ -1,6 +1,8 @@ # MallFrame { #mall.MallFrame } -`MallFrame(self, df)` +```python +MallFrame(df) +``` Extension to Polars that add ability to use an LLM to run batch predictions over a data frame @@ -36,20 +38,22 @@ reviews.llm.use(options = dict(seed = 100)) ### classify { #mall.MallFrame.classify } -`MallFrame.classify(col, labels='', additional='', pred_name='classify')` +```python +MallFrame.classify(col, labels='', additional='', pred_name='classify') +``` Classify text into specific categories. -#### Parameters +#### Parameters {.doc-section .doc-section-parameters} -| Name | Type | Description | Default | -|--------------|--------|-------------------------------------------------------------------------------------------------------------------------|--------------| -| `col` | str | The name of the text field to process | _required_ | -| `labels` | list | A list or a DICT object that defines the categories to classify the text as. It will return one of the provided labels. | `''` | -| `pred_name` | str | A character vector with the name of the new column where the prediction will be placed | `'classify'` | -| `additional` | str | Inserts this text into the prompt sent to the LLM | `''` | +| Name | Type | Description | Default | +|------------|--------|-------------------------------------------------------------------------------------------------------------------------|--------------| +| col | str | The name of the text field to process | _required_ | +| labels | list | A list or a DICT object that defines the categories to classify the text as. It will return one of the provided labels. | `''` | +| pred_name | str | A character vector with the name of the new column where the prediction will be placed | `'classify'` | +| additional | str | Inserts this text into the prompt sent to the LLM | `''` | -#### Examples +#### Examples {.doc-section .doc-section-examples} ```{python} reviews.llm.classify("review", ["appliance", "computer"]) @@ -67,19 +71,21 @@ reviews.llm.classify("review", {"appliance" : "1", "computer" : "2"}) ### custom { #mall.MallFrame.custom } -`MallFrame.custom(col, prompt='', valid_resps='', pred_name='custom')` +```python +MallFrame.custom(col, prompt='', valid_resps='', pred_name='custom') +``` Provide the full prompt that the LLM will process. -#### Parameters +#### Parameters {.doc-section .doc-section-parameters} -| Name | Type | Description | Default | -|-------------|--------|----------------------------------------------------------------------------------------|------------| -| `col` | str | The name of the text field to process | _required_ | -| `prompt` | str | The prompt to send to the LLM along with the `col` | `''` | -| `pred_name` | str | A character vector with the name of the new column where the prediction will be placed | `'custom'` | +| Name | Type | Description | Default | +|-----------|--------|----------------------------------------------------------------------------------------|------------| +| col | str | The name of the text field to process | _required_ | +| prompt | str | The prompt to send to the LLM along with the `col` | `''` | +| pred_name | str | A character vector with the name of the new column where the prediction will be placed | `'custom'` | -#### Examples +#### Examples {.doc-section .doc-section-examples} ```{python} my_prompt = ( @@ -94,20 +100,28 @@ reviews.llm.custom("review", prompt = my_prompt) ### extract { #mall.MallFrame.extract } -`MallFrame.extract(col, labels='', expand_cols=False, additional='', pred_name='extract')` +```python +MallFrame.extract( + col, + labels='', + expand_cols=False, + additional='', + pred_name='extract', +) +``` Pull a specific label from the text. -#### Parameters +#### Parameters {.doc-section .doc-section-parameters} -| Name | Type | Description | Default | -|--------------|--------|----------------------------------------------------------------------------------------|-------------| -| `col` | str | The name of the text field to process | _required_ | -| `labels` | list | A list or a DICT object that defines tells the LLM what to look for and return | `''` | -| `pred_name` | str | A character vector with the name of the new column where the prediction will be placed | `'extract'` | -| `additional` | str | Inserts this text into the prompt sent to the LLM | `''` | +| Name | Type | Description | Default | +|------------|--------|----------------------------------------------------------------------------------------|-------------| +| col | str | The name of the text field to process | _required_ | +| labels | list | A list or a DICT object that defines tells the LLM what to look for and return | `''` | +| pred_name | str | A character vector with the name of the new column where the prediction will be placed | `'extract'` | +| additional | str | Inserts this text into the prompt sent to the LLM | `''` | -#### Examples +#### Examples {.doc-section .doc-section-examples} ```{python} # Use 'labels' to let the function know what to extract @@ -146,20 +160,27 @@ reviews.llm.extract( ### sentiment { #mall.MallFrame.sentiment } -`MallFrame.sentiment(col, options=['positive', 'negative', 'neutral'], additional='', pred_name='sentiment')` +```python +MallFrame.sentiment( + col, + options=['positive', 'negative', 'neutral'], + additional='', + pred_name='sentiment', +) +``` Use an LLM to run a sentiment analysis -#### Parameters +#### Parameters {.doc-section .doc-section-parameters} -| Name | Type | Description | Default | -|--------------|--------------|----------------------------------------------------------------------------------------|---------------------------------------| -| `col` | str | The name of the text field to process | _required_ | -| `options` | list or dict | A list of the sentiment options to use, or a named DICT object | `['positive', 'negative', 'neutral']` | -| `pred_name` | str | A character vector with the name of the new column where the prediction will be placed | `'sentiment'` | -| `additional` | str | Inserts this text into the prompt sent to the LLM | `''` | +| Name | Type | Description | Default | +|------------|--------------|----------------------------------------------------------------------------------------|---------------------------------------| +| col | str | The name of the text field to process | _required_ | +| options | list or dict | A list of the sentiment options to use, or a named DICT object | `['positive', 'negative', 'neutral']` | +| pred_name | str | A character vector with the name of the new column where the prediction will be placed | `'sentiment'` | +| additional | str | Inserts this text into the prompt sent to the LLM | `''` | -#### Examples +#### Examples {.doc-section .doc-section-examples} ```{python} reviews.llm.sentiment("review") @@ -182,20 +203,22 @@ reviews.llm.sentiment("review", {"positive" : 1, "negative" : 0}) ### summarize { #mall.MallFrame.summarize } -`MallFrame.summarize(col, max_words=10, additional='', pred_name='summary')` +```python +MallFrame.summarize(col, max_words=10, additional='', pred_name='summary') +``` Summarize the text down to a specific number of words. -#### Parameters +#### Parameters {.doc-section .doc-section-parameters} -| Name | Type | Description | Default | -|--------------|--------|----------------------------------------------------------------------------------------|-------------| -| `col` | str | The name of the text field to process | _required_ | -| `max_words` | int | Maximum number of words to use for the summary | `10` | -| `pred_name` | str | A character vector with the name of the new column where the prediction will be placed | `'summary'` | -| `additional` | str | Inserts this text into the prompt sent to the LLM | `''` | +| Name | Type | Description | Default | +|------------|--------|----------------------------------------------------------------------------------------|-------------| +| col | str | The name of the text field to process | _required_ | +| max_words | int | Maximum number of words to use for the summary | `10` | +| pred_name | str | A character vector with the name of the new column where the prediction will be placed | `'summary'` | +| additional | str | Inserts this text into the prompt sent to the LLM | `''` | -#### Examples +#### Examples {.doc-section .doc-section-examples} ```{python} # Use max_words to set the maximum number of words to use for the summary @@ -209,20 +232,22 @@ reviews.llm.summarize("review", 5, pred_name = "review_summary") ### translate { #mall.MallFrame.translate } -`MallFrame.translate(col, language='', additional='', pred_name='translation')` +```python +MallFrame.translate(col, language='', additional='', pred_name='translation') +``` Translate text into another language. -#### Parameters +#### Parameters {.doc-section .doc-section-parameters} -| Name | Type | Description | Default | -|--------------|--------|----------------------------------------------------------------------------------------|-----------------| -| `col` | str | The name of the text field to process | _required_ | -| `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 | `'translation'` | -| `additional` | str | Inserts this text into the prompt sent to the LLM | `''` | +| Name | Type | Description | Default | +|------------|--------|----------------------------------------------------------------------------------------|-----------------| +| col | str | The name of the text field to process | _required_ | +| 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 | `'translation'` | +| additional | str | Inserts this text into the prompt sent to the LLM | `''` | -#### Examples +#### Examples {.doc-section .doc-section-examples} ```{python} reviews.llm.translate("review", "spanish") @@ -234,21 +259,23 @@ reviews.llm.translate("review", "french") ### use { #mall.MallFrame.use } -`MallFrame.use(backend='', model='', _cache='_mall_cache', **kwargs)` +```python +MallFrame.use(backend='', model='', _cache='_mall_cache', **kwargs) +``` Define the model, backend, and other options to use to interact with the LLM. -#### Parameters +#### Parameters {.doc-section .doc-section-parameters} -| Name | Type | Description | Default | -|------------|-------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------| -| `backend` | str \| Chat | The name of the backend to use, or a `chatlas` chat object. At the beginning of the session it defaults to "ollama". If passing `""`, it will remain unchanged | `''` | -| `model` | str | The name of the model tha the backend should use. At the beginning of the session it defaults to "llama3.2". If passing `""`, it will remain unchanged | `''` | -| `_cache` | str | The path of where to save the cached results. Passing `""` disables the cache | `'_mall_cache'` | -| `**kwargs` | | Arguments to pass to the downstream Python call. In this case, the `chat` function in `ollama` | `{}` | +| Name | Type | Description | Default | +|----------|-----------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------| +| backend | str \| Chat \| Client | The name of the backend to use, or an Ollama Client object, or a `chatlas` Chat object. At the beginning of the session it defaults to "ollama". If passing `""`, it will remain unchanged | `''` | +| model | str | The name of the model tha the backend should use. At the beginning of the session it defaults to "llama3.2". If passing `""`, it will remain unchanged | `''` | +| _cache | str | The path of where to save the cached results. Passing `""` disables the cache | `'_mall_cache'` | +| **kwargs | | Arguments to pass to the downstream Python call. In this case, the `chat` function in `ollama` | `{}` | -#### Examples +#### Examples {.doc-section .doc-section-examples} ```{python} # Additional arguments will be passed 'as-is' to the @@ -274,7 +301,7 @@ reviews.llm.use(_cache = "") ``` ```{python} -# Use a `chatlas` object +# Use a `chatlas` object from chatlas import ChatOpenAI chat = ChatOpenAI() reviews.llm.use(chat) @@ -282,21 +309,23 @@ reviews.llm.use(chat) ### verify { #mall.MallFrame.verify } -`MallFrame.verify(col, what='', yes_no=[1, 0], additional='', pred_name='verify')` +```python +MallFrame.verify(col, what='', yes_no=[1, 0], additional='', pred_name='verify') +``` Check to see if something is true about the text. -#### Parameters +#### Parameters {.doc-section .doc-section-parameters} -| Name | Type | Description | Default | -|--------------|--------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------| -| `col` | str | The name of the text field to process | _required_ | -| `what` | str | The statement or question that needs to be verified against the provided text | `''` | -| `yes_no` | list | A positional list of size 2, which contains the values to return if true and false. The first position will be used as the 'true' value, and the second as the 'false' value | `[1, 0]` | -| `pred_name` | str | A character vector with the name of the new column where the prediction will be placed | `'verify'` | -| `additional` | str | Inserts this text into the prompt sent to the LLM | `''` | +| Name | Type | Description | Default | +|------------|--------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------| +| col | str | The name of the text field to process | _required_ | +| what | str | The statement or question that needs to be verified against the provided text | `''` | +| yes_no | list | A positional list of size 2, which contains the values to return if true and false. The first position will be used as the 'true' value, and the second as the 'false' value | `[1, 0]` | +| pred_name | str | A character vector with the name of the new column where the prediction will be placed | `'verify'` | +| additional | str | Inserts this text into the prompt sent to the LLM | `''` | -#### Examples +#### Examples {.doc-section .doc-section-examples} ```{python} reviews.llm.verify("review", "is the customer happy") diff --git a/reference/_api_index.qmd b/reference/_api_index.qmd index 093be32..b5e7f51 100644 --- a/reference/_api_index.qmd +++ b/reference/_api_index.qmd @@ -1,9 +1,17 @@ # Function reference {.doc .doc-index} -## mall +## Polars | | | | --- | --- | -| [MallFrame](MallFrame.qmd#mall.MallFrame) | Extension to Polars that add ability to use | \ No newline at end of file +| [MallFrame](MallFrame.qmd#mall.MallFrame) | Extension to Polars that add ability to use | + +## Vectors + + + +| | | +| --- | --- | +| [LLMVec](LLMVec.qmd#mall.LLMVec) | Class that adds ability to use an LLM to run batch predictions | \ No newline at end of file diff --git a/reference/index.qmd b/reference/index.qmd index 22633e7..670db0b 100644 --- a/reference/index.qmd +++ b/reference/index.qmd @@ -8,7 +8,7 @@ -[MallFrame](MallFrame.qmd#mall.MallFrame) +### MallFrame       Extension to Polars that add ability to use an LLM to run batch predictions over a data frame @@ -24,5 +24,22 @@ an LLM to run batch predictions over a data frame | [use](MallFrame.qmd#mall.MallFrame.use) | Define the model, backend, and other options to use to | | [verify](MallFrame.qmd#mall.MallFrame.verify) | Check to see if something is true about the text. | +

+ +### LLMVec + +       Class that adds ability to use an LLM +to run batch predictions + +| Name | Description | +| --- | --- | +| [classify](LLMVec.qmd#mall.LLMVec.classify) | Classify text into specific categories. | +| [custom](LLMVec.qmd#mall.LLMVec.custom) | Provide the full prompt that the LLM will process. | +| [extract](LLMVec.qmd#mall.LLMVec.extract) | Pull a specific label from the text. | +| [sentiment](LLMVec.qmd#mall.LLMVec.sentiment) | Use an LLM to run a sentiment analysis | +| [summarize](LLMVec.qmd#mall.LLMVec.summarize) | Summarize the text down to a specific number of words. | +| [translate](LLMVec.qmd#mall.LLMVec.translate) | Translate text into another language. | +| [verify](LLMVec.qmd#mall.LLMVec.verify) | Check to see if something is true about the text. | + :::