fix(fastapi+nextjs): handles custom url model availability issue

This commit is contained in:
sauravniraula 2025-07-12 23:07:36 +05:45
parent a3383765b8
commit ee5eb060b9
No known key found for this signature in database
GPG key ID: 60FCC1B5A5E83326
4 changed files with 21 additions and 2 deletions

View file

@ -11,4 +11,6 @@ class ListAvailableCustomModelsHandler:
self.api_key = api_key
async def get(self, logging_service: LoggingService, log_metadata: LogMetadata):
print("-" * 40)
print(self.url, self.api_key)
return await list_available_custom_models(self.url, self.api_key)

View file

@ -42,6 +42,7 @@ async def list_available_custom_models(
client = openai.AsyncOpenAI(api_key=api_key or "null", base_url=url)
models = []
async for model in client.models.list():
print(model)
models.append(model.id)
return models

View file

@ -215,8 +215,14 @@ const SettingsPage = () => {
api_key: llmConfig.CUSTOM_LLM_API_KEY || ''
})
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
setCustomModels(data);
// Only set customModelsChecked to true if the API call succeeds
setCustomModelsChecked(true);
// Check if currently selected model is still available
@ -233,6 +239,8 @@ const SettingsPage = () => {
}
} catch (error) {
console.error('Error fetching custom models:', error);
// Don't set customModelsChecked to true on error, so the button remains visible
setCustomModels([]);
toast({
title: 'Error',
description: 'Failed to fetch available models. Please check your URL and API key.',
@ -639,7 +647,7 @@ const SettingsPage = () => {
{customModelsChecked && customModels.length === 0 && (
<div className="p-3 bg-yellow-50 border border-yellow-200 rounded-lg">
<p className="text-sm text-yellow-800">
No models found. Please check your URL and API key, or try again.
No models found. Please make sure models are available.
</p>
</div>
)}

View file

@ -322,11 +322,19 @@ export default function Home() {
api_key: llmConfig.CUSTOM_LLM_API_KEY || ''
})
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
setCustomModels(data);
// Only set customModelsChecked to true if the API call succeeds
setCustomModelsChecked(true);
} catch (error) {
console.error('Error fetching custom models:', error);
// Don't set customModelsChecked to true on error, so the button remains visible
setCustomModels([]);
toast({
title: 'Error',
description: 'Failed to fetch available models. Please check your URL and API key.',
@ -724,7 +732,7 @@ export default function Home() {
{customModelsChecked && customModels.length === 0 && (
<div className="mb-4 p-3 bg-yellow-50 border border-yellow-200 rounded-lg">
<p className="text-sm text-yellow-800">
No models found. Please check your URL and API key, or try again.
No models found. Please make sure models are available.
</p>
</div>
)}