-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
131 lines (109 loc) · 3.99 KB
/
Copy pathserver.js
File metadata and controls
131 lines (109 loc) · 3.99 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const express = require('express');
const { OpenAI } = require('openai');
const dotenv = require('dotenv');
const path = require('path');
const bodyParser = require('body-parser');
const fs = require('fs');
// Load environment variables
dotenv.config();
// Initialize Express app
const app = express();
const PORT = process.env.PORT || 3000;
// Create OpenAI client
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
// Load system prompt from file
let systemPrompt;
try {
const promptPath = path.join(__dirname, 'systemprompt.txt');
console.log(`Attempting to load system prompt from: ${promptPath}`);
if (fs.existsSync(promptPath)) {
systemPrompt = fs.readFileSync(promptPath, 'utf8');
console.log('System prompt loaded successfully:');
console.log('First 50 characters:', systemPrompt.substring(0, 50));
console.log('Length:', systemPrompt.length);
} else {
console.error(`File not found: ${promptPath}`);
systemPrompt = 'You are a helpful assistant.'; // Fallback prompt
console.log('Using fallback system prompt because file was not found');
}
} catch (error) {
console.error('Error loading system prompt:', error);
systemPrompt = 'You are a helpful assistant.'; // Fallback prompt
console.log('Using fallback system prompt due to error');
}
// Middleware for parsing JSON bodies
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname)));
// Debug middleware to log all requests
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`);
console.log('Request body:', req.body);
next();
});
// API endpoint for chat with streaming
app.post('/api/chat', async (req, res) => {
console.log('Received request to /api/chat');
const userMessage = req.body?.message;
if (!userMessage) {
console.log('No message provided in request');
return res.status(400).json({ error: 'Message is required' });
}
console.log(`User message: "${userMessage}"`);
console.log('Using system prompt:', systemPrompt.substring(0, 50) + '...');
try {
// Set up SSE headers for streaming
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
// Create streaming completion
const stream = await openai.chat.completions.create({
model: 'gpt-4.1-2025-04-14',
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: userMessage }
],
stream: true,
});
console.log('Starting OpenAI stream');
// Stream the response chunks to the client
let fullResponse = '';
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content || '';
if (content) {
fullResponse += content;
res.write(`data: ${JSON.stringify({ content })}\n\n`);
}
}
console.log('Stream complete');
console.log('Full response length:', fullResponse.length);
// Send a "done" event to signify the stream is complete
res.write(`data: ${JSON.stringify({ done: true })}\n\n`);
res.end();
} catch (error) {
console.error('Error processing request:', error);
// If we've already started streaming, send an error event
if (res.headersSent) {
res.write(`data: ${JSON.stringify({ error: 'Failed to get complete response from AI' })}\n\n`);
res.end();
} else {
// Otherwise send a regular JSON error response
res.status(500).json({ error: 'Failed to get response from AI', details: error.message });
}
}
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error('Server error:', err);
res.status(500).json({ error: 'Server error', details: err.message });
});
// Handle all other routes
app.all('*', (req, res) => {
console.log(`Route not found: ${req.method} ${req.url}`);
res.status(404).json({ error: 'Route not found' });
});
// Start the server
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});