-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (52 loc) · 1.89 KB
/
index.js
File metadata and controls
69 lines (52 loc) · 1.89 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import express from "express";
import cors from "cors";
import path from "path";
import { API_KEY, MODEL_NAME } from "./config.js";
const app = express();
const port = 3001;
const __dirname = path.resolve();
app.use(cors());
app.use(express.json());
// Serve HTML
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "index.html"));
});
// AI Endpoint
app.post("/ask", async (req, res) => {
const { question, language } = req.body;
console.log(`📨 Question: "${question}" | Language: ${language}`);
// The Magic Prompt
const prompt = `
You are Bureaucracy Copilot, an expert in Indian government processes.
Task: Explain the following process simply, step-by-step.
Format: Use clear headings, bullet points, and bold text for important documents.
Target Audience: A beginner who doesn't understand legal words.
IMPORTANT: Write the ENTIRE response in ${language}.
User Question: ${question}
`;
const url = `https://generativelanguage.googleapis.com/v1beta/models/${MODEL_NAME}:generateContent?key=${API_KEY}`;
try {
const response = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
contents: [{ parts: [{ text: prompt }] }]
}),
});
const data = await response.json();
if (!response.ok) {
console.error("❌ Google Error:", data);
return res.status(500).json({ error: data.error?.message || "AI Error" });
}
const answer = data.candidates[0].content.parts[0].text;
console.log("✅ AI Answered in " + language);
res.json({ result: answer });
} catch (error) {
console.error("🔥 Server Error:", error);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.listen(port, () => {
console.log(`\n🚀 Bureaucracy Copilot V3 is Live!`);
console.log(`👉 http://localhost:${port}\n`);
});