File tree Expand file tree Collapse file tree 2 files changed +54
-0
lines changed
firebase-ai/app/src/main/java/com/google/firebase/quickstart/ai Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Original file line number Diff line number Diff line change 1+ package com.google.firebase.quickstart.ai.feature.text
2+
3+ import com.google.firebase.Firebase
4+ import com.google.firebase.ai.Chat
5+ import com.google.firebase.ai.ai
6+ import com.google.firebase.ai.type.Content
7+ import com.google.firebase.ai.type.GenerativeBackend
8+ import com.google.firebase.ai.type.content
9+ import com.google.firebase.quickstart.ai.ui.UiChatMessage
10+ import kotlinx.serialization.Serializable
11+
12+ @Serializable
13+ object TranslationRoute
14+
15+ class TranslationViewModel : ChatViewModel () {
16+ override val initialPrompt: String
17+ get() = """
18+ Translate the following text to Spanish:
19+ Hey, are you down to grab some pizza later? I'm starving!
20+ """ .trimIndent()
21+
22+ private var chat: Chat
23+
24+ init {
25+ val generativeModel = Firebase .ai(
26+ backend = GenerativeBackend .googleAI()
27+ ).generativeModel(
28+ modelName = " gemini-3.1-flash-lite-preview" ,
29+ systemInstruction = content {
30+ text(" Only output the translated text" )
31+ }
32+ )
33+
34+ chat = generativeModel.startChat()
35+ }
36+
37+ override suspend fun performSendMessage (
38+ prompt : Content ,
39+ currentMessages : List <UiChatMessage >
40+ ) {
41+ val response = chat.sendMessage(prompt)
42+ validateAndDisplayResponse(response, currentMessages)
43+ }
44+ }
Original file line number Diff line number Diff line change @@ -36,6 +36,8 @@ import com.google.firebase.quickstart.ai.feature.text.SvgRoute
3636import com.google.firebase.quickstart.ai.feature.text.SvgViewModel
3737import com.google.firebase.quickstart.ai.feature.text.ThinkingChatRoute
3838import com.google.firebase.quickstart.ai.feature.text.ThinkingChatViewModel
39+ import com.google.firebase.quickstart.ai.feature.text.TranslationRoute
40+ import com.google.firebase.quickstart.ai.feature.text.TranslationViewModel
3941import com.google.firebase.quickstart.ai.feature.text.TravelTipsRoute
4042import com.google.firebase.quickstart.ai.feature.text.TravelTipsViewModel
4143import com.google.firebase.quickstart.ai.feature.text.VideoHashtagGeneratorRoute
@@ -46,6 +48,14 @@ import com.google.firebase.quickstart.ai.feature.text.WeatherChatRoute
4648import com.google.firebase.quickstart.ai.feature.text.WeatherChatViewModel
4749
4850val FIREBASE_AI_SAMPLES = listOf (
51+ Sample (
52+ title = " Translate text" ,
53+ description = " Use Gemini 3.1 Flash-Lite to translate text" ,
54+ route = TranslationRoute ,
55+ screenType = ScreenType .CHAT ,
56+ viewModelClass = TranslationViewModel ::class ,
57+ categories = listOf (Category .TEXT )
58+ ),
4959 Sample (
5060 title = " Travel tips" ,
5161 description = " The user wants the model to help a new traveler" +
You can’t perform that action at this time.
0 commit comments