From b5acf34ef8c7cddebd0b6978a5ab8135e5cfb9dc Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Thu, 26 Dec 2024 18:59:57 +0530 Subject: [PATCH] feat(chat-window): fix bugs handling custom openai, closes #529 --- ui/components/ChatWindow.tsx | 46 +++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/ui/components/ChatWindow.tsx b/ui/components/ChatWindow.tsx index fd3d0a6a..62fa9dce 100644 --- a/ui/components/ChatWindow.tsx +++ b/ui/components/ChatWindow.tsx @@ -43,6 +43,14 @@ const useSocket = ( let embeddingModelProvider = localStorage.getItem( 'embeddingModelProvider', ); + let openAIBaseURL = + chatModelProvider === 'custom_openai' + ? localStorage.getItem('openAIBaseURL') + : null; + let openAIPIKey = + chatModelProvider === 'custom_openai' + ? localStorage.getItem('openAIApiKey') + : null; const providers = await fetch( `${process.env.NEXT_PUBLIC_API_URL}/models`, @@ -62,16 +70,18 @@ const useSocket = ( if (!chatModel || !chatModelProvider) { const chatModelProviders = providers.chatModelProviders; - chatModelProvider = Object.keys(chatModelProviders)[0]; + chatModelProvider = + chatModelProvider || Object.keys(chatModelProviders)[0]; if (chatModelProvider === 'custom_openai') { toast.error( - 'Seems like you are using the custom OpenAI provider, please open the settings and configure the API key and base URL', + 'Seems like you are using the custom OpenAI provider, please open the settings and enter a model name to use.', ); setError(true); return; } else { chatModel = Object.keys(chatModelProviders[chatModelProvider])[0]; + if ( !chatModelProviders || Object.keys(chatModelProviders).length === 0 @@ -108,18 +118,42 @@ const useSocket = ( if ( Object.keys(chatModelProviders).length > 0 && - !chatModelProviders[chatModelProvider] + (((!openAIBaseURL || !openAIPIKey) && + chatModelProvider === 'custom_openai') || + !chatModelProviders[chatModelProvider]) ) { - chatModelProvider = Object.keys(chatModelProviders)[0]; + const chatModelProvidersKeys = Object.keys(chatModelProviders); + chatModelProvider = + chatModelProvidersKeys.find( + (key) => Object.keys(chatModelProviders[key]).length > 0, + ) || chatModelProvidersKeys[0]; + + if ( + chatModelProvider === 'custom_openai' && + (!openAIBaseURL || !openAIPIKey) + ) { + toast.error( + 'Seems like you are using the custom OpenAI provider, please open the settings and configure the API key and base URL', + ); + setError(true); + return; + } + localStorage.setItem('chatModelProvider', chatModelProvider); } if ( chatModelProvider && - chatModelProvider != 'custom_openai' && + (!openAIBaseURL || !openAIPIKey) && !chatModelProviders[chatModelProvider][chatModel] ) { - chatModel = Object.keys(chatModelProviders[chatModelProvider])[0]; + chatModel = Object.keys( + chatModelProviders[ + Object.keys(chatModelProviders[chatModelProvider]).length > 0 + ? chatModelProvider + : Object.keys(chatModelProviders)[0] + ], + )[0]; localStorage.setItem('chatModel', chatModel); }