From ad627eacaea8c5a6fc929ab8e00d7a5a7b99dbd7 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Tue, 20 Aug 2024 20:01:33 -0500 Subject: [PATCH] refine confidence --- .../page/knowledge-base/vector/+page.svelte | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/routes/page/knowledge-base/vector/+page.svelte b/src/routes/page/knowledge-base/vector/+page.svelte index 373d924..35b1af7 100644 --- a/src/routes/page/knowledge-base/vector/+page.svelte +++ b/src/routes/page/knowledge-base/vector/+page.svelte @@ -74,7 +74,7 @@ isFromSearch = false; searchVectorKnowledge({ text: util.trim(text), - confidence: !isNaN(Number(confidence)) ? Number(confidence) : 0 + confidence: Number(validateConfidenceNumber(confidence)) }, selectedCollection).then(res => { items = res || []; isFromSearch = true; @@ -108,17 +108,31 @@ } /** @param {any} e */ - function validateConfidence(e) { + function validateConfidenceInput(e) { var reg = new RegExp(regex, 'g'); if (!reg.test(e.key)) { e.preventDefault(); } } + /** @param {string} value */ + function validateConfidenceNumber(value) { + const num = Number(value); + + if (isNaN(num) || num < 0) { + confidence = '0.0'; + } else if (num >= 1) { + confidence = '1.0'; + } else { + confidence = num.toFixed(2); + } + return confidence; + } + /** @param {any} e */ function changeConfidence(e) { const value = e.target.value; - confidence = !isNaN(value) && Number(value) >= 0 ? Number(value).toFixed(2) : '0.0'; + validateConfidenceNumber(value); } @@ -312,7 +326,7 @@ type="text" class="text-center" bind:value={confidence} - on:keydown={(e) => validateConfidence(e)} + on:keydown={(e) => validateConfidenceInput(e)} on:blur={(e) => changeConfidence(e)} />