import { createServer } from "node:http" import { createLLMFirewall } from "llm-firewall-js" const firewall = createLLMFirewall() const server = createServer(async (req, res) => { firewall.setCors(req, res) if (req.method === "OPTIONS") { res.writeHead(204) res.end() return } if (req.url !== "/api/chat" || req.method !== "POST") { res.writeHead(404, { "Content-Type": "application/json" }) res.end(JSON.stringify({ error: "Not found." })) return } const body = await readJson(req) const inputCheck = await firewall.inspectInput({ request: req, body, text: body.question || body.prompt || "", }) if (!inputCheck.ok) { firewall.reject(res, inputCheck) return } const modelData = await callYourLLMProvider(body) const outputCheck = firewall.inspectOutput(modelData) if (!outputCheck.ok) { firewall.reject(res, outputCheck) return } res.writeHead(200, { "Content-Type": "application/json" }) res.end(JSON.stringify(outputCheck.data)) }) server.listen(3000, () => { console.log("Generic Node example running at http://localhost:3000") }) async function readJson(req) { const chunks = [] for await (const chunk of req) chunks.push(chunk) const raw = Buffer.concat(chunks).toString("utf8") return raw ? JSON.parse(raw) : {} } async function callYourLLMProvider(body) { return { message: `Replace this with your LLM provider call. User asked: ${body.question || body.prompt || ""}`, } }