refac: citations -> sources

This commit is contained in:
Timothy Jaeryang Baek
2024-11-21 19:46:09 -08:00
parent 7062e637e8
commit 81386e9b04
9 changed files with 165 additions and 126 deletions
+4 -4
View File
@@ -5,7 +5,7 @@ type TextStreamUpdate = {
done: boolean;
value: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
citations?: any;
sources?: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
selectedModelId?: any;
error?: any;
@@ -67,8 +67,8 @@ async function* openAIStreamToIterator(
break;
}
if (parsedData.citations) {
yield { done: false, value: '', citations: parsedData.citations };
if (parsedData.sources) {
yield { done: false, value: '', sources: parsedData.sources };
continue;
}
@@ -98,7 +98,7 @@ async function* streamLargeDeltasAsRandomChunks(
yield textStreamUpdate;
return;
}
if (textStreamUpdate.citations) {
if (textStreamUpdate.sources) {
yield textStreamUpdate;
continue;
}
+11 -11
View File
@@ -236,10 +236,10 @@
message.code_executions = message.code_executions;
} else {
// Regular citation.
if (message?.citations) {
message.citations.push(data);
if (message?.sources) {
message.sources.push(data);
} else {
message.citations = [data];
message.sources = [data];
}
}
} else if (type === 'message') {
@@ -664,7 +664,7 @@
content: m.content,
info: m.info ? m.info : undefined,
timestamp: m.timestamp,
...(m.citations ? { citations: m.citations } : {})
...(m.sources ? { sources: m.sources } : {})
})),
chat_id: chatId,
session_id: $socket?.id,
@@ -718,7 +718,7 @@
content: m.content,
info: m.info ? m.info : undefined,
timestamp: m.timestamp,
...(m.citations ? { citations: m.citations } : {})
...(m.sources ? { sources: m.sources } : {})
})),
...(event ? { event: event } : {}),
chat_id: chatId,
@@ -1278,8 +1278,8 @@
console.log(line);
let data = JSON.parse(line);
if ('citations' in data) {
responseMessage.citations = data.citations;
if ('sources' in data) {
responseMessage.sources = data.sources;
// Only remove status if it was initially set
if (model?.info?.meta?.knowledge ?? false) {
responseMessage.statusHistory = responseMessage.statusHistory.filter(
@@ -1632,7 +1632,7 @@
const textStream = await createOpenAITextStream(res.body, $settings.splitLargeChunks);
for await (const update of textStream) {
const { value, done, citations, selectedModelId, error, usage } = update;
const { value, done, sources, selectedModelId, error, usage } = update;
if (error) {
await handleOpenAIError(error, null, model, responseMessage);
break;
@@ -1658,8 +1658,8 @@
continue;
}
if (citations) {
responseMessage.citations = citations;
if (sources) {
responseMessage.sources = sources;
// Only remove status if it was initially set
if (model?.info?.meta?.knowledge ?? false) {
responseMessage.statusHistory = responseMessage.statusHistory.filter(
@@ -1938,7 +1938,7 @@
if (res && res.ok && res.body) {
const textStream = await createOpenAITextStream(res.body, $settings.splitLargeChunks);
for await (const update of textStream) {
const { value, done, citations, error, usage } = update;
const { value, done, sources, error, usage } = update;
if (error || done) {
break;
}
@@ -7,9 +7,9 @@
const i18n = getContext('i18n');
export let citations = [];
export let sources = [];
let _citations = [];
let citations = [];
let showPercentage = false;
let showRelevance = true;
@@ -17,8 +17,8 @@
let selectedCitation: any = null;
let isCollapsibleOpen = false;
function calculateShowRelevance(citations: any[]) {
const distances = citations.flatMap((citation) => citation.distances ?? []);
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;
@@ -36,29 +36,31 @@
return true;
}
function shouldShowPercentage(citations: any[]) {
const distances = citations.flatMap((citation) => citation.distances ?? []);
function shouldShowPercentage(sources: any[]) {
const distances = sources.flatMap((citation) => citation.distances ?? []);
return distances.every((d) => d !== undefined && d >= -1 && d <= 1);
}
$: {
_citations = citations.reduce((acc, citation) => {
if (Object.keys(citation).length === 0) {
citations = sources.reduce((acc, source) => {
if (Object.keys(source).length === 0) {
return acc;
}
citation.document.forEach((document, index) => {
const metadata = citation.metadata?.[index];
const distance = citation.distances?.[index];
source.document.forEach((document, index) => {
const metadata = source.metadata?.[index];
const distance = source.distances?.[index];
// Within the same citation there could be multiple documents
const id = metadata?.source ?? 'N/A';
let source = citation?.source;
let _source = source?.source;
if (metadata?.name) {
source = { ...source, name: metadata.name };
_source = { ..._source, name: metadata.name };
}
if (id.startsWith('http://') || id.startsWith('https://')) {
source = { ...source, name: id, url: id };
_source = { ..._source, name: id, url: id };
}
const existingSource = acc.find((item) => item.id === id);
@@ -70,7 +72,7 @@
} else {
acc.push({
id: id,
source: source,
source: _source,
document: [document],
metadata: metadata ? [metadata] : [],
distances: distance !== undefined ? [distance] : undefined
@@ -80,8 +82,8 @@
return acc;
}, []);
showRelevance = calculateShowRelevance(_citations);
showPercentage = shouldShowPercentage(_citations);
showRelevance = calculateShowRelevance(citations);
showPercentage = shouldShowPercentage(citations);
}
</script>
@@ -92,11 +94,11 @@
{showRelevance}
/>
{#if _citations.length > 0}
{#if citations.length > 0}
<div class=" py-0.5 -mx-0.5 w-full flex gap-1 items-center flex-wrap">
{#if _citations.length <= 3}
{#if citations.length <= 3}
<div class="flex text-xs font-medium">
{#each _citations as citation, idx}
{#each citations as citation, idx}
<button
id={`source-${citation.source.name}`}
class="no-toggle outline-none flex dark:text-gray-300 p-1 bg-white dark:bg-gray-900 rounded-xl max-w-96"
@@ -105,7 +107,7 @@
selectedCitation = citation;
}}
>
{#if _citations.every((c) => c.distances !== undefined)}
{#if citations.every((c) => c.distances !== undefined)}
<div class="bg-gray-50 dark:bg-gray-800 rounded-full size-4">
{idx + 1}
</div>
@@ -127,7 +129,7 @@
<span class="whitespace-nowrap hidden sm:inline">{$i18n.t('References from')}</span>
<div class="flex items-center">
<div class="flex text-xs font-medium items-center">
{#each _citations.slice(0, 2) as citation, idx}
{#each citations.slice(0, 2) as citation, idx}
<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={() => {
@@ -138,7 +140,7 @@
e.stopPropagation();
}}
>
{#if _citations.every((c) => c.distances !== undefined)}
{#if citations.every((c) => c.distances !== undefined)}
<div class="bg-gray-50 dark:bg-gray-800 rounded-full size-4">
{idx + 1}
</div>
@@ -152,7 +154,7 @@
</div>
<div class="flex items-center gap-1 whitespace-nowrap">
<span class="hidden sm:inline">{$i18n.t('and')}</span>
{_citations.length - 2}
{citations.length - 2}
<span>{$i18n.t('more')}</span>
</div>
</div>
@@ -166,7 +168,7 @@
</div>
<div slot="content">
<div class="flex text-xs font-medium">
{#each _citations as citation, idx}
{#each citations as citation, idx}
<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={() => {
@@ -174,7 +176,7 @@
selectedCitation = citation;
}}
>
{#if _citations.every((c) => c.distances !== undefined)}
{#if citations.every((c) => c.distances !== undefined)}
<div class="bg-gray-50 dark:bg-gray-800 rounded-full size-4">
{idx + 1}
</div>
@@ -7,11 +7,12 @@
import LightBlub from '$lib/components/icons/LightBlub.svelte';
import { chatId, mobile, showArtifacts, showControls, showOverview } from '$lib/stores';
import ChatBubble from '$lib/components/icons/ChatBubble.svelte';
import { stringify } from 'postcss';
export let id;
export let content;
export let model = null;
export let citations = null;
export let sources = null;
export let save = false;
export let floatingButtons = true;
@@ -131,7 +132,31 @@
{content}
{model}
{save}
sourceIds={(citations ?? []).map((c) => c?.source?.name)}
sourceIds={(sources ?? []).reduce((acc, s) => {
let ids = [];
s.document.forEach((document, index) => {
const metadata = s.metadata?.[index];
const id = metadata?.source ?? 'N/A';
if (metadata?.name) {
ids.push(metadata.name);
return ids;
}
if (id.startsWith('http://') || id.startsWith('https://')) {
ids.push(id);
} else {
ids.push(s?.source?.name ?? id);
}
return ids;
});
acc = [...acc, ...ids];
// remove duplicates
return acc.filter((item, index) => acc.indexOf(item) === index);
}, [])}
{onSourceClick}
on:update={(e) => {
dispatch('update', e.detail);
@@ -64,7 +64,7 @@
};
done: boolean;
error?: boolean | { content: string };
citations?: string[];
sources?: string[];
code_executions?: {
uuid: string;
name: string;
@@ -621,7 +621,7 @@
<ContentRenderer
id={message.id}
content={message.content}
citations={message.citations}
sources={message.sources}
floatingButtons={message?.done}
save={!readOnly}
{model}
@@ -662,8 +662,8 @@
<Error content={message?.error?.content ?? message.content} />
{/if}
{#if message.citations && (model?.info?.meta?.capabilities?.citations ?? true)}
<Citations citations={message.citations} />
{#if (message?.sources || message?.citations) && (model?.info?.meta?.capabilities?.citations ?? true)}
<Citations sources={message?.sources ?? message?.citations} />
{/if}
{#if message.code_executions}