RegexMatch

class opik.evaluation.metrics.RegexMatch(regex: str | Pattern, name: str = 'regex_match_metric', track: bool = True)

Bases: BaseMetric

A metric that checks if an output string matches a given regular expression pattern.

This metric returns a score of 1.0 if the output string matches the regex pattern, and 0.0 otherwise.

Parameters:
  • regex – The regular expression pattern to match against. Can be a string or a compiled regex pattern.

  • name – The name of the metric. Defaults to “regex_match_metric”.

  • track – Whether to track the metric. Defaults to True.

Example

>>> from opik.evaluation.metrics import RegexMatch
>>> regex_metric = RegexMatch(r"\d{3}-\d{2}-\d{4}")
>>> result = regex_metric.score("My SSN is 123-45-6789")
>>> print(result.value)
1.0
>>> result = regex_metric.score("My phone is 555-1234")
>>> print(result.value)
0.0
score(output: str, **ignored_kwargs: Any) ScoreResult

Calculate the score based on whether the output string matches the regex pattern.

Parameters:
  • output – The output string to check against the regex pattern.

  • **ignored_kwargs – Additional keyword arguments that are ignored.

Returns:

A ScoreResult object with a value of 1.0 if the output

matches the regex pattern, 0.0 otherwise.

Return type:

score_result.ScoreResult

async ascore(*args: Any, **kwargs: Any) ScoreResult | List[ScoreResult]

Async public method that can be called independently.