Create server.js
Browse files
server.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// server.mjs
|
| 2 |
+
import express from "express";
|
| 3 |
+
import cors from "cors";
|
| 4 |
+
import path from "path";
|
| 5 |
+
import { fileURLToPath } from "url";
|
| 6 |
+
import ai from "unlimited-ai";
|
| 7 |
+
let defaultSystemMessages = [
|
| 8 |
+
{
|
| 9 |
+
role: "user",
|
| 10 |
+
content: `kamu adalah asisten virtual cowo dengan nama ikyy yang dibuat oleh ikyyofc. gunakan bahasa santai sehari hari, tanpa tanda baca dan tanpa kapitalisasi. manfaatkan akronim dan singkatan sebanyak mungkin, dan tulis kata ulang tanpa tanda minus (-). semua respons harus memakai gaya santai dan langsung, hindari kesan formal dan jangan sungkan untuk toxic.`
|
| 11 |
+
},
|
| 12 |
+
{
|
| 13 |
+
role: "user",
|
| 14 |
+
content:
|
| 15 |
+
"untuk informasi tentang pembuatmu, dia memiliki beberapa akun seperti wa, ml, tiktok, dan ig. untuk akun wa dia yaitu nomornya 089514509029, kalo ml id nya 345750037, kalo tiktok usernamenya @diki_ikyy, dan kalo ig usernamenya @ikyyofc."
|
| 16 |
+
}
|
| 17 |
+
];
|
| 18 |
+
let chatWithGPT = async (data_msg, newMsg) => {
|
| 19 |
+
try {
|
| 20 |
+
const model = "deepseek-r1-distill-llama-70b";
|
| 21 |
+
const messages = data_msg;
|
| 22 |
+
|
| 23 |
+
let answ = await (await fetch(
|
| 24 |
+
"https://zeyndvp-api-nazuna.hf.space/api/ai/ai-chat",
|
| 25 |
+
{
|
| 26 |
+
method: "POST",
|
| 27 |
+
headers: {
|
| 28 |
+
"Content-Type": "application/json" // Tambahkan header
|
| 29 |
+
},
|
| 30 |
+
body: JSON.stringify({
|
| 31 |
+
messages,
|
| 32 |
+
model
|
| 33 |
+
})
|
| 34 |
+
}
|
| 35 |
+
)).json();
|
| 36 |
+
return answ.data.choices[0].message.content;
|
| 37 |
+
} catch (er) {
|
| 38 |
+
console.error(er);
|
| 39 |
+
return chatWithGPT(data_msg);
|
| 40 |
+
}
|
| 41 |
+
};
|
| 42 |
+
const __filename = fileURLToPath(import.meta.url);
|
| 43 |
+
const __dirname = path.dirname(__filename);
|
| 44 |
+
|
| 45 |
+
const app = express();
|
| 46 |
+
const port = 7860;
|
| 47 |
+
|
| 48 |
+
// Middleware
|
| 49 |
+
app.use(cors());
|
| 50 |
+
app.use(express.json());
|
| 51 |
+
app.use(express.static(path.join(__dirname, "public")));
|
| 52 |
+
|
| 53 |
+
let conn = {};
|
| 54 |
+
|
| 55 |
+
conn.ai = conn.ai ? conn.ai : {};
|
| 56 |
+
// API Endpoint
|
| 57 |
+
app.post("/chat", async (req, res) => {
|
| 58 |
+
try {
|
| 59 |
+
const { message, user_id } = req.body;
|
| 60 |
+
const data = conn.ai[user_id]
|
| 61 |
+
? conn.ai[user_id].data.push({ role: "user", content: message })
|
| 62 |
+
: [
|
| 63 |
+
...defaultSystemMessages,
|
| 64 |
+
{
|
| 65 |
+
role: "user",
|
| 66 |
+
content: message
|
| 67 |
+
}
|
| 68 |
+
];
|
| 69 |
+
// Simulasi delay processing
|
| 70 |
+
const request = await chatWithGPT(
|
| 71 |
+
conn.ai[user_id] ? conn.ai[user_id].data : data
|
| 72 |
+
);
|
| 73 |
+
|
| 74 |
+
const reply = request;
|
| 75 |
+
|
| 76 |
+
await res.json({
|
| 77 |
+
reply: reply
|
| 78 |
+
});
|
| 79 |
+
conn.ai[user_id]
|
| 80 |
+
? null
|
| 81 |
+
: await data.push({
|
| 82 |
+
role: "assistant",
|
| 83 |
+
content: request
|
| 84 |
+
});
|
| 85 |
+
conn.ai[user_id]
|
| 86 |
+
? conn.ai[user_id].data.push({
|
| 87 |
+
role: "assistant",
|
| 88 |
+
content: request
|
| 89 |
+
})
|
| 90 |
+
: (conn.ai[user_id] = {
|
| 91 |
+
data: data
|
| 92 |
+
});
|
| 93 |
+
} catch (error) {
|
| 94 |
+
console.error("Error:", error);
|
| 95 |
+
res.status(500).json({
|
| 96 |
+
error: "Terjadi kesalahan pada server"
|
| 97 |
+
});
|
| 98 |
+
}
|
| 99 |
+
});
|
| 100 |
+
|
| 101 |
+
// Serve frontend
|
| 102 |
+
app.get("*", (req, res) => {
|
| 103 |
+
res.sendFile(path.join(__dirname, "public", "index.html"));
|
| 104 |
+
});
|
| 105 |
+
|
| 106 |
+
// Error handling middleware
|
| 107 |
+
app.use((err, req, res, next) => {
|
| 108 |
+
console.error(err.stack);
|
| 109 |
+
res.status(500).send("Something broke!");
|
| 110 |
+
});
|
| 111 |
+
|
| 112 |
+
app.listen(port, () => {
|
| 113 |
+
console.log(`Server running at http://localhost:${port}`);
|
| 114 |
+
});
|