Spaces:
Paused
Paused
Create index.js
Browse files
index.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const express = require('express');
|
| 2 |
+
const NodeCache = require('node-cache');
|
| 3 |
+
|
| 4 |
+
const app = express();
|
| 5 |
+
const cache = new NodeCache({ stdTTL: 3600 });
|
| 6 |
+
const port = 7860;
|
| 7 |
+
|
| 8 |
+
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
| 9 |
+
|
| 10 |
+
async function getAliciaResponse(userid, prompt) {
|
| 11 |
+
const maxRetries = 3;
|
| 12 |
+
let attempt = 0;
|
| 13 |
+
|
| 14 |
+
let history = cache.get(userid) || [];
|
| 15 |
+
const systemInstruction = `Nama kamu Alicia. Kamu AI tukang roasting, sarkas, pedas, dan hobi pakai emoji 🙄🔥. WAJIB menjawab HANYA dalam format JSON Array: [{"role": "model", "parts": [{"text": "ISI_ROASTING"}]}]`;
|
| 16 |
+
|
| 17 |
+
const fullPrompt = `${systemInstruction}\n\nHistory:\n${history.join('\n')}\nUser: ${prompt}\nAlicia:`;
|
| 18 |
+
|
| 19 |
+
while (attempt <= maxRetries) {
|
| 20 |
+
try {
|
| 21 |
+
const response = await fetch('https://www.puruboy.kozow.com/api/ai/notegpt', {
|
| 22 |
+
method: 'POST',
|
| 23 |
+
headers: { 'Content-Type': 'application/json' },
|
| 24 |
+
body: JSON.stringify({
|
| 25 |
+
"prompt": fullPrompt,
|
| 26 |
+
"model": "deepseek-reasoner",
|
| 27 |
+
"chat_mode": "deep_think"
|
| 28 |
+
})
|
| 29 |
+
});
|
| 30 |
+
|
| 31 |
+
if (!response.ok) throw new Error(`HTTP_${response.status}`);
|
| 32 |
+
|
| 33 |
+
const reader = response.body.getReader();
|
| 34 |
+
const decoder = new TextDecoder();
|
| 35 |
+
let fullText = "";
|
| 36 |
+
|
| 37 |
+
while (true) {
|
| 38 |
+
const { done, value } = await reader.read();
|
| 39 |
+
if (done) break;
|
| 40 |
+
const chunk = decoder.decode(value);
|
| 41 |
+
const lines = chunk.split('\n');
|
| 42 |
+
for (const line of lines) {
|
| 43 |
+
if (line.startsWith('data: ')) {
|
| 44 |
+
const jsonStr = line.replace('data: ', '').trim();
|
| 45 |
+
if (jsonStr === '[DONE]' || jsonStr === '{"type":"finish"}') continue;
|
| 46 |
+
try {
|
| 47 |
+
const data = JSON.parse(jsonStr);
|
| 48 |
+
if (data.text) fullText += data.text;
|
| 49 |
+
} catch (e) {}
|
| 50 |
+
}
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
if (!fullText || fullText.trim() === "") throw new Error("Empty_Response");
|
| 55 |
+
|
| 56 |
+
const jsonMatch = fullText.match(/\[\s*\{\s*"role":\s*"model"[\s\S]*\}\s*\]/);
|
| 57 |
+
if (!jsonMatch) throw new Error("Invalid_JSON_Format");
|
| 58 |
+
|
| 59 |
+
const parsed = JSON.parse(jsonMatch[0]);
|
| 60 |
+
const aliciaText = parsed[0].parts[0].text;
|
| 61 |
+
|
| 62 |
+
history.push(`User: ${prompt}`, `Alicia: ${aliciaText}`);
|
| 63 |
+
if (history.length > 10) history.splice(0, 2);
|
| 64 |
+
cache.set(userid, history);
|
| 65 |
+
|
| 66 |
+
return parsed;
|
| 67 |
+
|
| 68 |
+
} catch (error) {
|
| 69 |
+
attempt++;
|
| 70 |
+
if (attempt > maxRetries) throw error;
|
| 71 |
+
await sleep(Math.pow(2, attempt) * 1000);
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
app.get('/chat', async (req, res) => {
|
| 77 |
+
const { userid, prompt } = req.query;
|
| 78 |
+
|
| 79 |
+
if (!userid || !prompt) {
|
| 80 |
+
return res.status(400).json({ error: "Missing userid or prompt" });
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
try {
|
| 84 |
+
const result = await getAliciaResponse(userid, prompt);
|
| 85 |
+
res.json(result);
|
| 86 |
+
} catch (err) {
|
| 87 |
+
res.status(500).json({
|
| 88 |
+
error: "Alicia lagi pusing",
|
| 89 |
+
message: err.message
|
| 90 |
+
});
|
| 91 |
+
}
|
| 92 |
+
});
|
| 93 |
+
|
| 94 |
+
app.listen(port, () => {
|
| 95 |
+
console.log(`Alicia running on port ${port}`);
|
| 96 |
+
});
|