Base de connaissances reprise de Support-AI : corpus, import, instructions, tests
Ajoute aussi WEISSHORN et MISSISSIPPI au test du corpus, et un message clair quand LibreDesk est injoignable. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3a6e693d6d
commit
cce2e039ed
130 files changed
+2784
No files matched your search
@@ -0,0 +1,48 @@
|
||||
"""Un faux LibreDesk (http.server) : enregistre chaque requête, rend les réponses qu'on lui donne."""
|
||||
import json
|
||||
import threading
|
||||
import urllib.parse
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
|
||||
|
||||
class FauxLibreDesk:
|
||||
def __init__(self):
|
||||
self.requetes: list[tuple[str, str, dict | None]] = [] # (méthode, chemin avec la requête, corps JSON)
|
||||
self.entetes: list[dict] = []
|
||||
self.reponses: dict[tuple[str, str], object] = {} # (méthode, chemin sans requête) → data, code d'erreur, ou bytes bruts
|
||||
faux = self
|
||||
|
||||
class Gestion(BaseHTTPRequestHandler):
|
||||
def _repondre(self):
|
||||
n = int(self.headers.get("Content-Length") or 0)
|
||||
corps = json.loads(self.rfile.read(n)) if n else None
|
||||
faux.requetes.append((self.command, self.path, corps))
|
||||
faux.entetes.append(dict(self.headers))
|
||||
cle = (self.command, urllib.parse.urlsplit(self.path).path)
|
||||
r = faux.reponses.get(cle, {})
|
||||
if callable(r):
|
||||
r = r(self.path)
|
||||
code = r if isinstance(r, int) else 200
|
||||
texte = r if isinstance(r, bytes) else json.dumps(
|
||||
{"message": "erreur"} if isinstance(r, int) else {"data": r}).encode()
|
||||
self.send_response(code)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(texte)))
|
||||
self.end_headers()
|
||||
self.wfile.write(texte)
|
||||
|
||||
do_GET = do_POST = do_PUT = do_DELETE = _repondre
|
||||
|
||||
def log_message(self, *_):
|
||||
pass
|
||||
|
||||
self._serveur = ThreadingHTTPServer(("127.0.0.1", 0), Gestion)
|
||||
self.url = f"http://127.0.0.1:{self._serveur.server_address[1]}"
|
||||
threading.Thread(target=self._serveur.serve_forever, daemon=True).start()
|
||||
|
||||
def ecritures(self) -> list[tuple[str, str, dict | None]]:
|
||||
return [r for r in self.requetes if r[0] != "GET"]
|
||||
|
||||
def arreter(self):
|
||||
self._serveur.shutdown()
|
||||
self._serveur.server_close()
|
||||
@@ -0,0 +1,63 @@
|
||||
"""Le corpus versionné de la base de connaissances : présent, propre, sans rien d'interne."""
|
||||
import re
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from base_connaissances.decoupage import lire_corpus
|
||||
|
||||
CORPUS = Path(__file__).resolve().parents[1] / "base_connaissances" / "corpus"
|
||||
STATIONS = ["BREITHORN", "CLARIDEN", "FLORIDA", "JUNGFRAU", "LENZSPITZE", "MICHIGAN", "NADELHORN", "NEBRASKA",
|
||||
"NIEDERHORN", "NORDEND", "SNCF01", "SUSTENHORN", "WILDSTRUBEL", "VERMONT", "WEISSHORN", "MISSISSIPPI"]
|
||||
INTERDITS = ["python3", "agent-tools", ".py", "Marc", "Barcelone", "Vaud", "GN-1", "GN-5", "seuil provisoire",
|
||||
"seuils provisoires"]
|
||||
|
||||
|
||||
class TestCorpus(unittest.TestCase):
|
||||
def test_corpus_present(self):
|
||||
self.assertGreaterEqual(len(lire_corpus(CORPUS)), 60)
|
||||
|
||||
def test_titres_uniques_et_contenus_non_vides(self):
|
||||
x = lire_corpus(CORPUS)
|
||||
self.assertEqual(len({e.titre for e in x}), len(x))
|
||||
self.assertTrue(all(e.contenu.strip() and e.source.strip() for e in x))
|
||||
|
||||
def test_taille_raisonnable(self):
|
||||
trop_longs = [e.cle for e in lire_corpus(CORPUS) if len(e.contenu) > 8000]
|
||||
self.assertEqual(trop_longs, [])
|
||||
|
||||
def test_rien_d_interne(self):
|
||||
for e in lire_corpus(CORPUS):
|
||||
texte = e.titre + "\n" + e.contenu
|
||||
for s in STATIONS:
|
||||
self.assertIsNone(re.search(rf"\b{s}\b", texte, re.I), f"{s} dans {e.cle}")
|
||||
for i in INTERDITS:
|
||||
self.assertNotIn(i.lower(), texte.lower(), f"{i} dans {e.cle}")
|
||||
|
||||
def test_sources_sans_chemin_interne(self): # la source part dans LibreDesk avec l'extrait
|
||||
chemins = [e.cle for e in lire_corpus(CORPUS) if "/" in e.source or ".md" in e.source]
|
||||
self.assertEqual(chemins, [])
|
||||
|
||||
def test_mesure_au_dessus_du_seuil_pas_dite_retiree(self): # elle est jugée non valable mais livrée
|
||||
for e in lire_corpus(CORPUS):
|
||||
if e.source.startswith("Savoir métier"):
|
||||
for faux in ("retirée", "retirées", "supprimée"):
|
||||
self.assertNotIn(faux, e.contenu, f"{faux} dans {e.cle}")
|
||||
|
||||
def test_pas_de_chiffres_d_une_seule_station_presentes_comme_tels(self):
|
||||
for e in lire_corpus(CORPUS):
|
||||
for interne in ("Ces chiffres viennent", "station solaire de 21 Ah", "n'a pas été observé"):
|
||||
self.assertNotIn(interne, e.contenu, f"{interne} dans {e.cle}")
|
||||
|
||||
def test_instructions_ne_contredisent_pas_la_faq(self):
|
||||
texte = (CORPUS.parent / "instructions.md").read_text()
|
||||
for faux in ("Heures en UTC, dites comme telles", "jamais que la station les rejette", "adresse IP)"):
|
||||
self.assertNotIn(faux, texte)
|
||||
|
||||
def test_instructions_presentes(self):
|
||||
texte = (CORPUS.parent / "instructions.md").read_text()
|
||||
self.assertIn("langue du client", texte)
|
||||
self.assertLessEqual(len(texte), 4000)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,63 @@
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from base_connaissances.decoupage import Extrait, decouper_markdown, decouper_pdf, ecrire_corpus, lire_corpus
|
||||
|
||||
TOC = ("Contents\nInstall . . . . . . 1\n Does it start? . . . . . 1\n How long does the\n battery last? . . . . 2\n"
|
||||
"Data . . . . 3 Which formats? . . . . 3\n")
|
||||
CORPS = ("Install\nDoes it start?\nCheck power.\nRevision 1.5\n2\nAIMsight FAQ\nCheck fuse.\n"
|
||||
"How long does the battery last?\nAbout 3 days: � sun; � mains.\nData\nWhich formats?\nCSV.\n")
|
||||
PARASITES = ["Revision 1.5", "AIMsight FAQ"]
|
||||
|
||||
|
||||
class TestDecouperPdf(unittest.TestCase):
|
||||
def test_un_extrait_par_question_avec_corps(self):
|
||||
x = decouper_pdf(TOC + CORPS, PARASITES)
|
||||
self.assertEqual([e.titre for e in x], ["Does it start?", "How long does the battery last?", "Which formats?"])
|
||||
|
||||
def test_parasites_et_numeros_de_page_retires(self):
|
||||
x = decouper_pdf(TOC + CORPS, PARASITES)
|
||||
self.assertEqual(x[0].contenu, "Check power.\nCheck fuse.")
|
||||
|
||||
def test_page_precedee_d_un_joint_invisible(self): # pdftotext rend « . . . 1 » (U+2060) dans les vrais PDF
|
||||
x = decouper_pdf(TOC.replace(" 1\n", " 1\n") + CORPS, PARASITES)
|
||||
self.assertEqual(x[0].titre, "Does it start?")
|
||||
|
||||
def test_titre_du_corps_sur_deux_lignes(self): # pdftotext coupe les longues questions dans le corps
|
||||
corps = CORPS.replace("How long does the battery last?\n", "How long does the battery\nlast?\n")
|
||||
x = decouper_pdf(TOC + corps, PARASITES)
|
||||
self.assertEqual([e.titre for e in x], ["Does it start?", "How long does the battery last?", "Which formats?"])
|
||||
self.assertEqual(x[0].contenu, "Check power.\nCheck fuse.")
|
||||
|
||||
def test_saut_de_page_dans_la_table(self): # « Revision 1.5 / 1 / AIMsight FAQ » entre deux entrées de la table
|
||||
toc = TOC.replace("Data . . . . 3 ", "Data . . . . 3\nRevision 1.5\n1\nAIMsight FAQ\n")
|
||||
self.assertIn("Which formats?", [e.titre for e in decouper_pdf(toc + CORPS, PARASITES)])
|
||||
|
||||
def test_puces(self):
|
||||
self.assertIn("\n- sun;\n- mains.", decouper_pdf(TOC + CORPS, [])[1].contenu)
|
||||
|
||||
|
||||
class TestDecouperMarkdown(unittest.TestCase):
|
||||
def test_markdown_court_un_extrait(self):
|
||||
x = decouper_markdown("# Le cycle thermique\n\nLe jour, ...\n", "labeling/connaissances/cycle-thermique.md")
|
||||
self.assertEqual([(e.titre, e.source) for e in x],
|
||||
[("Le cycle thermique", "labeling/connaissances/cycle-thermique.md")])
|
||||
|
||||
def test_markdown_long_par_section(self):
|
||||
texte = "# Doc\n\n## A\n\n" + "a" * 4000 + "\n\n## B\n\n" + "b" * 4000 + "\n"
|
||||
self.assertEqual([e.titre for e in decouper_markdown(texte, "x.md")], ["Doc — A", "Doc — B"])
|
||||
|
||||
|
||||
class TestCorpus(unittest.TestCase):
|
||||
def test_aller_retour_du_corpus(self):
|
||||
with tempfile.TemporaryDirectory() as t:
|
||||
d = Path(t)
|
||||
x = [Extrait("Which formats?", "CSV.", "AIMsight FAQ, rév. 1.5")]
|
||||
ecrire_corpus(x, d)
|
||||
self.assertEqual(lire_corpus(d), x)
|
||||
self.assertEqual((d / "which-formats.md").read_text().splitlines()[-1], "Source : AIMsight FAQ, rév. 1.5")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,23 @@
|
||||
"""Le paquet base_connaissances ne parle à LibreDesk que par les routes des extraits (design §2, plan Global Constraints)."""
|
||||
import re
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
PAQUET = Path(__file__).resolve().parents[1] / "base_connaissances"
|
||||
AUTORISEES = {"/api/v1/ai/snippets", "/api/v1/ai/snippets/{id}"} # la route PUT s'écrit f"/api/v1/ai/snippets/{id}"
|
||||
|
||||
|
||||
class TestGardeFouBase(unittest.TestCase):
|
||||
def test_seules_les_routes_des_extraits(self):
|
||||
for f in PAQUET.glob("*.py"):
|
||||
texte = f.read_text()
|
||||
self.assertEqual(set(re.findall(r"/api/v1/[a-z0-9_/{}-]+", texte)) - AUTORISEES, set(), f.name)
|
||||
for interdit in ("/messages", "/conversations", "DELETE", "automations", "assistants"):
|
||||
self.assertNotIn(interdit, texte, f"{interdit} dans {f.name}")
|
||||
|
||||
def test_le_paquet_a_bien_un_client(self): # sans importer.py, le test ci-dessus passerait à vide
|
||||
self.assertTrue((PAQUET / "importer.py").exists())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,93 @@
|
||||
import socket
|
||||
import unittest
|
||||
|
||||
from base_connaissances.decoupage import Extrait
|
||||
from base_connaissances.importer import Extraits, html, synchroniser
|
||||
from faux_libredesk import FauxLibreDesk
|
||||
|
||||
LISTE = ("GET", "/api/v1/ai/snippets")
|
||||
E1 = Extrait("Which formats?", "CSV < 2 Mo.\n\n- csv\n- json", "AIMsight FAQ, rév. 1.5")
|
||||
E2 = Extrait("Which timezone?", "UTC.", "AIMsight FAQ, rév. 1.5")
|
||||
|
||||
|
||||
def snippet(id_, titre, contenu, source="manual"):
|
||||
return {"id": id_, "title": titre, "content": contenu, "enabled": True, "source": source}
|
||||
|
||||
|
||||
class TestImporter(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.faux = FauxLibreDesk()
|
||||
self.addCleanup(self.faux.arreter)
|
||||
|
||||
def api(self, ecrire=True):
|
||||
return Extraits(self.faux.url, "k", "s", ecrire=ecrire)
|
||||
|
||||
def test_cree_les_absents(self):
|
||||
self.faux.reponses[LISTE] = []
|
||||
b = synchroniser(self.api(), [E1, E2])
|
||||
self.assertEqual(b.crees, [E1.titre, E2.titre])
|
||||
self.assertEqual([(m, c["title"]) for m, _, c in self.faux.ecritures()], [("POST", E1.titre), ("POST", E2.titre)])
|
||||
self.assertEqual(self.faux.ecritures()[0][2], {"title": E1.titre, "content": html(E1), "enabled": True})
|
||||
|
||||
def test_rejouer_ne_cree_pas_de_doublon(self):
|
||||
self.faux.reponses[LISTE] = [snippet(7, E1.titre, html(E1))]
|
||||
self.assertEqual(synchroniser(self.api(), [E1]).inchanges, [E1.titre])
|
||||
self.assertEqual(self.faux.ecritures(), [])
|
||||
|
||||
def test_html_reformate_reste_inchange(self):
|
||||
reformate = html(E1).replace("><", ">\n<").replace("<", "<")
|
||||
self.faux.reponses[LISTE] = [snippet(7, E1.titre, reformate)]
|
||||
self.assertEqual(synchroniser(self.api(), [E1]).inchanges, [E1.titre])
|
||||
|
||||
def test_contenu_change_met_a_jour(self):
|
||||
self.faux.reponses[LISTE] = [snippet(7, E1.titre, "<p>ancien</p>")]
|
||||
b = synchroniser(self.api(), [E1])
|
||||
self.assertEqual(b.modifies, [E1.titre])
|
||||
self.assertEqual(self.faux.ecritures()[0][:2], ("PUT", "/api/v1/ai/snippets/7"))
|
||||
|
||||
def test_extrait_inconnu_signale_pas_supprime(self):
|
||||
self.faux.reponses[LISTE] = [snippet(9, "Ancien titre", "x")]
|
||||
b = synchroniser(self.api(), [E1])
|
||||
self.assertEqual(b.inconnus, ["Ancien titre"])
|
||||
self.assertNotIn("DELETE", [m for m, _, _ in self.faux.requetes])
|
||||
|
||||
def test_extrait_appris_ignore(self): # source « conversation » : appris par LibreDesk, pas à nous
|
||||
self.faux.reponses[LISTE] = [snippet(9, "Appris", "x", source="conversation")]
|
||||
self.assertEqual(synchroniser(self.api(), [E1]).inconnus, [])
|
||||
|
||||
def test_simulation_n_ecrit_rien(self):
|
||||
self.faux.reponses[LISTE] = [snippet(7, E2.titre, "<p>ancien</p>")]
|
||||
b = synchroniser(self.api(ecrire=False), [E1, E2])
|
||||
self.assertEqual((b.crees, b.modifies), ([E1.titre], [E2.titre]))
|
||||
self.assertEqual(self.faux.ecritures(), [])
|
||||
|
||||
def test_cle_envoyee(self):
|
||||
self.faux.reponses[LISTE] = []
|
||||
self.api().lister()
|
||||
self.assertEqual(self.faux.entetes[-1]["Authorization"], "token k:s")
|
||||
|
||||
def test_adresse_injoignable_arrete_avec_un_message(self):
|
||||
with socket.socket() as s: # un port libre, que personne n'écoute une fois fermé
|
||||
s.bind(("127.0.0.1", 0))
|
||||
port = s.getsockname()[1]
|
||||
with self.assertRaises(SystemExit) as fin:
|
||||
Extraits(f"http://127.0.0.1:{port}", "k", "s", ecrire=False).lister()
|
||||
self.assertIn("injoignable", str(fin.exception.code))
|
||||
|
||||
def test_erreur_http_arrete(self):
|
||||
self.faux.reponses[LISTE] = 403
|
||||
with self.assertRaises(SystemExit):
|
||||
synchroniser(self.api(), [E1])
|
||||
|
||||
|
||||
class TestHtml(unittest.TestCase):
|
||||
def test_html_echappe_et_listes(self):
|
||||
h = html(Extrait("T", "a < b\n\n- un\n- deux", "S"))
|
||||
self.assertEqual(h, "<p>a < b</p><ul><li>un</li><li>deux</li></ul><p>Source : S</p>")
|
||||
|
||||
def test_lignes_d_un_paragraphe_gardees(self):
|
||||
self.assertEqual(html(Extrait("T", "ligne 1\nligne 2", "S")), "<p>ligne 1<br>ligne 2</p><p>Source : S</p>")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user