Public Access
63 lines
2.6 KiB
Python
63 lines
2.6 KiB
Python
import os
|
|
import re
|
|
from modules.logger import log_event
|
|
|
|
RAG_BASE_DIR = "rag_data"
|
|
THEMATIQUES = ["minecraft", "droit", "serveur", "general"]
|
|
|
|
def initialiser_structure_rag():
|
|
"""Crée l'arborescence des dossiers RAG s'ils n'existent pas."""
|
|
os.makedirs(RAG_BASE_DIR, exist_ok=True)
|
|
for theme in THEMATIQUES:
|
|
os.makedirs(os.path.join(RAG_BASE_DIR, theme), exist_ok=True)
|
|
|
|
def indexer_document(thematique: str, nom_fichier: str, contenu: str):
|
|
"""Enregistre un document dans la thématique correspondante."""
|
|
initialiser_structure_rag()
|
|
if thematique not in THEMATIQUES:
|
|
thematique = "general"
|
|
chemin = os.path.join(RAG_BASE_DIR, thematique, nom_fichier)
|
|
try:
|
|
with open(chemin, "w", encoding="utf-8") as f:
|
|
f.write(contenu)
|
|
log_event("INFO", "RAG", f"Document {nom_fichier} indexé dans {thematique}.")
|
|
return True
|
|
except Exception as e:
|
|
log_event("ERROR", "RAG", f"Échec d'indexation pour {nom_fichier} : {e}")
|
|
return False
|
|
|
|
def rechercher_contexte_rag(requete: str, thematique: str = None, top_k: int = 3) -> str:
|
|
"""
|
|
Recherche les passages les plus pertinents dans la base documentaire.
|
|
Filtre par thématique si spécifiée, sinon cherche globalement.
|
|
"""
|
|
initialiser_structure_rag()
|
|
dossiers_cibles = [os.path.join(RAG_BASE_DIR, thematique)] if thematique in THEMATIQUES else [os.path.join(RAG_BASE_DIR, t) for t in THEMATIQUES]
|
|
|
|
mots_cles = set(re.findall(r'\w+', requete.lower()))
|
|
resultats = []
|
|
|
|
for dossier in dossiers_cibles:
|
|
if not os.path.exists(dossier):
|
|
continue
|
|
for fichier in os.listdir(dossier):
|
|
chemin = os.path.join(dossier, fichier)
|
|
if os.path.isfile(chemin):
|
|
try:
|
|
with open(chemin, "r", encoding="utf-8", errors="ignore") as f:
|
|
contenu = f.read()
|
|
paragraphes = contenu.split("\n\n")
|
|
for p in paragraphes:
|
|
texte_p = p.strip()
|
|
if not texte_p:
|
|
continue
|
|
mots_p = set(re.findall(r'\w+', texte_p.lower()))
|
|
score = len(mots_cles.intersection(mots_p))
|
|
if score > 0:
|
|
resultats.append((score, fichier, texte_p))
|
|
except Exception as e:
|
|
log_event("WARNING", "RAG", f"Erreur lecture {chemin} : {e}")
|
|
|
|
resultats.sort(key=lambda x: x[0], reverse=True)
|
|
extraits = [f"[{res[1]}] {res[2]}" for res in resultats[:top_k]]
|
|
return "\n---\n".join(extraits) |