File size: 20,996 Bytes
9d29c62 | 1 2 3 4 5 6 7 8 9 10 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 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 | # domain/semantic_bank.py โ V7.2.5: Semantic Composer
#
# The SemanticBank is a governed, deterministic source of pedagogical narratives.
# Instead of letting LLM #2 generate free text (risky), the NarrativeComposer picks
# from curated, teacher-grade Hebrew templates and injects server-signed results.
#
# CTO directive: text must sound like a real private tutor speaking to a 10th/12th grader.
# Write at eye level โ encouraging, intuitive, NOT like system error messages.
import random
import logging
from dataclasses import dataclass, field
from collections import Counter
from typing import List, Dict, Optional
from domain import telemetry
logger = logging.getLogger(__name__)
DRIFT_ALERT_THRESHOLD = 0.35 # Emit alert when one variant dominates above this fraction
# โโโ Data Model โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
@dataclass
class SemanticEntry:
"""
A single pedagogical concept in the bank.
Each entry holds multiple variant fragments to ensure narrative diversity.
"""
concept_tag: str # matches Planner Enum action (e.g. "SOLVE_EQUATION")
openings: List[str] # 3 opening phrases โ how to START the explanation
bridges: List[str] # 3 logic bridges โ HOW to connect the steps
analogies: List[str] # 2 analogy/metaphor sentences โ the "aha!" moments
closing: str # 1 closing encouragement sentence
# โโโ The Bank โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
SEMANTIC_BANK: Dict[str, SemanticEntry] = {
"SOLVE_EQUATION": SemanticEntry(
concept_tag="SOLVE_EQUATION",
openings=[
"ืืืื ื ืคืชืืจ ืืช ืืืฉืืืื ืฆืขื ืืืจ ืฆืขื, ืื ืชืืื, ืื ืคืืืช ืืคืืื ืืฉื ืจืื.",
"ืืื ืืืืืช ืืช ืขืจื ืื ืขืื, ืื ืื ื ืืืืืื ืืืืื ืืืชื ืืฉื ื ืฆืืื ืืืฉืืืื.",
"ื ืืฉืื ืขื ืืืฉืืืื ืืื ืืืื ืืื: ืื ืื ืฉืขืืฉืื ืืฆื ืืืื, ืขืืฉืื ืื ืืฆื ืฉืืื.",
],
bridges=[
"ืืืจื ืืคืขืืื ืืื, ืื ืื ืฉื ืืชืจ ืืื:",
"ืืืฉื ืคืฉื ืืช ืื ืื ืฉืงืืืื ื, ืืชืืฆืื ืืชืืื:",
"ืืืฉืืืื 'ืืชื ืงืชื' ืืขืืฉืื ืืจืืจ ืฉืืชืฉืืื ืืื:",
],
analogies=[
"ืืฉืื ืขื ืื ืืื ืืืื โ ืื ืื ื ืืกืืจืื ืจืืืื ืืื ืืื ืขื ืฉืื ืขืื ืืฉืืฃ ืืืืจื.",
"ืืืืืง ืืื ืฉืืฉืืงืื ืืืืืืื โ ืื ืื ื ืืืคืฉืื ืืช x ืขื ืฉืืื ืืืจ ืื ืืืื ืืืชืืื.",
],
closing="ืขืฉืืช ืืช ืื! ืื ืืืจื ืืจืฉืืืช ืฉืื ืืชืืืืงืืื ืคืืชืจืื ืืฉืืืืืช.",
),
"SIMPLIFY": SemanticEntry(
concept_tag="SIMPLIFY",
openings=[
"ืืืืืื ืืื ื ืจืื ืืกืืื, ืืื ืื ื ืกืืจ ืืืชื โ ืืื ืืชืงืคื ืืฆืืจื ืืจืื ืืืชืจ ื ืงืืื.",
"ืืคืืฉืื, ืืืืจื ืืื ืืืชืื ืืช ืืืชื ืืืืจ โ ืจืง ืืฆืืจื ืืืื ืืืืืช ืืืืชืจ.",
"ืืื ืืกืืจ ืืืจ ืืืืืื โ ืื ืื ื ืืืกืคืื ืืืืจืื ืืืืื ืืืกืืงืื ืื ืฉืืืืชืจ.",
],
bridges=[
"ืืืจื ืฉื ืืกืืฃ ืืช ืื ืืืืืจืื ืืืืืื ืืื, ืืืืืื ืืคืฉืื ืืื:",
"ืืฉืืกืืงืื ืืช ืื 'ืืจืขืฉ ืืืชืืื', ืื ืฉื ืฉืืจ ืืื:",
"ืืฆืืจื ืืคืฉืืื ืืื ืงืืื ืืืืชืจ ืฉื ืืืืืื:",
],
analogies=[
"ืืืืื ืฉืืฉ ืื ืืจืื ืฉืืจืืช ืฉื ืืกืฃ โ ืืชื ืืืืืฃ ืืืชื ืืฉืืจ ืืื ืืืื. ืื ืืืืืง ืื ืฉืื ืื ื ืขืืฉืื ืขื ืืืืืื.",
"ืืื ืืงืคื ืืคืช ืขื ืง โ ืืกืืฃ ืงืืืืช ืืช ืืืชื ืืืืืข, ืจืง ืืืืื ืฉื ืื ืืฉืืช.",
],
closing="ืืืคื! ืคืืฉืื ืื ืืืช ืืืืฉืืจืื ืืื ืฉืืืืฉืืื ืืืชืืืืงื โ ืืขืืฉืื ืืชื ืืืืข ืืขืฉืืช ืืช ืื.",
),
"FACTOR": SemanticEntry(
concept_tag="FACTOR",
openings=[
"ืคืืจืืง ืืืืจืืื ืื ืืื ืืืฆืื ืืช 'ืืื ืืื ืื' ืฉืืจืืืืื ืืช ืืืืืื ืฉืื ื.",
"ืืืงืื ืืจืืืช ืืืืื ืืื ืืืื, ืื ืื ื ืืืคืฉืื ืฉื ื ืืืืืืื ืงืื ืื ืฉืืืืคืืื ืื ืืื.",
"ืืืจืืง ืืคืืจืืง ืืืืจืืื ืืื ืืืืืช ืื 'ืืกืชืชืจ' ืืชืื ืืืืืื ืืืคืฉืจ ืืืืฆืื ืืืืฆื.",
],
bridges=[
"ืืฉื ืืฆืื ืืช ืืืืจื ืืืฉืืชืฃ, ื ืงืื:",
"ืืืจื ืืคืืจืืง, ืืืืืื ื ืืชื ืืฆืืจื ืืืคืืืช:",
"ืืืืจืืื ืฉืืจืืืืื ืืช ืืืืืื ืื:",
],
analogies=[
"ืคืืจืืง ืืืืจืืื ืืื ืืื ืืคืจืง ืืกืคืจ ืืืืืงื ืจืืฉืื ืืช โ ืื ืื ื ืืืฆืืื ืืช ื-DNA ืืืชืืื ืฉื ืืืืืื.",
"ืืืืื ืฉืืชื ืืคืจืง ืืจืื ืืืื ืืงืืคืกืืืช ืงืื ืืช โ ืื ืงืืคืกื ืืื ืืืจื. ืืืื ืื ืืจืืืืืช ืืช ืืืงืืจ.",
],
closing="ืคืืจืืง ืืืืจืืื ืืื ืืืืืง ืื ืฉืืืคืฉืจ ืื ื ืืคืชืืจ ืืฉืืืืืช ืจืืืืขืืืช โ ืชืืืจ ืืช ืืืื ืืื ืืื.",
),
"EXPAND": SemanticEntry(
concept_tag="EXPAND",
openings=[
"ืขืืฉืื ื ืคืจืืฉ ืืช ืืกืืืจืืื โ ื ืืคืื ืื ืืืืจ ืืชืื ืืกืืืจืืื ืขื ืื ืื ืฉืืืืฆื ืืื.",
"ืคืชืืืช ืกืืืจืืื ืืื ืืื 'ืืคืชืื' ืืชื ื ืขืืืคื โ ืื ืฉืืกืชืชืจ ืืคื ืื ืืืฆื ืืืืจ.",
"ื ืฉืชืืฉ ืืืืง ืืคืืืื ืืื ืืืจืืื ืืช ืืืืืื ืืืจืืืช ืืช ืื ืืืืืจืื ืื ืคืจื.",
],
bridges=[
"ืืืืจ ืคืชืืืช ืืกืืืจืืื ืืืืกืืฃ ืืืืืจืื ืืืืืื:",
"ืืฉืืจืืืืื ืืื ืืืกืืจืื:",
"ืืืืืื ืืืืจืื, ืขื ืื ืืืืืจืื ืืืืืื:",
],
analogies=[
"ืืฉืื ืขื ืื ืืื ืืืืคืื ืชืืืืจ โ ืื ืงื ืืช ืฉืืืฉื ืฉืงืื, ืื ืืื ืขื ืชืคืืืื ืืื ื ืืช, ืืชื ืืืฉื ืืื ืชืคืืืื ืืืื ืื ื ืืช ืืฉ ืืกื ืืื.",
"ืคืชืืืช ืกืืืจืืื ืืื ืืื ืืืืื ืืฆืง โ ืืืืฆืื ืืืจืืืืื ืขื ืฉืืื ืฉืืื ืื ืจืื.",
],
closing="ืืจืืืช ืืืืืื ืื ืืืืื ืืช ืืกืืกืืช ืฉืชืฉืชืืฉ ืื ืฉืื ืืฉืื โ ืืืืจ ืฉืืืช ืื.",
),
"FIND_DERIVATIVE": SemanticEntry(
concept_tag="FIND_DERIVATIVE",
openings=[
"ืื ืืืจืช ืืืืจืช ืื ื ืืื ืืืจ ืืคืื ืงืฆืื ืขืืื ืื ืืืจืืช โ ืืื ืืื 'ืื ืืืืืจืืช' ืฉื ืืืจืฃ.",
"ืืื ืืืฆืื ืืช ืื ืืืจืช, ืื ืื ื ืฉืืืืื: ืื x ืื ืงืฆืช ืงืืืื โ ืืื y ืืฉืชื ื?",
"ื ืืฉื ืืช ืื ืืืจืช ืืคื ืืืื ืืืืืจื ืฉื ืืืื โ ืื ืืจืื ืืืชืจ ืืืืจ ืืืืืืจื ืืืกืืกืืช.",
],
bridges=[
"ืืคื ืืื ืืืืืจื ืืจืฉืืืช ืื ืืืจืืช, ืงืืืื ื:",
"ืื ืืืจืช, ืฉืืืืฆืืช ืืช ืฉืืคืืข ืืืฉืืง ืืคืื ืงืฆืื, ืืื:",
"ืงืฆื ืืฉืื ืื ืืืืืื ืฉื ืืคืื ืงืฆืื ืืื:",
],
analogies=[
"ืื ืืคืื ืงืฆืื ืืื ืืกืืื ื ืกืืขื, ืื ืืืจืช ืืื ืืื-ืืืืจืืช โ ืืื ืืืืจืช ืื ืืื ืืืจ ืืชื ื ืข ืืื ืจืืข.",
"ืืืืื ืฉืืชื ืืืคืก ืขื ืืจ โ ืื ืืืจืช ืืืืจืช ืื ืืื ื ืงืืื ืขื ืืื ืืชืืืืืช. ืืืืืืช = ืขืืืื, ืฉืืืืืช = ืืืจืืื.",
],
closing="ืื ืืืจืช ืคืืชืืช ืขืืื ืฉืื โ ืื ืืชืื ืงืฆืืืช ืืขื ืืื ืืงื. ืื ืืืืื ืขื ืืืืืื ืืช ืืื.",
),
"FIND_INTEGRAL": SemanticEntry(
concept_tag="FIND_INTEGRAL",
openings=[
"ืืืื ืืืจื ืืื ืืคืขืืื ืืืคืืื ืืืืืจื โ ืื ืื ื ืฉืืืืื 'ืื ืืคืื ืงืฆืื ืฉืื ืืืจืช ืฉืื ืืื ืื?'.",
"ืืื ืืืฉื ืืช ืืืื ืืืจื, ื ืฉืชืืฉ ืื ืืกืืืืช ืืืกืืกืืืช ืืืืืง ื-C, ื-ืงืืืข ืืืื ืืืจืฆืื.",
"ืืืื ืืืจื ืืื ืืกืืื ื ืืชื ืื ื ืืฉืคืื ืฉืืื ืฉื ืคืื ืงืฆืืืช โ ืฉืื ืืช ืื ืืื ืืงืืืข ืืืื.",
],
bridges=[
"ืืืจื ืืื ืืืจืฆืื ืืคื ืืื ืืืคืืื ืฉื ืื ืืืจืช:",
"ืืคืื ืงืฆืื ืืคืจืืืืืืืืช ืฉืืจืืืื ืืช ืืืื ืืืจื ืืื:",
"ืืืฆืืข ืืืื ืืืจืฆืื ื ืืชื ืื ื:",
],
analogies=[
"ืื ืื ืืืจืช ืืื ืื-ืืืืืจืืช, ืืืื ืืืจื ืืื ืื-ืืืจืืง โ ืืื ืืืกืฃ ืืช ืื ืืฉืื ืืืื ืืงืื ืื ืืกืืื ืืื.",
"ืืฉืื ืขื ืืื ืืืจืฆืื ืืื ืืืืื ืกืจื ืืืืืจ โ ืื ืื ื 'ืืืืืจืื' ืืช ืืืืืจื ืื ืงืืืช ืืืืฆื ืฉืื.",
],
closing="ืืื ืืืจืฆืื ืืื ืื ืืฉืืื ืืื ืคืื ืืืกืืืื โ ืืขืืฉืื ืืฉ ืื ืืช ืืืื ืืืฉื ืฉืืืื, ื ืคืืื ืืขืื.",
),
"SUBSTITUTE": SemanticEntry(
concept_tag="SUBSTITUTE",
openings=[
"ืืื ืืืืืง ืืช ืืคืชืจืื (ืื ืืืฉืืื ืืืฉืื), ื ืฆืื ืืช ืืขืจื ืืืืืข ืื ืคืฉื.",
"ืืืฆืื ืืื ืืืจื ืฉืื ื ืืืืจ ืืื ืฉื ื ืืืงื ืืืขืื โ ืืฆืืืื ืื ืฉืืืืขืื ืืจืืืื ืื ืืืฆื.",
"ื ืืืืฃ ืืช ืืืฉืชื ื ืืขืจื ืื ืชืื ืื ืืฉื โ ืื ืืฉืืืจ ืื ื ืืืืื ืืจืื ืืืชืจ ืคืฉืื.",
],
bridges=[
"ืืืืจ ืืืฆืื ืืืคืืฉืื:",
"ืืฉื ืฆืื ืื ืคืฉื ืืช ืืืืืื ืฉืงืืืื ื:",
"ืขืจื ืืืฆืื ืื ืื:",
],
analogies=[
"ืืฆืื ืืื ืืื ืืืื ืฉื ืืืืคืก โ ืื ืื ื ืืืืืคืื ืืช 'ืื ืขืื' ืืขืจื ืืืืฉื ืฉืืฆืื ื.",
"ืืืืื ืืชืืื ืฉืืืืจ 'ืกืคื ืกืืืจ' โ ืืจืืข ืฉืืชื ืืืืข ืืื ืืืื ืืกืคื, ืืชื ืืืื ืืืฉื ืืืืช ืืืืืงืช.",
],
closing="ืืฆืืื! ืืืฆืื ืืื ืื ืืจื ืืฆืืื ืช ืืืืืง ืฉืืคืชืจืื ืฉืืฆืืช ืืื ื ืืื.",
),
# โโ V7.3: Geometry / Analytic Entries โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
"FIND_AXIS_INTERSECTIONS": SemanticEntry(
concept_tag="FIND_AXIS_INTERSECTIONS",
openings=[
"ืืื ืืืืืช ืืืคื ืืืขืื ืคืืืฉ ืืช ืืฆืืจืื, ื ืฉืื ืฉืืื ืคืฉืืื: ืื ืงืืจื ืืฉื ืงืืื ื ืืฆืืช ืืืฉ ืขื ืฆืืจ ื-x? ื-y ืฉืื ืฉืืื ืืืคืก! ืืืืคื.",
"ื ืงืืืืช ืืืชืื ืขื ืืฆืืจืื ืื ืื ืงืืืืช ืฉืืื ืืืขืื ืืืฆื ืืช 'ืืืืืฉืื' ืฉื ืืขืจืืช ืืฆืืจืื.",
"ืืืจื ืืืชืจ ืืช ื ืงืืืืช ืืืืชืื ืขื ืืฆืืจืื ืืื ืืืฆืื ืืคืก: ืคืขื ืขืืืจ x ืืคืขื ืขืืืจ y, ืืืจืืืช ืื ืืคืชืจืื ืฉืืืฆื.",
],
bridges=[
"ืืฉื ืฆืื ืื ืคืชืืจ, ืื ืงืืืืช ืฉืืฆืื ื ืื:",
"ืืืืจ ืืืฆืื ืืงืืืช ืืคืชืจืื ืืช, ื ืงืืืืช ืืืืชืื ืขื ืืฆืืจืื ืื:",
"ืืคืชืจืื ืืช ืฉืงืืืื ื ืืฆืืื ืื ืืช ืื ืงืืืืช ืฉืืื ืืืขืื ืืืฆื ืืช ืืฆืืจืื:",
],
analogies=[
"ืืฉืื ืขื ืืขืื ืืื ืืืืจ ืฉืืชืืืื ืขื ืจืฆืคื โ ื ืงืืืืช ืืืืชืื ืขื ืฆืืจ ื-x ืื ืืืืืง ืืืงืืืืช ืฉืืืืืจ ื ืืืข ืืจืฆืคื.",
"ืืืืื ืฉืืชื ืืกืชืื ืขื ืืคื ืืืืคืฉ ืืืคื ืืืืฉ ืจืืฉื ืืืฆื ืืช ืื ืืจ โ ืื ืืืืืง ืื ืฉืื ืื ื ืขืืฉืื ืขื ืืืขืื ืืืฆืืจืื.",
],
closing="ืืฆืื ื ืืช ืื ื ืงืืืืช ืืืืข ืฉื ืืืขืื ืขื ืืขืจืืช ืืฆืืจืื โ ืขืืืื ืืืืืงืช!",
),
"CALCULATE_SLOPE_AND_LINE": SemanticEntry(
concept_tag="CALCULATE_SLOPE_AND_LINE",
openings=[
"ืืื ืืืฆืื ืืช ืืฉืืืืช ืืืฉืจ ืืขืืืจ ืืจื ืฉืชื ื ืงืืืืช, ื ืืฉื ืงืืื ืืช ืืฉืืคืืข โ ืืื 'ืชืืื' ืืืฉืจ.",
"ืฉืืคืืข ืืืฉืจ ืืื ืืืืก ืืื ืืฉืื ืื ืืืืื ืืฉืื ืื ืืืืงืฃ: ืขืืืื ืืืงื ืืืื ืืืคืงืืช.",
"ืืืฉืจ ืฉืืืืจ ืฉืชื ื ืงืืืืช ืืืืืจ ืืืืืืื ืขื ืืื ืืฉืืคืืข ืฉืื ืื ืงืืืช ืืขืืจ ืืืช.",
],
bridges=[
"ืืืืจ ืืืฉืื ืืฉืืคืืข, ืืฉืืืืช ืืืฉืจ ืืื:",
"ืืฉื ืืกืืฃ ืืช ืืฉืืคืืข ืฉืืฆืื ื ืื ืืกืืช ืืืฉืจ, ื ืงืื:",
"ืืืฉืจ ืฉืืืืจ ืืจืื ืืืขืื ืืจืืฉืืช ืืฆืืจืื ืืื:",
],
analogies=[
"ืฉืืคืืข ืืื ืืื ืืืจืืืช โ ืื ืขืืืื ืืจืืข ืงืืืืช ืืื ืฉืืืฉื ืฆืขืืื ืงืืืื, ืืฉืืคืืข ืืื ืืจืืขื ืืืงื ืฉืืืฉื.",
"ืืืืื ืืฉืจ ืืื ืืจื ืืื ืฉืชื ืขืจืื โ ืืฉืืคืืข ืืื ืืื ืืืจืื ืขืืืื ืืื ืงืืืืืืจ ืฉื ืืกืขืื.",
],
closing="ืงืืืื ื ืืช ืืฉืืืืช ืืืฉืจ ืืฆืืจื ืืืคืืจืฉืช โ ืงื ืืฉืจ ืฉืขืืืจ ืืืืืง ืืจื ืฉืชื ืื ืงืืืืช ืื ืชืื ืืช.",
),
"CALCULATE_DISTANCE": SemanticEntry(
concept_tag="CALCULATE_DISTANCE",
openings=[
"ืืื ืืืืืง ืืื ื ืงืืื ื ืืฆืืช ืขื ืืืขืื, ื ืืืื ืืช ืืืจืืง ืืื ื ืืืื ืืจืื ืืืขืื ืื ืฉืืื ืืจืืืืก.",
"ื ืืกืืช ืืืจืืง ืืื ืฉืชื ื ืงืืืืช ืืื ืฉืืจืฉ ืฉื ืกืืื ืจืืืืขื ืืืคืจืฉืื โ ืืืืืง ืืื ืืฉืคื ืคืืชืืืจืก.",
"ืืฉืืื ืคืฉืืื: ืืื ืื ืงืืื ื ืืฆืืช ืืืืืง ืืจืืืืก ืืืืจืื, ืงืจืื ืืืชืจ, ืื ืจืืืง ืืืชืจ?",
],
bridges=[
"ืืืืจ ืืืฉืื ืืืจืืง, ืืืืฆื ืืื:",
"ืืฉืืฉืืืื ืืช ืืืจืืง ืฉืืฆืื ื ืืจืืืืก ืืืขืื, ืืชืืจืจ ืฉ:",
"ืชืืฆืืช ืืืฉืื ืืืจืืง ืืื ืืจืืืืก:",
],
analogies=[
"ืืืืื ืืืืื ืขืืืื โ ืื ื ืงืืื ืขื ืืื ื ืืฆืืช ืืืืืง ืืืืชื ืืจืืง ืืืืจืื. ืื ื ืงืืื ืงืจืืื ืืืชืจ โ ืืื ืืชืืื. ืจืืืงื ืืืชืจ โ ืืืืฆื ืื.",
"ืืจืืง ืฉืชื ื ืงืืืืช ืื ืืื ืืืืื ืขื ืกืจืื ืืื ืฉืชื ืขืจืื ืขื ืืคื โ ืคืืชืืืจืก ืขืืฉื ืืช ืืขืืืื.",
],
closing="ืืืงื ื ืืืืืง: ืืืจืืง ืืืฉื, ืืืฉืืื ืืจืืืืก, ืืืืงืื ืื ืงืืื ื ืงืืข ืืืืืืืช ืืชืืืืช.",
),
}
# โโโ Diversity Engine โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
class DiversityEngine:
"""
Selects narrative variants from the SemanticBank with diversity enforcement.
Tracks selection history per concept to detect "narrative laziness" (Drift):
if one variant is chosen more than DRIFT_ALERT_THRESHOLD fraction of the time,
emit a PEDAGOGICAL_DRIFT telemetry alert.
"""
def __init__(self):
# Counter per concept_tag: tracks how many times each (variant_type, index) was picked
self._history: Dict[str, Counter] = {}
def _record(self, concept_tag: str, variant_key: str):
if concept_tag not in self._history:
self._history[concept_tag] = Counter()
self._history[concept_tag][variant_key] += 1
def drift_score(self, concept_tag: str) -> float:
"""
Returns the dominance fraction of the most-selected variant for this concept.
Score = max_count / total_count.
0.0 = perfectly uniform, 1.0 = always the same variant.
"""
if concept_tag not in self._history or not self._history[concept_tag]:
return 0.0
counts = self._history[concept_tag]
total = sum(counts.values())
if total == 0:
return 0.0
return max(counts.values()) / total
def _pick(self, concept_tag: str, variant_type: str, options: List[str]) -> str:
"""
Weighted random selection that avoids the most recently over-used variants.
Falls back to uniform random if history is thin (< 5 picks).
"""
n = len(options)
history_key_prefix = f"{variant_type}:"
if concept_tag not in self._history or sum(self._history[concept_tag].values()) < 5:
# Not enough history โ uniform random
idx = random.randrange(n)
else:
counts = self._history[concept_tag]
# Weight = inverse of how often we've picked this index
weights = []
for i in range(n):
pick_count = counts.get(f"{history_key_prefix}{i}", 0)
weights.append(1.0 / (1 + pick_count))
# Weighted choice
total_w = sum(weights)
r = random.uniform(0, total_w)
cumulative = 0.0
idx = n - 1 # fallback
for i, w in enumerate(weights):
cumulative += w
if r <= cumulative:
idx = i
break
key = f"{history_key_prefix}{idx}"
self._record(concept_tag, key)
# Check drift and emit telemetry
score = self.drift_score(concept_tag)
if score > DRIFT_ALERT_THRESHOLD:
logger.warning(
f"[DRIFT_MONITOR] Pedagogical drift detected for '{concept_tag}': "
f"drift_score={score:.2f} > threshold={DRIFT_ALERT_THRESHOLD}. "
f"Selection history: {dict(self._history[concept_tag])}"
)
telemetry.emit_pedagogical_drift(concept_tag, score)
return options[idx]
def compose(self, action: str, signed_steps: list) -> str:
"""
Main entry point: builds a full Hebrew pedagogical narrative for the given action.
Inserts {{step_id}} placeholders where the server results will be shown.
Returns a string with {{placeholder}} notation (not yet injected).
"""
entry = SEMANTIC_BANK.get(action)
if entry is None:
logger.warning(
f"[SEMANTIC_BANK] No entry for action '{action}'. "
f"Falling back to LLM #2 renderer."
)
return None # Signal to caller: use LLM fallback
concept_tag = entry.concept_tag
# Pick diverse variants
opening = self._pick(concept_tag, "opening", entry.openings)
bridge = self._pick(concept_tag, "bridge", entry.bridges)
analogy = self._pick(concept_tag, "analogy", entry.analogies)
closing = entry.closing # Always the same (one closing per concept by design)
# Build placeholder string from signed steps
if not signed_steps:
placeholder_str = ""
elif len(signed_steps) == 1:
placeholder_str = f"{{{{{signed_steps[0]['id']}}}}}"
else:
parts = [f"{{{{{s['id']}}}}}" for s in signed_steps]
placeholder_str = " โ ".join(parts)
# Compose the narrative (V7.2.5: no emojis/em-dashes โ must pass scan_for_math_leakage)
narrative = (
f"{opening}\n\n"
f"{analogy}\n\n"
f"{bridge} {placeholder_str}\n\n"
f"{closing}"
)
logger.info(
f"[SEMANTIC_BANK] Composed narrative for '{action}' | "
f"drift_score={self.drift_score(concept_tag):.2f}"
)
return narrative
# Module-level singleton โ shared across all requests in the process
_engine = DiversityEngine()
def get_diversity_engine() -> DiversityEngine:
"""Returns the process-level DiversityEngine singleton."""
return _engine
|