Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,45 @@ import com.google.ai.sample.feature.chat.ChatViewModel
import com.google.ai.sample.feature.multimodal.PhotoReasoningViewModel
import com.google.ai.sample.feature.text.SummarizeViewModel

// Model options
enum class ModelOption(val displayName: String, val modelName: String) {
GEMINI_FLASH_LITE("Gemini Flash Lite", "gemini-2.0-flash-lite"),
GEMINI_FLASH("Gemini Flash", "gemini-2.0-flash"),
GEMINI_FLASH_PREVIEW("Gemini 2.5 Flash Preview", "gemini-2.5-flash-preview-04-17"),
GEMINI_PRO("Gemini 2.5 Pro", "gemini-2.5-pro-exp-03-25")
}

val GenerativeViewModelFactory = object : ViewModelProvider.Factory {
// Current selected model name
private var currentModelName = "gemini-2.0-flash-lite"
private var currentModelName = ModelOption.GEMINI_FLASH_LITE.modelName

/**
* Set the model to high reasoning capability (gemini-2.5-pro-preview-03-25)
*/
fun highReasoningModel() {
currentModelName = "gemini-2.5-pro-exp-03-25"
currentModelName = ModelOption.GEMINI_PRO.modelName
}

/**
* Set the model to low reasoning capability (gemini-2.0-flash-lite)
*/
fun lowReasoningModel() {
currentModelName = "gemini-2.0-flash-lite"
currentModelName = ModelOption.GEMINI_FLASH_LITE.modelName
}

/**
* Set the model to a specific model option
*/
fun setModel(modelOption: ModelOption) {
currentModelName = modelOption.modelName
}

/**
* Get the current model option
*/
fun getCurrentModel(): ModelOption {
return ModelOption.values().find { it.modelName == currentModelName }
?: ModelOption.GEMINI_FLASH_LITE
}

override fun <T : ViewModel> create(
Expand Down Expand Up @@ -93,13 +116,13 @@ val GenerativeViewModelFactory = object : ViewModelProvider.Factory {
// Add companion object with static methods for easier access
object GenerativeAiViewModelFactory {
// Current selected model name - duplicated from GenerativeViewModelFactory
private var currentModelName = "gemini-2.0-flash-lite"
private var currentModelName = ModelOption.GEMINI_FLASH_LITE.modelName

/**
* Set the model to high reasoning capability (gemini-2.5-pro-preview-03-25)
*/
fun highReasoningModel() {
currentModelName = "gemini-2.5-pro-exp-03-25"
currentModelName = ModelOption.GEMINI_PRO.modelName
// Also update the original factory to keep them in sync
(GenerativeViewModelFactory as ViewModelProvider.Factory).apply {
if (this is ViewModelProvider.Factory) {
Expand All @@ -118,7 +141,26 @@ object GenerativeAiViewModelFactory {
* Set the model to low reasoning capability (gemini-2.0-flash-lite)
*/
fun lowReasoningModel() {
currentModelName = "gemini-2.0-flash-lite"
currentModelName = ModelOption.GEMINI_FLASH_LITE.modelName
// Also update the original factory to keep them in sync
(GenerativeViewModelFactory as ViewModelProvider.Factory).apply {
if (this is ViewModelProvider.Factory) {
try {
val field = this.javaClass.getDeclaredField("currentModelName")
field.isAccessible = true
field.set(this, currentModelName)
} catch (e: Exception) {
// Fallback if reflection fails
}
}
}
}

/**
* Set the model to a specific model option
*/
fun setModel(modelOption: ModelOption) {
currentModelName = modelOption.modelName
// Also update the original factory to keep them in sync
(GenerativeViewModelFactory as ViewModelProvider.Factory).apply {
if (this is ViewModelProvider.Factory) {
Expand All @@ -132,4 +174,12 @@ object GenerativeAiViewModelFactory {
}
}
}

/**
* Get the current model option
*/
fun getCurrentModel(): ModelOption {
return ModelOption.values().find { it.modelName == currentModelName }
?: ModelOption.GEMINI_FLASH_LITE
}
}
80 changes: 80 additions & 0 deletions app/src/main/kotlin/com/google/ai/sample/MenuScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,24 @@ package com.google.ai.sample

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
Expand All @@ -50,6 +58,12 @@ fun MenuScreen(
MenuItem("photo_reasoning", R.string.menu_reason_title, R.string.menu_reason_description),
MenuItem("chat", R.string.menu_chat_title, R.string.menu_chat_description)
)

// Get current model
val currentModel = GenerativeAiViewModelFactory.getCurrentModel()
var selectedModel by remember { mutableStateOf(currentModel) }
var expanded by remember { mutableStateOf(false) }

LazyColumn(
Modifier
.padding(top = 16.dp, bottom = 16.dp)
Expand Down Expand Up @@ -82,6 +96,72 @@ fun MenuScreen(
}
}

// Model Selection
item {
Card(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 8.dp)
) {
Column(
modifier = Modifier
.padding(all = 16.dp)
.fillMaxWidth()
) {
Text(
text = "Model Selection",
style = MaterialTheme.typography.titleMedium
)

Spacer(modifier = Modifier.height(8.dp))

Text(
text = "Current model: ${selectedModel.displayName}",
style = MaterialTheme.typography.bodyMedium
)

Spacer(modifier = Modifier.height(8.dp))

Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
Spacer(modifier = Modifier.weight(1f))

Button(
onClick = { expanded = true }
) {
Text("Change Model")
}

DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
// Zeige die Modelle in der gewünschten Reihenfolge an
val orderedModels = listOf(
ModelOption.GEMINI_FLASH_LITE,
ModelOption.GEMINI_FLASH,
ModelOption.GEMINI_FLASH_PREVIEW,
ModelOption.GEMINI_PRO
)

orderedModels.forEach { modelOption ->
DropdownMenuItem(
text = { Text(modelOption.displayName) },
onClick = {
selectedModel = modelOption
GenerativeAiViewModelFactory.setModel(modelOption)
expanded = false
}
)
}
}
}
}
}
}

// Menu Items
items(menuItems) { menuItem ->
Card(
Expand Down