feat(settings): conditionally pick selected models

This commit is contained in:
ItzCrazyKns 2024-05-06 20:00:56 +05:30
parent e8fe74ae7c
commit 4cb0aeeee3
No known key found for this signature in database
GPG Key ID: 8162927C7CCE3065
1 changed files with 44 additions and 15 deletions

View File

@ -33,12 +33,8 @@ const SettingsDialog = ({
const [selectedEmbeddingModel, setSelectedEmbeddingModel] = useState< const [selectedEmbeddingModel, setSelectedEmbeddingModel] = useState<
string | null string | null
>(null); >(null);
const [customOpenAIApiKey, setCustomOpenAIApiKey] = useState<string | null>( const [customOpenAIApiKey, setCustomOpenAIApiKey] = useState<string>('');
null, const [customOpenAIBaseURL, setCustomOpenAIBaseURL] = useState<string>('');
);
const [customOpenAIBaseURL, setCustomOpenAIBaseURL] = useState<string | null>(
null,
);
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [isUpdating, setIsUpdating] = useState(false); const [isUpdating, setIsUpdating] = useState(false);
@ -51,16 +47,49 @@ const SettingsDialog = ({
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
}); });
const data = await res.json();
const data = (await res.json()) as SettingsType;
setConfig(data); setConfig(data);
setSelectedChatModelProvider(localStorage.getItem('chatModelProvider'));
setSelectedChatModel(localStorage.getItem('chatModel')); const chatModelProvidersKeys = Object.keys(
setSelectedEmbeddingModelProvider( data.chatModelProviders || {},
localStorage.getItem('embeddingModelProvider'),
); );
setSelectedEmbeddingModel(localStorage.getItem('embeddingModel')); const embeddingModelProvidersKeys = Object.keys(
setCustomOpenAIApiKey(localStorage.getItem('openAIApiKey')); data.embeddingModelProviders || {},
setCustomOpenAIBaseURL(localStorage.getItem('openAIBaseUrl')); );
const defaultChatModelProvider =
chatModelProvidersKeys.length > 0 ? chatModelProvidersKeys[0] : '';
const defaultEmbeddingModelProvider =
embeddingModelProvidersKeys.length > 0
? embeddingModelProvidersKeys[0]
: '';
const chatModelProvider =
localStorage.getItem('chatModelProvider') ||
defaultChatModelProvider ||
'';
const chatModel =
localStorage.getItem('chatModel') ||
(data.chatModelProviders &&
data.chatModelProviders[chatModelProvider]?.[0]) ||
'';
const embeddingModelProvider =
localStorage.getItem('embeddingModelProvider') ||
defaultEmbeddingModelProvider ||
'';
const embeddingModel =
localStorage.getItem('embeddingModel') ||
(data.embeddingModelProviders &&
data.embeddingModelProviders[embeddingModelProvider]?.[0]) ||
'';
setSelectedChatModelProvider(chatModelProvider);
setSelectedChatModel(chatModel);
setSelectedEmbeddingModelProvider(embeddingModelProvider);
setSelectedEmbeddingModel(embeddingModel);
setCustomOpenAIApiKey(localStorage.getItem('openAIApiKey') || '');
setCustomOpenAIBaseURL(localStorage.getItem('openAIBaseUrl') || '');
setIsLoading(false); setIsLoading(false);
}; };
@ -223,7 +252,7 @@ const SettingsDialog = ({
</div> </div>
<div className="flex flex-col space-y-1"> <div className="flex flex-col space-y-1">
<p className="text-white/70 text-sm"> <p className="text-white/70 text-sm">
Custom OpenAI API Key (optional) Custom OpenAI API Key
</p> </p>
<input <input
type="text" type="text"