| |
| |
| |
| |
| |
| import json, re, sys, unicodedata |
| from os.path import normpath |
|
|
| |
|
|
|
|
| def detecter_degenerescence(txt, seuil=4): |
| """Un 8-gramme de MOTS qui apparaît ≥seuil fois = boucle (quel que soit sa période — |
| la v1 ne comparait que des fenêtres ADJACENTES et ratait toute période ≠ 8 : bug attrapé |
| par les cas synthétiques de G1, jamais par un run modèle). Court-circuit : boucle de |
| caractères (zéros du 09/08).""" |
| compact = txt.replace(" ", "").replace("\n", "") |
| if len(compact) >= 40 and len(set(compact)) <= 2: |
| return True |
| mots = txt.split() |
| if len(mots) < 8 * 2: |
| return False |
| from collections import Counter |
| c = Counter(tuple(mots[i:i + 8]) for i in range(len(mots) - 7)) |
| return max(c.values()) >= seuil |
|
|
|
|
| def _normaliser_nombre(s): |
| s = unicodedata.normalize("NFKC", s) |
| s = s.replace("\\boxed{", "").replace("}", "").replace("$", "") |
| s = s.replace("\\(", "").replace("\\)", "").replace("**", "").replace("*", "") |
| s = s.replace(" ", " ").replace(" ", " ") |
| s = re.sub(r"(?<=\d)[ _](?=\d{3}\b)", "", s) |
| s = s.strip().rstrip(".") |
| return s |
|
|
|
|
| def extraire_answer(txt): |
| """-> (classe, valeur|None). Ne regarde QUE après le DERNIER </think>.""" |
| if "<think>" in txt and "</think>" not in txt: |
| return ("tronque", None) |
| rep = txt.split("</think>")[-1] |
| if detecter_degenerescence(rep): |
| return ("degenere", None) |
| |
| |
| occs = list(re.finditer(r"(?i)\banswer\b\s*[:=]?\s*([^\n]+)", rep)) |
| if not occs: |
| return ("format_fail", None) |
| val = _normaliser_nombre(occs[-1].group(1)) |
| if val.upper() == "UNSURE": |
| return ("unsure", None) |
| m = re.fullmatch(r"[+-]?\d+", val) |
| if m: |
| return ("exact", int(m.group(0))) |
| return ("format_fail", None) |
|
|
|
|
| |
|
|
| def extraire_final(txt): |
| if "<think>" in txt and "</think>" not in txt: |
| return ("tronque", None) |
| rep = txt.split("</think>")[-1] |
| lignes = [l.strip() for l in rep.splitlines() if l.strip().upper().startswith("FINAL")] |
| if not lignes: |
| return ("format_fail", None) |
| return ("ok", re.sub(r"(?i)^final\s*[:=]?\s*", "", lignes[-1]).strip()) |
|
|
|
|
| def equiv_sympy(candidat, cibles): |
| """candidat (str) équivalent à L'UNE des cibles (liste FERMÉE de conventions) ?""" |
| import sympy |
| from sympy.parsing.sympy_parser import parse_expr |
| try: |
| c = parse_expr(candidat.replace("^", "**"), evaluate=True) |
| except Exception: |
| return False |
| for t in cibles: |
| try: |
| cible = parse_expr(str(t), evaluate=True) |
| if sympy.simplify(c - cible) == 0: |
| return True |
| |
| |
| |
| num_c, num_t = complex(sympy.N(c, 20)), complex(sympy.N(cible, 20)) |
| if abs(num_c - num_t) <= 1e-9 * max(1.0, abs(num_t)): |
| return True |
| except Exception: |
| continue |
| return False |
|
|
|
|
| |
|
|
| def _canon(o): |
| if isinstance(o, dict): |
| return {unicodedata.normalize("NFC", k): _canon(v) for k, v in sorted(o.items())} |
| if isinstance(o, list): |
| return [_canon(x) for x in o] |
| if isinstance(o, str): |
| s = unicodedata.normalize("NFC", o) |
| return normpath(s) if s.startswith("/") else s |
| if isinstance(o, float) and o.is_integer(): |
| return int(o) |
| return o |
|
|
|
|
| def json_deep_equal(a_txt, b_obj): |
| try: |
| a = json.loads(a_txt) |
| except Exception: |
| return ("invalide", False) |
| return ("ok", _canon(a) == _canon(b_obj)) |
|
|
|
|
| |
| def _tests(): |
| T = [] |
| A = extraire_answer |
| |
| T += [("t1-01", A("blah</think>\nANSWER: 248") == ("exact", 248)), |
| ("t1-02", A("<think>je pense ANSWER: 999</think>\nANSWER: 24") == ("exact", 24)), |
| ("t1-03", A("</think>ANSWER: \\boxed{248}") == ("exact", 248)), |
| ("t1-04", A("</think>ANSWER: $1080$") == ("exact", 1080)), |
| ("t1-05", A("</think>ANSWER: 1 080") == ("exact", 1080)), |
| ("t1-06", A("</think>ANSWER: 1 080") == ("exact", 1080)), |
| ("t1-07", A("</think>ANSWER: UNSURE") == ("unsure", None)), |
| ("t1-08", A("</think>answer: unsure") == ("unsure", None)), |
| ("t1-09", A("<think>coupé en plein raisonnement") == ("tronque", None)), |
| ("t1-10", A("</think>the answer is 248")[0] == "format_fail"), |
| ("t1-11", A("</think>ANSWER: twenty-four")[0] == "format_fail"), |
| ("t1-12", A("</think>" + "0" * 300)[0] == "degenere"), |
| ("t1-13", A("</think>ANSWER: -14") == ("exact", -14)), |
| ("t1-14", A("</think>ANSWER: 24 8")[0] == "format_fail"), |
| ("t1-15", A("</think>ANSWER: 248.") == ("exact", 248)), |
| ("t1-16", A("</think>ANSWER : 248") == ("exact", 248)), |
| ("t1-17", A("</think>ANSWER= 248") == ("exact", 248)), |
| ("t1-18", A("</think>Réflexion.\nANSWER: 7\nvoila") == ("exact", 7)), |
| ("t1-19", A("</think>ANSWER: 24\nANSWER: 248") == ("exact", 248)), |
| ("t1-20", A("</think>ANSWER: **248**") == ("exact", 248)), |
| ("t1-21", A("pas de think du tout ANSWER: 11") == ("exact", 11)), |
| ("t1-22", A("</think>ANSWER: \\(14\\)") == ("exact", 14)), |
| ("t1-23", A("</think>ANSWER: 2,5")[0] == "format_fail"), |
| ("t1-24", A("</think>ANSWER: 0") == ("exact", 0)), |
| ("t1-25", A("</think>ANSWER: 7920") == ("exact", 7920)), |
| ("t1-26", A("</think>ANSWER:248") == ("exact", 248)), |
| ("t1-27", A("</think>\n\nANSWER: 496\n") == ("exact", 496)), |
| ("t1-28", A("</think>" + ("les mots se repetent ici encore et encore oui " * 30))[0] == "degenere"), |
| ("t1-29", A("<think>a</think>ok<think>b</think>ANSWER: 3") == ("exact", 3)), |
| ("t1-30", A("</think>ANSWER: 1_080") == ("exact", 1080))] |
| |
| F = extraire_final |
| T += [("t2-01", F("</think>FINAL: 11/(16*pi**2)")[1] == "11/(16*pi**2)"), |
| ("t2-02", F("<think>coupé")[0] == "tronque"), |
| ("t2-03", F("</think>rien de final")[0] == "format_fail"), |
| ("t2-04", equiv_sympy("11/(16*pi**2)", ["11/(16*pi**2)"])), |
| ("t2-05", equiv_sympy("(11)/(16*pi^2)", ["11/(16*pi**2)"])), |
| ("t2-06", equiv_sympy("0.6875/pi**2", ["11/(16*pi**2)"])), |
| ("t2-07", not equiv_sympy("7/(16*pi**2)", ["11/(16*pi**2)"])), |
| ("t2-08", equiv_sympy("11 - 2*3/3", ["9"])), |
| ("t2-09", not equiv_sympy("n'importe quoi(", ["9"])), |
| ("t2-10", equiv_sympy("22/2", ["11", "11/(16*pi**2)"])), |
| ("t2-11", F("</think>FINAL: x + FINAL: y")[1] == "x + FINAL: y"), |
| ("t2-12", equiv_sympy("sqrt(4)", ["2"])), |
| ("t2-13", equiv_sympy("15.666666666666666", ["47/3"])), |
| ("t2-14", not equiv_sympy("15.67", ["47/3"])), |
| ("t2-15", equiv_sympy("0.16460905349794238683", ["40/243"]))] |
| |
| J = json_deep_equal |
| ref = {"name": "read_file", "arguments": {"path": "/a/b.md", "limit": 200}} |
| T += [("t5-01", J('{"name":"read_file","arguments":{"path":"/a/b.md","limit":200}}', ref) == ("ok", True)), |
| ("t5-02", J('{"arguments":{"limit":200,"path":"/a/b.md"},"name":"read_file"}', ref) == ("ok", True)), |
| ("t5-03", J('{"name":"read_file","arguments":{"path":"/a//b.md","limit":200}}', ref) == ("ok", True)), |
| ("t5-04", J('{"name":"read_file","arguments":{"path":"/a/b.md","limit":200.0}}', ref) == ("ok", True)), |
| ("t5-05", J('{"name":"read_file","arguments":{"path":"/a/c.md","limit":200}}', ref) == ("ok", False)), |
| ("t5-06", J('pas du json', ref)[0] == "invalide"), |
| ("t5-07", J('{"name":"write_file","arguments":{"path":"/a/b.md","limit":200}}', ref) == ("ok", False)), |
| ("t5-08", J(json.dumps({"name": "read_file", "arguments": {"path": "/a/b.md", "limit": 200}}), ref) == ("ok", True)), |
| ("t5-09", J('{"name":"read_file","arguments":{"path":"/caf\\u00e9/x.md","limit":1}}', |
| {"name": "read_file", "arguments": {"path": "/café/x.md", "limit": 1}}) == ("ok", True)), |
| ("t5-10", J('{"name":"read_file","arguments":{"path":"/a/b.md"}}', ref) == ("ok", False)), |
| ("t5-11", J('{"name":"read_file","arguments":{"path":"/a/b.md","limit":"200"}}', ref) == ("ok", False)), |
| ("t5-12", J('[1,2,3]', [1, 2, 3]) == ("ok", True))] |
| |
| D = detecter_degenerescence |
| T += [("t6-01", D("0" * 300)), |
| ("t6-02", D("dekameters " * 60)), |
| ("t6-03", not D("une liste : " + " ".join(f"item{i} valeur{i}" for i in range(60)))), |
| ("t6-04", not D("texte normal de physique sur le confinement et les boucles de Wilson qui ne se repete pas du tout car chaque mot change")), |
| ("t6-05", D(("exactement la meme phrase de huit mots ici oui " * 25))), |
| ("t6-06", not D("ANSWER: 248")), |
| ("t6-07", not D(" ".join(str(i) for i in range(200)))), |
| ("t6-08", D("ab" * 200)), |
| ("t6-09", not D("11 11 11")), |
| ("t6-10", not D(", ".join(f"fichier_{c}.md" for c in "abcdefghijklmnopqrstuvwxyz")))] |
| return T |
|
|
|
|
| if __name__ == "__main__": |
| tests = _tests() |
| ko = [(n) for n, ok in tests if not ok] |
| par_scorer = {} |
| for n, ok in tests: |
| k = n.split("-")[0] |
| a, b = par_scorer.get(k, (0, 0)) |
| par_scorer[k] = (a + ok, b + 1) |
| for k, (a, b) in sorted(par_scorer.items()): |
| print(f" {k}: {a}/{b}") |
| if ko: |
| print(f" ÉCHECS: {ko}") |
| sys.exit(1) |
| print(f" G1 OK — {len(tests)}/{len(tests)} cas synthétiques passés") |
|
|