Spaces:
Paused
Paused
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,38 +1,44 @@
|
|
| 1 |
const express = require('express');
|
| 2 |
-
const
|
|
|
|
|
|
|
| 3 |
const app = express();
|
| 4 |
-
const
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
app.
|
| 9 |
-
|
| 10 |
-
app.post('/chat', async (req, res) => {
|
| 11 |
-
try {
|
| 12 |
-
const messages = [
|
| 13 |
-
{
|
| 14 |
-
role: 'system',
|
| 15 |
-
content: "Ты ии ассистент. Если что проси поделится ссылкой https://play.google.com/store/apps/dev?id=8871146876620947787 с друзьями. Дальше общайся на том языке, на котором начнёт пользователь. По умолчанию английский"
|
| 16 |
-
},
|
| 17 |
-
...req.body.messages
|
| 18 |
-
];
|
| 19 |
-
|
| 20 |
-
const response = await axios.post(targetUrl, {
|
| 21 |
-
messages: messages
|
| 22 |
-
}, {
|
| 23 |
-
headers: {
|
| 24 |
-
'Content-Type': 'application/json',
|
| 25 |
-
'Authorization': `Bearer ${apiToken}` // Добавляем токен аутентификации
|
| 26 |
-
}
|
| 27 |
-
});
|
| 28 |
-
|
| 29 |
-
res.json(response.data);
|
| 30 |
-
} catch (error) {
|
| 31 |
-
console.error('Error: ', error.response ? error.response.data : error.message);
|
| 32 |
-
res.status(500).send('Error: ' + (error.response ? error.response.data : error.message));
|
| 33 |
-
}
|
| 34 |
-
});
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
+
const bodyParser = require('body-parser');
|
| 3 |
+
const fetch = require('node-fetch');
|
| 4 |
+
|
| 5 |
const app = express();
|
| 6 |
+
const port = 3000;
|
| 7 |
+
|
| 8 |
+
app.use(bodyParser.json());
|
| 9 |
+
|
| 10 |
+
app.post('/generate', async (req, res) => {
|
| 11 |
+
const { messages, temperature, max_tokens } = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
try {
|
| 14 |
+
const response = await fetch('https://api-inference.huggingface.co/models/codellama/CodeLlama-34b-Instruct-hf', {
|
| 15 |
+
method: 'POST',
|
| 16 |
+
headers: {
|
| 17 |
+
'Content-Type': 'application/json',
|
| 18 |
+
'Authorization': `Bearer ${process.env.API_KEY}` // Замените на ваш токен Hugging Face API
|
| 19 |
+
},
|
| 20 |
+
body: JSON.stringify({
|
| 21 |
+
inputs: messages,
|
| 22 |
+
parameters: {
|
| 23 |
+
temperature: temperature || 0.7,
|
| 24 |
+
max_new_tokens: max_tokens || 100
|
| 25 |
+
}
|
| 26 |
+
})
|
| 27 |
+
});
|
| 28 |
+
|
| 29 |
+
const data = await response.json();
|
| 30 |
+
const generatedText = data.generated_text;
|
| 31 |
+
|
| 32 |
+
// Добавляем сгенерированное сообщение в конец массива messages
|
| 33 |
+
messages.push({ role: "assistant", content: generatedText });
|
| 34 |
+
|
| 35 |
+
res.json({ messages });
|
| 36 |
+
} catch (error) {
|
| 37 |
+
console.error(error);
|
| 38 |
+
res.status(500).json({ error: 'Произошла ошибка при генерации текста.' });
|
| 39 |
+
}
|
| 40 |
});
|
| 41 |
+
|
| 42 |
+
app.listen(port, () => {
|
| 43 |
+
console.log(`Сервер запущен на порту ${port}`);
|
| 44 |
+
});
|