File size: 649 Bytes
c4bf3b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re

STOP_SEQUENCES = [
    "Question:",
    "Context:",
    "Answer:",
    "User:",
    "Assistant:"
]

def clean_answer(text: str) -> str:
    """
    Nettoyage de base de la réponse LLM
    """

    if not text:
        return ""

    # 1. strip global
    text = text.strip()

    # 2. couper si le modèle recommence un dialogue
    for stop in STOP_SEQUENCES:
        if stop in text:
            text = text.split(stop)[0]

    # 3. enlever répétitions de whitespace
    text = re.sub(r"\s+", " ", text)

    # 4. enlever phrases incomplètes finales (très simple)
    text = re.sub(r"\b\w{1,2}$", "", text).strip()

    return text