Repository navigation
Expand file tree
/
Copy pathapi-server.js
More file actions
400 lines (365 loc) · 12.8 KB
/
Copy pathapi-server.js
File metadata and controls
400 lines (365 loc) · 12.8 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import express from 'express';
import cors from 'cors';
import dotenv from 'dotenv';
import { readFileSync } from 'fs';
import { chunkit, cramit, sentenceit } from './chunkit.js';
dotenv.config();
// Read package.json for version
const packageJson = JSON.parse(readFileSync(new URL('./package.json', import.meta.url)));
const VERSION = packageJson.version;
// Initialize Express app
const app = express();
const PORT = process.env.PORT || 3001;
const API_AUTH_TOKEN = process.env.API_AUTH_TOKEN;
// Middleware
app.use(cors());
app.use(express.json({ limit: '50mb' }));
// Auth middleware for API routes
const authMiddleware = (req, res, next) => {
// Only apply auth if API_AUTH_TOKEN is set
if (API_AUTH_TOKEN) {
const authHeader = req.headers.authorization;
if (!authHeader || authHeader !== `Bearer ${API_AUTH_TOKEN}`) {
return res.status(401).json({
error: 'Unauthorized',
message: 'Invalid or missing authorization token'
});
}
}
next();
};
// Landing page with terminal-style docs
app.get('/', (req, res) => {
const landingPage = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Semantic Chunking API v${VERSION}</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: #0a0a0a;
color: #00ff00;
font-family: 'Courier New', Courier, monospace;
padding: 20px;
line-height: 1.6;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
h1, h2, h3 {
color: #00ff00;
margin: 20px 0 10px 0;
text-shadow: 0 0 10px #00ff00;
}
h1 {
font-size: 2em;
border-bottom: 2px solid #00ff00;
padding-bottom: 10px;
}
h2 {
font-size: 1.5em;
margin-top: 30px;
}
h3 {
font-size: 1.2em;
}
.header {
text-align: center;
margin-bottom: 40px;
}
code {
background: #1a1a1a;
padding: 2px 6px;
border-radius: 3px;
color: #00ff00;
}
pre {
background: #1a1a1a;
padding: 15px;
border-radius: 5px;
overflow-x: auto;
border: 1px solid #00ff00;
margin: 10px 0;
}
pre code {
background: none;
padding: 0;
}
.endpoint {
margin: 20px 0;
padding: 15px;
border: 1px solid #00ff00;
border-radius: 5px;
background: #0f0f0f;
}
.method {
display: inline-block;
padding: 4px 8px;
background: #00ff00;
color: #0a0a0a;
font-weight: bold;
border-radius: 3px;
margin-right: 10px;
}
.route {
color: #00ff00;
font-weight: bold;
}
a {
color: #00ff00;
text-decoration: none;
border-bottom: 1px dashed #00ff00;
}
a:hover {
text-shadow: 0 0 5px #00ff00;
}
.info-box {
background: #1a1a1a;
border-left: 4px solid #00ff00;
padding: 15px;
margin: 20px 0;
}
.status {
color: ${API_AUTH_TOKEN ? '#ffff00' : '#00ff00'};
}
@media (max-width: 768px) {
body {
padding: 10px;
}
h1 {
font-size: 1.5em;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🍱 Semantic Chunking API</h1>
<p>Version ${VERSION} | <a href="https://github.com/jparkerweb/semantic-chunking" target="_blank">GitHub</a> | <a href="https://www.npmjs.com/package/semantic-chunking" target="_blank">NPM</a></p>
</div>
<div class="info-box">
<strong>Status:</strong> <span class="status">● ${API_AUTH_TOKEN ? 'PROTECTED (Auth Required)' : 'OPEN ACCESS'}</span><br>
<strong>Port:</strong> ${PORT}<br>
<strong>Environment:</strong> ${process.env.NODE_ENV || 'development'}
</div>
<h2>📚 Documentation</h2>
<p>For complete documentation, see:</p>
<ul>
<li><a href="https://github.com/jparkerweb/semantic-chunking/blob/main/API.md" target="_blank">API.md</a> - Full API reference</li>
<li><a href="https://github.com/jparkerweb/semantic-chunking/blob/main/README.md" target="_blank">README.md</a> - Package documentation</li>
</ul>
<h2>📡 API Endpoints</h2>
<div class="endpoint">
<span class="method">POST</span><span class="route">/api/chunkit</span>
<p>Semantically chunk text based on sentence similarity.</p>
<pre><code>curl -X POST http://localhost:${PORT}/api/chunkit \\
-H "Content-Type: application/json" \\${API_AUTH_TOKEN ? `\n -H "Authorization: Bearer YOUR_TOKEN_HERE" \\` : ''}
-d '{
"documents": [
{
"document_name": "test",
"document_text": "Your text here..."
}
],
"options": {
"maxTokenSize": 500,
"similarityThreshold": 0.5,
"onnxEmbeddingModel": "Xenova/all-MiniLM-L6-v2",
"dtype": "q8",
"maxMergesPerPass": 500,
"maxUncappedPasses": 100,
"maxMergesPerPassPercentage": 40,
"uncappedCandidateMerges": 12
}
}'</code></pre>
<p style="margin-top: 10px; font-size: 0.9em; opacity: 0.8;">Merge optimization params: <code>maxMergesPerPass</code>, <code>maxUncappedPasses</code>, <code>maxMergesPerPassPercentage</code>, <code>uncappedCandidateMerges</code></p>
</div>
<div class="endpoint">
<span class="method">POST</span><span class="route">/api/cramit</span>
<p>Pack sentences into dense chunks up to max token size.</p>
<pre><code>curl -X POST http://localhost:${PORT}/api/cramit \\
-H "Content-Type: application/json" \\${API_AUTH_TOKEN ? `\n -H "Authorization: Bearer YOUR_TOKEN_HERE" \\` : ''}
-d '{
"documents": [{"document_text": "Your text here..."}],
"options": {"maxTokenSize": 500}
}'</code></pre>
</div>
<div class="endpoint">
<span class="method">POST</span><span class="route">/api/sentenceit</span>
<p>Split text into individual sentences.</p>
<pre><code>curl -X POST http://localhost:${PORT}/api/sentenceit \\
-H "Content-Type: application/json" \\${API_AUTH_TOKEN ? `\n -H "Authorization: Bearer YOUR_TOKEN_HERE" \\` : ''}
-d '{
"documents": [{"document_text": "Your text here..."}],
"options": {}
}'</code></pre>
</div>
<div class="endpoint">
<span class="method">GET</span><span class="route">/api/health</span>
<p>Health check endpoint.</p>
<pre><code>curl http://localhost:${PORT}/api/health</code></pre>
</div>
<div class="endpoint">
<span class="method">GET</span><span class="route">/api/version</span>
<p>Get API version information.</p>
<pre><code>curl http://localhost:${PORT}/api/version</code></pre>
</div>
<h2>🔐 Authentication</h2>
${API_AUTH_TOKEN ? `
<div class="info-box">
<p><strong>⚠️ Authentication is ENABLED</strong></p>
<p>All API endpoints require a Bearer token in the Authorization header:</p>
<pre><code>Authorization: Bearer YOUR_TOKEN_HERE</code></pre>
</div>
` : `
<div class="info-box">
<p><strong>✓ Authentication is DISABLED</strong></p>
<p>To enable authentication, set the <code>API_AUTH_TOKEN</code> environment variable:</p>
<pre><code>export API_AUTH_TOKEN=your-secret-token
# or in docker-compose.yml:
environment:
- API_AUTH_TOKEN=your-secret-token</code></pre>
</div>
`}
<h2>🐳 Docker Deployment</h2>
<pre><code># Using docker-compose (recommended)
docker-compose up -d semantic-chunking-api
# Or with docker run
docker run -p ${PORT}:${PORT} \\
-v ./models:/app/models \\
-e API_AUTH_TOKEN=your-token \\
semantic-chunking</code></pre>
<footer style="margin-top: 60px; padding-top: 20px; border-top: 1px solid #00ff00; text-align: center; opacity: 0.7;">
<p>Maintained by <a href="https://www.equilllabs.com" target="_blank">eQuill Labs</a></p>
</footer>
</div>
</body>
</html>`;
res.send(landingPage);
});
// Health check endpoint (no auth required)
app.get('/api/health', (req, res) => {
res.json({
status: 'ok',
version: VERSION,
timestamp: new Date().toISOString()
});
});
// Version endpoint (no auth required)
app.get('/api/version', (req, res) => {
res.json({
version: VERSION,
package: 'semantic-chunking'
});
});
// Apply auth middleware to all /api/* routes except health and version
app.use('/api/chunkit', authMiddleware);
app.use('/api/cramit', authMiddleware);
app.use('/api/sentenceit', authMiddleware);
// Chunkit endpoint
app.post('/api/chunkit', async (req, res) => {
try {
const { documents, options = {} } = req.body;
if (!documents || !Array.isArray(documents)) {
return res.status(400).json({
error: 'Bad Request',
message: 'Request body must include a "documents" array'
});
}
// Set default model paths
const processedOptions = {
...options,
localModelPath: options.localModelPath || './models',
modelCacheDir: options.modelCacheDir || './models'
};
const result = await chunkit(documents, processedOptions);
res.json(result);
} catch (error) {
console.error('Error in /api/chunkit:', error);
res.status(500).json({
error: 'Internal Server Error',
message: error.message,
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined
});
}
});
// Cramit endpoint
app.post('/api/cramit', async (req, res) => {
try {
const { documents, options = {} } = req.body;
if (!documents || !Array.isArray(documents)) {
return res.status(400).json({
error: 'Bad Request',
message: 'Request body must include a "documents" array'
});
}
const processedOptions = {
...options,
localModelPath: options.localModelPath || './models',
modelCacheDir: options.modelCacheDir || './models'
};
const result = await cramit(documents, processedOptions);
res.json(result);
} catch (error) {
console.error('Error in /api/cramit:', error);
res.status(500).json({
error: 'Internal Server Error',
message: error.message,
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined
});
}
});
// Sentenceit endpoint
app.post('/api/sentenceit', async (req, res) => {
try {
const { documents, options = {} } = req.body;
if (!documents || !Array.isArray(documents)) {
return res.status(400).json({
error: 'Bad Request',
message: 'Request body must include a "documents" array'
});
}
const processedOptions = {
...options,
localModelPath: options.localModelPath || './models',
modelCacheDir: options.modelCacheDir || './models'
};
const result = await sentenceit(documents, processedOptions);
res.json(result);
} catch (error) {
console.error('Error in /api/sentenceit:', error);
res.status(500).json({
error: 'Internal Server Error',
message: error.message,
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined
});
}
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({
error: 'Something broke!',
message: err.message,
stack: process.env.NODE_ENV === 'development' ? err.stack : undefined
});
});
// Start server
app.listen(PORT, () => {
console.log(`\n${'='.repeat(50)}`);
console.log(`🍱 Semantic Chunking API Server v${VERSION}`);
console.log(`${'='.repeat(50)}`);
console.log(`Server running on http://localhost:${PORT}`);
console.log(`Auth: ${API_AUTH_TOKEN ? '🔒 ENABLED' : '🔓 DISABLED'}`);
console.log(`Environment: ${process.env.NODE_ENV || 'development'}`);
console.log(`${'='.repeat(50)}\n`);
});