Spaces:
Sleeping
Sleeping
Download server.js from Sebacu/saniplus-chatbot: direct link, hf CLI and curl.
- Browser
- Download file 11.1 kB
-
https://huggingface.co/spaces/Sebacu/saniplus-chatbot/resolve/main/server.js
- Command line
-
hf download hf://spaces/Sebacu/saniplus-chatbot/server.js
-
curl -L -o server.js https://huggingface.co/spaces/Sebacu/saniplus-chatbot/resolve/main/server.js
11.1 kB
| import express from "express"; | |
| import Anthropic from "@anthropic-ai/sdk"; | |
| import { fileURLToPath } from "url"; | |
| import { dirname, join } from "path"; | |
| import { SYSTEM_PROMPT } from "./kb.js"; | |
| const __dirname = dirname(fileURLToPath(import.meta.url)); | |
| const app = express(); | |
| app.set("trust proxy", 1); // Render/Fly sit behind a proxy → use X-Forwarded-For | |
| app.use(express.json({ limit: "256kb" })); | |
| app.use(express.static(join(__dirname, "public"))); | |
| /* ---------- Lightweight in-memory per-IP rate limiter (no deps) ---------- | |
| Sliding window. Protects the spendy endpoints from abuse / runaway crawlers. | |
| Tune with env vars; defaults are sensible for a public showcase. */ | |
| const RL_WINDOW_MS = Number(process.env.RATE_WINDOW_MS || 60_000); | |
| const RL_MAX = Number(process.env.RATE_MAX || 20); // requests per window per IP | |
| const hits = new Map(); // ip -> number[] (timestamps) | |
| function rateLimit(req, res, next) { | |
| const ip = req.ip || req.socket.remoteAddress || "unknown"; | |
| const now = Date.now(); | |
| const arr = (hits.get(ip) || []).filter((t) => now - t < RL_WINDOW_MS); | |
| if (arr.length >= RL_MAX) { | |
| const retry = Math.ceil((RL_WINDOW_MS - (now - arr[0])) / 1000); | |
| res.setHeader("Retry-After", String(retry)); | |
| return res.status(429).json({ error: "rate_limited", retry_after: retry }); | |
| } | |
| arr.push(now); | |
| hits.set(ip, arr); | |
| next(); | |
| } | |
| // Periodically evict idle IPs so the map can't grow unbounded. | |
| setInterval(() => { | |
| const now = Date.now(); | |
| for (const [ip, arr] of hits) { | |
| const live = arr.filter((t) => now - t < RL_WINDOW_MS); | |
| if (live.length) hits.set(ip, live); else hits.delete(ip); | |
| } | |
| }, RL_WINDOW_MS).unref(); | |
| const MODEL = "claude-opus-4-8"; | |
| const hasKey = !!process.env.ANTHROPIC_API_KEY; | |
| const client = hasKey ? new Anthropic() : null; | |
| // Optional shared access code. When ACCESS_CODE is set, the chat endpoint | |
| // requires a matching `x-access-code` header. Leave unset for open local dev. | |
| const ACCESS_CODE = process.env.ACCESS_CODE || ""; | |
| const gateOn = !!ACCESS_CODE; | |
| // Constant-time-ish compare to avoid trivial timing leaks. | |
| function codeOk(supplied) { | |
| if (!gateOn) return true; | |
| const a = String(supplied || ""), b = ACCESS_CODE; | |
| if (a.length !== b.length) return false; | |
| let diff = 0; | |
| for (let i = 0; i < b.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i); | |
| return diff === 0; | |
| } | |
| // Verify an access code (used by the UI login overlay). | |
| app.post("/api/gate", rateLimit, (req, res) => { | |
| res.json({ ok: codeOk(req.body?.code) }); | |
| }); | |
| /* ---------- Deterministic safety gate (runs BEFORE the LLM) ---------- | |
| Clinical/emergency questions never reach the model. This is the | |
| defense-in-depth layer; the system prompt is the second layer. */ | |
| const EMERGENCY = /(brustschmerz|chest pain|herzinfarkt|heart attack|schlaganfall|stroke|atemnot|can'?t breathe|cannot breathe|trouble breathing|bewusstlos|unconscious|überdosis|overdose|stark.*blut|severe bleeding|anaphyla|suizid|suicid|umbringen|kill myself|notfall|боль в груди|задыхаюсь|без сознания|ألم في الصدر|إغماء|نوبة قلبية)/i; | |
| const CLINICAL = /(wie ?viel|how much|how many|dosier|dosage|\bdose\b|einnehmen|should i take|darf ich.*nehmen|can i take|wechselwirk|interaction|kombinier|combine|zusammen.*einnehmen|nebenwirkung|side effect|symptom|diagnos|krankheit|\bdisease\b|schwanger|pregnan|behandeln|\btreat\b|wogegen hilft|what should i take|is it safe to|gefährlich.*nehmen|сколько.*принимать|дозиров|можно ли принимать|كم.*جرعة|الجرعة|تفاعل)/i; | |
| const PII = /(r[üue]+ckruf|call me back|ruf(t|en sie)? mich.*(zur[üue]+ck)|zur[üue]+ckruf|обратный звонок|معاودة الاتصال|geri arama)/i; | |
| function classify(text) { | |
| if (EMERGENCY.test(text)) return "EMERGENCY"; | |
| if (CLINICAL.test(text)) return "CLINICAL"; | |
| if (PII.test(text)) return "PII"; | |
| return "SAFE"; | |
| } | |
| // Localised fixed templates for the gated categories (no LLM call needed). | |
| function detectLang(s) { | |
| const x = " " + s.toLowerCase() + " "; | |
| if (/[-ۿ]/.test(s)) return "ar"; | |
| if (/[Ѐ-ӿ]/.test(s)) return "ru"; | |
| if (/[şğıİ]|merhaba|eczane|hangi/.test(x)) return "tr"; | |
| if (/\b(how|what|where|can|i|the|my|chest|take|emergency|hours|pharmacy)\b/.test(x)) return "en"; | |
| return "de"; | |
| } | |
| const TPL = { | |
| EMERGENCY: { | |
| de: "🚨 Das klingt nach einem Notfall. Bitte rufen Sie sofort den Rettungsdienst: 112. Die diensthabende Notapotheke finden Sie tagesaktuell über die Notdienstsuche (aponet.de). Bleiben Sie ruhig – Hilfe ist unterwegs.", | |
| en: "🚨 This sounds like an emergency. Please call emergency services immediately: 112. You can find the on-duty pharmacy via the official Notdienst search (aponet.de). Stay calm — help is on the way.", | |
| tr: "🚨 Bu acil bir durum gibi görünüyor. Lütfen hemen 112'yi arayın. Nöbetçi eczaneyi aponet.de üzerinden bulabilirsiniz. Sakin olun, yardım yolda.", | |
| ru: "🚨 Похоже на экстренный случай. Срочно позвоните 112. Дежурную аптеку можно найти на aponet.de. Сохраняйте спокойствие, помощь уже в пути.", | |
| ar: "🚨 يبدو أنها حالة طارئة. يُرجى الاتصال فوراً بالرقم 112. يمكنك إيجاد صيدلية الطوارئ عبر aponet.de. ابقَ هادئاً، المساعدة في الطريق.", | |
| }, | |
| CLINICAL: { | |
| de: "Das ist eine medizinische Frage, die ich nicht beantworten darf. 🩺 Das bespricht am besten unser Apothekenteam persönlich mit Ihnen. Möchten Sie einen Termin, einen Rückruf oder die nächste Filiale?", | |
| en: "That's a medical question I'm not allowed to answer. 🩺 Our pharmacy team is the right place for this. Would you like an appointment, a callback, or your nearest branch?", | |
| tr: "Bu tıbbi bir soru ve yanıtlayamam. 🩺 Bunu en iyi eczane ekibimiz değerlendirir. Randevu, geri arama veya en yakın şube ister misiniz?", | |
| ru: "Это медицинский вопрос, на который я не могу ответить. 🩺 Лучше всего поможет наша команда аптеки. Хотите записаться, заказать обратный звонок или узнать ближайший филиал?", | |
| ar: "هذا سؤال طبي لا يمكنني الإجابة عليه. 🩺 فريق الصيدلية هو الأفضل لمساعدتك. هل تريد موعداً أو معاودة اتصال أو أقرب فرع؟", | |
| }, | |
| PII: { | |
| de: "📞 Gern! Bitte hinterlassen Sie Name und Telefonnummer, dann ruft unser Team zurück. (Hinweis: In dieser Demo werden keine echten Daten gespeichert; in der Produktion würde vorab Ihre Einwilligung eingeholt.)", | |
| en: "📞 Of course! Please leave your name and phone number and our team will call you back. (Note: no real data is stored in this demo; in production we'd request your consent first.)", | |
| tr: "📞 Tabii! Lütfen adınızı ve telefon numaranızı bırakın, ekibimiz sizi arasın. (Not: bu demoda gerçek veri saklanmaz.)", | |
| ru: "📞 Конечно! Оставьте имя и номер телефона, и наша команда перезвонит. (В этой демоверсии данные не сохраняются.)", | |
| ar: "📞 بالطبع! يُرجى ترك اسمك ورقم هاتفك وسيعاود فريقنا الاتصال بك. (ملاحظة: لا تُحفظ بيانات حقيقية في هذا العرض.)", | |
| }, | |
| }; | |
| function template(cat, lang) { | |
| const m = TPL[cat]; | |
| return (m && (m[lang] || m.en)) || ""; | |
| } | |
| /* ---------------------- Chat endpoint (SSE stream) ---------------------- */ | |
| app.post("/api/chat", rateLimit, async (req, res) => { | |
| if (!codeOk(req.get("x-access-code"))) { | |
| return res.status(401).json({ error: "unauthorized" }); | |
| } | |
| const messages = Array.isArray(req.body?.messages) ? req.body.messages : []; | |
| const last = [...messages].reverse().find((m) => m.role === "user"); | |
| const userText = last?.content || ""; | |
| const forcedLang = req.body?.lang && req.body.lang !== "auto" ? req.body.lang : null; | |
| const lang = forcedLang || detectLang(userText); | |
| // SSE headers | |
| res.setHeader("Content-Type", "text/event-stream"); | |
| res.setHeader("Cache-Control", "no-cache"); | |
| res.setHeader("Connection", "keep-alive"); | |
| const send = (obj) => res.write(`data: ${JSON.stringify(obj)}\n\n`); | |
| const cat = classify(userText); | |
| // Gated categories → deterministic template, no model call. | |
| if (cat !== "SAFE") { | |
| send({ type: "meta", category: cat }); | |
| send({ type: "delta", text: template(cat, lang) }); | |
| send({ type: "done" }); | |
| return res.end(); | |
| } | |
| send({ type: "meta", category: "SAFE" }); | |
| if (!hasKey) { | |
| send({ | |
| type: "delta", | |
| text: | |
| "⚙️ Demo backend is running, but no ANTHROPIC_API_KEY is configured, so the live model isn't connected.\n\nSet the key and restart to enable real AI answers:\n • PowerShell: $env:ANTHROPIC_API_KEY=\"sk-ant-...\"; npm start\n\n(The safety gate above — emergency & clinical routing — works without a key.)", | |
| }); | |
| send({ type: "done" }); | |
| return res.end(); | |
| } | |
| try { | |
| // Build clean history for the API (user/assistant only). | |
| const apiMessages = messages | |
| .filter((m) => m.role === "user" || m.role === "assistant") | |
| .map((m) => ({ role: m.role, content: String(m.content || "") })) | |
| .slice(-12); | |
| if (apiMessages.length === 0 || apiMessages[0].role !== "user") { | |
| apiMessages.unshift({ role: "user", content: userText }); | |
| } | |
| const stream = client.messages.stream({ | |
| model: MODEL, | |
| max_tokens: 1024, | |
| // No thinking → snappy FAQ-style replies; system prompt forbids exposing reasoning. | |
| system: [{ type: "text", text: SYSTEM_PROMPT, cache_control: { type: "ephemeral" } }], | |
| messages: apiMessages, | |
| }); | |
| stream.on("text", (delta) => send({ type: "delta", text: delta })); | |
| const final = await stream.finalMessage(); | |
| send({ type: "done", usage: final.usage }); | |
| res.end(); | |
| } catch (err) { | |
| console.error("LLM error:", err?.message || err); | |
| send({ | |
| type: "delta", | |
| text: | |
| lang === "de" | |
| ? "Entschuldigung, es gab gerade ein technisches Problem. Bitte versuchen Sie es erneut oder kontaktieren Sie Ihre SaniPlus-Filiale." | |
| : "Sorry, there was a technical problem. Please try again or contact your SaniPlus branch.", | |
| }); | |
| send({ type: "done", error: true }); | |
| res.end(); | |
| } | |
| }); | |
| app.get("/api/health", (_req, res) => res.json({ ok: true, model: MODEL, llm: hasKey, gate: gateOn })); | |
| const PORT = process.env.PORT || 8100; | |
| app.listen(PORT, () => { | |
| console.log(`SaniPlus LLM chatbot on http://localhost:${PORT}`); | |
| console.log(hasKey ? "LLM: connected (Claude)" : "LLM: NO API KEY — safety gate works, model answers disabled"); | |
| console.log(gateOn ? "Access gate: ON (ACCESS_CODE required)" : "Access gate: OFF (open)"); | |
| }); | |