Skip to content

Commit 6842a2a

Browse files
Change llm menu (#11)
* Add files via upload * Add files via upload
1 parent 47efde3 commit 6842a2a

2 files changed

Lines changed: 136 additions & 6 deletions

File tree

app/src/main/kotlin/com/google/ai/sample/GenerativeAiViewModelFactory.kt

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,45 @@ import com.google.ai.sample.feature.chat.ChatViewModel
1010
import com.google.ai.sample.feature.multimodal.PhotoReasoningViewModel
1111
import com.google.ai.sample.feature.text.SummarizeViewModel
1212

13+
// Model options
14+
enum class ModelOption(val displayName: String, val modelName: String) {
15+
GEMINI_FLASH_LITE("Gemini Flash Lite", "gemini-2.0-flash-lite"),
16+
GEMINI_FLASH("Gemini Flash", "gemini-2.0-flash"),
17+
GEMINI_FLASH_PREVIEW("Gemini 2.5 Flash Preview", "gemini-2.5-flash-preview-04-17"),
18+
GEMINI_PRO("Gemini 2.5 Pro", "gemini-2.5-pro-exp-03-25")
19+
}
20+
1321
val GenerativeViewModelFactory = object : ViewModelProvider.Factory {
1422
// Current selected model name
15-
private var currentModelName = "gemini-2.0-flash-lite"
23+
private var currentModelName = ModelOption.GEMINI_FLASH_LITE.modelName
1624

1725
/**
1826
* Set the model to high reasoning capability (gemini-2.5-pro-preview-03-25)
1927
*/
2028
fun highReasoningModel() {
21-
currentModelName = "gemini-2.5-pro-exp-03-25"
29+
currentModelName = ModelOption.GEMINI_PRO.modelName
2230
}
2331

2432
/**
2533
* Set the model to low reasoning capability (gemini-2.0-flash-lite)
2634
*/
2735
fun lowReasoningModel() {
28-
currentModelName = "gemini-2.0-flash-lite"
36+
currentModelName = ModelOption.GEMINI_FLASH_LITE.modelName
37+
}
38+
39+
/**
40+
* Set the model to a specific model option
41+
*/
42+
fun setModel(modelOption: ModelOption) {
43+
currentModelName = modelOption.modelName
44+
}
45+
46+
/**
47+
* Get the current model option
48+
*/
49+
fun getCurrentModel(): ModelOption {
50+
return ModelOption.values().find { it.modelName == currentModelName }
51+
?: ModelOption.GEMINI_FLASH_LITE
2952
}
3053

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

98121
/**
99122
* Set the model to high reasoning capability (gemini-2.5-pro-preview-03-25)
100123
*/
101124
fun highReasoningModel() {
102-
currentModelName = "gemini-2.5-pro-exp-03-25"
125+
currentModelName = ModelOption.GEMINI_PRO.modelName
103126
// Also update the original factory to keep them in sync
104127
(GenerativeViewModelFactory as ViewModelProvider.Factory).apply {
105128
if (this is ViewModelProvider.Factory) {
@@ -118,7 +141,26 @@ object GenerativeAiViewModelFactory {
118141
* Set the model to low reasoning capability (gemini-2.0-flash-lite)
119142
*/
120143
fun lowReasoningModel() {
121-
currentModelName = "gemini-2.0-flash-lite"
144+
currentModelName = ModelOption.GEMINI_FLASH_LITE.modelName
145+
// Also update the original factory to keep them in sync
146+
(GenerativeViewModelFactory as ViewModelProvider.Factory).apply {
147+
if (this is ViewModelProvider.Factory) {
148+
try {
149+
val field = this.javaClass.getDeclaredField("currentModelName")
150+
field.isAccessible = true
151+
field.set(this, currentModelName)
152+
} catch (e: Exception) {
153+
// Fallback if reflection fails
154+
}
155+
}
156+
}
157+
}
158+
159+
/**
160+
* Set the model to a specific model option
161+
*/
162+
fun setModel(modelOption: ModelOption) {
163+
currentModelName = modelOption.modelName
122164
// Also update the original factory to keep them in sync
123165
(GenerativeViewModelFactory as ViewModelProvider.Factory).apply {
124166
if (this is ViewModelProvider.Factory) {
@@ -132,4 +174,12 @@ object GenerativeAiViewModelFactory {
132174
}
133175
}
134176
}
177+
178+
/**
179+
* Get the current model option
180+
*/
181+
fun getCurrentModel(): ModelOption {
182+
return ModelOption.values().find { it.modelName == currentModelName }
183+
?: ModelOption.GEMINI_FLASH_LITE
184+
}
135185
}

app/src/main/kotlin/com/google/ai/sample/MenuScreen.kt

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,24 @@ package com.google.ai.sample
1818

1919
import androidx.compose.foundation.layout.Column
2020
import androidx.compose.foundation.layout.Row
21+
import androidx.compose.foundation.layout.Spacer
2122
import androidx.compose.foundation.layout.fillMaxWidth
23+
import androidx.compose.foundation.layout.height
2224
import androidx.compose.foundation.layout.padding
2325
import androidx.compose.foundation.lazy.LazyColumn
2426
import androidx.compose.foundation.lazy.items
2527
import androidx.compose.material3.Button
2628
import androidx.compose.material3.Card
29+
import androidx.compose.material3.DropdownMenu
30+
import androidx.compose.material3.DropdownMenuItem
2731
import androidx.compose.material3.MaterialTheme
2832
import androidx.compose.material3.Text
2933
import androidx.compose.material3.TextButton
3034
import androidx.compose.runtime.Composable
35+
import androidx.compose.runtime.getValue
36+
import androidx.compose.runtime.mutableStateOf
37+
import androidx.compose.runtime.remember
38+
import androidx.compose.runtime.setValue
3139
import androidx.compose.ui.Alignment
3240
import androidx.compose.ui.Modifier
3341
import androidx.compose.ui.res.stringResource
@@ -50,6 +58,12 @@ fun MenuScreen(
5058
MenuItem("photo_reasoning", R.string.menu_reason_title, R.string.menu_reason_description),
5159
MenuItem("chat", R.string.menu_chat_title, R.string.menu_chat_description)
5260
)
61+
62+
// Get current model
63+
val currentModel = GenerativeAiViewModelFactory.getCurrentModel()
64+
var selectedModel by remember { mutableStateOf(currentModel) }
65+
var expanded by remember { mutableStateOf(false) }
66+
5367
LazyColumn(
5468
Modifier
5569
.padding(top = 16.dp, bottom = 16.dp)
@@ -82,6 +96,72 @@ fun MenuScreen(
8296
}
8397
}
8498

99+
// Model Selection
100+
item {
101+
Card(
102+
modifier = Modifier
103+
.fillMaxWidth()
104+
.padding(horizontal = 16.dp, vertical = 8.dp)
105+
) {
106+
Column(
107+
modifier = Modifier
108+
.padding(all = 16.dp)
109+
.fillMaxWidth()
110+
) {
111+
Text(
112+
text = "Model Selection",
113+
style = MaterialTheme.typography.titleMedium
114+
)
115+
116+
Spacer(modifier = Modifier.height(8.dp))
117+
118+
Text(
119+
text = "Current model: ${selectedModel.displayName}",
120+
style = MaterialTheme.typography.bodyMedium
121+
)
122+
123+
Spacer(modifier = Modifier.height(8.dp))
124+
125+
Row(
126+
modifier = Modifier.fillMaxWidth(),
127+
verticalAlignment = Alignment.CenterVertically
128+
) {
129+
Spacer(modifier = Modifier.weight(1f))
130+
131+
Button(
132+
onClick = { expanded = true }
133+
) {
134+
Text("Change Model")
135+
}
136+
137+
DropdownMenu(
138+
expanded = expanded,
139+
onDismissRequest = { expanded = false }
140+
) {
141+
// Zeige die Modelle in der gewünschten Reihenfolge an
142+
val orderedModels = listOf(
143+
ModelOption.GEMINI_FLASH_LITE,
144+
ModelOption.GEMINI_FLASH,
145+
ModelOption.GEMINI_FLASH_PREVIEW,
146+
ModelOption.GEMINI_PRO
147+
)
148+
149+
orderedModels.forEach { modelOption ->
150+
DropdownMenuItem(
151+
text = { Text(modelOption.displayName) },
152+
onClick = {
153+
selectedModel = modelOption
154+
GenerativeAiViewModelFactory.setModel(modelOption)
155+
expanded = false
156+
}
157+
)
158+
}
159+
}
160+
}
161+
}
162+
}
163+
}
164+
85165
// Menu Items
86166
items(menuItems) { menuItem ->
87167
Card(

0 commit comments

Comments
 (0)