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`; if (!process.env.WEBHOOK_SECRET || !process.env.HF_TOKEN) { console.error( "This app needs those env variables to be defined", "WEBHOOK_SECRET, HF_TOKEN" ); process.exit(); } 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) { console.error("incorrect 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 const commentUrl = req.body.discussion.url.api + "/comment"; const commentApiResponse = await fetch(commentUrl, { method: "POST", headers: { Authorization: `Bearer ${process.env.HF_TOKEN}`, }, body: JSON.stringify({ comment: continuationText }), }); const apiOutput = await commentApiResponse.json(); console.log(apiOutput); } else { console.error(`API Error`, await response.json()); } } res.json({ success: true }); }); app.listen(PORT, () => { console.debug(`server started at http://localhost:${PORT}`); });