open-webui/src/lib/components/chat/Messages/CitationsModal.svelte

197 lines
6 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';
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">
<div class=" text-lg font-medium self-center capitalize">
{$i18n.t('Citation')}
</div>
<button
class="self-center"
on:click={() => {
show = false;
}}
>
2025-06-25 22:44:45 +00:00
<XMark />
</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
2024-05-15 06:16:22 +00:00
class="flex flex-col w-full dark:text-gray-200 overflow-y-scroll max-h-[22rem] scrollbar-hidden"
2024-05-06 23:19:48 +00:00
>
2024-05-06 22:14:33 +00:00
{#each mergedDocuments as document, documentIdx}
<div class="flex flex-col w-full">
<div class="text-sm font-medium dark:text-gray-300">
{$i18n.t('Source')}
</div>
2024-07-15 09:54:56 +00:00
{#if document.source?.name}
2024-09-23 13:48:12 +00:00
<Tooltip
2024-10-19 07:45:29 +00:00
className="w-fit"
2024-09-23 13:48:12 +00:00
content={$i18n.t('Open file')}
2024-10-19 07:45:29 +00:00
placement="top-start"
2024-10-12 13:18:56 +00:00
tippyOptions={{ duration: [500, 0] }}
2024-09-23 13:48:12 +00:00
>
2024-10-19 07:45:29 +00:00
<div class="text-sm dark:text-gray-400 flex items-center gap-2 w-fit">
2024-09-23 13:48:12 +00:00
<a
2025-02-16 03:27:25 +00:00
class="hover:text-gray-500 dark:hover:text-gray-100 underline grow"
2024-09-23 13:48:12 +00:00
href={document?.metadata?.file_id
? `${WEBUI_API_BASE_URL}/files/${document?.metadata?.file_id}/content${document?.metadata?.page !== undefined ? `#page=${document.metadata.page + 1}` : ''}`
2024-10-22 04:35:01 +00:00
: document.source?.url?.includes('http')
? document.source.url
2024-09-23 13:48:12 +00:00
: `#`}
target="_blank"
>
2025-04-11 22:27:25 +00:00
{decodeString(document?.metadata?.name ?? document.source.name)}
2024-09-23 13:48:12 +00:00
</a>
{#if Number.isInteger(document?.metadata?.page)}
<span class="text-xs text-gray-500 dark:text-gray-400">
({$i18n.t('page')}
{document.metadata.page + 1})
</span>
{/if}
2024-09-23 13:48:12 +00:00
</div>
</Tooltip>
{#if document.metadata?.parameters}
<div class="text-sm font-medium dark:text-gray-300 mt-2">
{$i18n.t('Parameters')}
</div>
2025-05-23 20:43:38 +00:00
<pre
class="text-sm dark:text-gray-400 bg-gray-50 dark:bg-gray-800 p-2 rounded-md overflow-auto max-h-40">{JSON.stringify(
document.metadata.parameters,
null,
2
)}</pre>
{/if}
{#if showRelevance}
<div class="text-sm font-medium dark:text-gray-300 mt-2">
{$i18n.t('Relevance')}
2024-10-12 13:18:56 +00:00
</div>
{#if document.distance !== undefined}
<Tooltip
2024-10-19 07:45:29 +00:00
className="w-fit"
2024-10-17 20:30:21 +00:00
content={$i18n.t('Semantic distance to query')}
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
{#if typeof document?.distance === 'number'}
<span class="text-gray-500 dark:text-gray-500">
({(document?.distance ?? 0).toFixed(4)})
</span>
{/if}
{: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>
{:else}
<div class="text-sm dark:text-gray-400">
{$i18n.t('No distance available')}
</div>
{/if}
{/if}
2024-07-15 09:54:56 +00:00
{:else}
<div class="text-sm dark:text-gray-400">
{$i18n.t('No source available')}
</div>
{/if}
</div>
2024-05-06 22:14:33 +00:00
<div class="flex flex-col w-full">
2024-10-14 08:37:54 +00:00
<div class=" text-sm font-medium dark:text-gray-300 mt-2">
2024-05-06 22:14:33 +00:00
{$i18n.t('Content')}
</div>
2024-11-13 00:07:39 +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">
{document.document}
</pre>
{/if}
</div>
2024-05-06 22:04:37 +00:00
2024-05-06 22:14:33 +00:00
{#if documentIdx !== mergedDocuments.length - 1}
2025-02-16 03:27:25 +00:00
<hr class="border-gray-100 dark:border-gray-850 my-3" />
2024-05-06 22:14:33 +00:00
{/if}
{/each}
</div>
</div>
</div>
</Modal>