AgentAsJudge reward
AgentAsJudge
Bases: ProgramAsJudge
Evaluate the output of a program using a tool-calling agent.
Unlike LMAsJudge, which grades from a single read of the inputs, this
judge is a FunctionCallingAgent: it can call the given tools (run the
predicted code, read a file, query a database, fetch a page) to gather
evidence, then writes a critique and grades the prediction. Use it when
the correctness of an output cannot be judged from its text alone.
Example:
import asyncio
import numpy as np
import synalinks
class Query(synalinks.DataModel):
query: str = synalinks.Field(description="The user query")
class Answer(synalinks.DataModel):
answer: str = synalinks.Field(description="The answer to the query")
async def calculate(expression: str):
"""Calculate the result of a mathematical expression.
Use it to check any arithmetic in the prediction instead of computing
it in your head.
Args:
expression (str): The expression to evaluate, such as '2 + 2'. It
can contain numbers, operators (+, -, *, /), parentheses and
spaces.
"""
if not all(char in "0123456789+-*/(). " for char in expression):
return {"result": None, "log": "Error: invalid characters in expression"}
return {
"result": eval(expression, {"__builtins__": None}, {}),
"log": "Successfully executed",
}
async def main():
language_model = synalinks.LanguageModel(model="ollama/mistral")
# The program to train: a plain generator answering math questions.
x0 = synalinks.Input(data_model=Query)
x1 = await synalinks.Generator(
data_model=Answer,
language_model=language_model,
)(x0)
program = synalinks.Program(inputs=x0, outputs=x1, name="math_qa")
# The judge re-does the arithmetic with its tool before grading.
program.compile(
reward=synalinks.rewards.AgentAsJudge(
language_model=language_model,
tools=[synalinks.Tool(calculate)],
),
optimizer=synalinks.optimizers.RandomFewShot(),
)
x_train = np.array(
[
Query(query="How much is 152648 + 485?"),
Query(query="What is 12 * 12?"),
Query(query="Compute (3 + 4) * 5."),
Query(query="What is 1000 / 8?"),
],
dtype="object",
)
y_train = np.array(
[
Answer(answer="153133"),
Answer(answer="144"),
Answer(answer="35"),
Answer(answer="125"),
],
dtype="object",
)
history = await program.fit(
x=x_train,
y=y_train,
epochs=4,
validation_split=0.25,
callbacks=[
synalinks.callbacks.EarlyStopping(
monitor="val_reward",
mode="max",
patience=2,
),
],
)
if __name__ == "__main__":
asyncio.run(main())
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language_model
|
LanguageModel
|
The language model to use. |
None
|
tools
|
list
|
The tools the judge can call to gather evidence
(see |
None
|
prompt_template
|
str
|
The default jinja2 prompt template
to use (see |
None
|
examples
|
list
|
The default examples to use in the prompt
(see |
None
|
instructions
|
str
|
The default instructions for the tool-calling turns.
Defaults to |
None
|
final_instructions
|
str
|
Optional. The instructions for the final
grading turn. Defaults to |
None
|
score_type
|
type | str
|
Optional. The scale the judge picks the reward
from: |
Rating20
|
max_iterations
|
int
|
Optional. The maximum number of tool-calling turns before the judge must grade (Default to 5). |
5
|
use_chain_of_thought
|
bool
|
Optional. Whether the tool-calling turns think step by step before choosing tools (Default to False). |
False
|
workdir
|
str
|
Optional. The judge's working directory
(see |
None
|
skills
|
list
|
Optional. Agent Skills roots (see |
None
|
reduction
|
str
|
Optional. The reward reduction (Default to |
'mean'
|
name
|
str
|
Optional. string name of the reward instance. |
'agent_as_judge'
|
in_mask
|
list
|
Optional. list of keys to keep to compute the reward. |
None
|
out_mask
|
list
|
Optional. list of keys to remove to compute the reward. |
None
|
in_mask_pattern
|
str
|
Optional. Regex pattern; fields whose names match
are kept (combined with |
None
|
out_mask_pattern
|
str
|
Optional. Regex pattern; fields whose names match
are dropped (combined with |
None
|
Source code in synalinks/src/rewards/agent_as_judge.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | |
AgentAsJudgeProgram
Bases: Program
Evaluate the output of a program using a tool-calling agent.
The judge is a FunctionCallingAgent whose final answer is a critique and
a reward on the score_type scale; the reward is normalized to 0.0..1.0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language_model
|
LanguageModel
|
The language model to use. |
None
|
tools
|
list
|
The tools the judge can call to gather evidence
(see |
None
|
prompt_template
|
str
|
The default jinja2 prompt template
to use (see |
None
|
examples
|
list
|
The default examples to use in the prompt
(see |
None
|
instructions
|
str
|
The default instructions for the tool-calling turns.
Defaults to |
None
|
final_instructions
|
str
|
Optional. The instructions for the final
grading turn. Defaults to |
None
|
score_type
|
type | str
|
Optional. The scale the judge picks the reward
from (see |
Rating20
|
max_iterations
|
int
|
Optional. The maximum number of tool-calling turns before the judge must grade (Default to 5). |
5
|
use_chain_of_thought
|
bool
|
Optional. Whether the tool-calling turns think step by step before choosing tools (Default to False). |
False
|
workdir
|
str
|
Optional. The judge's working directory
(see |
None
|
skills
|
list
|
Optional. Agent Skills roots (see |
None
|
name
|
str
|
Optional. The name of the program. |
None
|
description
|
str
|
Optional. The description of the program. |
None
|
trainable
|
bool
|
Whether the program's variables should be trainable. |
True
|
Source code in synalinks/src/rewards/agent_as_judge.py
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 | |
default_agent_judge_instructions(score_type=Rating20)
Return the default instructions of AgentAsJudge for a score scale.
The instructions tell the judge to gather evidence with its tools before
grading, and spell out the grading scale in plain words so the language
model knows what the reward means even when the output schema is not
included in the prompt.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
score_type
|
type | str
|
The score scale (see |
Rating20
|
Returns:
| Type | Description |
|---|---|
str
|
The instructions string. |
Source code in synalinks/src/rewards/agent_as_judge.py
judge_with_agent(agent, score_type, inputs)
async
Run an agent judge on a [y_true, y_pred] pair and normalize its reward.
Shared by the agent-based judge programs (AgentAsJudgeProgram,
RLMAsJudgeProgram): the gold reference, when given, is prefixed with
gold and concatenated with the prediction, the agent grades the result
on the score_type scale, and the reward is rewritten to a 0.0..1.0 float.
An empty prediction, or an agent that produces nothing, scores 0.0 without
raising so training keeps going.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
Module
|
The judge agent; its output schema must carry a
|
required |
score_type
|
type
|
The score scale the agent grades on. |
required |
inputs
|
list
|
The |
required |
Returns:
| Type | Description |
|---|---|
JsonDataModel | SymbolicDataModel
|
The critique with a normalized reward. |