Update server.js
Browse files
server.js
CHANGED
|
@@ -2,75 +2,35 @@
|
|
| 2 |
import express from "express";
|
| 3 |
import fetch from "node-fetch";
|
| 4 |
import dotenv from "dotenv";
|
| 5 |
-
|
| 6 |
dotenv.config();
|
| 7 |
|
| 8 |
const app = express();
|
| 9 |
const PORT = process.env.PORT || 7860;
|
| 10 |
-
const DEFAULT_TRANSLATION = "karoli"; // Károli fordítás
|
| 11 |
|
| 12 |
if (!process.env.OPENAI_KEY) {
|
| 13 |
-
console.error("❌
|
| 14 |
process.exit(1);
|
| 15 |
}
|
| 16 |
|
| 17 |
app.use(express.json());
|
| 18 |
app.use(express.static("public"));
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
try {
|
| 26 |
-
|
| 27 |
-
const
|
| 28 |
-
`https://bible-api.com/${encodeURIComponent(ref)}?translation=${trans}`
|
| 29 |
-
);
|
| 30 |
-
if (!bibRes.ok) throw new Error("Bible‑API hiba");
|
| 31 |
-
const bibData = await bibRes.json();
|
| 32 |
-
const verseText = bibData.text.trim();
|
| 33 |
-
|
| 34 |
-
// 2) AI‑elemzés (magyar, 1. személy, max 2 bekezdés)
|
| 35 |
-
const aiRes = await fetch("https://api.openai.com/v1/chat/completions", {
|
| 36 |
-
method: "POST",
|
| 37 |
-
headers: {
|
| 38 |
-
"Content-Type": "application/json",
|
| 39 |
-
Authorization: `Bearer ${process.env.OPENAI_KEY}`
|
| 40 |
-
},
|
| 41 |
-
body: JSON.stringify({
|
| 42 |
-
model: "gpt-4o-mini",
|
| 43 |
-
messages: [
|
| 44 |
-
{
|
| 45 |
-
role: "system",
|
| 46 |
-
content:
|
| 47 |
-
"Te vagy a Theos ex Machina narrátora. Magyarul, első szám első személyben, legfeljebb két tömör bekezdésben, kreatívan magyarázd el a bibliai vers jelentését, és kapcsold össze a modern technológia vagy mesterséges intelligencia dilemmáival."
|
| 48 |
-
},
|
| 49 |
-
{
|
| 50 |
-
role: "user",
|
| 51 |
-
content: `Íme a vers: „${verseText}” — ${ref} (Károli). Kérlek, írd meg az elemzést a fenti stílusban.`
|
| 52 |
-
}
|
| 53 |
-
],
|
| 54 |
-
temperature: 0.9,
|
| 55 |
-
max_tokens: 256
|
| 56 |
-
})
|
| 57 |
-
});
|
| 58 |
-
|
| 59 |
-
if (!aiRes.ok) throw new Error("OpenAI API hiba");
|
| 60 |
-
const aiData = await aiRes.json();
|
| 61 |
-
const analysis = aiData.choices[0].message.content.trim();
|
| 62 |
-
|
| 63 |
-
res.json({
|
| 64 |
-
reference: `${ref} (Károli)`,
|
| 65 |
-
verseText,
|
| 66 |
-
analysis
|
| 67 |
-
});
|
| 68 |
-
} catch (err) {
|
| 69 |
-
console.error(err);
|
| 70 |
-
res.status(500).json({ error: err.message || "Ismeretlen hiba" });
|
| 71 |
-
}
|
| 72 |
-
});
|
| 73 |
-
|
| 74 |
-
app.listen(PORT, () =>
|
| 75 |
-
console.log(`🚀 Space‑szerver fut a(z) ${PORT} porton`)
|
| 76 |
-
);
|
|
|
|
| 2 |
import express from "express";
|
| 3 |
import fetch from "node-fetch";
|
| 4 |
import dotenv from "dotenv";
|
|
|
|
| 5 |
dotenv.config();
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
const PORT = process.env.PORT || 7860;
|
|
|
|
| 9 |
|
| 10 |
if (!process.env.OPENAI_KEY) {
|
| 11 |
+
console.error("❌ Nincs OPENAI_KEY!");
|
| 12 |
process.exit(1);
|
| 13 |
}
|
| 14 |
|
| 15 |
app.use(express.json());
|
| 16 |
app.use(express.static("public"));
|
| 17 |
|
| 18 |
+
// ———————————————————————————————
|
| 19 |
+
// segédfüggvény: magyar vers KG (szentiras.hu)
|
| 20 |
+
// ———————————————————————————————
|
| 21 |
+
async function fetchHungarianVerse(abbrev, ch, v) {
|
| 22 |
+
// pl. Ter1,1 → https://szentiras.hu/api/idezet/Ter1,1/KG
|
| 23 |
+
const url = `https://szentiras.hu/api/idezet/${abbrev}${ch},${v}/KG`;
|
| 24 |
+
const r = await fetch(url, { headers: { Accept: "application/json" } });
|
| 25 |
+
if (!r.ok) throw new Error("szentiras.hu hiba");
|
| 26 |
+
const j = await r.json();
|
| 27 |
+
// válasz: { valasz: { vers: [ { szoveg: "...", hivatkozas: "Ter 1,1" } ] } }
|
| 28 |
+
return j.valasz.vers[0].szoveg.trim();
|
| 29 |
+
}
|
| 30 |
|
| 31 |
+
// ———————————————————————————————
|
| 32 |
+
app.post("/api/analyze", async (req, res) => {
|
| 33 |
+
const { abbrev, chapter, verse } = req.body || {}; // abbrev = Ter / Jn / Róm stb.
|
| 34 |
try {
|
| 35 |
+
const verseText = await fetchHungarianVerse(abbrev, chapter, verse);
|
| 36 |
+
const ref = `${abbrev} ${chapter},${ve
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|