refac: citation rendering

This commit is contained in:
Timothy J. Baek 2024-05-06 15:59:38 -07:00
parent 64ed0d1089
commit aef2a514d1
2 changed files with 44 additions and 30 deletions

View File

@ -5,12 +5,11 @@
const i18n = getContext('i18n'); const i18n = getContext('i18n');
export let show = false; export let show = false;
export let citation: any[]; export let citation;
let mergedDocuments = []; let mergedDocuments = [];
onMount(async () => { $: if (citation) {
// Merge the document with its metadata
mergedDocuments = citation.document?.map((c, i) => { mergedDocuments = citation.document?.map((c, i) => {
return { return {
source: citation.source, source: citation.source,
@ -18,7 +17,7 @@
metadata: citation.metadata?.[i] metadata: citation.metadata?.[i]
}; };
}); });
}); }
</script> </script>
<Modal size="lg" bind:show> <Modal size="lg" bind:show>

View File

@ -66,8 +66,8 @@
let showRateComment = false; let showRateComment = false;
// Backend returns a list of citations per collection, we flatten it to citations per source let showCitationModal = false;
let citations = {}; let selectedCitation = null;
$: tokens = marked.lexer(sanitizeResponseContent(message.content)); $: tokens = marked.lexer(sanitizeResponseContent(message.content));
@ -134,24 +134,6 @@
allowHTML: true allowHTML: true
}); });
} }
if (message.citations) {
message.citations.forEach((citation) => {
citation.document.forEach((document, index) => {
const metadata = citation.metadata?.[index];
const source = citation?.source?.name ?? metadata?.source ?? 'N/A';
citations[source] = citations[source] || {
source: citation.source,
document: [],
metadata: []
};
citations[source].document.push(document);
citations[source].metadata.push(metadata);
});
});
}
}; };
const renderLatex = () => { const renderLatex = () => {
@ -346,6 +328,8 @@
}); });
</script> </script>
<CitationsModal bind:show={showCitationModal} citation={selectedCitation} />
{#key message.id} {#key message.id}
<div class=" flex w-full message-{message.id}" id="message-{message.id}"> <div class=" flex w-full message-{message.id}" id="message-{message.id}">
<ProfileImage <ProfileImage
@ -467,13 +451,43 @@
</div> </div>
</div> </div>
{#if Object.keys(citations).length > 0} <!-- if (message.citations) {
citations = message.citations.forEach((citation) => {
citation.document.forEach((document, index) => {
const metadata = citation.metadata?.[index];
const source = citation?.source?.name ?? metadata?.source ?? 'N/A';
citations[source] = citations[source] || {
source: citation.source,
document: [],
metadata: []
};
citations[source].document.push(document);
citations[source].metadata.push(metadata);
});
});
} -->
{#if message.citations}
<hr class=" dark:border-gray-800" /> <hr class=" dark:border-gray-800" />
<div class="my-2.5 w-full flex overflow-x-auto gap-2 flex-wrap"> <div class="my-2.5 w-full flex overflow-x-auto gap-2 flex-wrap">
{#each Object.keys(citations) as source, idx} {#each message.citations.reduce((acc, citation) => {
<CitationsModal bind:show={citations[source].show} citation={citations[source]} /> citation.document.forEach((document, index) => {
const metadata = citation.metadata?.[index];
const id = metadata?.source ?? 'N/A';
const existingSource = acc.find((item) => item.id === id);
if (existingSource) {
existingSource.document.push(document);
existingSource.metadata.push(metadata);
} else {
acc.push( { id: id, source: citation?.source, document: [document], metadata: metadata ? [metadata] : [] } );
}
});
return acc;
}, []) as citation, idx}
<div class="flex gap-1 text-xs font-semibold"> <div class="flex gap-1 text-xs font-semibold">
<div> <div>
[{idx + 1}] [{idx + 1}]
@ -482,10 +496,11 @@
<button <button
class="dark:text-gray-500 underline" class="dark:text-gray-500 underline"
on:click={() => { on:click={() => {
citations[source].show = !citations[source].show; showCitationModal = true;
selectedCitation = citation;
}} }}
> >
{citations[source].source.name} {citation.source.name}
</button> </button>
</div> </div>
{/each} {/each}