AlanRocha commited on
Commit
6e58dff
·
verified ·
1 Parent(s): 3feff2e

Create regexs.py

Browse files
Files changed (1) hide show
  1. regexs.py +54 -0
regexs.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+
3
+
4
+ def extract_last_ai_text(messages: list) -> str:
5
+ """Récupère le dernier message assistant avec du texte (évite fin de run ambiguë)."""
6
+ from langchain_core.messages import AIMessage
7
+
8
+ for m in reversed(messages):
9
+ if not isinstance(m, AIMessage):
10
+ continue
11
+ c = m.content
12
+ if isinstance(c, list):
13
+ parts = []
14
+ for block in c:
15
+ if isinstance(block, dict) and block.get("type") == "text":
16
+ parts.append(block.get("text", ""))
17
+ elif isinstance(block, str):
18
+ parts.append(block)
19
+ c = "".join(parts)
20
+ if isinstance(c, str) and c.strip():
21
+ return c
22
+ for m in reversed(messages):
23
+ c = getattr(m, "content", None)
24
+ if isinstance(c, list):
25
+ continue
26
+ if isinstance(c, str) and c.strip():
27
+ return c
28
+ return ""
29
+
30
+
31
+ def strip_final_answer_prefix(text: str) -> str:
32
+ """Retire le préfixe 'FINAL ANSWER:' pour ne garder que la valeur brute à soumettre."""
33
+ marker = "FINAL ANSWER:"
34
+ stripped = text.strip()
35
+ if stripped.upper().startswith(marker):
36
+ return stripped[len(marker):].strip()
37
+ return stripped
38
+
39
+
40
+ def normalize_gaia_answer(raw: str) -> str:
41
+ """normalise la sortie du LLM en un format uniforme FINAL ANSWER: <valeur>"""
42
+ if not raw:
43
+ return "FINAL ANSWER: "
44
+ text = raw.strip()
45
+ marker = "FINAL ANSWER:"
46
+ idx = text.lower().rfind(marker.lower())
47
+ if idx != -1:
48
+ tail = text[idx + len(marker) :].strip()
49
+ first = tail.split("\n", 1)[0].strip() if tail else ""
50
+ first = re.sub(r"[*_`]+", "", first).strip()
51
+ return f"{marker} {first}".strip() if first else f"{marker} "
52
+ first_line = text.split("\n", 1)[0].strip()
53
+ first_line = re.sub(r"[*_`]+", "", first_line).strip()
54
+ return f"{marker} {first_line}"