mirror of
https://github.com/db9979/wibosim.git
synced 2026-12-06 21:44:09 +01:00
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUhQVoPUusUzxgxLWkK
42 lines
3.0 KiB
JavaScript
42 lines
3.0 KiB
JavaScript
// Kleines Ersatz-Sprachmodell (OpenAI-kompatibel) für Tests und Sichtprüfungen ohne echtes Modell.
|
|
// Start: node test/mock-llm.js [port] → Endpunkt http://127.0.0.1:<port>/v1, Modell beliebig.
|
|
// MOCK_LLM_MODE=down → antwortet mit HTTP 503 (Ausfall), =slow → 30 s Verzögerung, =junk → kein JSON,
|
|
// =think → Denkmodell ohne Abschalten (abgeschnittenes <think>), =fenced → JSON im Codeblock, =reasoning → Antwort im Feld reasoning_content.
|
|
// Antwort: {"text": "..."} mit einem Satz, der Teile der Anfrage aufgreift; bei Nachrichtenbewertung ein Bewertungsobjekt.
|
|
import http from 'node:http';
|
|
|
|
export function startMockLlm(port = 0, mode = () => process.env.MOCK_LLM_MODE ?? 'ok') {
|
|
const srv = http.createServer((req, res) => {
|
|
let body = '';
|
|
req.on('data', (c) => { body += c; if (body.length > 1e6) req.destroy(); });
|
|
req.on('end', async () => {
|
|
const m = mode();
|
|
if (req.url.endsWith('/models')) { res.writeHead(200, { 'content-type': 'application/json' }); res.end(JSON.stringify({ data: [{ id: 'mock' }] })); return; }
|
|
if (m === 'down') { res.writeHead(503); res.end(); return; }
|
|
if (m === 'slow') await new Promise((r) => setTimeout(r, 30_000));
|
|
let msgs = [];
|
|
try { msgs = JSON.parse(body).messages ?? []; } catch { /* leer */ }
|
|
const sys = String(msgs[0]?.content ?? '');
|
|
const user = String(msgs.at(-1)?.content ?? '').replace(/\s+/g, ' ').slice(0, 160);
|
|
let content;
|
|
let body2 = {};
|
|
try { body2 = JSON.parse(body); } catch { /* leer */ }
|
|
const thinkOff = body2.think === false || body2.chat_template_kwargs?.enable_thinking === false || /\/no_think/.test(user);
|
|
if (m === 'junk') content = 'Leider weiß ich das nicht.';
|
|
else if (m === 'think' && !thinkOff) content = '<think>Ich überlege sehr lange, was ich antworten soll, und werde abgeschnitten';
|
|
else if (m === 'fenced') content = 'Gern! Hier ist die Antwort:\n```json\n{"text": "Antwort im Codeblock, mit Komma am Ende.",}\n```';
|
|
else if (m === 'reasoning') { res.writeHead(200, { 'content-type': 'application/json' }); res.end(JSON.stringify({ choices: [{ message: { role: 'assistant', content: '', reasoning_content: '{"text": "Antwort im Denkfeld."}' } }] })); return; }
|
|
else if (/"sentiment"/.test(sys)) content = JSON.stringify({ relevant: true, sentiment: 0.1, sectors: [], sectorImpact: {}, topics: {}, strength: 0.3, reason: 'Testbewertung' });
|
|
else content = JSON.stringify({ text: `Testantwort des Ersatzmodells: ${user || 'ohne Inhalt'}. Das ist ein zweiter Satz.` });
|
|
res.writeHead(200, { 'content-type': 'application/json' });
|
|
res.end(JSON.stringify({ choices: [{ message: { role: 'assistant', content } }] }));
|
|
});
|
|
});
|
|
return new Promise((r) => srv.listen(port, '127.0.0.1', () => r(srv)));
|
|
}
|
|
|
|
if (process.argv[1]?.endsWith('mock-llm.js')) {
|
|
const srv = await startMockLlm(Number(process.argv[2] ?? 11999));
|
|
console.log(`Ersatzmodell auf http://127.0.0.1:${srv.address().port}/v1`);
|
|
}
|