-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
62 lines (52 loc) · 1.58 KB
/
server.js
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
61
62
const express = require('express');
const { OpenAI } = require('openai');
const cors = require('cors');
require('dotenv').config({ path: '.env.js' });
const app = express();
const port = process.env.PORT || 3000;
// Configure OpenAI client for DeepSeek
const client = new OpenAI({
apiKey: process.env.DEEPSEEK_API_KEY,
baseURL: process.env.DEEPSEEK_BASE_URL || "https://api.deepseek.com"
});
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static('public'));
// Serve index.html
app.get('/', (req, res) => {
res.sendFile(__dirname + '/templates/index.html');
});
// Stream response route
app.post('/stream', async (req, res) => {
const { message } = req.body;
// Set headers for server-sent events
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-open');
try {
const stream = await client.chat.completions.create({
model: "deepseek-chat",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: message }
],
stream: true
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) {
// Send data with 'data:' prefix for SSE
res.write(`data: ${content}\n\n`);
}
}
res.end();
} catch (error) {
console.error('Streaming error:', error);
res.status(500).json({ error: 'Streaming failed' });
}
});
// Start server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});