Rooni commited on
Commit
1cc4d60
·
verified ·
1 Parent(s): 148d1d0

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +62 -55
server.js CHANGED
@@ -16,14 +16,14 @@ function getRandomApiKey() {
16
  }
17
 
18
  const limiter = rateLimit({
19
- windowMs: 60 * 1000, // 5 секунд
20
- max: 15, // лимит каждые 5 секунд на IP
21
  handler: function (req, res) {
22
- return res.status(429).json("wait");
23
  },
24
  });
25
 
26
- // Применение ограничителя скорости перед обработчиком маршрута /pl и /crebeta
27
  app.use('/pl', limiter);
28
  app.use('/plbeta', limiter);
29
 
@@ -34,52 +34,77 @@ app.post('/update', async (req, res) => {
34
  res.json({ content: `{"error":"", "title":"Требуется обновление", "text":"Текущая версия приложения устарела. Установите новую из нашего телеграм канала: @yufi_ru", "okb":"Обновить", "oklink":"https://t.me/yufi_ru", "cancelable":"false"}` });
35
  });
36
 
37
- app.post('/pl', async (req, res) => {
38
- const prompt = req.body.prompt;
39
- // const apiKey = req.body.api || getRandomApiKey();
40
-
41
- let prs; // Объявление переменной prs перед использованием
42
-
43
- if (req.body.mode === "1") {
44
- prs = start;
45
- } else {
46
- prs = startconnect;
47
- }
48
-
49
- if (!prompt) {
50
- return res.status(400).json("wait"); // Не удалось принять данные
51
- }
52
 
53
  try {
54
- const response = await axios.post('https://text.pollinations.ai/openai', { //https://openai-gemini-iota.vercel.app/v1/chat/completions
55
  messages: [{'role': 'system', 'content': prs}, {'role': 'user', 'content': prompt}],
56
- max_tokens: 2000,
57
  temperature: 0.19,
58
- // model: "gemini-1.5-pro-001",
59
  }, {
60
  headers: {
61
- // 'Authorization': `Bearer ${apiKey}`,
62
  'Content-Type': 'application/json',
63
  },
64
  });
65
 
66
  if (response.data.choices && response.data.choices.length > 0 && response.data.choices[0].message) {
67
- const content = response.data.choices[0].message.content.trim();
68
- res.json({ content });
69
  } else {
70
- res.json({ content: `{"error":"", "title":"Ошибка", "text":"Произошла ошибка на сервере. (Ошибка прочтения)", "okb":"Ок", "oklink":"", "cancelable":"true"}` });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  } catch (error) {
73
- console.error(error);
74
- res.json({ content: `{"error":"", "title":"Ошибка", "text":"Произошла ошибка на сервере. (Ошибка при генерации)", "okb":"Ок", "oklink":"", "cancelable":"true"}` });
75
  }
76
  });
77
 
78
  app.post('/plbeta', async (req, res) => {
79
  const prompt = req.body.prompt;
80
- //const apiKey = req.body.api || getRandomApiKey();
81
-
82
- let prs; // Объявление переменной prs перед использованием
83
 
84
  if (req.body.mode === "1") {
85
  prs = start;
@@ -88,37 +113,19 @@ app.post('/plbeta', async (req, res) => {
88
  }
89
 
90
  if (!prompt) {
91
- return res.status(400).json("wait"); // Не удалось принять данные
92
  }
93
 
94
  try {
95
- const response = await axios.post('https://text.pollinations.ai/openai', { //https://openai-gemini-iota.vercel.app/v1/chat/completions
96
- messages: [{'role': 'system', 'content': prs}, {'role': 'user', 'content': prompt}],
97
- max_tokens: 2000,
98
- temperature: 0.19,
99
- // model: "gemini-1.5-pro-001",
100
- }, {
101
- headers: {
102
- // 'Authorization': `Bearer ${apiKey}`,
103
- 'Content-Type': 'application/json',
104
- },
105
- });
106
-
107
- if (response.data.choices && response.data.choices.length > 0 && response.data.choices[0].message) {
108
- const content = response.data.choices[0].message.content.trim();
109
- console.log(`\n---\nПользователь: ${prompt}\n Ответ:\n ${content}`);
110
- res.json({ content });
111
- } else {
112
- res.json({ content: `{"error":"", "title":"Ошибка", "text":"Произошла ошибка на сервере. (Ошибка прочтения)", "okb":"Ок", "oklink":"", "cancelable":"true"}` });
113
- }
114
  } catch (error) {
115
- console.error(error);
116
- res.json({ content: `{"error":"", "title":"Ошибка", "text":"Произошла ошибка на сервере. (Ошибка при генерации)", "okb":"Ок", "oklink":"", "cancelable":"true"}` });
117
  }
118
  });
119
 
120
-
121
  const port = 7860;
122
  app.listen(port, () => {
123
  console.log(`API сервер запущен на порту ${port}`);
124
- });
 
16
  }
17
 
18
  const limiter = rateLimit({
19
+ windowMs: 60 * 1000, // 60 секунд
20
+ max: 15, // лимит каждые 60 секунд на IP
21
  handler: function (req, res) {
22
+ return res.status(429).json({ content: "wait" });
23
  },
24
  });
25
 
26
+ // Применение ограничителя скорости перед обработчиком маршрута /pl и /plbeta
27
  app.use('/pl', limiter);
28
  app.use('/plbeta', limiter);
29
 
 
34
  res.json({ content: `{"error":"", "title":"Требуется обновление", "text":"Текущая версия приложения устарела. Установите новую из нашего телеграм канала: @yufi_ru", "okb":"Обновить", "oklink":"https://t.me/yufi_ru", "cancelable":"false"}` });
35
  });
36
 
37
+ async function sendRequest(prompt, prs) {
38
+ const firstUrl = 'https://text.pollinations.ai/openai';
39
+ const secondUrl = 'https://text.pollinations.ai/qwen-coder';
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  try {
42
+ const response = await axios.post(firstUrl, {
43
  messages: [{'role': 'system', 'content': prs}, {'role': 'user', 'content': prompt}],
44
+ max_tokens: 1200,
45
  temperature: 0.19,
 
46
  }, {
47
  headers: {
 
48
  'Content-Type': 'application/json',
49
  },
50
  });
51
 
52
  if (response.data.choices && response.data.choices.length > 0 && response.data.choices[0].message) {
53
+ return response.data.choices[0].message.content.trim();
 
54
  } else {
55
+ throw new Error("Ошибка прочтения на первой ссылке");
56
+ }
57
+ } catch (error) {
58
+ console.error("Первая попытка не удалась:", error);
59
+ // Попытка с другой ссылкой
60
+ try {
61
+ const response = await axios.post(secondUrl, {
62
+ messages: [{'role': 'system', 'content': prs}, {'role': 'user', 'content': prompt}],
63
+ max_tokens: 1200,
64
+ temperature: 0.19,
65
+ }, {
66
+ headers: {
67
+ 'Content-Type': 'application/json',
68
+ },
69
+ });
70
+
71
+ if (response.data.choices && response.data.choices.length > 0 && response.data.choices[0].message) {
72
+ return response.data.choices[0].message.content.trim();
73
+ } else {
74
+ throw new Error("Ошибка прочтения на второй ссылке");
75
+ }
76
+ } catch (secondError) {
77
+ console.error("Вторая попытка не удалась:", secondError);
78
+ throw new Error("Ошибка при генерации");
79
  }
80
+ }
81
+ }
82
+
83
+ app.post('/pl', async (req, res) => {
84
+ const prompt = req.body.prompt;
85
+ let prs;
86
+
87
+ if (req.body.mode === "1") {
88
+ prs = start;
89
+ } else {
90
+ prs = startconnect;
91
+ }
92
+
93
+ if (!prompt) {
94
+ return res.status(400).json({ content: "wait" }); // Не удалось принять данные
95
+ }
96
+
97
+ try {
98
+ const content = await sendRequest(prompt, prs);
99
+ res.json({ content });
100
  } catch (error) {
101
+ res.json({ content: `{"error":"", "title":"Ошибка", "text":"Произошла ошибка на сервере. (${error.message})", "okb":"Ок", "oklink":"", "cancelable":"true"}` });
 
102
  }
103
  });
104
 
105
  app.post('/plbeta', async (req, res) => {
106
  const prompt = req.body.prompt;
107
+ let prs;
 
 
108
 
109
  if (req.body.mode === "1") {
110
  prs = start;
 
113
  }
114
 
115
  if (!prompt) {
116
+ return res.status(400).json({ content: "wait" }); // Не удалось принять данные
117
  }
118
 
119
  try {
120
+ const content = await sendRequest(prompt, prs);
121
+ console.log(`\n---\nПользователь: ${prompt}\n Ответ:\n ${content}`);
122
+ res.json({ content });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  } catch (error) {
124
+ res.json({ content: `{"error":"", "title":"Ошибка", "text":"Произошла ошибка на сервере. (${error.message})", "okb":"Ок", "oklink":"", "cancelable":"true"}` });
 
125
  }
126
  });
127
 
 
128
  const port = 7860;
129
  app.listen(port, () => {
130
  console.log(`API сервер запущен на порту ${port}`);
131
+ });