Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(whisper): raise decoder anti-hallucination thresholds on local transcription — fixes #1458

Gabriel Steinb7fdf80bfeed
diff --git a/src/helpers/whisperServer.js b/src/helpers/whisperServer.js
index ca8e8746f..541a0734a 100644
--- a/src/helpers/whisperServer.js
+++ b/src/helpers/whisperServer.js
@@ -32,6 +32,15 @@ const DEFAULT_WHISPER_THREADS = 4;
const MAX_AUTO_WHISPER_THREADS = 12;
const MAX_MANUAL_WHISPER_THREADS = 64;
const AUTO_THREAD_RATIO = 0.75;
+// Decoder anti-hallucination thresholds sent with every /inference request. whisper.cpp's
+// defaults (entropy 2.4, logprob -1.0) let a mostly-silent 30s decode window pass the
+// repetition check and emit training-data outro boilerplate ("Thank you for watching",
+// "Продолжение следует..."). These values cut the hallucinated-tail rate from 2.25% to
+// 0.06% over 4,814 real dictations. See #1458.
+const INFERENCE_DECODER_FIELDS = Object.freeze({
+ entropy_thold: "2.8",
+ logprob_thold: "-1.25",
+});
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
@@ -854,6 +863,14 @@ class WhisperServerManager extends EventEmitter {
`${language || "auto"}\r\n`
);
+ for (const [name, value] of Object.entries(INFERENCE_DECODER_FIELDS)) {
+ parts.push(
+ `--${boundary}\r\n` +
+ `Content-Disposition: form-data; name="${name}"\r\n\r\n` +
+ `${value}\r\n`
+ );
+ }
+
// Add initial prompt for custom dictionary words
if (initialPrompt) {
parts.push(
@@ -1118,6 +1135,7 @@ class WhisperServerManager extends EventEmitter {
module.exports = WhisperServerManager;
module.exports.buildWhisperServerArgs = buildWhisperServerArgs;
+module.exports.INFERENCE_DECODER_FIELDS = INFERENCE_DECODER_FIELDS;
module.exports.parseVulkanDevices = parseVulkanDevices;
module.exports.resolveVulkanPinAction = resolveVulkanPinAction;
module.exports.getVadSignature = getVadSignature;
diff --git a/test/helpers/whisperServerInferenceFields.test.js b/test/helpers/whisperServerInferenceFields.test.js
new file mode 100644
index 000000000..ab88cb3b1
--- /dev/null
+++ b/test/helpers/whisperServerInferenceFields.test.js
@@ -0,0 +1,99 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+const http = require("node:http");
+
+const WhisperServerManager = require("../../src/helpers/whisperServer");
+const { INFERENCE_DECODER_FIELDS } = require("../../src/helpers/whisperServer");
+
+function startCapturingServer() {
+ let captured = null;
+ const server = http.createServer((req, res) => {
+ const chunks = [];
+ req.on("data", (chunk) => chunks.push(chunk));
+ req.on("end", () => {
+ captured = Buffer.concat(chunks).toString("latin1");
+ res.writeHead(200, { "Content-Type": "application/json" });
+ res.end(JSON.stringify({ text: "ok" }));
+ });
+ });
+ return new Promise((resolve) => {
+ server.listen(0, "127.0.0.1", () =>
+ resolve({ server, port: server.address().port, getBody: () => captured })
+ );
+ });
+}
+
+function createManager(port) {
+ const manager = new WhisperServerManager();
+ manager.ready = true;
+ manager.hostname = "127.0.0.1";
+ manager.port = port;
+ manager.canConvert = true;
+ manager.process = {};
+ manager.modelPath = "/tmp/model.bin";
+ manager._convertToWav = async (buffer) => buffer;
+ return manager;
+}
+
+function fieldValue(body, name) {
+ const match = body.match(
+ new RegExp(`Content-Disposition: form-data; name="${name}"\\r\\n\\r\\n([^\\r]*)\\r\\n`)
+ );
+ return match ? match[1] : null;
+}
+
+test("pins the measured anti-hallucination thresholds from #1458", () => {
+ assert.deepEqual(INFERENCE_DECODER_FIELDS, {
+ entropy_thold: "2.8",
+ logprob_thold: "-1.25",
+ });
+});
+
+test("sends the decoder threshold fields alongside the existing inference fields", async (t) => {
+ const { server, port, getBody } = await startCapturingServer();
+ t.after(() => server.close());
+
+ const manager = createManager(port);
+ const result = await manager.transcribe(Buffer.from("audio"), {
+ language: "auto",
+ initialPrompt: "OpenWhispr",
+ });
+ assert.equal(result.text, "ok");
+
+ const body = getBody();
+ for (const [name, value] of Object.entries(INFERENCE_DECODER_FIELDS)) {
+ assert.equal(fieldValue(body, name), value, `expected ${name}=${value} in the request body`);
+ }
+ assert.equal(fieldValue(body, "language"), "auto");
+ assert.equal(fieldValue(body, "prompt"), "OpenWhispr");
+ assert.equal(fieldValue(body, "response_format"), "json");
+
+ // Field order: file, language, decoder thresholds, prompt, response_format, closing boundary.
+ const positions = ['name="file"', 'name="language"']
+ .concat(Object.keys(INFERENCE_DECODER_FIELDS).map((name) => `name="${name}"`))
+ .concat(['name="prompt"', 'name="response_format"'])
+ .map((marker) => body.indexOf(marker));
+ assert.ok(
+ positions.every((pos, i) => pos >= 0 && (i === 0 || pos > positions[i - 1])),
+ `expected ordered form fields, got positions ${positions.join(", ")}`
+ );
+
+ const boundary = body.slice(0, body.indexOf("\r\n"));
+ assert.ok(body.endsWith(`${boundary}--\r\n`), "expected the closing boundary to end the body");
+});
+
+test("sends the decoder threshold fields even without an initial prompt", async (t) => {
+ const { server, port, getBody } = await startCapturingServer();
+ t.after(() => server.close());
+
+ const manager = createManager(port);
+ await manager.transcribe(Buffer.from("audio"), { language: "en" });
+
+ const body = getBody();
+ for (const [name, value] of Object.entries(INFERENCE_DECODER_FIELDS)) {
+ assert.equal(fieldValue(body, name), value, `expected ${name}=${value} in the request body`);
+ }
+ assert.equal(fieldValue(body, "language"), "en");
+ assert.equal(fieldValue(body, "prompt"), null);
+ assert.equal(fieldValue(body, "response_format"), "json");
+});