Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add MMLUScore operator #728

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions tests/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,32 @@ def test_bleu_operator():
# Print the BLEU scores
print(scores)

def test_mmlu_operator():
import polars as pl
from uptrain.operators import MMLUScore

# Create a DataFrame
df = pl.DataFrame(
{
"text_generated": [
"This is the generated text.",
"Another generated sentence.",
],
"text_source": [
"This is the original source text.",
"This is a different source text.",
],
}
)

# Create an instance of the MMLUScore class
mmlu_op = MMLUScore()

# Calculate the MMLU scores
scores = mmlu_op.run(df)["output"]

# Print the MMLU scores
print(scores)

# uptrain.operators.language.meteor
def test_meteor_operator():
Expand Down
1 change: 1 addition & 0 deletions uptrain/operators/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ from .language.openai_evals import OpenaiEval, PromptEval
from .language.rouge import RougeScore
from .language.bleu import BLEUScore
from .language.meteor import METEORScore
from .language.mmlu import MMLUScore
from .language.text import (
DocsLinkVersion,
TextLength,
Expand Down
93 changes: 93 additions & 0 deletions uptrain/operators/language/mmlu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""
Implement checks to test if a piece of text aligns with the source text in terms of MMLU.

This module provides the `MMLUScore` class, which allows comparing a generated text with a source text to check their alignment using the MMLU score metric.

"""

from __future__ import annotations
import typing as t

from loguru import logger
import polars as pl
from uptrain.framework import Settings

if t.TYPE_CHECKING:
from uptrain.framework import Settings
from uptrain.operators.base import *
from uptrain.utilities import lazy_load_dep

torchmetrics_text = lazy_load_dep("torchmetrics.functional.text", "torchmetrics")

@register_op
class MMLUScore(ColumnOp):
"""
Operator to compare a generated text with a source text using the MMLU score metric.

Attributes:
col_in_generated (str): The name of the input column containing the generated text.
col_in_source (str): The name of the input column containing the source text.
col_out (str): The name of the output column containing the MMLU scores.

Returns:
dict: A dictionary containing the MMLU scores for each pair of generated and source text.

Example:
```
import polars as pl
from uptrain.operators import MMLUScore

# Create a DataFrame
df = pl.DataFrame({
"text_generated": ["This is the generated text.", "Another generated sentence."],
"text_source": ["This is the original source text.", "This is a different source text."]
})

# Create an instance of the MMLUScore class
mmlu_op = MMLUScore()

# Calculate the MMLU scores
scores = mmlu_op.run(df)["output"]

# Print the MMLU scores
print(scores["mmlu_score"])
```

Output:
```
shape: (2,)
Series: 'mmlu_score' [i64]
[
65
0
])
```

"""

col_in_generated: str = "text_generated"
col_in_source: str = "text_source"
col_out: str = "mmlu_score"

def setup(self, settings: Settings):
return self

def run(self, data: pl.DataFrame) -> TYPE_TABLE_OUTPUT:
text_generated = data.get_column(self.col_in_generated)
text_source = data.get_column(self.col_in_source)

results = []
scores = []
for i in range(len(text_generated)):
scorer = torchmetrics_text.mmlu_score
if text_source[i] is None or text_generated[i] is None:
scores.append({"mmlu": 0})
else:
preds = text_generated[i]
target = [text_source[i]]
scores.append(
{"mmlu": scorer(preds, target).item()}
)

results = pl.Series([int(x["mmlu"] * 100) for x in scores])
return {"output": data.with_columns([results.alias(self.col_out)])}