File tree 4 files changed +22
-17
lines changed
ai/app/src/main/java/com/google/firebase/quickstart/ai
4 files changed +22
-17
lines changed Original file line number Diff line number Diff line change @@ -38,6 +38,8 @@ import androidx.compose.ui.text.input.KeyboardCapitalization
38
38
import androidx.compose.ui.unit.dp
39
39
import androidx.lifecycle.compose.collectAsStateWithLifecycle
40
40
import androidx.lifecycle.viewmodel.compose.viewModel
41
+ import com.google.firebase.vertexai.type.Content
42
+ import com.google.firebase.vertexai.type.asTextOrNull
41
43
import java.util.UUID
42
44
import kotlinx.coroutines.launch
43
45
import kotlinx.serialization.Serializable
@@ -51,7 +53,12 @@ data class ChatMessage(
51
53
var text : String = " " ,
52
54
val participant : Participant = Participant .USER ,
53
55
var isPending : Boolean = false
54
- )
56
+ ) {
57
+ constructor (content: Content ) : this (
58
+ text = content.parts.first().asTextOrNull() ? : " " ,
59
+ participant = if (content.role == " user" ) Participant .USER else Participant .MODEL
60
+ )
61
+ }
55
62
56
63
@Serializable
57
64
class ChatRoute (val sampleId : String )
@@ -69,7 +76,6 @@ fun ChatScreen(
69
76
Column (
70
77
modifier = Modifier
71
78
.fillMaxSize()
72
- .background(MaterialTheme .colorScheme.errorContainer)
73
79
) {
74
80
ChatList (
75
81
messages,
Original file line number Diff line number Diff line change @@ -23,26 +23,21 @@ class ChatViewModel(
23
23
private val sample = FIREBASE_AI_SAMPLES .first { it.id == sampleId }
24
24
val initialPrompt = sample.initialPrompt?.parts?.first()?.asTextOrNull().orEmpty()
25
25
26
- private val _messageList : MutableList <ChatMessage > = sample.chatHistory.map { content ->
27
- ChatMessage (
28
- text = content.parts.first().asTextOrNull() ? : " " ,
29
- participant = if (content.role == " user" ) Participant .USER else Participant .MODEL ,
30
- isPending = false
31
- )
32
- }.toMutableStateList()
26
+ private val _messageList : MutableList <ChatMessage > =
27
+ sample.chatHistory.map { ChatMessage (it) }.toMutableStateList()
33
28
private val _messages = MutableStateFlow <List <ChatMessage >>(_messageList )
34
29
val messages: StateFlow <List <ChatMessage >> =
35
30
_messages
36
31
37
-
38
32
private val generativeModel: GenerativeModel
39
33
private val chat: Chat
40
34
41
35
init {
42
36
generativeModel = Firebase .vertexAI.generativeModel(
43
- " gemini-2.0-flash"
37
+ modelName = " gemini-2.0-flash" ,
38
+ systemInstruction = sample.systemInstructions
44
39
)
45
- chat = generativeModel.startChat()
40
+ chat = generativeModel.startChat(sample.chatHistory )
46
41
}
47
42
48
43
fun sendMessage (userMessage : String ) {
Original file line number Diff line number Diff line change @@ -9,10 +9,14 @@ val FIREBASE_AI_SAMPLES = listOf(
9
9
" travel tips for traveling." ,
10
10
navRoute = " chat" ,
11
11
categories = listOf (Category .TEXT ),
12
- systemInstructions = " You are a Travel assistant. You will answer" +
13
- " questions the user asks based on the information listed" +
14
- " in Relevant Information. Do not hallucinate. Do not use" +
15
- " the internet." ,
12
+ systemInstructions = content {
13
+ text(
14
+ " You are a Travel assistant. You will answer" +
15
+ " questions the user asks based on the information listed" +
16
+ " in Relevant Information. Do not hallucinate. Do not use" +
17
+ " the internet."
18
+ )
19
+ },
16
20
chatHistory = listOf (
17
21
content {
18
22
role = " user"
Original file line number Diff line number Diff line change @@ -23,6 +23,6 @@ data class Sample(
23
23
val categories : List <Category >,
24
24
// Optional parameters
25
25
val initialPrompt : Content ? = null ,
26
- val systemInstructions : String = " " ,
26
+ val systemInstructions : Content ? = null ,
27
27
val chatHistory : List <Content > = emptyList(),
28
28
)
You can’t perform that action at this time.
0 commit comments