| 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 |