Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
136 lines (113 loc) · 4.44 KB
/
Copy pathserver.js
File metadata and controls
136 lines (113 loc) · 4.44 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
import express from 'express';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import dotenv from 'dotenv';
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
const port = Number(process.env.PORT || 3000);
const lmkitBaseUrl = String(process.env.LMKIT_BASE_URL || 'http://127.0.0.1:5189').replace(/\/+$/, '');
const lmkitApiKey = String(process.env.LMKIT_API_KEY || '').trim();
const requestTimeoutMs = Number(process.env.LMKIT_TIMEOUT_MS || 120000);
app.disable('x-powered-by');
app.use(express.json({ limit: '2mb' }));
function lmkitUrl(pathname) {
return lmkitBaseUrl + pathname;
}
function upstreamHeaders() {
const headers = { Accept: 'application/json', 'Content-Type': 'application/json' };
if (lmkitApiKey) headers.Authorization = 'Bearer ' + lmkitApiKey;
return headers;
}
async function fetchWithTimeout(url, options = {}) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), requestTimeoutMs);
try {
return await fetch(url, { ...options, signal: controller.signal });
} finally {
clearTimeout(timeout);
}
}
async function readUpstreamError(response) {
const text = await response.text().catch(() => '');
let detail = text || response.statusText;
try {
const json = JSON.parse(text);
detail = json?.error?.message || json?.message || json?.detail || detail;
} catch {
// Keep plain-text error details.
}
return detail;
}
function extractText(data) {
const content = data?.choices?.[0]?.message?.content;
if (typeof content === 'string') return content;
if (Array.isArray(content)) {
return content.map((part) => typeof part === 'string' ? part : (part?.text || '')).join('');
}
return data?.result || data?.text || '';
}
app.get('/api/health', (_req, res) => {
res.json({ ok: true, service: 'algerian-laws-ai-gateway' });
});
app.get('/api/lmkit/models', async (_req, res) => {
try {
const response = await fetchWithTimeout(lmkitUrl('/v1/models'), {
method: 'GET',
headers: upstreamHeaders(),
});
if (!response.ok) {
return res.status(response.status).json({ error: await readUpstreamError(response) });
}
return res.json(await response.json());
} catch (error) {
const message = error?.name === 'AbortError' ? 'LM-Kit request timed out.' : (error?.message || 'Unable to reach LM-Kit One.');
return res.status(502).json({ error: message });
}
});
app.post('/api/lmkit/chat', async (req, res) => {
const { model = '', prompt, systemPrompt = 'You are a helpful, precise AI assistant.', temperature = 0.7 } = req.body || {};
if (typeof prompt !== 'string' || !prompt.trim()) {
return res.status(400).json({ error: 'A non-empty prompt is required.' });
}
if (!Number.isFinite(Number(temperature))) {
return res.status(400).json({ error: 'temperature must be a number.' });
}
const payload = {
messages: [
{ role: 'system', content: String(systemPrompt || '') },
{ role: 'user', content: prompt },
],
temperature: Math.min(2, Math.max(0, Number(temperature))),
stream: false,
};
if (typeof model === 'string' && model.trim()) payload.model = model.trim();
try {
const response = await fetchWithTimeout(lmkitUrl('/v1/chat/completions'), {
method: 'POST',
headers: upstreamHeaders(),
body: JSON.stringify(payload),
});
if (!response.ok) {
return res.status(response.status).json({ error: await readUpstreamError(response) });
}
const data = await response.json();
const text = extractText(data);
if (!text) return res.status(502).json({ error: 'LM-Kit One returned no assistant text.' });
return res.json({ text, model: data?.model || model || null, usage: data?.usage || null });
} catch (error) {
const message = error?.name === 'AbortError' ? 'LM-Kit request timed out.' : (error?.message || 'Unable to reach LM-Kit One.');
return res.status(502).json({ error: message });
}
});
const distDir = path.join(__dirname, 'dist');
app.use(express.static(distDir));
app.get('*', (req, res) => {
if (req.path.startsWith('/api/')) return res.status(404).json({ error: 'API endpoint not found.' });
return res.sendFile(path.join(distDir, 'index.html'));
});
app.listen(port, '0.0.0.0', () => {
console.log('Algerian Laws app + AI gateway listening on http://0.0.0.0:' + port);
console.log('LM-Kit upstream: ' + lmkitBaseUrl);
});