Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(ts): isConfident reads answer_confidence

nnlgsakibf246870dfb67
diff --git a/laya-ts/src/dx.ts b/laya-ts/src/dx.ts
index 7bb40e1f..6508f47d 100644
--- a/laya-ts/src/dx.ts
+++ b/laya-ts/src/dx.ts
@@ -41,7 +41,13 @@ export function topK(probs: Record<string, number>, k: number): string[] {
.map(([key]) => key);
}
-/** Confidence gate (default 0.8). */
-export function isConfident(answer: Pick<SystemAnswer, "confidence">, thresh = 0.8): boolean {
- return (answer.confidence ?? 0) >= thresh;
+/** Confidence gate (default 0.8). Reads answer_confidence (max(p), comparable
+ * across question types) when present, falling back to entropy confidence. */
+export function isConfident(
+ answer: Pick<SystemAnswer, "confidence"> & { answer_confidence?: unknown },
+ thresh = 0.8,
+): boolean {
+ const ac = (answer as { answer_confidence?: unknown }).answer_confidence;
+ const v = typeof ac === "number" && Number.isFinite(ac) ? ac : answer.confidence;
+ return (v ?? 0) >= thresh;
}
diff --git a/laya-ts/src/index.ts b/laya-ts/src/index.ts
index 86ca4dd7..bede8f3d 100644
--- a/laya-ts/src/index.ts
+++ b/laya-ts/src/index.ts
@@ -65,7 +65,6 @@ export {
TEMP_MAX,
} from "./common.js";
export type { QType, InternalQ, CollateItem, CollatedBatch, QuestionPrefix } from "./common.js";
-export { maxOf } from "./common.js";
export { q, best, topK, isConfident } from "./dx.js";
export { bpeEncode, metaspaceEncode, encodeWithData, parseTokenizerJson, loadTokenizerJson, CHECKPOINT_IDS, SPECIAL_ALIASES, METASPACE_REPLACEMENT } from "./tokenizer.js";
export type { TokenizerLike, TokenizerData, TokenizerIds, PreTokenizerKind } from "./tokenizer.js";
diff --git a/laya-ts/tests/dx.test.ts b/laya-ts/tests/dx.test.ts
index 60c5536e..e9550216 100644
--- a/laya-ts/tests/dx.test.ts
+++ b/laya-ts/tests/dx.test.ts
@@ -27,6 +27,10 @@ describe("result helpers", () => {
expect(isConfident({ confidence: 0.9 } as any, 0.8)).toBe(true);
expect(isConfident({ confidence: 0.5 } as any, 0.8)).toBe(false);
});
+ it("prefers answer_confidence over entropy confidence", () => {
+ expect(isConfident({ confidence: 0.5, answer_confidence: 0.9 } as any, 0.8)).toBe(true);
+ expect(isConfident({ confidence: 0.9, answer_confidence: 0.5 } as any, 0.8)).toBe(false);
+ });
});
describe("predictShortlist auto-embed", () => {