import * as express from "express"; const PORT = 7860; const BOT_USERNAME = "@discussion-bot"; const INFERENCE_URL = "https://api-inference.huggingface.co/models/bigscience/bloom"; const PROMPT = `Pretend that you are a bot that replies to discussions about machine learning, and reply to the following comment:\n`; const app = express(); // parse HTTP request bodies as json app.use(express.json()); app.get("/", (req, res) => { res.json({ hello: "world" }); }); app.post("/", async (req, res) => { if (req.header("X-Webhook-Secret") !== process.env.WEBHOOK_SECRET) { return res.status(400).json({ error: "incorrect secret" }); } console.log(req.body); const event = req.body.event; if ( event.action === "create" && event.scope === "discussion.comment" && req.body.comment.content.includes(BOT_USERNAME) ) { const response = await fetch(INFERENCE_URL, { method: "POST", body: JSON.stringify({ inputs: PROMPT + req.body.comment.content }), }); if (response.ok) { const output = await response.json(); const continuationText = output[0].generated_text.replace( PROMPT + req.body.comment.content, "" ); console.log(continuationText); /// Finally, let's post it as a comment in the same discussion } else { console.error(`API Error`, await response.json()); } } res.json({ success: true }); }); app.listen(PORT, () => { console.debug(`server started at http://localhost:${PORT}`); });