enh: show source documents vector distance + cleaner source view

This commit is contained in:
Jannik Streidl 2024-10-07 21:04:06 +02:00
parent ed9fbe153b
commit 86caca495b
3 changed files with 151 additions and 25 deletions

View File

@ -1,13 +1,24 @@
<script lang="ts">
import { getContext } from 'svelte';
import CitationsModal from './CitationsModal.svelte';
import Collapsible from '$lib/components/common/Collapsible.svelte';
import ChevronDown from '$lib/components/icons/ChevronDown.svelte';
import ChevronUp from '$lib/components/icons/ChevronUp.svelte';
const i18n = getContext('i18n');
export let citations = [];
let _citations = [];
let showCitationModal = false;
let selectedCitation = null;
let isCollapsibleOpen = false;
$: _citations = citations.reduce((acc, citation) => {
citation.document.forEach((document, index) => {
const metadata = citation.metadata?.[index];
const distance = citation.distances?.[index];
const id = metadata?.source ?? 'N/A';
let source = citation?.source;
@ -25,43 +36,120 @@
if (existingSource) {
existingSource.document.push(document);
existingSource.metadata.push(metadata);
if (distance !== undefined) existingSource.distances.push(distance);
} else {
acc.push({
id: id,
source: source,
document: [document],
metadata: metadata ? [metadata] : []
metadata: metadata ? [metadata] : [],
distances: distance !== undefined ? [distance] : undefined
});
}
});
return acc;
}, []);
let showCitationModal = false;
let selectedCitation = null;
$: if (_citations.every((citation) => citation.distances !== undefined)) {
// Sort citations by distance (relevance)
_citations = _citations.sort((a, b) => {
const aMinDistance = Math.min(...(a.distances ?? []));
const bMinDistance = Math.min(...(b.distances ?? []));
return aMinDistance - bMinDistance;
});
}
</script>
<CitationsModal bind:show={showCitationModal} citation={selectedCitation} />
{#if _citations.length > 0}
<div class="mt-1 mb-2 w-full flex gap-1 items-center flex-wrap">
{#each _citations as citation, idx}
<div class="flex gap-1 text-xs font-semibold">
<button
class="flex dark:text-gray-300 py-1 px-1 bg-gray-50 hover:bg-gray-100 dark:bg-gray-850 dark:hover:bg-gray-800 transition rounded-xl max-w-96"
on:click={() => {
showCitationModal = true;
selectedCitation = citation;
}}
{#if _citations.length <= 3}
{#each _citations as citation, idx}
<div class="flex gap-1 text-xs font-semibold">
<button
class="flex dark:text-gray-300 py-1 px-1 bg-gray-50 hover:bg-gray-100 dark:bg-gray-850 dark:hover:bg-gray-800 transition rounded-xl max-w-96"
on:click={() => {
showCitationModal = true;
selectedCitation = citation;
}}
>
{#if _citations.every((c) => c.distances !== undefined)}
<div class="bg-white dark:bg-gray-700 rounded-full size-4">
{idx + 1}
</div>
{/if}
<div class="flex-1 mx-2 line-clamp-1">
{citation.source.name}
</div>
</button>
</div>
{/each}
{:else}
<Collapsible bind:open={isCollapsibleOpen} className="w-full">
<div
class="flex items-center gap-2 text-gray-500 hover:text-gray-600 dark:hover:text-gray-400 transition cursor-pointer"
>
<div class="bg-white dark:bg-gray-700 rounded-full size-4">
{idx + 1}
<span>{$i18n.t('References from')}</span>
{#each _citations.slice(0, 2) as citation, idx}
<div class="flex gap-1 text-xs font-semibold">
<button
class="flex dark:text-gray-300 py-1 px-1 bg-gray-50 hover:bg-gray-100 dark:bg-gray-850 dark:hover:bg-gray-800 transition rounded-xl max-w-96"
on:click={() => {
showCitationModal = true;
selectedCitation = citation;
}}
>
{#if _citations.every((c) => c.distances !== undefined)}
<div class="bg-white dark:bg-gray-700 rounded-full size-4">
{idx + 1}
</div>
{/if}
<div class="flex-1 mx-2 line-clamp-1">
{citation.source.name}
</div>
</button>
</div>
{#if idx === 0}
<span class="-ml-2">,</span>
{/if}
{/each}
<span>{$i18n.t('and')}</span>
<div class="text-gray-600 dark:text-gray-400">
{_citations.length - 2}
</div>
<div class="flex-1 mx-2 line-clamp-1">
{citation.source.name}
<span>{$i18n.t('more')}</span>
{#if isCollapsibleOpen}
<ChevronUp strokeWidth="3.5" className="size-3.5" />
{:else}
<ChevronDown strokeWidth="3.5" className="size-3.5" />
{/if}
</div>
<div slot="content" class="mt-2">
<div class="flex flex-wrap gap-2">
{#each _citations as citation, idx}
<div class="flex gap-1 text-xs font-semibold">
<button
class="flex dark:text-gray-300 py-1 px-1 bg-gray-50 hover:bg-gray-100 dark:bg-gray-850 dark:hover:bg-gray-800 transition rounded-xl max-w-96"
on:click={() => {
showCitationModal = true;
selectedCitation = citation;
}}
>
{#if _citations.every((c) => c.distances !== undefined)}
<div class="bg-white dark:bg-gray-700 rounded-full size-4">
{idx + 1}
</div>
{/if}
<div class="flex-1 mx-2 line-clamp-1">
{citation.source.name}
</div>
</button>
</div>
{/each}
</div>
</button>
</div>
{/each}
</div>
</Collapsible>
{/if}
</div>
{/if}

View File

@ -2,6 +2,7 @@
import { getContext, onMount, tick } from 'svelte';
import Modal from '$lib/components/common/Modal.svelte';
import Tooltip from '$lib/components/common/Tooltip.svelte';
const i18n = getContext('i18n');
export let show = false;
@ -9,14 +10,32 @@
let mergedDocuments = [];
function calculatePercentage(distance) {
return Math.max(0, Math.min(100, (1 - distance / 2) * 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';
}
$: if (citation) {
mergedDocuments = citation.document?.map((c, i) => {
return {
source: citation.source,
document: c,
metadata: citation.metadata?.[i]
metadata: citation.metadata?.[i],
distance: citation.distances?.[i]
};
});
if (mergedDocuments.every((doc) => doc.distance !== undefined)) {
mergedDocuments.sort((a, b) => (a.distance ?? Infinity) - (b.distance ?? Infinity));
}
}
</script>
@ -61,9 +80,9 @@
placement="left"
tippyOptions={{ duration: [500, 0], animation: 'perspective' }}
>
<div class="text-sm dark:text-gray-400">
<div class="text-sm dark:text-gray-400 flex items-center gap-2">
<a
class="hover:text-gray-500 hover:dark:text-gray-100 underline"
class="hover:text-gray-500 hover:dark:text-gray-100 underline flex-grow"
href={document?.metadata?.file_id
? `/api/v1/files/${document?.metadata?.file_id}/content${document?.metadata?.page !== undefined ? `#page=${document.metadata.page + 1}` : ''}`
: document.source.name.includes('http')
@ -73,11 +92,28 @@
>
{document?.metadata?.name ?? document.source.name}
</a>
{document?.metadata?.page
? `(${$i18n.t('page')} ${document.metadata.page + 1})`
: ''}
{#if document?.metadata?.page}
<span class="text-xs text-gray-500 dark:text-gray-400">
({$i18n.t('page')}
{document.metadata.page + 1})
</span>
{/if}
</div>
</Tooltip>
{#if document.distance !== undefined}
<div class="text-sm font-medium dark:text-gray-300 mt-2">
{$i18n.t('Relevance')}
</div>
{@const percentage = calculatePercentage(document.distance)}
<div class="text-sm my-1 dark:text-gray-400 flex items-center gap-2">
<span class={`px-1 rounded font-medium ${getRelevanceColor(percentage)}`}>
{percentage.toFixed(0)}%
</span>
<span class="text-gray-500 dark:text-gray-500"
>({document.distance.toFixed(4)})</span
>
</div>
{/if}
{:else}
<div class="text-sm dark:text-gray-400">
{$i18n.t('No source available')}

View File

@ -443,6 +443,7 @@
"Modelfile Content": "Modelfile-Inhalt",
"Models": "Modelle",
"More": "Mehr",
"more": "mehr",
"Move to Top": "",
"Name": "Name",
"Name Tag": "Namens-Tag",
@ -784,5 +785,6 @@
"Your account status is currently pending activation.": "Ihr Kontostatus ist derzeit ausstehend und wartet auf Aktivierung.",
"Your entire contribution will go directly to the plugin developer; Open WebUI does not take any percentage. However, the chosen funding platform might have its own fees.": "",
"Youtube": "YouTube",
"Youtube Loader Settings": "YouTube-Ladeeinstellungen"
"Youtube Loader Settings": "YouTube-Ladeeinstellungen",
"References from": "Referenzen aus"
}