Heuristic metrics
Heuristic metrics are rule-based evaluation methods that allow you to check specific aspects of language model outputs. These metrics use predefined criteria or patterns to assess the quality, consistency, or characteristics of generated text.
You can use the following heuristic metrics:
Score an LLM response
You can score an LLM response by first initializing the metrics and then calling the score
method:
Metrics
Equals
The Equals
metric can be used to check if the output of an LLM exactly matches a specific string. It can be used in the following way:
Contains
The Contains
metric can be used to check if the output of an LLM contains a specific substring. It can be used in the following way:
RegexMatch
The RegexMatch
metric can be used to check if the output of an LLM matches a specified regular expression pattern. It can be used in the following way:
IsJson
The IsJson
metric can be used to check if the output of an LLM is valid. It can be used in the following way:
LevenshteinRatio
The LevenshteinRatio
metric can be used to check if the output of an LLM is valid. It can be used in the following way:
BLEU
The BLEU (Bilingual Evaluation Understudy) metrics estimate how close the LLM outputs are to one or more reference translations. Opik provides two separate classes:
SentenceBLEU
– Single-sentence BLEUCorpusBLEU
– Corpus-level BLEU Both rely on the underlying NLTK BLEU implementation with optional smoothing methods, weights, and variable n-gram orders.
You will need nltk library:
Use SentenceBLEU
to compute single-sentence BLEU between a single candidate and one (or more) references:
Use CorpusBLEU
to compute corpus-level BLEU for multiple candidates vs. multiple references. Each candidate and its references align by index in the list:
You can also customize n-grams, smoothing methods, or weights:
Note: If any candidate or reference is empty, SentenceBLEU or CorpusBLEU will raise a MetricComputationError. Handle or validate inputs accordingly.