diff --git a/src/lib/apis/openai/index.ts b/src/lib/apis/openai/index.ts index e38314a55..7d0d9415e 100644 --- a/src/lib/apis/openai/index.ts +++ b/src/lib/apis/openai/index.ts @@ -263,3 +263,53 @@ export const synthesizeOpenAISpeech = async ( return res; }; + +export const generateTitle = async ( + token: string = '', + template: string, + model: string, + prompt: string, + url: string = OPENAI_API_BASE_URL +) => { + let error = null; + + template = template.replace(/{{prompt}}/g, prompt); + + console.log(template); + + const res = await fetch(`${url}/chat/completions`, { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}` + }, + body: JSON.stringify({ + model: model, + messages: [ + { + role: 'user', + content: template + } + ], + stream: false + }) + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + console.log(err); + if ('detail' in err) { + error = err.detail; + } + return null; + }); + + if (error) { + throw error; + } + + return res?.choices[0]?.message?.content ?? 'New Chat'; +}; diff --git a/src/lib/components/chat/Settings/Interface.svelte b/src/lib/components/chat/Settings/Interface.svelte index 2d5862f40..ad9e05e7f 100644 --- a/src/lib/components/chat/Settings/Interface.svelte +++ b/src/lib/components/chat/Settings/Interface.svelte @@ -1,7 +1,7 @@ @@ -190,8 +203,9 @@
{$i18n.t('Set Title Auto-Generation Model')}
-
-
+
+
+
Local Models
+ + {#each $models as model} + {#if model.name !== 'hr'} + + {/if} + {/each} + +
diff --git a/src/routes/(app)/+page.svelte b/src/routes/(app)/+page.svelte index 417ddccda..2ccdd8d5a 100644 --- a/src/routes/(app)/+page.svelte +++ b/src/routes/(app)/+page.svelte @@ -19,7 +19,7 @@ } from '$lib/stores'; import { copyToClipboard, splitStream } from '$lib/utils'; - import { generateChatCompletion, cancelOllamaRequest, generateTitle } from '$lib/apis/ollama'; + import { generateChatCompletion, cancelOllamaRequest } from '$lib/apis/ollama'; import { addTagById, createNewChat, @@ -30,14 +30,14 @@ updateChatById } from '$lib/apis/chats'; import { queryCollection, queryDoc } from '$lib/apis/rag'; - import { generateOpenAIChatCompletion } from '$lib/apis/openai'; + import { generateOpenAIChatCompletion, generateTitle } from '$lib/apis/openai'; import MessageInput from '$lib/components/chat/MessageInput.svelte'; import Messages from '$lib/components/chat/Messages.svelte'; import ModelSelector from '$lib/components/chat/ModelSelector.svelte'; import Navbar from '$lib/components/layout/Navbar.svelte'; import { RAGTemplate } from '$lib/utils/rag'; - import { LITELLM_API_BASE_URL, OPENAI_API_BASE_URL } from '$lib/constants'; + import { LITELLM_API_BASE_URL, OLLAMA_API_BASE_URL, OPENAI_API_BASE_URL } from '$lib/constants'; import { WEBUI_BASE_URL } from '$lib/constants'; const i18n = getContext('i18n'); @@ -511,7 +511,8 @@ if (messages.length == 2 && messages.at(1).content !== '') { window.history.replaceState(history.state, '', `/c/${_chatId}`); - await generateChatTitle(_chatId, userPrompt); + const _title = await generateChatTitle(userPrompt); + await setChatTitle(_chatId, _title); } }; @@ -696,11 +697,8 @@ if (messages.length == 2) { window.history.replaceState(history.state, '', `/c/${_chatId}`); - if ($settings?.titleAutoGenerateModel) { - await generateChatTitle(_chatId, userPrompt); - } else { - await setChatTitle(_chatId, userPrompt); - } + const _title = await generateChatTitle(userPrompt); + await setChatTitle(_chatId, _title); } }; @@ -754,23 +752,35 @@ } }; - const generateChatTitle = async (_chatId, userPrompt) => { - if ($settings.titleAutoGenerate ?? true) { + const generateChatTitle = async (userPrompt) => { + if ($settings?.title?.auto ?? true) { + const model = $models.find((model) => model.id === selectedModels[0]); + + const titleModelId = + model?.external ?? false + ? $settings?.title?.modelExternal ?? selectedModels[0] + : $settings?.title?.model ?? selectedModels[0]; + const titleModel = $models.find((model) => model.id === titleModelId); + + console.log(titleModel); const title = await generateTitle( localStorage.token, - $settings?.titleGenerationPrompt ?? + $settings?.title?.prompt ?? $i18n.t( "Create a concise, 3-5 word phrase as a header for the following query, strictly adhering to the 3-5 word limit and avoiding the use of the word 'title':" ) + ' {{prompt}}', - $settings?.titleAutoGenerateModel ?? selectedModels[0], - userPrompt + titleModelId, + userPrompt, + titleModel?.external ?? false + ? titleModel.source === 'litellm' + ? `${LITELLM_API_BASE_URL}/v1` + : `${OPENAI_API_BASE_URL}` + : `${OLLAMA_API_BASE_URL}/v1` ); - if (title) { - await setChatTitle(_chatId, title); - } + return title; } else { - await setChatTitle(_chatId, `${userPrompt}`); + return `${userPrompt}`; } };