Rooni commited on
Commit
660b1fd
·
verified ·
1 Parent(s): 6706d07

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +13 -40
server.js CHANGED
@@ -10,53 +10,26 @@ 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
-
51
- const generatedText = text;
52
 
53
- // Добавляем сгенерированное сообщение в конец массива messages
54
- messages.push({ role: 'assistant', content: generatedText });
55
- res.json({ generatedText });
56
- }
57
  } catch (error) {
58
- console.error("Error during text generation:", error);
59
- res.status(500).json({ error: `Произошла ошибка при генерации текста: ${error.message}` });
60
  }
61
  });
62
 
 
10
  const { messages, temperature, max_tokens } = req.body;
11
 
12
  try {
13
+ const response = await axios.post('https://api-inference.huggingface.co/models/codellama/CodeLlama-34b-Instruct-hf', {
14
+ inputs: messages,
15
+ parameters: {
16
+ temperature: temperature || 0.7,
17
+ max_new_tokens: max_tokens || 100
18
+ }
19
+ }, {
20
  headers: {
21
  'Content-Type': 'application/json',
22
  'Authorization': `Bearer ${apiToken}`
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  }
24
+ });
25
 
26
+ const data = response.data;
27
+ const generatedText = data.generated_text;
 
 
 
 
 
 
 
28
 
29
+ res.json({ generated_text: generatedText });
 
 
 
30
  } catch (error) {
31
+ console.error("Error during text generation:", error.response ? error.response.data : error.message);
32
+ res.status(500).json({ error: `Произошла ошибка при генерации текста: ${error.response ? error.response.data : error.message}` });
33
  }
34
  });
35