541457bc24
- kb.js: entries { id, title, questions, answer, tags, pattern, handoff, order, source, updated } in DATA_DIR/kb.json (temp file + rename). BM25 over title, question variants and tags (answer at a low weight) with word bigrams; the top hits go into the system prompt as KNOWLEDGE BASE, and when nothing fits the prompt says to answer "I'm not sure, ask support" (Help at SITE_URL/help).
- One source: chatbot.js CANNED seeds the KB (ids c-*, regex = instant-answer pattern, same first-match order); kb-seed.js seeds the network notes from the home page, the wallets guide FAQ, the earning guide and the Coinbase support reply in LinkSpin wording (f-*). The fast path serves the KB entry; the in-code CANNED only answers if the KB store fails. Unedited seeds follow code changes, edited ones are left alone, deleted seeds stay deleted.
- Admin > Knowledge base: list, search, filter by source, add, edit, delete, Try a question. Em dashes in admin input become commas.
- chatbot.js: reasoning off for deepseek (as on IAP), keepalive-padded OpenRouter replies parsed, OPENROUTER_URL override for tests, KNOWLEDGE_DATE 2026-10-20. New public GET /api/chat/kb-stamp.
- QA: qa/kb-retrieval.cjs (12 sample questions rank the right entry first), qa/kb-chat.mjs (local copy + mock OpenRouter), bash qa/run.sh kb; walk lists admin#kb.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
49 lines
3.2 KiB
JavaScript
49 lines
3.2 KiB
JavaScript
// Knowledge-base retrieval unit test (kb.js): the right entry must rank first for each sample question,
|
|
// scored over the real seeds (chatbot.js CANNED + kb-seed.js). No server, no network.
|
|
// node qa/kb-retrieval.cjs
|
|
const path = require('path');
|
|
const kb = require(path.join(__dirname, '..', 'kb.js'));
|
|
const chatbot = require(path.join(__dirname, '..', 'chatbot.js'));
|
|
const { buildIndex, rank, relevant, validate } = kb._pure;
|
|
|
|
const entries = chatbot.seeds();
|
|
const idx = buildIndex(entries);
|
|
const CASES = [
|
|
['Can I hook up my Coinbase account to pay for a package?', 'f-coinbase'],
|
|
['can i just use coinbase', 'f-coinbase'],
|
|
['I already have an InstantAdPay account, will my upline carry over here?', 'f-team-here'],
|
|
['How many extra POL should I keep for gas fees?', 'f-how-much-pol'],
|
|
['MoonPay declined my bank card', 'f-moonpay-declined'],
|
|
['When does the daily set reset each day?', 'f-claim-streak'],
|
|
['How many videos can I watch per day?', 'f-visits-videos'],
|
|
['How do I rotate several offers behind one short link?', 'c-spin-links'],
|
|
['How do I put an ad slot on my own website?', 'c-zones'],
|
|
['Can I track conversions from PropellerAds with a postback?', 'c-sources'],
|
|
['Someone on Telegram asked for my recovery phrase', 'f-recovery-phrase'],
|
|
['Which network do I choose when withdrawing POL from Binance?', 'f-exchange-withdraw']
|
|
];
|
|
let bad = 0;
|
|
for (const [q, want] of CASES) {
|
|
const hits = rank(idx, q, 5), top = hits[0];
|
|
const ok = top && top.entry.id === want && relevant(hits).length > 0;
|
|
if (!ok) bad++;
|
|
console.log((ok ? 'ok ' : 'FAIL ') + q + ' -> ' + hits.slice(0, 3).map(h => h.entry.id + ' ' + h.score).join(', ') + (ok ? '' : ' (wanted ' + want + ')'));
|
|
}
|
|
// off-topic questions should bring nothing back, so the prompt says "I'm not sure, ask support"
|
|
for (const q of ['What is the weather in Paris tomorrow?', 'Recommend a good pizza recipe']) {
|
|
const r = relevant(rank(idx, q, 5));
|
|
if (r.length) bad++;
|
|
console.log((r.length ? 'FAIL ' : 'ok ') + 'off-topic: ' + q + ' -> ' + (r.map(h => h.entry.id + ' ' + h.score).join(', ') || 'nothing'));
|
|
}
|
|
// admin input hygiene: em dashes are turned into commas, a bad pattern is refused
|
|
const v = validate({ title: 'T', answer: 'One — two', questions: 'a\nb', tags: 'X, y' });
|
|
if (v.answer !== 'One, two' || v.questions.length !== 2 || v.tags[0] !== 'x') { bad++; console.log('FAIL validate', v); } else console.log('ok validate cleans em dashes, splits questions and tags');
|
|
if (!validate({ title: 'T', answer: 'a', pattern: '(' }).error) { bad++; console.log('FAIL validate accepted a broken pattern'); } else console.log('ok validate refuses a broken pattern');
|
|
// every CANNED regex still answers through the KB fast path in the same order
|
|
const ordered = entries.filter(e => e.pattern).sort((a, b) => a.order - b.order);
|
|
const first = msg => ordered.find(e => new RegExp(e.pattern, 'i').test(msg));
|
|
const canned = chatbot.CANNED.find(c => c.re.test('how do I withdraw'));
|
|
if (!first('how do I withdraw') || first('how do I withdraw').answer !== canned.a) { bad++; console.log('FAIL fast-path order'); } else console.log('ok fast path keeps CANNED order');
|
|
console.log(bad ? bad + ' failure(s)' : 'all retrieval checks passed');
|
|
process.exit(bad ? 1 : 0);
|