diff --git a/src/lib/components/chat/Messages/Citations.svelte b/src/lib/components/chat/Messages/Citations.svelte index b9a8198aa..0a09b3b55 100644 --- a/src/lib/components/chat/Messages/Citations.svelte +++ b/src/lib/components/chat/Messages/Citations.svelte @@ -11,18 +11,34 @@ let _citations = []; let showPercentage = false; + let showRelevance = true; let showCitationModal = false; let selectedCitation: any = null; let isCollapsibleOpen = false; + function calculateShowRelevance(citations: any[]) { + const distances = citations.flatMap((citation) => citation.distances ?? []); + const inRange = distances.filter((d) => d !== undefined && d >= -1 && d <= 1).length; + const outOfRange = distances.filter((d) => d !== undefined && (d < -1 || d > 1)).length; + + if (distances.length === 0) { + return false; + } + + if ( + (inRange === distances.length - 1 && outOfRange === 1) || + (outOfRange === distances.length - 1 && inRange === 1) + ) { + return false; + } + + return true; + } + function shouldShowPercentage(citations: any[]) { - return citations.every( - (citation) => - citation.distances && - citation.distances.length > 0 && - citation.distances.every((d: number) => d !== undefined && d >= -1 && d <= 1) - ); + const distances = citations.flatMap((citation) => citation.distances ?? []); + return distances.every((d) => d !== undefined && d >= -1 && d <= 1); } $: { @@ -60,11 +76,17 @@ return acc; }, []); + showRelevance = calculateShowRelevance(_citations); showPercentage = shouldShowPercentage(_citations); } - + {#if _citations.length > 0}
diff --git a/src/lib/components/chat/Messages/CitationsModal.svelte b/src/lib/components/chat/Messages/CitationsModal.svelte index 6c9c96d9a..ad965e350 100644 --- a/src/lib/components/chat/Messages/CitationsModal.svelte +++ b/src/lib/components/chat/Messages/CitationsModal.svelte @@ -8,6 +8,7 @@ export let show = false; export let citation; export let showPercentage = false; + export let showRelevance = true; let mergedDocuments = []; @@ -103,35 +104,37 @@ {/if}
-
- {$i18n.t('Relevance')} -
- {#if document.distance !== undefined} - -
- {#if showPercentage} - {@const percentage = calculatePercentage(document.distance)} - - {percentage.toFixed(2)}% - - - ({document.distance.toFixed(4)}) - - {:else} - - {document.distance.toFixed(4)} - - {/if} -
-
- {:else} -
- {$i18n.t('No distance available')} + {#if showRelevance} +
+ {$i18n.t('Relevance')}
+ {#if document.distance !== undefined} + +
+ {#if showPercentage} + {@const percentage = calculatePercentage(document.distance)} + + {percentage.toFixed(2)}% + + + ({document.distance.toFixed(4)}) + + {:else} + + {document.distance.toFixed(4)} + + {/if} +
+
+ {:else} +
+ {$i18n.t('No distance available')} +
+ {/if} {/if} {:else}