Chat: treat a few stray words as a garbled answer and use the knowledge base
Browse files- app/renderer/knowledge.js +6 -0
- chat/chat.js +8 -1
app/renderer/knowledge.js
CHANGED
|
@@ -118,6 +118,12 @@ export function repeatedText(answer) {
|
|
| 118 |
return /(.{2,30}?)\1{9,}/s.test(answer) || (words.length >= 30 && new Set(words).size / words.length < 0.2);
|
| 119 |
}
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
// Grounding rules for a chat answer (same as check_chat() in training/build_chat.py).
|
| 122 |
const NUMBER = /(?<![\w.])\d+(?:\.\d+)?(?!\w)/g;
|
| 123 |
export function chatProblems(userPrompt, answer) {
|
|
|
|
| 118 |
return /(.{2,30}?)\1{9,}/s.test(answer) || (words.length >= 30 && new Set(words).size / words.length < 0.2);
|
| 119 |
}
|
| 120 |
|
| 121 |
+
// A scrap instead of an answer ("Answer to 1:4:30"): under 6 words, no citation. Real short replies (greetings,
|
| 122 |
+
// "I can't see that") are longer than this.
|
| 123 |
+
export function tooShort(answer) {
|
| 124 |
+
return !/\[\d+\]/.test(answer) && (answer.match(/[A-Za-z]{2,}/g) || []).length < 6;
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
// Grounding rules for a chat answer (same as check_chat() in training/build_chat.py).
|
| 128 |
const NUMBER = /(?<![\w.])\d+(?:\.\d+)?(?!\w)/g;
|
| 129 |
export function chatProblems(userPrompt, answer) {
|
chat/chat.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
// Devseis Security Assistant: chat about endpoint security with the Devseis model running on the device (WebLLM),
|
| 2 |
// grounded in the Devseis knowledge base (library/knowledge.json), plus audits: the phone check on phones, the desktop
|
| 3 |
// app (or its report file) on computers. Conversations are stored only in this browser.
|
| 4 |
-
import { Knowledge, chatMessages, auditBrief, chatProblems, searchWithAudit, repeatedText } from '../app/renderer/knowledge.js';
|
| 5 |
import { Library, findingPrompt, findingTemplate, summaryPrompt, summaryTemplate, aiPrompt, aiTemplate, writeAnswer, ANSWER_SCHEMAS } from '../app/renderer/auditor-core.js';
|
| 6 |
import { buildReportHtml } from '../app/renderer/report.js';
|
| 7 |
import { detectDevice } from '../mobile/detect.js';
|
|
@@ -255,6 +255,13 @@ async function aiAnswer(chat, reply, messages, passages) {
|
|
| 255 |
if (fault === 'slow') return fallback(reply, passages, 'The AI was too slow on this device, so this answer comes from the knowledge base.');
|
| 256 |
if (!started) throw new Error('The AI returned no answer');
|
| 257 |
reply.content = clean(reply.content);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
reply.noSources = !/\[\d+\]/.test(reply.content) && /can only help|don't have|do not have|not able to|can't answer|cannot answer|sorry/i.test(reply.content);
|
| 259 |
reply.cited = /\[\d+\]/.test(reply.content);
|
| 260 |
if (chatProblems(messages[messages.length - 1].content, reply.content).length) reply.note = 'Some details in this answer could not be matched to the sources. Check them in the sources below before acting.';
|
|
|
|
| 1 |
// Devseis Security Assistant: chat about endpoint security with the Devseis model running on the device (WebLLM),
|
| 2 |
// grounded in the Devseis knowledge base (library/knowledge.json), plus audits: the phone check on phones, the desktop
|
| 3 |
// app (or its report file) on computers. Conversations are stored only in this browser.
|
| 4 |
+
import { Knowledge, chatMessages, auditBrief, chatProblems, searchWithAudit, repeatedText, tooShort } from '../app/renderer/knowledge.js';
|
| 5 |
import { Library, findingPrompt, findingTemplate, summaryPrompt, summaryTemplate, aiPrompt, aiTemplate, writeAnswer, ANSWER_SCHEMAS } from '../app/renderer/auditor-core.js';
|
| 6 |
import { buildReportHtml } from '../app/renderer/report.js';
|
| 7 |
import { detectDevice } from '../mobile/detect.js';
|
|
|
|
| 255 |
if (fault === 'slow') return fallback(reply, passages, 'The AI was too slow on this device, so this answer comes from the knowledge base.');
|
| 256 |
if (!started) throw new Error('The AI returned no answer');
|
| 257 |
reply.content = clean(reply.content);
|
| 258 |
+
// A few stray words means the graphics chip garbled the answer; twice in a row, stop using the AI here.
|
| 259 |
+
if (tooShort(reply.content)) {
|
| 260 |
+
state.scraps = (state.scraps || 0) + 1;
|
| 261 |
+
if (state.scraps >= 2) { turnOffBrokenAi(); return fallback(reply, passages, 'The AI wrote unreadable text on this device, so Arx switched to answers from the knowledge base. Details: tap the device bar.'); }
|
| 262 |
+
return fallback(reply, passages, 'The AI\'s answer was unreadable, so this one comes from the knowledge base.');
|
| 263 |
+
}
|
| 264 |
+
state.scraps = 0;
|
| 265 |
reply.noSources = !/\[\d+\]/.test(reply.content) && /can only help|don't have|do not have|not able to|can't answer|cannot answer|sorry/i.test(reply.content);
|
| 266 |
reply.cited = /\[\d+\]/.test(reply.content);
|
| 267 |
if (chatProblems(messages[messages.length - 1].content, reply.content).length) reply.note = 'Some details in this answer could not be matched to the sources. Check them in the sources below before acting.';
|