File size: 1,450 Bytes
2e22a88
 
cf5db11
 
 
 
 
 
b865405
 
 
2e22a88
b865405
2e22a88
 
 
cf5db11
b865405
 
 
2e22a88
cf5db11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e22a88
cf5db11
 
 
 
 
 
2e22a88
 
 
b865405
2e22a88
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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}`);
});