open-webui/src/lib/components/chat/Messages/Citations/CitationModal.svelte

180 lines
5.3 KiB
Svelte
Raw Normal View History

<script lang="ts">
import { getContext, onMount, tick } from 'svelte';
import Modal from '$lib/components/common/Modal.svelte';
2024-09-23 13:48:12 +00:00
import Tooltip from '$lib/components/common/Tooltip.svelte';
import { WEBUI_API_BASE_URL } from '$lib/constants';
2025-06-25 22:44:45 +00:00
import XMark from '$lib/components/icons/XMark.svelte';
2025-08-06 09:48:43 +00:00
import Textarea from '$lib/components/common/Textarea.svelte';
2025-06-25 22:44:45 +00:00
const i18n = getContext('i18n');
export let show = false;
2024-05-06 22:59:38 +00:00
export let citation;
2024-10-12 13:18:56 +00:00
export let showPercentage = false;
export let showRelevance = true;
let mergedDocuments = [];
2024-10-12 13:18:56 +00:00
function calculatePercentage(distance: number) {
2025-04-07 02:14:24 +00:00
if (typeof distance !== 'number') return null;
if (distance < 0) return 0;
if (distance > 1) return 100;
2024-11-13 13:54:39 +00:00
return Math.round(distance * 10000) / 100;
}
2024-10-12 13:18:56 +00:00
function getRelevanceColor(percentage: number) {
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';
}
2024-05-06 22:59:38 +00:00
$: if (citation) {
mergedDocuments = citation.document?.map((c, i) => {
return {
2024-05-06 22:49:00 +00:00
source: citation.source,
document: c,
metadata: citation.metadata?.[i],
distance: citation.distances?.[i]
};
});
if (mergedDocuments.every((doc) => doc.distance !== undefined)) {
2024-11-25 02:11:48 +00:00
mergedDocuments = mergedDocuments.sort(
(a, b) => (b.distance ?? Infinity) - (a.distance ?? Infinity)
);
}
2024-05-06 22:59:38 +00:00
}
2025-04-11 22:27:25 +00:00
const decodeString = (str: string) => {
try {
return decodeURIComponent(str);
} catch (e) {
return str;
}
};
</script>
<Modal size="lg" bind:show>
<div>
2024-05-06 22:14:33 +00:00
<div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-2">
2025-09-09 14:01:59 +00:00
<div class=" text-lg font-medium self-center">
{#if citation?.source?.name}
{@const document = mergedDocuments?.[0]}
{#if document?.metadata?.file_id || document.source?.url?.includes('http')}
<Tooltip
className="w-fit"
content={$i18n.t('Open file')}
placement="top-start"
tippyOptions={{ duration: [500, 0] }}
>
<a
class="hover:text-gray-500 dark:hover:text-gray-100 underline grow"
href={document?.metadata?.file_id
? `${WEBUI_API_BASE_URL}/files/${document?.metadata?.file_id}/content${document?.metadata?.page !== undefined ? `#page=${document.metadata.page + 1}` : ''}`
: document.source?.url?.includes('http')
? document.source.url
: `#`}
target="_blank"
>
{decodeString(citation?.source?.name)}
</a>
</Tooltip>
{:else}
{decodeString(citation?.source?.name)}
{/if}
{:else}
{$i18n.t('Citation')}
{/if}
</div>
<button
class="self-center"
on:click={() => {
show = false;
}}
>
2025-06-27 11:44:26 +00:00
<XMark className={'size-5'} />
</button>
</div>
2024-05-06 22:14:33 +00:00
2024-05-06 23:19:48 +00:00
<div class="flex flex-col md:flex-row w-full px-6 pb-5 md:space-x-4">
<div
2025-09-09 14:01:59 +00:00
class="flex flex-col w-full dark:text-gray-200 overflow-y-scroll max-h-[22rem] scrollbar-hidden gap-1"
2024-05-06 23:19:48 +00:00
>
2024-05-06 22:14:33 +00:00
{#each mergedDocuments as document, documentIdx}
2025-09-09 14:01:59 +00:00
<div class="flex flex-col w-full gap-2">
{#if document.metadata?.parameters}
<div>
<div class="text-sm font-medium dark:text-gray-300 mb-1">
{$i18n.t('Parameters')}
</div>
2025-08-06 09:48:43 +00:00
<Textarea readonly value={JSON.stringify(document.metadata.parameters, null, 2)}
></Textarea>
2025-09-09 14:01:59 +00:00
</div>
{/if}
<div>
<div
class=" text-sm font-medium dark:text-gray-300 flex items-center gap-2 w-fit mb-1"
>
{$i18n.t('Content')}
{#if showRelevance && document.distance !== undefined}
<Tooltip
2024-10-19 07:45:29 +00:00
className="w-fit"
2025-09-09 14:01:59 +00:00
content={$i18n.t('Relevance')}
2024-10-19 07:45:29 +00:00
placement="top-start"
tippyOptions={{ duration: [500, 0] }}
>
2024-10-19 07:45:29 +00:00
<div class="text-sm my-1 dark:text-gray-400 flex items-center gap-2 w-fit">
{#if showPercentage}
{@const percentage = calculatePercentage(document.distance)}
2025-04-07 02:14:24 +00:00
{#if typeof percentage === 'number'}
<span
class={`px-1 rounded-sm font-medium ${getRelevanceColor(percentage)}`}
>
{percentage.toFixed(2)}%
</span>
{/if}
2025-04-19 09:47:48 +00:00
{:else if typeof document?.distance === 'number'}
<span class="text-gray-500 dark:text-gray-500">
2025-03-21 00:54:13 +00:00
({(document?.distance ?? 0).toFixed(4)})
</span>
{/if}
</div>
</Tooltip>
{/if}
2025-09-09 14:01:59 +00:00
{#if Number.isInteger(document?.metadata?.page)}
<span class="text-sm text-gray-500 dark:text-gray-400">
({$i18n.t('page')}
{document.metadata.page + 1})
</span>
{/if}
2024-07-15 09:54:56 +00:00
</div>
2025-09-09 14:01:59 +00:00
{#if document.metadata?.html}
<iframe
class="w-full border-0 h-auto rounded-none"
sandbox="allow-scripts allow-forms allow-same-origin"
srcdoc={document.document}
title={$i18n.t('Content')}
></iframe>
{:else}
<pre class="text-sm dark:text-gray-400 whitespace-pre-line">
2024-11-13 00:07:39 +00:00
{document.document}
</pre>
2025-09-09 14:01:59 +00:00
{/if}
</div>
</div>
2024-05-06 22:14:33 +00:00
{/each}
</div>
</div>
</div>
</Modal>