afnio.cognitive.modules.lm_judge_evaluator
afnio.cognitive.modules.lm_judge_evaluator.LMJudgeEvaluator
Bases: Module
Evaluates predictions using a language model (LM) as the judge.
This module leverages the LMJudgeEvaluator
operation from afnio.autodiff.evaluator to perform model-based evaluations.
The forward method accepts a list of messages that construct the evaluation
prompt, with optional inputs to dynamically fill placeholders within message
templates. A prediction is compared against a target (optional) to generate
a score and an explanation.
When processing a batch of predictions and targets, reduction_fn function
aggregates individual scores (e.g., using sum to compute a total score). The
reduction_fn_purpose parameter is a brief description of the aggregation's purpose
(e.g., "summation"). If aggregation is not desired, set reduction_fn and
reduction_fn_purpose to None. The success_fn checks if all evaluations are
successful, allowing the backward pass to skip unnecessary gradient computations.
This module supports both evaluation (eval_mode=True) and optimization
(eval_mode=False) modes.
The forward_model_client specifies the LM responsible for evaluation, while
completion_args allows customization of generation parameters like temperature,
max tokens, and seed.
Examples:
>>> from afnio import cognitive as cog
>>> from afnio.models.openai import OpenAI
>>> from afnio import set_backward_model_client
>>> fwd_model_client = OpenAI()
>>> fwd_model_args = {"model": "gpt-4o", "temperature": 0.5}
>>> set_backward_model_client("openai/gpt-4o")
>>> class Evaluator(cog.Module):
... def __init__(self):
... super().__init__()
... self.judge = cog.LMJudgeEvaluator()
... def forward(self, fwd_model, messages, prediction, target, inputs, **completion_args):
... return self.judge(fwd_model, messages, prediction, target, inputs, **completion_args)
>>> task = afnio.Variable(
... "Evaluate if the translation is {metric}.",
... role="evaluation task",
... requires_grad=True
... )
>>> format = afnio.Variable(
... "Provide 'score' (true/false) and 'explanation' in JSON.",
... role="output format"
... )
>>> metric = afnio.Variable(["accurate", "accurate"], role="metric")
>>> user = afnio.Variable(
... "<PREDICTION>{prediction}</PREDICTION><TARGET>{target}</TARGET>",
.. role="user query"
... )
>>> prediction = afnio.Variable(
... ["Hola Mundo", "Salve a tutti"],
... role="translated text",
... requires_grad=True
... )
>>> target = ["Ciao Mondo", "Salve a tutti"]
>>> messages = [
... {"role": "system", "content": [task, format]},
... {"role": "user", "content": [user]},
... ]
>>> eval = Evaluator()
>>> score, explanation = eval(
... fwd_model_client,
... messages,
... prediction,
... target,
... inputs={"metric": metric},
... reduction_fn=sum,
... reduction_fn_purpose="summation",
... **fwd_model_args
... )
>>> print(score.data)
1
>>> print(explanation.data)
'The evaluation function, designed using an LM as the judge, compared the <DATA> fields of the predicted variable and the target variable across all samples in the batch. These scores were then aggregated using the reduction function 'summation', resulting in a final aggregated score: 1.'
>>> explanation.backward()
>>> system.grad[0].data
'The translated text should be in Italian.'
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the LM response to generate the evaluation |
TypeError
|
If the types of |
ValueError
|
If the lengths of |
See Also
afnio.autodiff.evaluator.LMJudgeEvaluator
for the underlying operation.
Source code in afnio/cognitive/modules/lm_judge_evaluator.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
forward(forward_model_client, messages, prediction, target=None, inputs=None, success_fn=None, reduction_fn=sum, reduction_fn_purpose='summation', eval_mode=True, **completion_args)
Forward pass for the LM Judge evaluator function.
Warning
Users should not call this method directly. Instead, they should call the
module instance itself, which will internally invoke this forward method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
forward_model_client
|
ChatCompletionModel | None
|
The LM model client used for the forward pass evaluation. |
required |
messages
|
MultiTurnMessages
|
A list of messages that compose the prompt/context for the LM.
Each message is a dictionary with a |
required |
prediction
|
Variable
|
The predicted variable to evaluate, which can have scalar or
list |
required |
target
|
str | list[str] | Variable | None
|
The target (ground truth) to compare against, which can be a string,
a list of strings, or a |
None
|
inputs
|
dict[str, str | Variable] | None
|
A dictionary mapping placeholder names to their corresponding
values, which can be strings or |
None
|
success_fn
|
Callable[[List[Any]], bool] | None
|
A user-defined function that takes the list of scores returned
by the LM Judge and returns |
None
|
reduction_fn
|
Callable[[List[Any]], Any] | None
|
An optional function to aggregate scores across a batch of
predictions and targets. If |
sum
|
reduction_fn_purpose
|
str | Variable | None
|
A brief description of the purpose of |
'summation'
|
eval_mode
|
bool | Variable
|
Indicates the evaluation mode. If |
True
|
**completion_args
|
Additional keyword arguments to pass to the LM model
client's |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
score |
Variable
|
A variable containing the evaluation score(s),
or their aggregation if |
explanation |
Variable
|
A variable containing the explanation(s) of the evaluation,
or their aggregation if |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the LM response to generate the evaluation |
TypeError
|
If the types of |
ValueError
|
If the lengths of |
Source code in afnio/cognitive/modules/lm_judge_evaluator.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |