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

187 lines
5.8 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
export let citations = [];
2024-09-12 14:06:02 +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;
function calculateShowRelevance(citations: any[]) {
const distances = citations.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-10-12 21:18:56 +08:00
function shouldShowPercentage(citations: any[]) {
const distances = citations.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
$: {
_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;
2024-08-19 02:59:59 +08:00
2024-10-12 21:18:56 +08:00
if (metadata?.name) {
source = { ...source, name: metadata.name };
}
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-11 11:24:53 +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,
source: source,
document: [document],
metadata: metadata ? [metadata] : [],
distances: distance !== undefined ? [distance] : undefined
});
}
});
return acc;
}, []);
showRelevance = calculateShowRelevance(_citations);
2024-10-12 21:18:56 +08:00
showPercentage = shouldShowPercentage(_citations);
}
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
{#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">
{#if _citations.length <= 3}
2024-11-11 11:47:55 +08:00
<div class="flex text-xs font-medium">
2024-11-11 11:24:53 +08:00
{#each _citations as citation, idx}
<button
2024-11-22 09:58:29 +08:00
id={`source-${citation.source.name}`}
2024-11-22 10:02:33 +08:00
class="no-toggle outline-none flex dark:text-gray-300 p-1 bg-white hover:bg-gray-50 dark:bg-gray-900 dark:hover:bg-gray-850 transition rounded-xl max-w-96"
on:click={() => {
showCitationModal = true;
selectedCitation = citation;
}}
>
{#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-11 11:24:53 +08:00
<div class="flex-1 mx-1 line-clamp-1 truncate">
{citation.source.name}
</div>
</button>
2024-11-11 11:24:53 +08:00
{/each}
</div>
{:else}
<Collapsible bind:open={isCollapsibleOpen} className="w-full">
<div
2024-11-11 11:47:55 +08:00
class="flex 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
>
2024-10-14 16:37:54 +08:00
<div class="flex-grow flex items-center gap-1 overflow-hidden">
2024-10-12 21:18:56 +08:00
<span class="whitespace-nowrap hidden sm:inline">{$i18n.t('References from')}</span>
2024-10-14 16:37:54 +08:00
<div class="flex items-center">
2024-11-11 11:47:55 +08:00
<div class="flex text-xs font-medium items-center">
2024-10-14 16:37:54 +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();
}}
>
{#if _citations.every((c) => c.distances !== undefined)}
<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}
2024-11-11 11:47:55 +08:00
<div class="flex-1 mx-1 line-clamp-1 truncate">
{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>
<div class="flex items-center gap-1 whitespace-nowrap">
<span class="hidden sm:inline">{$i18n.t('and')}</span>
2024-11-11 16:50:43 +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">
<div class="flex text-xs font-medium">
{#each _citations 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;
}}
>
{#if _citations.every((c) => c.distances !== undefined)}
<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}
<div class="flex-1 mx-1 line-clamp-1 truncate">
{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}