Model Selection Feature¶
Overview¶
The chat application now supports switching between different Google Vertex AI models in real-time through a UI selector. This allows users to choose the most appropriate model for their use case without modifying configuration files.
Available Models¶
The following Vertex AI models are currently supported:
- Gemini 1.5 Flash (
gemini-1.5-flash-002) - Default - Fast and efficient model for most tasks
-
Best for: Quick responses, high throughput scenarios
-
Gemini 1.5 Pro (
gemini-1.5-pro-002) - Most capable model for complex reasoning
-
Best for: Complex analysis, detailed explanations
-
Gemini 1.0 Pro (
gemini-1.0-pro) - Previous generation model
-
Best for: Stable, tested use cases
-
Gemini 1.0 Pro Vision (
gemini-1.0-pro-vision) - Multimodal model with vision capabilities
- Best for: Image analysis and multimodal interactions
Architecture¶
Components¶
1. Model Constants (src/lib/constants/vertex-ai-models.ts)¶
Defines all available models with their metadata:
export const VERTEX_AI_MODELS = {
"gemini-1.5-flash-002": {
id: "gemini-1.5-flash-002",
name: "Gemini 1.5 Flash",
description: "Fast and efficient model for most tasks",
},
// ... other models
};
2. ModelSelector Component (src/components/chat/model-selector.tsx)¶
A client component that provides the UI for model selection:
- Uses shadcn/ui Select component (Radix UI)
- Displays model names and descriptions
- Disables during active chat requests
- Follows accessibility best practices
3. Updated ChatService (src/lib/services/chat-service.ts)¶
Now accepts an optional modelId parameter:
4. Updated API Route (src/app/api/chat/route.ts)¶
Validates and passes the selected model to the ChatService:
const { messages, modelId } = parsedBody.data;
const stream = await chatService.stream(messages, modelId);
5. Updated useChat Hook (src/lib/hooks/use-chat.ts)¶
Manages the selected model state and includes it in API requests:
Data Flow¶
- User selects a model from the dropdown
setSelectedModelupdates the state inuseChathook- On message submission, the selected model is sent to
/api/chat - API validates the model ID against the schema
- ChatService uses the specified model for the Vertex AI API call
- Response streams back to the user
Type Safety¶
The implementation uses TypeScript's type system to ensure type safety:
This ensures that only valid model IDs can be used throughout the application.
Validation¶
The chat request schema validates the model ID:
export const chatRequestSchema = z.object({
messages: z.array(/* ... */),
modelId: z.enum(Object.keys(VERTEX_AI_MODELS)).optional(),
});
UI/UX Considerations¶
- Placement: The model selector is placed at the top of the chat interface, above the message history
- Disabled State: The selector is disabled during active requests to prevent mid-conversation model changes
- Visual Feedback: Each model shows both a name and description to help users make informed choices
- Default Selection: The default model (Gemini 1.5 Flash) is pre-selected on first load
Testing¶
Unit Tests¶
- Model Constants (
tests/unit/vertex-ai-models.test.ts) - Validates model structure
- Ensures default model exists
-
Verifies model metadata
-
ModelSelector Component (
tests/unit/model-selector.test.tsx) - Tests rendering with selected model
- Tests model change callback
- Tests disabled state
-
Tests accessibility features
-
ChatService (
tests/unit/chat-service.test.ts) - Tests custom model ID usage
- Tests fallback to default model
- Tests model parameter passing
Storybook¶
The ModelSelector component has Storybook stories for:
- Default state
- Different model selections
- Disabled state
Configuration¶
Environment Variables¶
The GOOGLE_VERTEX_AI_MODEL_ID environment variable is still used as a fallback:
- If a model ID is provided via the UI, it takes precedence
- If no model ID is provided, the environment variable is used
- If neither is available,
DEFAULT_MODEL_IDis used
Adding New Models¶
To add a new model:
- Update
VERTEX_AI_MODELSinsrc/lib/constants/vertex-ai-models.ts:
export const VERTEX_AI_MODELS = {
// ... existing models
"new-model-id": {
id: "new-model-id",
name: "Model Name",
description: "Model description",
},
} as const;
- Update tests to include the new model
- Update documentation
The model will automatically appear in the dropdown.
Security Considerations¶
- Validation: All model IDs are validated against the whitelist
- Type Safety: TypeScript ensures only valid model IDs can be used
- No User Input: Model IDs come from predefined constants, not user input
Performance¶
- Model selection is instant (no API calls required)
- State is managed locally in React
- No additional network requests for model switching
Accessibility¶
- Proper ARIA labels on the select component
- Keyboard navigation support
- Screen reader friendly descriptions
- Disabled state clearly indicated
Future Enhancements¶
Potential improvements:
- Persist selected model in localStorage or user preferences
- Show model capabilities/limitations in the UI
- Add model cost information
- Support for model-specific parameters (temperature, top-k, etc.)
- Model performance metrics and comparisons
- Auto-select best model based on query type