-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
159 lines (137 loc) · 5.31 KB
/
server.js
File metadata and controls
159 lines (137 loc) · 5.31 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env node
/**
* Node.js server that hosts the RAGE pipeline endpoints (/ingest, /inference),
* logs “Python Ingest: Batches…” for batch progress lines from Python
* rather than labeling them as errors.
*/
const http = require('http');
const fs = require('fs');
const { spawn } = require('child_process');
const path = require('path');
const PORT = 3000;
// Use an environment variable to specify Python path, fallback to 'python'
const pythonExecutable = process.env.PYTHON_PATH || 'python';
console.log('Using Python executable:', pythonExecutable);
const memoryFolder = path.join(__dirname, 'memory');
if (!fs.existsSync(memoryFolder)) {
fs.mkdirSync(memoryFolder);
}
/**
* Serves static files (index.html, style.css) for minimal front-end testing
*/
function serveFile(res, filePath, contentType) {
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(500);
return res.end(`Error loading ${filePath}`);
}
res.writeHead(200, { 'Content-Type': contentType });
res.end(data);
});
}
const server = http.createServer((req, res) => {
if (req.method === 'GET') {
if (req.url === '/' || req.url === '/index.html') {
serveFile(res, path.join(__dirname, 'index.html'), 'text/html');
} else if (req.url === '/style.css') {
serveFile(res, path.join(__dirname, 'style.css'), 'text/css');
} else {
res.writeHead(404);
res.end('Not found');
}
}
else if (req.method === 'POST' && req.url === '/ingest') {
// /ingest route for data ingestion
let body = '';
req.on('data', chunk => { body += chunk.toString(); });
req.on('end', () => {
const params = new URLSearchParams(body);
const folderpath = params.get('folderpath') || '';
const urlpath = params.get('urlpath') || '';
const filetext = params.get('filetext') || '';
const chunkSize = params.get('chunkSize') || '128';
// Possibly save user-pasted text to docs/ folder
let tempFilePath = null;
if (filetext.trim().length > 0) {
const docsFolder = path.join(__dirname, 'docs');
if (!fs.existsSync(docsFolder)) fs.mkdirSync(docsFolder);
tempFilePath = path.join(docsFolder, `pasted_${Date.now()}.txt`);
fs.writeFileSync(tempFilePath, filetext, 'utf-8');
}
const ingestionPayload = {
chunk_size: parseInt(chunkSize, 10),
filepaths: tempFilePath ? [tempFilePath] : [],
folderpaths: folderpath ? [folderpath] : [],
urls: urlpath ? [urlpath] : []
};
// Spawn Python ingestion script
const pyProcess = spawn(pythonExecutable, ['rag_inference.py', 'ingest']);
pyProcess.stdin.write(JSON.stringify(ingestionPayload));
pyProcess.stdin.end();
let resultData = '';
pyProcess.stdout.on('data', (data) => {
resultData += data.toString();
});
pyProcess.stderr.on('data', (data) => {
const stderr = data.toString().trim();
if (stderr.includes('Batches:')) {
// Show as progress line
console.log(`Python Ingest: ${stderr}`);
} else if (stderr) {
// Actual error line
console.error(`Python Ingest Error: ${stderr}`);
}
});
pyProcess.on('close', (code) => {
const memFileName = path.join(memoryFolder, `ingest_${Date.now()}.json`);
fs.writeFileSync(memFileName, JSON.stringify({ ingestionPayload, resultData }, null, 2));
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: "ingested", memoryFile: memFileName, details: resultData }, null, 2));
});
});
}
else if (req.method === 'POST' && req.url === '/inference') {
// /inference route for final query
let body = '';
req.on('data', chunk => { body += chunk.toString(); });
req.on('end', () => {
const params = new URLSearchParams(body);
const userQuery = params.get('query') || 'No query provided';
const backend = params.get('backend') || 'local';
const pyProcess = spawn(pythonExecutable, ['rag_inference.py', userQuery, backend]);
let resultData = '';
pyProcess.stdout.on('data', (data) => {
resultData += data.toString();
});
pyProcess.stderr.on('data', (data) => {
const stderr = data.toString().trim();
// For inference, typically no "Batches:" lines, but let's handle similarly
if (stderr.includes('Batches:')) {
console.log(`Python Ingest: ${stderr}`);
} else if (stderr) {
console.error(`Python Error: ${stderr}`);
}
});
pyProcess.on('close', (code) => {
try {
const jsonResponse = JSON.parse(resultData);
const memFileName = path.join(memoryFolder, `inference_${Date.now()}.json`);
fs.writeFileSync(memFileName, JSON.stringify(jsonResponse, null, 2));
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ memoryFile: memFileName, ...jsonResponse }, null, 2));
} catch (err) {
console.error('Error parsing Python output:', err);
res.writeHead(500);
res.end('Internal Server Error');
}
});
});
}
else {
res.writeHead(404);
res.end('Not Found');
}
});
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
});