From 4c6567c46fcaf3ac4c6ea33c143820c8d8389e55 Mon Sep 17 00:00:00 2001 From: Jun Siang Cheah Date: Mon, 6 May 2024 22:06:37 +0800 Subject: [PATCH] feat: group citations by source --- .../chat/Messages/CitationsModal.svelte | 2 - .../chat/Messages/ResponseMessage.svelte | 37 ++++++++++++++++--- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/lib/components/chat/Messages/CitationsModal.svelte b/src/lib/components/chat/Messages/CitationsModal.svelte index 6ed4125ca..79a9a9720 100644 --- a/src/lib/components/chat/Messages/CitationsModal.svelte +++ b/src/lib/components/chat/Messages/CitationsModal.svelte @@ -49,7 +49,6 @@
{#each mergedDocuments as document} -
{$i18n.t('Source')} @@ -58,7 +57,6 @@ {document.metadata?.source ?? $i18n.t('No source available')}
-
{$i18n.t('Content')} diff --git a/src/lib/components/chat/Messages/ResponseMessage.svelte b/src/lib/components/chat/Messages/ResponseMessage.svelte index d7da104fe..de59239e1 100644 --- a/src/lib/components/chat/Messages/ResponseMessage.svelte +++ b/src/lib/components/chat/Messages/ResponseMessage.svelte @@ -67,6 +67,8 @@ let showRateComment = false; let showCitations = {}; + // Backend returns a list of citations per collection, we flatten it to citations per source + let flattenedCitations = {}; $: tokens = marked.lexer(sanitizeResponseContent(message.content)); @@ -133,6 +135,28 @@ allowHTML: true }); } + + if (message.citations) { + for (const citation of message.citations) { + const zipped = (citation?.document ?? []).map(function (document, index) { + return [document, citation.metadata?.[index]]; + }); + for (const [document, metadata] of zipped) { + const source = metadata?.source ?? 'N/A'; + if (source in flattenedCitations) { + flattenedCitations[source].document.push(document); + flattenedCitations[source].metadata.push(metadata); + } else { + flattenedCitations[source] = { + document: [document], + metadata: [metadata] + }; + } + } + } + console.log(flattenedCitations); + console.log(Object.keys(flattenedCitations)); + } }; const renderLatex = () => { @@ -363,16 +387,19 @@ {/each}
{/if} - {#if message.citations} + {#if flattenedCitations}
- {#each message.citations as citation} + {#each [...Object.keys(flattenedCitations)] as source}
- +