|
1 | | -const express = require('express'); |
2 | | -const rateLimit = require('express-rate-limit'); |
3 | | -const fs = require('fs'); |
| 1 | +require("dotenv").config(); |
| 2 | +const express = require("express"); |
| 3 | +const rateLimit = require("express-rate-limit"); |
| 4 | +const fs = require("fs"); |
| 5 | +const axios = require("axios"); |
4 | 6 |
|
5 | 7 | const app = express(); |
6 | | -app.set('trust proxy', true); |
| 8 | +app.set("trust proxy", true); |
7 | 9 | const PORT = process.env.PORT || 3000; |
8 | 10 |
|
| 11 | +const shouldUseLLM = true; |
| 12 | +const model = "meta-llama/llama-3.3-8b-instruct:free"; |
| 13 | +const OPENROUTER_API_KEY = process.env.OPENROUTER_API_KEY; |
| 14 | + |
9 | 15 | // Load reasons from JSON |
10 | | -const reasons = JSON.parse(fs.readFileSync('./reasons.json', 'utf-8')); |
| 16 | +const reasons = JSON.parse(fs.readFileSync("./reasons.json", "utf-8")); |
11 | 17 |
|
12 | 18 | // Rate limiter: 120 requests per minute per IP |
13 | 19 | const limiter = rateLimit({ |
14 | 20 | windowMs: 60 * 1000, // 1 minute |
15 | 21 | max: 120, |
16 | 22 | keyGenerator: (req, res) => { |
17 | | - return req.headers['cf-connecting-ip'] || req.ip; // Fallback if header missing (or for non-CF) |
| 23 | + return req.headers["cf-connecting-ip"] || req.ip; // Fallback if header missing (or for non-CF) |
| 24 | + }, |
| 25 | + message: { |
| 26 | + error: "Too many requests, please try again later. (120 reqs/min/IP)", |
18 | 27 | }, |
19 | | - message: { error: "Too many requests, please try again later. (120 reqs/min/IP)" } |
20 | 28 | }); |
21 | 29 |
|
22 | 30 | app.use(limiter); |
23 | 31 |
|
24 | 32 | // Random rejection reason endpoint |
25 | | -app.get('/no', (req, res) => { |
| 33 | +app.get("/no", async (req, res) => { |
| 34 | + if (shouldUseLLM && OPENROUTER_API_KEY) { |
| 35 | + const theme = req.query.theme?.toLowerCase(); |
| 36 | + const userMessage = theme |
| 37 | + ? `Give me a excuse to say no to something, related to the theme: ${theme}.` |
| 38 | + : `Give me a excuse to say no to something.`; |
| 39 | + |
| 40 | + try { |
| 41 | + const llmResponse = await axios.post( |
| 42 | + "https://openrouter.ai/api/v1/chat/completions", |
| 43 | + { |
| 44 | + model, |
| 45 | + messages: [ |
| 46 | + { |
| 47 | + role: "system", |
| 48 | + content: |
| 49 | + "You are an API that generates a single, creative, humorous, and random excuse to say 'no'. Your response must contain only the excuse — no greetings, explanations, or additional text.", |
| 50 | + }, |
| 51 | + { |
| 52 | + role: "user", |
| 53 | + content: userMessage, |
| 54 | + }, |
| 55 | + ], |
| 56 | + }, |
| 57 | + { |
| 58 | + headers: { |
| 59 | + Authorization: `Bearer ${OPENROUTER_API_KEY}`, |
| 60 | + "Content-Type": "application/json", |
| 61 | + }, |
| 62 | + } |
| 63 | + ); |
| 64 | + |
| 65 | + const reason = llmResponse.data.choices?.[0]?.message?.content?.trim(); |
| 66 | + |
| 67 | + if (reason) { |
| 68 | + return res.json({ reason, theme, source: "llm" }); |
| 69 | + } |
| 70 | + |
| 71 | + console.warn("LLM response was empty, falling back to local reasons."); |
| 72 | + } catch (err) { |
| 73 | + console.error("LLM error, falling back to local reasons:", err.message); |
| 74 | + } |
| 75 | + } |
| 76 | + |
26 | 77 | const reason = reasons[Math.floor(Math.random() * reasons.length)]; |
27 | | - res.json({ reason }); |
| 78 | + res.json({ reason, source: "offline" }); |
28 | 79 | }); |
29 | 80 |
|
30 | 81 | // Start server |
|
0 commit comments