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

205 lines
6.2 KiB
Svelte
Raw Normal View History

2024-08-19 02:59:59 +08:00
<script lang="ts">
import { getContext } from 'svelte';
2024-08-19 02:59:59 +08:00
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');
2024-08-19 02:59:59 +08:00
2024-11-22 11:46:09 +08:00
export let sources = [];
2024-08-19 02:59:59 +08:00
2024-11-22 11:46:09 +08:00
let citations = [];
2024-10-12 21:18:56 +08:00
let showPercentage = false;
let showRelevance = true;
2024-08-19 02:59:59 +08:00
let showCitationModal = false;
2024-10-12 21:18:56 +08:00
let selectedCitation: any = null;
let isCollapsibleOpen = false;
2024-11-22 11:46:09 +08:00
function calculateShowRelevance(sources: any[]) {
const distances = sources.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;
}
2024-11-22 11:46:09 +08:00
function shouldShowPercentage(sources: any[]) {
const distances = sources.flatMap((citation) => citation.distances ?? []);
return distances.every((d) => d !== undefined && d >= -1 && d <= 1);
2024-10-12 21:18:56 +08:00
}
2024-08-19 02:59:59 +08:00
2024-10-12 21:18:56 +08:00
$: {
2024-11-22 11:46:09 +08:00
citations = sources.reduce((acc, source) => {
if (Object.keys(source).length === 0) {
2024-11-22 10:26:38 +08:00
return acc;
}
2024-11-22 11:46:09 +08:00
source.document.forEach((document, index) => {
const metadata = source.metadata?.[index];
const distance = source.distances?.[index];
// Within the same citation there could be multiple documents
2024-10-12 21:18:56 +08:00
const id = metadata?.source ?? 'N/A';
2024-11-22 11:46:09 +08:00
let _source = source?.source;
2024-08-19 02:59:59 +08:00
2024-10-12 21:18:56 +08:00
if (metadata?.name) {
2024-11-22 11:46:09 +08:00
_source = { ..._source, name: metadata.name };
2024-10-12 21:18:56 +08:00
}
2024-08-19 02:59:59 +08:00
2024-10-12 21:18:56 +08:00
if (id.startsWith('http://') || id.startsWith('https://')) {
2024-11-22 11:46:09 +08:00
_source = { ..._source, name: id, url: id };
2024-10-12 21:18:56 +08:00
}
2024-08-19 02:59:59 +08:00
2024-10-12 21:18:56 +08:00
const existingSource = acc.find((item) => item.id === id);
if (existingSource) {
existingSource.document.push(document);
existingSource.metadata.push(metadata);
if (distance !== undefined) existingSource.distances.push(distance);
} else {
acc.push({
id: id,
2024-11-22 11:46:09 +08:00
source: _source,
2024-10-12 21:18:56 +08:00
document: [document],
metadata: metadata ? [metadata] : [],
distances: distance !== undefined ? [distance] : undefined
});
}
});
return acc;
}, []);
2024-11-22 11:46:09 +08:00
showRelevance = calculateShowRelevance(citations);
showPercentage = shouldShowPercentage(citations);
2024-10-12 21:18:56 +08:00
}
2024-09-12 14:06:02 +08:00
</script>
<CitationsModal
bind:show={showCitationModal}
citation={selectedCitation}
{showPercentage}
{showRelevance}
/>
2024-09-12 14:06:02 +08:00
2024-11-22 11:46:09 +08:00
{#if citations.length > 0}
2024-11-11 11:47:55 +08:00
<div class=" py-0.5 -mx-0.5 w-full flex gap-1 items-center flex-wrap">
2024-11-22 11:46:09 +08:00
{#if citations.length <= 3}
2025-02-06 17:28:33 +08:00
<div class="flex text-xs font-medium flex-wrap">
2024-11-22 11:46:09 +08:00
{#each citations as citation, idx}
<button
2025-02-13 15:55:14 +08:00
id={`source-${idx}`}
2024-11-22 10:05:45 +08:00
class="no-toggle outline-none flex dark:text-gray-300 p-1 bg-white dark:bg-gray-900 rounded-xl max-w-96"
on:click={() => {
showCitationModal = true;
selectedCitation = citation;
}}
>
2024-11-22 11:46:09 +08:00
{#if citations.every((c) => c.distances !== undefined)}
2024-11-11 11:24:53 +08:00
<div class="bg-gray-50 dark:bg-gray-800 rounded-full size-4">
{idx + 1}
</div>
{/if}
2024-11-22 10:05:45 +08:00
<div
2025-02-06 17:28:33 +08:00
class="flex-1 mx-1 truncate text-black/60 hover:text-black dark:text-white/60 dark:hover:text-white transition"
2024-11-22 10:05:45 +08:00
>
{citation.source.name}
</div>
</button>
2024-11-11 11:24:53 +08:00
{/each}
</div>
{:else}
2025-02-06 17:28:33 +08:00
<Collapsible
2025-02-13 05:47:54 +08:00
id="collapsible-sources"
2025-02-06 17:28:33 +08:00
bind:open={isCollapsibleOpen}
className="w-full max-w-full "
buttonClassName="w-fit max-w-full"
>
<div
2025-02-06 17:28:33 +08:00
class="flex w-full overflow-auto items-center gap-2 text-gray-500 hover:text-gray-600 dark:hover:text-gray-400 transition cursor-pointer"
2024-09-12 14:06:02 +08:00
>
2025-02-06 17:28:33 +08:00
<div
class="flex-1 flex items-center gap-1 overflow-auto scrollbar-none w-full max-w-full"
>
<span class="whitespace-nowrap hidden sm:inline flex-shrink-0"
>{$i18n.t('References from')}</span
>
<div class="flex items-center overflow-auto scrollbar-none w-full max-w-full flex-1">
2024-11-11 11:47:55 +08:00
<div class="flex text-xs font-medium items-center">
2024-11-22 11:46:09 +08:00
{#each citations.slice(0, 2) as citation, idx}
2024-11-11 11:47:55 +08:00
<button
class="no-toggle outline-none flex dark:text-gray-300 p-1 bg-gray-50 hover:bg-gray-100 dark:bg-gray-900 dark:hover:bg-gray-850 transition rounded-xl max-w-96"
on:click={() => {
showCitationModal = true;
selectedCitation = citation;
}}
on:pointerup={(e) => {
e.stopPropagation();
}}
>
2024-11-22 11:46:09 +08:00
{#if citations.every((c) => c.distances !== undefined)}
2024-11-11 11:47:55 +08:00
<div class="bg-gray-50 dark:bg-gray-800 rounded-full size-4">
{idx + 1}
2024-10-12 21:18:56 +08:00
</div>
{/if}
2025-02-06 17:28:33 +08:00
<div class="flex-1 mx-1 truncate">
2024-11-11 11:47:55 +08:00
{citation.source.name}
</div>
</button>
2024-10-14 16:37:54 +08:00
{/each}
2024-11-11 11:47:55 +08:00
</div>
2024-10-12 21:18:56 +08:00
</div>
2025-02-06 17:28:33 +08:00
<div class="flex items-center gap-1 whitespace-nowrap flex-shrink-0">
2024-10-12 21:18:56 +08:00
<span class="hidden sm:inline">{$i18n.t('and')}</span>
2024-11-22 11:46:09 +08:00
{citations.length - 2}
2024-10-12 21:18:56 +08:00
<span>{$i18n.t('more')}</span>
</div>
2024-10-12 21:18:56 +08:00
</div>
<div class="flex-shrink-0">
{#if isCollapsibleOpen}
<ChevronUp strokeWidth="3.5" className="size-3.5" />
{:else}
<ChevronDown strokeWidth="3.5" className="size-3.5" />
{/if}
2024-09-12 14:06:02 +08:00
</div>
</div>
2024-11-11 11:47:55 +08:00
<div slot="content">
2025-02-06 17:28:33 +08:00
<div class="flex text-xs font-medium flex-wrap">
2024-11-22 11:46:09 +08:00
{#each citations as citation, idx}
2024-11-11 11:47:55 +08:00
<button
2025-02-13 15:55:14 +08:00
id={`source-${idx}`}
2024-11-11 11:47:55 +08:00
class="no-toggle outline-none flex dark:text-gray-300 p-1 bg-gray-50 hover:bg-gray-100 dark:bg-gray-900 dark:hover:bg-gray-850 transition rounded-xl max-w-96"
on:click={() => {
showCitationModal = true;
selectedCitation = citation;
}}
>
2024-11-22 11:46:09 +08:00
{#if citations.every((c) => c.distances !== undefined)}
2024-11-11 11:47:55 +08:00
<div class="bg-gray-50 dark:bg-gray-800 rounded-full size-4">
{idx + 1}
</div>
2024-11-11 11:47:55 +08:00
{/if}
2025-02-06 17:28:33 +08:00
<div class="flex-1 mx-1 truncate">
2024-11-11 11:47:55 +08:00
{citation.source.name}
</div>
</button>
{/each}
2024-09-12 14:06:02 +08:00
</div>
</div>
</Collapsible>
{/if}
2024-09-12 14:06:02 +08:00
</div>
{/if}