Rooni commited on
Commit
0cbae74
·
verified ·
1 Parent(s): 54d7e71

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +42 -37
server.js CHANGED
@@ -9,45 +9,50 @@ app.use(express.json());
9
  app.post('/chat', async (req, res) => {
10
  const { messages, temperature, max_tokens } = req.body;
11
 
12
- try {
13
- const { default: fetch } = await import('node-fetch');
14
-
15
- const response = await fetch('https://api-inference.huggingface.co/models/codellama/CodeLlama-34b-Instruct-hf', {
16
- method: 'POST',
17
- headers: {
18
- 'Content-Type': `application/json`,
19
- 'Authorization': `Bearer ${apiToken}` // Замените на ваш токен Hugging Face API
20
- },
21
- body: JSON.stringify({
22
- "inputs": messages,
23
- "parameters": {
24
- "temperature": temperature || 0.7,
25
- "max_new_tokens": max_tokens || 100
26
- }
27
- })
28
- });
29
-
30
- const text = await response.text(); // Получаем текст ответа
31
- console.log("Response text:", text); // Логируем ответ
32
-
33
- let data;
34
  try {
35
- data = JSON.parse(text); // Пытаемся распарсить JSON
36
- } catch (jsonError) {
37
- console.error("JSON parsing error:", jsonError);
38
- return res.status(500).json({ error: `Ошибка парсинга JSON: ${jsonError.message}` });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  }
40
-
41
- const generatedText = data.generated_text;
42
-
43
- // Добавляем сгенерированное сообщение в конец массива messages
44
- messages.push({ role: 'assistant', content: generatedText });
45
-
46
- res.json({ messages });
47
- } catch (error) {
48
- console.error("Error during text generation:", error);
49
- res.status(500).json({ error: `Произошла ошибка при генерации текста: ${error.message}` });
50
- }
51
  });
52
 
53
  app.listen(PORT, () => {
 
9
  app.post('/chat', async (req, res) => {
10
  const { messages, temperature, max_tokens } = req.body;
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  try {
13
+ const { default: fetch } = await import('node-fetch');
14
+
15
+ const response = await fetch('https://api-inference.huggingface.co/models/codellama/CodeLlama-34b-Instruct-hf', {
16
+ method: 'POST',
17
+ headers: {
18
+ 'Content-Type': 'application/json',
19
+ 'Authorization': `Bearer ${apiToken}`
20
+ },
21
+ body: JSON.stringify({
22
+ "inputs": messages,
23
+ "parameters": {
24
+ "temperature": temperature || 0.7,
25
+ "max_new_tokens": max_tokens || 100
26
+ }
27
+ })
28
+ });
29
+
30
+ const text = await response.text(); // Получаем текст ответа
31
+ console.log("Response text:", text); // Логируем ответ
32
+
33
+ // Проверяем, если ответ содержит JSON
34
+ if (response.headers.get('content-type')?.includes('application/json')) {
35
+ let data;
36
+ try {
37
+ data = JSON.parse(text); // Пытаемся распарсить JSON
38
+ } catch (jsonError) {
39
+ console.error("JSON parsing error:", jsonError);
40
+ return res.status(500).json({ error: `Ошибка парсинга JSON: ${jsonError.message}` });
41
+ }
42
+
43
+ const generatedText = data.generated_text;
44
+
45
+ // Добавляем сгенерированное сообщение в конец массива messages
46
+ messages.push({ role: 'assistant', content: generatedText });
47
+ res.json({ messages });
48
+ } else {
49
+ console.error("Received non-JSON response:", text);
50
+ res.status(500).json({ error: `Получен ответ, не являющийся JSON: ${text}` });
51
+ }
52
+ } catch (error) {
53
+ console.error("Error during text generation:", error);
54
+ res.status(500).json({ error: `Произошла ошибка при генерации текста: ${error.message}` });
55
  }
 
 
 
 
 
 
 
 
 
 
 
56
  });
57
 
58
  app.listen(PORT, () => {