From 4d6489366112762c192c586d592a256722f321e7 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Mon, 27 May 2024 14:55:08 -0700 Subject: [PATCH] refac: styling --- src/lib/apis/openai/index.ts | 14 ----- src/lib/components/chat/Chat.svelte | 36 ++++++----- .../chat/Messages/ResponseMessage.svelte | 19 ++++-- .../ResponseMessage/WebSearchResults.svelte | 60 +++++++++++++++++++ src/lib/components/icons/ChevronUp.svelte | 15 +++++ 5 files changed, 110 insertions(+), 34 deletions(-) create mode 100644 src/lib/components/chat/Messages/ResponseMessage/WebSearchResults.svelte create mode 100644 src/lib/components/icons/ChevronUp.svelte diff --git a/src/lib/apis/openai/index.ts b/src/lib/apis/openai/index.ts index 05b9162da..22c458d30 100644 --- a/src/lib/apis/openai/index.ts +++ b/src/lib/apis/openai/index.ts @@ -403,18 +403,12 @@ export const generateSearchQuery = async ( let error = null; // TODO: Allow users to specify the prompt - // Get the current date in the format "January 20, 2024" const currentDate = new Intl.DateTimeFormat('en-US', { year: 'numeric', month: 'long', day: '2-digit' }).format(new Date()); - const yesterdayDate = new Intl.DateTimeFormat('en-US', { - year: 'numeric', - month: 'long', - day: '2-digit' - }).format(new Date()); const res = await fetch(`${url}/chat/completions`, { method: 'POST', @@ -431,14 +425,6 @@ export const generateSearchQuery = async ( role: 'assistant', content: `You are tasked with generating web search queries. Give me an appropriate query to answer my question for google search. Answer with only the query. Today is ${currentDate}.` }, - { - role: 'user', - content: 'What is the current weather in Paris?' - }, - { - role: 'assistant', - content: `Weather in Paris ${currentDate}` - }, { role: 'user', content: prompt diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 3115ae8ae..c3ff70903 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -434,17 +434,21 @@ }; messages = messages; - const searchQuery = await generateChatSearchQuery(model, parentId); - if (!searchQuery) { - toast.warning($i18n.t('No search query generated')); - responseMessage.status = { - ...responseMessage.status, - done: true, - error: true, - description: 'No search query generated' - }; - messages = messages; - return; + const prompt = history.messages[parentId].content; + let searchQuery = prompt; + if (prompt.length > 100) { + searchQuery = await generateChatSearchQuery(model, prompt); + if (!searchQuery) { + toast.warning($i18n.t('No search query generated')); + responseMessage.status = { + ...responseMessage.status, + done: true, + error: true, + description: 'No search query generated' + }; + messages = messages; + return; + } } responseMessage.status = { @@ -469,7 +473,8 @@ responseMessage.status = { ...responseMessage.status, done: true, - description: $i18n.t('Searched {{count}} sites', { count: results.filenames.length }) + description: $i18n.t('Searched {{count}} sites', { count: results.filenames.length }), + urls: results.filenames }; if (responseMessage?.files ?? undefined === undefined) { @@ -1034,15 +1039,14 @@ } }; - const generateChatSearchQuery = async (modelId: string, messageId: string) => { + const generateChatSearchQuery = async (modelId: string, prompt: string) => { const model = $models.find((model) => model.id === modelId); const taskModelId = model?.owned_by === 'openai' ?? false ? $settings?.title?.modelExternal ?? modelId : $settings?.title?.model ?? modelId; const taskModel = $models.find((model) => model.id === taskModelId); - const userMessage = history.messages[messageId]; - const userPrompt = userMessage.content; + const previousMessages = messages .filter((message) => message.role === 'user') .map((message) => message.content); @@ -1051,7 +1055,7 @@ localStorage.token, taskModelId, previousMessages, - userPrompt, + prompt, taskModel?.owned_by === 'openai' ?? false ? `${OPENAI_API_BASE_URL}` : `${OLLAMA_API_BASE_URL}/v1` diff --git a/src/lib/components/chat/Messages/ResponseMessage.svelte b/src/lib/components/chat/Messages/ResponseMessage.svelte index 9b4d7d1a7..9dd525690 100644 --- a/src/lib/components/chat/Messages/ResponseMessage.svelte +++ b/src/lib/components/chat/Messages/ResponseMessage.svelte @@ -34,6 +34,7 @@ import RateComment from './RateComment.svelte'; import CitationsModal from '$lib/components/chat/Messages/CitationsModal.svelte'; import Spinner from '$lib/components/common/Spinner.svelte'; + import WebSearchResults from './ResponseMessage/WebSearchResults.svelte'; export let message; export let siblings; @@ -389,11 +390,21 @@ {/if} -
-
- {message.status.description} + {#if message?.status?.action === 'web_search' && message?.status?.urls} + +
+
+ {message.status.description} +
+
+
+ {:else} +
+
+ {message.status.description} +
-
+ {/if}
{/if} diff --git a/src/lib/components/chat/Messages/ResponseMessage/WebSearchResults.svelte b/src/lib/components/chat/Messages/ResponseMessage/WebSearchResults.svelte new file mode 100644 index 000000000..454b94c16 --- /dev/null +++ b/src/lib/components/chat/Messages/ResponseMessage/WebSearchResults.svelte @@ -0,0 +1,60 @@ + + + + +
+ + + {#if state} + + {:else} + + {/if} +
+
+ + + {#each urls as url, urlIdx} + + {url} + +
+ + + + +
+
+ {/each} +
+
diff --git a/src/lib/components/icons/ChevronUp.svelte b/src/lib/components/icons/ChevronUp.svelte new file mode 100644 index 000000000..6fe7ca1cc --- /dev/null +++ b/src/lib/components/icons/ChevronUp.svelte @@ -0,0 +1,15 @@ + + + + +