Download llm.js from noirebox/ask: direct link, hf CLI and curl.
- Browser
- Download file 4.41 kB
-
https://huggingface.co/spaces/noirebox/ask/resolve/main/llm.js
- Command line
-
hf download hf://spaces/noirebox/ask/llm.js
-
curl -L -o llm.js https://huggingface.co/spaces/noirebox/ask/resolve/main/llm.js
4.41 kB
| // llm.js — rédaction de la réponse par un LLM ouvert, à partir UNIQUEMENT des | |
| // passages retrouvés localement (RAG verrouillé). Endpoint gratuit sans clé | |
| // (pollinations, openai-compatible, CORS *) ; streaming SSE. | |
| // En cas d'échec (limite, réseau), l'appelant retombe sur le mode preuve. | |
| const ENDPOINT = "https://text.pollinations.ai/openai"; | |
| const MODEL = "openai-fast"; | |
| const systemPrompt = (voice) => `Tu es NoireBox Ask, l'assistant conformité de LOI.EXE / NoireBox (AI Act européen + RGPD). | |
| RÈGLES ABSOLUES : | |
| - Réponds UNIQUEMENT à partir des PASSAGES fournis. N'invente jamais d'article, de chiffre, de date ou d'obligation. | |
| - Cite tes sources inline : [AI Act art. 12(2)], [RGPD art. 17], [exigence RIA-012-01], [source : titre]… | |
| - Hiérarchie d'autorité si les passages se contredisent : droit officiel (EUR-Lex, CNIL, Conseil d'État) > code officiel (GPAI CoP) > doctrine (CNCDH) > communication Commission > communauté/recherche > produit NoireBox. Signale le conflit et suis le niveau le plus officiel. | |
| - Si les passages ne suffisent pas, dis-le franchement et propose de reformuler. | |
| - Réponds en français. | |
| STYLE : ${voice === "tech" | |
| ? "auditeur / développeur : concis et technique. Donnes les exigences avec leur identifiant et la règle vérifiable (règle normexe). Markdown léger (gras, listes) autorisé." | |
| : "chef de projet / métier : langage clair, sans jargon juridique. Explique ce qu'on attend concrètement et pourquoi ça doit être prouvé par un test, pas promis."}`; | |
| const userBlock = (g, question) => { | |
| const parts = []; | |
| if (g.articles.length) { | |
| parts.push("ARTICLES OFFICIELS (consolidations EUR-Lex) :"); | |
| for (const a of g.articles) { | |
| parts.push(`[${a.acte} art. ${a.article}${a.heading ? " — " + a.heading : ""}]\n${a.text}`); | |
| } | |
| } | |
| if (g.requirements.length) { | |
| parts.push("EXIGENCES VÉRIFIABLES (moteur LOI.EXE — chaque exigence correspond à un test exécutable) :"); | |
| for (const r of g.requirements) { | |
| parts.push(`[${r.req_id}, ${r.acte} art. ${r.ref}, contrôle ${r.check_type}${r.machine ? ", règle : " + r.rule : ""}] ${r.obligation}`); | |
| } | |
| } | |
| if (g.passages.length) { | |
| parts.push("PASSAGES DE CONTEXTE (du plus officiel au moins officiel) :"); | |
| for (const p of g.passages) { | |
| parts.push(`[${p.tier_label} — ${p.title}]\n${p.text}`); | |
| } | |
| } | |
| parts.push(`QUESTION : ${question}`); | |
| return parts.join("\n\n"); | |
| }; | |
| // streamAnswer : appelle le LLM en streaming, appelle onDelta(fragment) au fil | |
| // de l'eau, retourne le texte complet. Lève une erreur typée en cas d'échec. | |
| export async function streamAnswer({ grounding, question, voice, history = [], onDelta, signal }) { | |
| const messages = [ | |
| { role: "system", content: systemPrompt(voice) }, | |
| ...history.slice(-4), | |
| { role: "user", content: userBlock(grounding, question) }, | |
| ]; | |
| const res = await fetch(ENDPOINT, { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ model: MODEL, messages, stream: true, private: true }), | |
| signal, | |
| }); | |
| if (!res.ok) { | |
| let msg = `HTTP ${res.status}`; | |
| try { | |
| const j = await res.json(); | |
| if (j?.error?.code === "FREE_LIMIT") msg = "limite gratuite (1 réponse / 30 s) — réessaie dans un instant"; | |
| else if (j?.error?.message) msg = j.error.message; | |
| } catch { /* corps non JSON */ } | |
| throw new Error(msg); | |
| } | |
| const ctype = res.headers.get("content-type") || ""; | |
| if (!ctype.includes("text/event-stream")) { | |
| // réponse non streamée : JSON complet | |
| const j = await res.json(); | |
| const text = j?.choices?.[0]?.message?.content || ""; | |
| onDelta(text); | |
| return text; | |
| } | |
| const reader = res.body.getReader(); | |
| const dec = new TextDecoder(); | |
| let buf = "", full = ""; | |
| while (true) { | |
| const { done, value } = await reader.read(); | |
| if (done) break; | |
| buf += dec.decode(value, { stream: true }); | |
| const lines = buf.split("\n"); | |
| buf = lines.pop(); | |
| for (const line of lines) { | |
| const t = line.trim(); | |
| if (!t.startsWith("data:")) continue; | |
| const payload = t.slice(5).trim(); | |
| if (payload === "[DONE]") continue; | |
| try { | |
| const delta = JSON.parse(payload)?.choices?.[0]?.delta?.content || ""; | |
| if (delta) { full += delta; onDelta(delta); } | |
| } catch { /* fragment SSE tronqué */ } | |
| } | |
| } | |
| return full; | |
| } | |