-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
70 lines (58 loc) · 2.07 KB
/
server.js
File metadata and controls
70 lines (58 loc) · 2.07 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
70
const express = require("express");
const fetch = require("node-fetch"); // node-fetch v2 required
const cors = require("cors");
const path = require("path");
require("dotenv").config();
console.log("Loaded API Key:", process.env.OPENAI_API_KEY?.slice(0, 10) + "...");
const app = express();
// Middleware
app.use(cors());
app.use(express.json());
// 🔹 Serve static files from the "public" folder
app.use(express.static(path.join(__dirname, "public")));
// 🔹 Root route fallback
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
// 🔹 API endpoint for interpreting omens
app.post("/api/omen", async (req, res) => {
const { species } = req.body;
try {
const response = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${process.env.OPENAI_API_KEY}`
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
temperature: 0.9,
messages: [
{
role: "system",
content: "You are an ornithologist who interprets bird sightings as omens. Speak in brief, prophetic phrases — evocative but concise, no more than 2 sentences. Give advice based on the bird omen."
},
{
role: "user",
content: `What is the symbolic meaning of seeing a ${species}? Speak with confidence and clarity.`
}
]
})
});
const data = await response.json();
console.log("🔍 Raw OpenAI response:", JSON.stringify(data, null, 2));
if (!data.choices || !data.choices[0]) {
throw new Error("No choices returned from OpenAI.");
}
const omen = data.choices[0].message.content;
res.json({ omen });
} catch (error) {
console.error("🔴 Error fetching omen:", error.message);
res.status(500).json({ error: "Failed to interpret the omen." });
}
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Proxy server running on port ${PORT}`);
});