| |
| 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')); |