This document describes the dynamic model fetching feature implemented in MultiGPT.
The application now supports dynamically fetching available models from AI provider APIs instead of relying solely on hardcoded model lists. This ensures users always have access to the latest models available from each provider.
-
ModelResponse.kt - Data Transfer Objects (DTOs)
OpenAIModelsResponse&OpenAIModel- For OpenAI and OpenAI-compatible APIs (OpenAI, Groq)GoogleModelsResponse&GoogleModel- For Google AI PlatformOllamaModelsResponse&OllamaModel- For Ollama local modelsModelInfo- Unified model representationModelFetchResult- Sealed class for handling fetch states (Loading, Success, Error)
-
ModelFetchService.kt - Service Layer
ModelFetchService- Interface defining model fetching contractModelFetchServiceImpl- Implementation handling API calls to different providers- Supports: OpenAI, Anthropic, Google, Groq, and Ollama
-
ModelFetchModule.kt - Dependency Injection
- Dagger Hilt module providing ModelFetchService as a singleton
-
SetupViewModel.kt - ViewModel Enhancement
- Added
fetchModelsForPlatform()- Triggers model fetching for a specific platform - Added
modelFetchState- StateFlow tracking fetch operation status - Added
fetchedModels- StateFlow storing retrieved models - Added
getFallbackModels()- Returns hardcoded models as fallback
- Added
-
SelectModelScreen.kt - UI Enhancement
- Automatically fetches models when screen is displayed
- Shows loading indicator during fetch
- Displays error message with retry option on failure
- Falls back to hardcoded models if fetch fails
- Uses fetched models when available
- User navigates to model selection screen
LaunchedEffecttriggersfetchModelsForPlatform()for the current platform- ViewModel updates
modelFetchStatetoLoading - Service makes API call to provider's models endpoint
- On success:
- Models are parsed and transformed to
ModelInfo modelFetchStateupdated toSuccessfetchedModelsupdated with retrieved models- UI displays fetched models
- Models are parsed and transformed to
- On failure:
modelFetchStateupdated toErrorwith message- UI shows error with retry button
- UI falls back to hardcoded models
- Endpoint:
{apiUrl}/models - Authentication: Bearer token (API key)
- Filters: Only includes models containing "gpt" in the ID
- Sorting: By creation date (newest first)
- Endpoint:
{apiUrl}/models - Authentication: Bearer token (API key)
- Uses OpenAI-compatible format
- Sorting: By creation date (newest first)
- Endpoint:
{apiUrl}/v1beta/models?key={apiKey} - Authentication: API key in query parameter
- Filters: Only includes models supporting "generateContent"
- Extracts model ID from resource name
- No public models endpoint available
- Returns updated hardcoded list of known models
- Includes Claude 3.5 Sonnet, Haiku, Opus models
- Endpoint:
{apiUrl}/api/tags - Authentication: None required (local)
- Returns locally installed models
- Displays model size in description
- Always Up-to-Date: Users automatically get access to new models as providers release them
- Better UX: Real-time model availability based on user's API credentials
- Fallback Support: Graceful degradation to hardcoded models if fetch fails
- Provider Flexibility: Easy to add support for new providers
- Error Handling: Clear feedback when fetching fails with retry option
- Shows "Fetching models…" message with spinner
- Non-blocking - user can still interact with the screen
- Seamlessly displays fetched models
- Models show provider-specific information (owner, description, size)
- Maintains custom model option for flexibility
- Displays error message explaining the issue
- Provides "Retry" button to attempt fetch again
- Shows "Using fallback models" message
- Continues showing hardcoded models for selection
-
OpenAI Model Fetching
- Enter valid OpenAI API key
- Navigate to OpenAI model selection
- Verify models are fetched and displayed
- Check that new models (if any) appear in the list
-
Error Handling
- Enter invalid API key
- Navigate to model selection
- Verify error message appears
- Click retry button
- Verify fallback models are still selectable
-
Ollama Model Fetching
- Set up local Ollama instance
- Enter Ollama API URL
- Navigate to Ollama model selection
- Verify locally installed models appear
-
Network Timeout
- Disable network connection
- Navigate to model selection
- Verify timeout is handled gracefully
- Verify fallback models work
- Caching: Implement persistent caching to avoid repeated API calls
- Background Refresh: Periodically refresh model lists in the background
- Model Metadata: Display more detailed model information (context window, pricing, etc.)
- Model Search: Add search/filter functionality for providers with many models
- Model Comparison: Allow users to compare model capabilities side-by-side
- Network timeout: 5 minutes (configured in NetworkClient.kt)
- Suitable for slower connections and larger model lists
All error messages are localized:
fetching_models- "Fetching models…"using_fallback_models- "Using fallback models"retry- "Retry"
- All network operations run on
Dispatchers.IO - StateFlow ensures thread-safe state management
- LaunchedEffect manages lifecycle-aware fetching
- Uses existing NetworkClient with JSON serialization
- Automatic error handling and retry logic
- Supports multiple transport mechanisms
- ModelFetchService is provided as singleton via Hilt
- Automatically injected into SetupViewModel
- Easy to mock for testing
- Verify API key is valid and has proper permissions
- Check network connectivity
- Verify API URL is correct
- Check Ktor client logs for detailed error messages
- Ensure fetched models have higher priority than fallback
- Check provider-specific filtering logic
- Verify model response parsing
- Consider implementing caching
- Reduce network timeout if needed
- Implement pagination for providers with many models
| Provider | Endpoint | Authentication |
|---|---|---|
| OpenAI | {apiUrl}/models |
Bearer {api_key} |
| Groq | {apiUrl}/models |
Bearer {api_key} |
{apiUrl}/v1beta/models?key={api_key} |
Query parameter | |
| Anthropic | N/A (hardcoded) | N/A |
| Ollama | {apiUrl}/api/tags |
None |
- 1.0.0 (2024-11-30): Initial implementation
- Dynamic model fetching for all providers
- Loading and error states
- Fallback to hardcoded models
- Retry functionality