RLMAsJudge reward
RLMAsJudge
Bases: ProgramAsJudge
Evaluate the output of a program using a RecursiveLanguageModelAgent.
Where AgentAsJudge calls a fixed set of tools, this judge writes Python
in a persistent sandbox: the gold reference and the prediction are bound
as the inputs dict, so it can execute the predicted code, recompute a
result, diff long outputs field by field, or delegate semantic comparison
of carved-out snippets to a sub-LM through llm_query. The prompt only
sees a summary of the inputs, which keeps the judge usable on predictions
too large to grade in one context window. It ends by calling
submit(result={"critique": ..., "reward": ...}); if it runs out of
iterations first, a final grading step formats the trajectory instead.
Because the judge can compute its verdict in Python (a pass rate, a
field-by-field match ratio), the reward is not restricted to the discrete
members of score_type: any integer or float between its lowest and
highest member is accepted, then normalized to 0.0..1.0.
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 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 in its sandbox before grading.
program.compile(
reward=synalinks.rewards.RLMAsJudge(
language_model=language_model,
# The sub-LM helpers are not needed to check arithmetic.
recursive=False,
max_iterations=5,
),
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
|
sub_language_model
|
LanguageModel
|
Optional. The sub-LM behind
|
None
|
tools
|
list
|
Optional. Extra |
None
|
native_tools
|
list
|
Optional. |
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 code turns.
Defaults to |
None
|
final_instructions
|
str
|
Optional. The instructions for the fallback
grading turn, used when the judge runs out of iterations without
calling |
None
|
score_type
|
type | str
|
Optional. The scale bounding the reward:
|
Rating20
|
max_iterations
|
int
|
Optional. The maximum number of code turns before the fallback grading step (Default to 20). |
20
|
use_chain_of_thought
|
bool
|
Optional. Whether the code turns think step by step before writing a snippet (Default to False). |
False
|
timeout
|
int
|
Optional. Per-turn execution budget in seconds (Default to 60). |
60
|
recursive
|
bool
|
Optional. Whether to expose |
True
|
max_llm_calls
|
int
|
Optional. The sub-LM call budget per evaluation (Default to 50). |
50
|
max_output_chars
|
int
|
Optional. Maximum characters of REPL output kept per turn (Default to 10_000). |
10000
|
workdir
|
str
|
Optional. Host directory seeding the sandbox
(see |
None
|
skills
|
list
|
Optional. Agent Skills roots (see |
None
|
sandbox
|
Sandbox
|
Optional. A pre-built sandbox reused across
evaluations (see |
None
|
sandbox_type
|
type
|
Optional. The |
None
|
reduction
|
str
|
Optional. The reward reduction (Default to |
'mean'
|
name
|
str
|
Optional. string name of the reward instance. |
'rlm_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/rlm_as_judge.py
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 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 | |
RLMAsJudgeProgram
Bases: Program
Evaluate the output of a program using a RecursiveLanguageModelAgent.
The judge writes Python in a persistent sandbox to verify the prediction,
then submits a critique and a reward. The reward is any integer or float
within the bounds of score_type, so it can be computed in code; it is
normalized to 0.0..1.0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language_model
|
LanguageModel
|
The language model to use. |
None
|
sub_language_model
|
LanguageModel
|
Optional. The sub-LM behind
|
None
|
tools
|
list
|
Optional. Extra |
None
|
native_tools
|
list
|
Optional. |
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 code turns.
Defaults to |
None
|
final_instructions
|
str
|
Optional. The instructions for the fallback
grading turn, used when the judge runs out of iterations without
calling |
None
|
score_type
|
type | str
|
Optional. The scale bounding the reward
(see |
Rating20
|
max_iterations
|
int
|
Optional. The maximum number of code turns before the fallback grading step (Default to 20). |
20
|
use_chain_of_thought
|
bool
|
Optional. Whether the code turns think step by step before writing a snippet (Default to False). |
False
|
timeout
|
int
|
Optional. Per-turn execution budget in seconds (Default to 60). |
60
|
recursive
|
bool
|
Optional. Whether to expose |
True
|
max_llm_calls
|
int
|
Optional. The sub-LM call budget per evaluation (Default to 50). |
50
|
max_output_chars
|
int
|
Optional. Maximum characters of REPL output kept per turn (Default to 10_000). |
10000
|
workdir
|
str
|
Optional. Host directory seeding the sandbox
(see |
None
|
skills
|
list
|
Optional. Agent Skills roots (see |
None
|
sandbox
|
Sandbox
|
Optional. A pre-built sandbox reused across
evaluations (see |
None
|
sandbox_type
|
type
|
Optional. The |
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/rlm_as_judge.py
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 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 | |
default_rlm_judge_instructions(score_type=Rating20, recursive=True, max_llm_calls=50)
Return the default instructions of RLMAsJudge for a score scale.
The instructions are the RecursiveLanguageModelAgent defaults (how to
drive the sandbox, and the llm_query helpers when recursive is set)
followed by the judging task: where the prediction and the gold reference
live in the inputs dict, how to verify with code, and what to submit.
The grading scale is spelled out in plain words so the language model
knows what the reward means.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
score_type
|
type | str
|
The score scale (see |
Rating20
|
recursive
|
bool
|
Whether the sub-LM helpers are available. |
True
|
max_llm_calls
|
int
|
The sub-LM call budget rendered in the recursive instructions. |
50
|
Returns:
| Type | Description |
|---|---|
str
|
The instructions string. |
Source code in synalinks/src/rewards/rlm_as_judge.py
rlm_critique_schema(score_type=Rating20)
Build the CritiqueWithReward schema handed to the RLM judge.
Same as critique_with_reward_schema, except the reward is a bounded
number rather than an enum of the members of score_type, so a value
computed inside the sandbox (a pass rate scaled to the range, say)
passes submit validation whether it is an int or a float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
score_type
|
type | str
|
The score scale (see |
Rating20
|
Returns:
| Type | Description |
|---|---|
dict
|
The JSON schema of the judge's final answer. |
Source code in synalinks/src/rewards/rlm_as_judge.py
rlm_reward_description(score_type=Rating20)
Return the LM-facing description of the RLMAsJudge reward scale.
The judge computes its reward in Python, so unlike LMAsJudge it is not
limited to the discrete members of score_type: any integer or float
between the lowest and highest member is accepted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
score_type
|
type | str
|
The score scale (see |
Rating20
|
Returns:
| Type | Description |
|---|---|
str
|
The description string. |