From a8ec73d01e4163b7cefdc39e229a378b09660de1 Mon Sep 17 00:00:00 2001 From: Jun Siang Cheah Date: Wed, 2 Oct 2024 23:12:42 +0100 Subject: [PATCH] feat: show the user the entirety of the usage response --- src/lib/apis/streaming/index.ts | 2 + src/lib/components/chat/Chat.svelte | 2 +- .../chat/Messages/ResponseMessage.svelte | 67 ++++++++++++------- 3 files changed, 46 insertions(+), 25 deletions(-) diff --git a/src/lib/apis/streaming/index.ts b/src/lib/apis/streaming/index.ts index f91edad83..722cca9a1 100644 --- a/src/lib/apis/streaming/index.ts +++ b/src/lib/apis/streaming/index.ts @@ -18,6 +18,8 @@ type ResponseUsage = { completion_tokens: number; /** Sum of the above two fields */ total_tokens: number; + /** Any other fields that aren't part of the base OpenAI spec */ + [other: string]: unknown; }; // createOpenAITextStream takes a responseBody with a SSE response, diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index eeb2f330f..9c9707e6d 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -1393,7 +1393,7 @@ } if (usage) { - responseMessage.info = { ...usage, openai: true }; + responseMessage.info = { ...usage, openai: true, usage }; } if (citations) { diff --git a/src/lib/components/chat/Messages/ResponseMessage.svelte b/src/lib/components/chat/Messages/ResponseMessage.svelte index ff1983cbd..7bd6563fe 100644 --- a/src/lib/components/chat/Messages/ResponseMessage.svelte +++ b/src/lib/components/chat/Messages/ResponseMessage.svelte @@ -18,7 +18,8 @@ extractParagraphsForAudio, extractSentencesForAudio, cleanText, - getMessageContentParts + getMessageContentParts, + sanitizeResponseContent } from '$lib/utils'; import { WEBUI_BASE_URL } from '$lib/constants'; @@ -73,6 +74,7 @@ prompt_eval_duration?: number; total_duration?: number; load_duration?: number; + usage?: unknown; }; annotation?: { type: string; rating: number }; } @@ -621,30 +623,32 @@ fill="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" - > + + + + + {:else if speaking} + + + + + {:else} + ? message.info.usage + ? `
${sanitizeResponseContent(
+														JSON.stringify(message.info.usage, null, 2)
+															.replace(/"([^(")"]+)":/g, '$1:')
+															.slice(1, -1)
+															.split('\n')
+															.map((line) => line.slice(2))
+															.map((line) => (line.endsWith(',') ? line.slice(0, -1) : line))
+															.join('\n')
+													)}
` + : `prompt_tokens: ${message.info.prompt_tokens ?? 'N/A'}
completion_tokens: ${message.info.completion_tokens ?? 'N/A'}
total_tokens: ${message.info.total_tokens ?? 'N/A'}` : `response_token/s: ${ @@ -854,10 +870,11 @@ stroke-linejoin="round" class="w-4 h-4" xmlns="http://www.w3.org/2000/svg" - > + + @@ -903,10 +920,11 @@ stroke-linejoin="round" class="w-4 h-4" xmlns="http://www.w3.org/2000/svg" - > + + {/if} @@ -1079,6 +1097,7 @@ -ms-overflow-style: none; /* IE and Edge */ scrollbar-width: none; /* Firefox */ } + @keyframes shimmer { 0% { background-position: 200% 0;