Skip to content

Commit

Permalink
fix: do not show relevances it you have mixed collections (cosine + l2)
Browse files Browse the repository at this point in the history
  • Loading branch information
jannikstdl committed Oct 14, 2024
1 parent 33c3dbd commit 79c834d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 35 deletions.
36 changes: 29 additions & 7 deletions src/lib/components/chat/Messages/Citations.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
$: {
Expand Down Expand Up @@ -60,11 +76,17 @@
return acc;
}, []);
showRelevance = calculateShowRelevance(_citations);
showPercentage = shouldShowPercentage(_citations);
}
</script>

<CitationsModal bind:show={showCitationModal} citation={selectedCitation} {showPercentage} />
<CitationsModal
bind:show={showCitationModal}
citation={selectedCitation}
{showPercentage}
{showRelevance}
/>

{#if _citations.length > 0}
<div class="mt-1 mb-2 w-full flex gap-1 items-center flex-wrap">
Expand Down
59 changes: 31 additions & 28 deletions src/lib/components/chat/Messages/CitationsModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
export let show = false;
export let citation;
export let showPercentage = false;
export let showRelevance = true;
let mergedDocuments = [];
Expand Down Expand Up @@ -103,35 +104,37 @@
{/if}
</div>
</Tooltip>
<div class="text-sm font-medium dark:text-gray-300 mt-2">
{$i18n.t('Relevance')}
</div>
{#if document.distance !== undefined}
<Tooltip
content={$i18n.t('Semantic distance to query from vector store')}
placement="left"
tippyOptions={{ duration: [500, 0] }}
>
<div class="text-sm my-1 dark:text-gray-400 flex items-center gap-2">
{#if showPercentage}
{@const percentage = calculatePercentage(document.distance)}
<span class={`px-1 rounded font-medium ${getRelevanceColor(percentage)}`}>
{percentage.toFixed(2)}%
</span>
<span class="text-gray-500 dark:text-gray-500">
({document.distance.toFixed(4)})
</span>
{:else}
<span class="text-gray-500 dark:text-gray-500">
{document.distance.toFixed(4)}
</span>
{/if}
</div>
</Tooltip>
{:else}
<div class="text-sm dark:text-gray-400">
{$i18n.t('No distance available')}
{#if showRelevance}
<div class="text-sm font-medium dark:text-gray-300 mt-2">
{$i18n.t('Relevance')}
</div>
{#if document.distance !== undefined}
<Tooltip
content={$i18n.t('Semantic distance to query from vector store')}
placement="left"
tippyOptions={{ duration: [500, 0] }}
>
<div class="text-sm my-1 dark:text-gray-400 flex items-center gap-2">
{#if showPercentage}
{@const percentage = calculatePercentage(document.distance)}
<span class={`px-1 rounded font-medium ${getRelevanceColor(percentage)}`}>
{percentage.toFixed(2)}%
</span>
<span class="text-gray-500 dark:text-gray-500">
({document.distance.toFixed(4)})
</span>
{:else}
<span class="text-gray-500 dark:text-gray-500">
{document.distance.toFixed(4)}
</span>
{/if}
</div>
</Tooltip>
{:else}
<div class="text-sm dark:text-gray-400">
{$i18n.t('No distance available')}
</div>
{/if}
{/if}
{:else}
<div class="text-sm dark:text-gray-400">
Expand Down

0 comments on commit 79c834d

Please sign in to comment.