mirror of
https://github.com/open-webui/open-webui
synced 2025-06-26 18:26:48 +00:00
enh: show source documents vector distance + cleaner source view
This commit is contained in:
parent
ed9fbe153b
commit
86caca495b
@ -1,13 +1,24 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { getContext } from 'svelte';
|
||||||
import CitationsModal from './CitationsModal.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 = [];
|
export let citations = [];
|
||||||
|
|
||||||
let _citations = [];
|
let _citations = [];
|
||||||
|
|
||||||
|
let showCitationModal = false;
|
||||||
|
let selectedCitation = null;
|
||||||
|
let isCollapsibleOpen = false;
|
||||||
|
|
||||||
$: _citations = citations.reduce((acc, citation) => {
|
$: _citations = citations.reduce((acc, citation) => {
|
||||||
citation.document.forEach((document, index) => {
|
citation.document.forEach((document, index) => {
|
||||||
const metadata = citation.metadata?.[index];
|
const metadata = citation.metadata?.[index];
|
||||||
|
const distance = citation.distances?.[index];
|
||||||
const id = metadata?.source ?? 'N/A';
|
const id = metadata?.source ?? 'N/A';
|
||||||
let source = citation?.source;
|
let source = citation?.source;
|
||||||
|
|
||||||
@ -25,43 +36,120 @@
|
|||||||
if (existingSource) {
|
if (existingSource) {
|
||||||
existingSource.document.push(document);
|
existingSource.document.push(document);
|
||||||
existingSource.metadata.push(metadata);
|
existingSource.metadata.push(metadata);
|
||||||
|
if (distance !== undefined) existingSource.distances.push(distance);
|
||||||
} else {
|
} else {
|
||||||
acc.push({
|
acc.push({
|
||||||
id: id,
|
id: id,
|
||||||
source: source,
|
source: source,
|
||||||
document: [document],
|
document: [document],
|
||||||
metadata: metadata ? [metadata] : []
|
metadata: metadata ? [metadata] : [],
|
||||||
|
distances: distance !== undefined ? [distance] : undefined
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return acc;
|
return acc;
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
let showCitationModal = false;
|
$: if (_citations.every((citation) => citation.distances !== undefined)) {
|
||||||
let selectedCitation = null;
|
// 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>
|
</script>
|
||||||
|
|
||||||
<CitationsModal bind:show={showCitationModal} citation={selectedCitation} />
|
<CitationsModal bind:show={showCitationModal} citation={selectedCitation} />
|
||||||
|
|
||||||
{#if _citations.length > 0}
|
{#if _citations.length > 0}
|
||||||
<div class="mt-1 mb-2 w-full flex gap-1 items-center flex-wrap">
|
<div class="mt-1 mb-2 w-full flex gap-1 items-center flex-wrap">
|
||||||
{#each _citations as citation, idx}
|
{#if _citations.length <= 3}
|
||||||
<div class="flex gap-1 text-xs font-semibold">
|
{#each _citations as citation, idx}
|
||||||
<button
|
<div class="flex gap-1 text-xs font-semibold">
|
||||||
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"
|
<button
|
||||||
on:click={() => {
|
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"
|
||||||
showCitationModal = true;
|
on:click={() => {
|
||||||
selectedCitation = citation;
|
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">
|
<span>{$i18n.t('References from')}</span>
|
||||||
{idx + 1}
|
{#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>
|
||||||
<div class="flex-1 mx-2 line-clamp-1">
|
<span>{$i18n.t('more')}</span>
|
||||||
{citation.source.name}
|
{#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>
|
</div>
|
||||||
</button>
|
</div>
|
||||||
</div>
|
</Collapsible>
|
||||||
{/each}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
import { getContext, onMount, tick } from 'svelte';
|
import { getContext, onMount, tick } from 'svelte';
|
||||||
import Modal from '$lib/components/common/Modal.svelte';
|
import Modal from '$lib/components/common/Modal.svelte';
|
||||||
import Tooltip from '$lib/components/common/Tooltip.svelte';
|
import Tooltip from '$lib/components/common/Tooltip.svelte';
|
||||||
|
|
||||||
const i18n = getContext('i18n');
|
const i18n = getContext('i18n');
|
||||||
|
|
||||||
export let show = false;
|
export let show = false;
|
||||||
@ -9,14 +10,32 @@
|
|||||||
|
|
||||||
let mergedDocuments = [];
|
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) {
|
$: if (citation) {
|
||||||
mergedDocuments = citation.document?.map((c, i) => {
|
mergedDocuments = citation.document?.map((c, i) => {
|
||||||
return {
|
return {
|
||||||
source: citation.source,
|
source: citation.source,
|
||||||
document: c,
|
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>
|
</script>
|
||||||
|
|
||||||
@ -61,9 +80,9 @@
|
|||||||
placement="left"
|
placement="left"
|
||||||
tippyOptions={{ duration: [500, 0], animation: 'perspective' }}
|
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
|
<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
|
href={document?.metadata?.file_id
|
||||||
? `/api/v1/files/${document?.metadata?.file_id}/content${document?.metadata?.page !== undefined ? `#page=${document.metadata.page + 1}` : ''}`
|
? `/api/v1/files/${document?.metadata?.file_id}/content${document?.metadata?.page !== undefined ? `#page=${document.metadata.page + 1}` : ''}`
|
||||||
: document.source.name.includes('http')
|
: document.source.name.includes('http')
|
||||||
@ -73,11 +92,28 @@
|
|||||||
>
|
>
|
||||||
{document?.metadata?.name ?? document.source.name}
|
{document?.metadata?.name ?? document.source.name}
|
||||||
</a>
|
</a>
|
||||||
{document?.metadata?.page
|
{#if document?.metadata?.page}
|
||||||
? `(${$i18n.t('page')} ${document.metadata.page + 1})`
|
<span class="text-xs text-gray-500 dark:text-gray-400">
|
||||||
: ''}
|
({$i18n.t('page')}
|
||||||
|
{document.metadata.page + 1})
|
||||||
|
</span>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</Tooltip>
|
</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}
|
{:else}
|
||||||
<div class="text-sm dark:text-gray-400">
|
<div class="text-sm dark:text-gray-400">
|
||||||
{$i18n.t('No source available')}
|
{$i18n.t('No source available')}
|
||||||
|
@ -443,6 +443,7 @@
|
|||||||
"Modelfile Content": "Modelfile-Inhalt",
|
"Modelfile Content": "Modelfile-Inhalt",
|
||||||
"Models": "Modelle",
|
"Models": "Modelle",
|
||||||
"More": "Mehr",
|
"More": "Mehr",
|
||||||
|
"more": "mehr",
|
||||||
"Move to Top": "",
|
"Move to Top": "",
|
||||||
"Name": "Name",
|
"Name": "Name",
|
||||||
"Name Tag": "Namens-Tag",
|
"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 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.": "",
|
"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": "YouTube",
|
||||||
"Youtube Loader Settings": "YouTube-Ladeeinstellungen"
|
"Youtube Loader Settings": "YouTube-Ladeeinstellungen",
|
||||||
|
"References from": "Referenzen aus"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user