File size: 1,714 Bytes
bb1af57
 
 
 
 
 
bd588c8
bb1af57
bd588c8
 
4f82925
bd588c8
4f82925
 
 
 
 
 
95f0cda
bb1af57
238b644
bb1af57
 
 
 
238b644
bb1af57
 
 
238b644
 
 
 
 
 
 
bb1af57
238b644
bb1af57
 
 
 
 
 
 
 
 
 
 
 
 
bd588c8
bb1af57
 
 
 
 
bd588c8
 
bb1af57
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//require('dotenv').config();
const Groq = require('groq-sdk');
const express = require('express');
const fs = require('fs');
const path = require('path');
const os = require('os');

const groq = new Groq({ apiKey: process.env.GROQ_API_KEY });
const app = express();
app.use(express.json());
app.set('json spaces', 4);

app.all('/', async (req, res) => {
  const files = fs.readdirSync(path.join(os.tmpdir()));
  res.json({
    "total session": files?.length
  });
});

app.post('/chat', async (req, res) => {
  const { sessionId, message, temperature = 0.6, system = '' } = req.body;
  if (!sessionId || !message) return res.status(400).json({ error: 'Missing sessionId or message' });

  const sessionFile = path.join(os.tmpdir(), sessionId);
  let history = fs.existsSync(sessionFile) ? JSON.parse(fs.readFileSync(sessionFile, 'utf8')) : [];
  
  history.push({ role: 'user', content: message });

  const chatCompletion = await groq.chat.completions.create({
    messages: system ? [
      {
        role: 'system', 
        content: system
      },
      ...history
    ] : history,
    model: 'deepseek-r1-distill-qwen-32b',
    temperature,
    max_completion_tokens: 4096,
    top_p: 0.95,
    stream: true
  });

  res.setHeader('Content-Type', 'text/plain');
  res.setHeader('Transfer-Encoding', 'chunked');

  let responseText = '';
  for await (const chunk of chatCompletion) {
    const content = chunk.choices[0]?.delta?.content || '';
    responseText += content;
    res.write(content);
  }

  history.push({ role: 'assistant', content: responseText });
  fs.writeFileSync(sessionFile, JSON.stringify(history));

  res.end();
});

app.listen(7860, () => console.log('Server running on port 7860'));