SelfCritique module
SelfCritique
Bases: Module
Useful to critique the given inputs.
This component critique the inputs given and eventually generate an intermediate reward between 0.0 and 1.0.
You can enable or disable the intermediate reward computation by
using the return_reward flag (default to True).
The scale the language model picks the reward from is controlled by
score_type: synalinks.Score (default, 11 float levels), a finer
synalinks.FineScore (21 levels), or an integer Likert-style
synalinks.Rating (1 to 5), synalinks.Rating10 (1 to 10) or
synalinks.Rating20 (1 to 20). Whatever the scale, the reward in
the module's output is automatically normalized to a float between
0.0 (lowest level) and 1.0 (highest level), so downstream consumers
such as ProgramAsJudge / LMAsJudge always see a 0..1 reward.
To have more accurate results, ensure that the inputs are provided along
with the output to evaluate using return_inputs in your modules.
Example:
import synalink
import asyncio
class Query(synalinks.DataModel):
query: str = synalinks.Field(
description="The user query",
)
class Answer(synalinks.DataModel):
answer: str = synalinks.Field(
description="The correct answer",
)
async def main():
language_model = synalinks.LanguageModel(
model="ollama/mistral",
)
x0 = synalinks.Input(data_model=Query)
x1 = await synalinks.ChainOfThought(
data_model=Answer,
language_model=language_model,
return_inputs=True,
)(x0)
x2 = await synalinks.SelfCritique(
language_model=language_model,
)(x1)
program = synalinks.Program(
inputs=x0,
outputs=x2,
name="answer_with_cot_and_self_critique",
description="Useful to answer accurately",
)
if __name__ == "__main__":
asyncio.run(main())
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
language_model
|
LanguageModel
|
The language model to use. |
None
|
prompt_template
|
str
|
The jinja2 prompt template (see |
None
|
examples
|
list
|
The default list of examples, the examples are a list of tuples containing input/output JSON pairs. |
None
|
instructions
|
str
|
The default instructions being a string containing
instructions for the language model. If not provided, defaults to
|
None
|
seed_instructions
|
list
|
Optional. A list of instructions to use as seed for the optimization. If not provided, use the default instructions as seed. |
None
|
temperature
|
float
|
Optional. The temperature for the LM call. |
None
|
max_tokens
|
int
|
Optional. Maximum number of tokens to generate. Default None (the model's own default; caps generation length when set). |
None
|
top_p
|
float
|
Optional. The nucleus sampling probability for the LM call. Default None (the model's own default). |
None
|
top_k
|
int
|
Optional. The top-k sampling cutoff for the LM call. Default None (the model's own default). |
None
|
reasoning_effort
|
string
|
Optional. The reasoning effort for the LM call between ['minimal', 'low', 'medium', 'high', 'disable', 'none', None]. Default to None (no reasoning). |
None
|
use_inputs_schema
|
bool
|
Optional. Whether or not use the inputs schema in
the prompt (Default to False) (see |
False
|
use_outputs_schema
|
bool
|
Optional. Whether or not use the outputs schema in
the prompt (Default to False) (see |
False
|
return_reward
|
bool
|
Optional. Whether or not to compute an intermediate reward. |
True
|
score_type
|
type | str
|
Optional. The scale the language model picks the
reward from: |
None
|
return_inputs
|
bool
|
Optional. Whether or not to concatenate the inputs to
the outputs (Default to True) (see |
True
|
name
|
str
|
Optional. The name of the module. |
None
|
description
|
str
|
Optional. The description of the module. |
None
|
trainable
|
bool
|
Whether the module's variables should be trainable. |
True
|
Source code in synalinks/src/modules/ttc/self_critique.py
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 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 | |
critique_with_reward_schema(score_type)
Build the CritiqueWithReward schema for a given score type.
The reward property is replaced by an inline enum of the members of
score_type (e.g. [1, 2, 3, 4, 5] for Rating) so the language
model picks a value on that scale; $defs from the default Score
field are dropped so the schema stays self-contained.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
score_type
|
type | str
|
The score scale (see |
required |
Returns:
| Type | Description |
|---|---|
dict
|
The JSON schema handed to the underlying |
Source code in synalinks/src/modules/ttc/self_critique.py
default_critique_instructions(score_type=None, return_reward=True)
Return the default instructions of SelfCritique for a score scale.
The instructions spell out the grading scale in plain words (e.g. "an
integer between 1 and 5, 1 being very bad and 5 very good") so the
language model knows what the reward means even when the output schema
is not included in the prompt (use_outputs_schema=False, the default).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
score_type
|
type | str
|
The score scale (see |
None
|
return_reward
|
bool
|
Whether a |
True
|
Returns:
| Type | Description |
|---|---|
str
|
The instructions string. |