From 9d4d96429f22ad943115df5c04907ee84e76c468 Mon Sep 17 00:00:00 2001 From: Jannik Streidl Date: Sat, 12 Oct 2024 13:51:27 +0200 Subject: [PATCH] only show relevance pertentage score if distances are in cosinus silimarity range --- .../chat/Messages/CitationsModal.svelte | 45 +++++++++++-------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/src/lib/components/chat/Messages/CitationsModal.svelte b/src/lib/components/chat/Messages/CitationsModal.svelte index d2510d924..c7a68d6fb 100644 --- a/src/lib/components/chat/Messages/CitationsModal.svelte +++ b/src/lib/components/chat/Messages/CitationsModal.svelte @@ -11,19 +11,20 @@ let mergedDocuments = []; function calculatePercentage(distance) { - return Math.max(0, Math.min(100, (1 - distance / 2) * 100)); + if (distance < 0) return 100; + if (distance > 1) return 0; + return Math.round((1 - distance) * 100); } - function getRelevanceColor(percentage) { - if (percentage >= 80) - return 'bg-green-200 dark:bg-green-800 text-green-800 dark:text-green-200'; - if (percentage >= 60) - return 'bg-yellow-200 dark:bg-yellow-800 text-yellow-800 dark:text-yellow-200'; - if (percentage >= 40) - return 'bg-orange-200 dark:bg-orange-800 text-orange-800 dark:text-orange-200'; - return 'bg-red-200 dark:bg-red-800 text-red-800 dark:text-red-200'; + function shouldShowPercentage(documents) { + const validDistances = documents.filter( + (d) => d.distance !== undefined && d.distance >= 0 && d.distance <= 1 + ); + return validDistances.length >= 2; } + $: showPercentage = shouldShowPercentage(mergedDocuments); + $: if (citation) { mergedDocuments = citation.document?.map((c, i) => { return { @@ -104,15 +105,23 @@
{$i18n.t('Relevance')}
- {@const percentage = calculatePercentage(document.distance)} -
- - {percentage.toFixed(0)}% - - ({document.distance.toFixed(4)}) -
+ {#if showPercentage} + {@const percentage = calculatePercentage(document.distance)} +
+ + {percentage}% + + ({document.distance.toFixed(4)}) +
+ {:else} +
+ + {document.distance.toFixed(4)} + +
+ {/if} {/if} {:else}