-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from mlverse/verify
Adds verify to mall
- Loading branch information
Showing
23 changed files
with
807 additions
and
33 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"hash": "07283324dfba84486e7dfa2870efbcb4", | ||
"result": { | ||
"engine": "knitr", | ||
"markdown": "---\ntitle: \"Verify if a statement about the text is true or not\"\nexecute:\n eval: true\n freeze: true\n---\n\n\n\n\n\n[R/llm-verify.R](https://github.com/mlverse/mall/blob/main/R/llm-verify.R)\n\n## llm_verify\n\n## Description\n Use a Large Language Model (LLM) to see if something is true or not based the provided text \n\n\n## Usage\n```r\n \nllm_verify( \n .data, \n col, \n what, \n yes_no = factor(c(1, 0)), \n pred_name = \".verify\", \n additional_prompt = \"\" \n) \n \nllm_vec_verify( \n x, \n what, \n yes_no = factor(c(1, 0)), \n additional_prompt = \"\", \n preview = FALSE \n) \n```\n\n## Arguments\n|Arguments|Description|\n|---|---|\n| .data | A `data.frame` or `tbl` object that contains the text to be analyzed |\n| col | The name of the field to analyze, supports `tidy-eval` |\n| what | The statement or question that needs to be verified against the provided text |\n| yes_no | A size 2 vector that specifies the expected output. It is positional. The first item is expected to be value to return if the statement about the provided text is true, and the second if it is not. Defaults to: `factor(c(1, 0))` |\n| pred_name | A character vector with the name of the new column where the prediction will be placed |\n| additional_prompt | Inserts this text into the prompt sent to the LLM |\n| x | A vector that contains the text to be analyzed |\n| preview | It returns the R call that would have been used to run the prediction. It only returns the first record in `x`. Defaults to `FALSE` Applies to vector function only. |\n\n\n\n## Value\n `llm_verify` returns a `data.frame` or `tbl` object. `llm_vec_verify` returns a vector that is the same length as `x`. \n\n\n## Examples\n\n\n::: {.cell}\n\n```{.r .cell-code}\n \nlibrary(mall) \n \ndata(\"reviews\") \n \nllm_use(\"ollama\", \"llama3.2\", seed = 100, .silent = TRUE) \n \n# By default it will return 1 for 'true', and 0 for 'false', \n# the new column will be a factor type \nllm_verify(reviews, review, \"is the customer happy\") \n#> # A tibble: 3 × 2\n#> review .verify\n#> <chr> <fct> \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# The yes_no argument can be modified to return a different response \n# than 1 or 0. First position will be 'true' and second, 'false' \nllm_verify(reviews, review, \"is the customer happy\", c(\"y\", \"n\")) \n#> # A tibble: 3 × 2\n#> review .verify\n#> <chr> <chr> \n#> 1 This has been the best TV I've ever used. Great screen, and sound. y \n#> 2 I regret buying this laptop. It is too slow and the keyboard is too n… n \n#> 3 Not sure how to feel about my new washing machine. Great color, but h… n\n \n# Number can also be used, this would be in the case that you wish to match \n# the output values of existing predictions \nllm_verify(reviews, review, \"is the customer happy\", c(2, 1)) \n#> # A tibble: 3 × 2\n#> review .verify\n#> <chr> <dbl>\n#> 1 This has been the best TV I've ever used. Great screen, and sound. 2\n#> 2 I regret buying this laptop. It is too slow and the keyboard is too n… 1\n#> 3 Not sure how to feel about my new washing machine. Great color, but h… 1\n```\n:::\n", | ||
"supporting": [], | ||
"filters": [ | ||
"rmarkdown/pagebreak.lua" | ||
], | ||
"includes": {}, | ||
"engineDependencies": {}, | ||
"preserve": {}, | ||
"postProcess": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"project": "mall", "version": "0.0.9999", "count": 16, "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", "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"}]} | ||
{"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"}]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# mall | ||
|
||
## Intro | ||
|
||
Run multiple LLM predictions against a data frame. The predictions are | ||
processed row-wise over a specified column. It works using a | ||
pre-determined one-shot prompt, along with the current row’s content. | ||
|
||
## Install | ||
|
||
To install from Github, use: | ||
|
||
``` python | ||
pip install "mall @ git+https://[email protected]/edgararuiz/mall.git@python#subdirectory=python" | ||
``` | ||
|
||
## Examples | ||
|
||
``` python | ||
import mall | ||
import polars as pl | ||
|
||
reviews = pl.DataFrame( | ||
data=[ | ||
"This has been the best TV I've ever used. Great screen, and sound.", | ||
"I regret buying this laptop. It is too slow and the keyboard is too noisy", | ||
"Not sure how to feel about my new washing machine. Great color, but hard to figure" | ||
], | ||
schema=[("review", pl.String)], | ||
) | ||
``` | ||
|
||
## Sentiment | ||
|
||
|
||
``` python | ||
reviews.llm.sentiment("review") | ||
``` | ||
|
||
<small>shape: (3, 2)</small> | ||
|
||
| review | sentiment | | ||
|----------------------------------|------------| | ||
| str | str | | ||
| "This has been the best TV I've… | "positive" | | ||
| "I regret buying this laptop. I… | "negative" | | ||
| "Not sure how to feel about my … | "neutral" | | ||
|
||
## Summarize | ||
|
||
``` python | ||
reviews.llm.summarize("review", 5) | ||
``` | ||
|
||
<small>shape: (3, 2)</small> | ||
|
||
| review | summary | | ||
|----------------------------------|----------------------------------| | ||
| str | str | | ||
| "This has been the best TV I've… | "it's a great tv" | | ||
| "I regret buying this laptop. I… | "laptop not worth the money" | | ||
| "Not sure how to feel about my … | "feeling uncertain about new pu… | | ||
|
||
## Translate (as in ‘English to French’) | ||
|
||
``` python | ||
reviews.llm.translate("review", "spanish") | ||
``` | ||
|
||
<small>shape: (3, 2)</small> | ||
|
||
| review | translation | | ||
|----------------------------------|----------------------------------| | ||
| str | str | | ||
| "This has been the best TV I've… | "Esta ha sido la mejor TV que h… | | ||
| "I regret buying this laptop. I… | "Lo lamento comprar este portát… | | ||
| "Not sure how to feel about my … | "No estoy seguro de cómo sentir… | | ||
|
||
## Classify | ||
|
||
``` python | ||
reviews.llm.classify("review", ["computer", "appliance"]) | ||
``` | ||
|
||
<small>shape: (3, 2)</small> | ||
|
||
| review | classify | | ||
|----------------------------------|-------------| | ||
| str | str | | ||
| "This has been the best TV I've… | "appliance" | | ||
| "I regret buying this laptop. I… | "appliance" | | ||
| "Not sure how to feel about my … | "appliance" | | ||
|
||
## LLM session setup | ||
|
||
``` python | ||
reviews.llm.use(options = dict(seed = 100)) | ||
``` | ||
|
||
{'backend': 'ollama', 'model': 'llama3.2', 'options': {'seed': 100}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--- | ||
format: gfm | ||
--- | ||
|
||
# mall | ||
|
||
## Intro | ||
|
||
Run multiple LLM predictions against a data frame. The predictions are processed row-wise over a specified column. It works using a pre-determined one-shot prompt, along with the current row’s content. | ||
|
||
## Install | ||
|
||
To install from Github, use: | ||
|
||
```python | ||
pip install "mall @ git+https://[email protected]/edgararuiz/mall.git@python#subdirectory=python" | ||
``` | ||
|
||
## Examples | ||
|
||
```{python} | ||
#| include: false | ||
import polars as pl | ||
from polars.dataframe._html import HTMLFormatter | ||
html_formatter = get_ipython().display_formatter.formatters['text/html'] | ||
html_formatter.for_type(pl.DataFrame, lambda df: "\n".join(HTMLFormatter(df).render())) | ||
``` | ||
|
||
|
||
```{python} | ||
import mall | ||
import polars as pl | ||
data = mall.MallData | ||
reviews = data.reviews | ||
``` | ||
|
||
```{python} | ||
#| include: false | ||
reviews.llm.use(options = dict(seed = 100)) | ||
``` | ||
|
||
|
||
## Sentiment | ||
|
||
```{python} | ||
reviews.llm.sentiment("review") | ||
``` | ||
|
||
## Summarize | ||
|
||
```{python} | ||
reviews.llm.summarize("review", 5) | ||
``` | ||
|
||
## Translate (as in 'English to French') | ||
|
||
```{python} | ||
reviews.llm.translate("review", "spanish") | ||
``` | ||
|
||
## Classify | ||
|
||
```{python} | ||
reviews.llm.classify("review", ["computer", "appliance"]) | ||
``` | ||
|
||
## LLM session setup | ||
|
||
```{python} | ||
reviews.llm.use(options = dict(seed = 100)) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.