Ricky01anjay commited on
Commit
8922034
·
verified ·
1 Parent(s): 73f7c00

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +21 -12
server.js CHANGED
@@ -7,14 +7,15 @@ 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 {
@@ -53,17 +54,25 @@ async function getAliciaResponse(userid, prompt) {
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++;
@@ -74,23 +83,23 @@ async function getAliciaResponse(userid, prompt) {
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
  });
 
7
 
8
  const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
9
 
10
+ async function getAIResponse(userid, prompt, system) {
11
  const maxRetries = 3;
12
  let attempt = 0;
13
 
14
  let history = cache.get(userid) || [];
15
+ const baseSystem = system || "Kamu adalah asisten AI yang membantu.";
16
+ const formatRule = 'WAJIB merespon HANYA dalam format JSON: [{"role": "model", "parts": [{"text": "JAWABAN_DISINI"}]}]';
17
 
18
+ const fullPrompt = `System Instruction: ${baseSystem}\nFormatting Rule: ${formatRule}\n\nHistory:\n${history.join('\n')}\nUser: ${prompt}\nAI:`;
19
 
20
  while (attempt <= maxRetries) {
21
  try {
 
54
 
55
  if (!fullText || fullText.trim() === "") throw new Error("Empty_Response");
56
 
57
+ let finalJson;
58
  const jsonMatch = fullText.match(/\[\s*\{\s*"role":\s*"model"[\s\S]*\}\s*\]/);
 
59
 
60
+ if (jsonMatch) {
61
+ try {
62
+ finalJson = JSON.parse(jsonMatch[0]);
63
+ } catch (e) {
64
+ finalJson = [{ role: 'model', parts: [{ text: fullText.trim() }] }];
65
+ }
66
+ } else {
67
+ finalJson = [{ role: 'model', parts: [{ text: fullText.trim() }] }];
68
+ }
69
 
70
+ const extractedText = finalJson[0].parts[0].text;
71
+ history.push(`User: ${prompt}`, `AI: ${extractedText}`);
72
  if (history.length > 10) history.splice(0, 2);
73
  cache.set(userid, history);
74
 
75
+ return finalJson;
76
 
77
  } catch (error) {
78
  attempt++;
 
83
  }
84
 
85
  app.get('/chat', async (req, res) => {
86
+ const { userid, prompt, system } = req.query;
87
 
88
  if (!userid || !prompt) {
89
  return res.status(400).json({ error: "Missing userid or prompt" });
90
  }
91
 
92
  try {
93
+ const result = await getAIResponse(userid, prompt, system);
94
  res.json(result);
95
  } catch (err) {
96
  res.status(500).json({
97
+ error: "Internal Server Error",
98
  message: err.message
99
  });
100
  }
101
  });
102
 
103
  app.listen(port, () => {
104
+ console.log(`Server running on port ${port}`);
105
  });