Skip to content

Commit ab8a9bf

Browse files
committed
rewire chat history
1 parent abb4cc5 commit ab8a9bf

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

ai/app/src/main/java/com/google/firebase/quickstart/ai/feature/text/ChatScreen.kt

+8-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ import androidx.compose.ui.text.input.KeyboardCapitalization
3838
import androidx.compose.ui.unit.dp
3939
import androidx.lifecycle.compose.collectAsStateWithLifecycle
4040
import androidx.lifecycle.viewmodel.compose.viewModel
41+
import com.google.firebase.vertexai.type.Content
42+
import com.google.firebase.vertexai.type.asTextOrNull
4143
import java.util.UUID
4244
import kotlinx.coroutines.launch
4345
import kotlinx.serialization.Serializable
@@ -51,7 +53,12 @@ data class ChatMessage(
5153
var text: String = "",
5254
val participant: Participant = Participant.USER,
5355
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+
}
5562

5663
@Serializable
5764
class ChatRoute(val sampleId: String)
@@ -69,7 +76,6 @@ fun ChatScreen(
6976
Column(
7077
modifier = Modifier
7178
.fillMaxSize()
72-
.background(MaterialTheme.colorScheme.errorContainer)
7379
) {
7480
ChatList(
7581
messages,

ai/app/src/main/java/com/google/firebase/quickstart/ai/feature/text/ChatViewModel.kt

+5-10
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,21 @@ class ChatViewModel(
2323
private val sample = FIREBASE_AI_SAMPLES.first { it.id == sampleId }
2424
val initialPrompt = sample.initialPrompt?.parts?.first()?.asTextOrNull().orEmpty()
2525

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()
3328
private val _messages = MutableStateFlow<List<ChatMessage>>(_messageList)
3429
val messages: StateFlow<List<ChatMessage>> =
3530
_messages
3631

37-
3832
private val generativeModel: GenerativeModel
3933
private val chat: Chat
4034

4135
init {
4236
generativeModel = Firebase.vertexAI.generativeModel(
43-
"gemini-2.0-flash"
37+
modelName = "gemini-2.0-flash",
38+
systemInstruction = sample.systemInstructions
4439
)
45-
chat = generativeModel.startChat()
40+
chat = generativeModel.startChat(sample.chatHistory)
4641
}
4742

4843
fun sendMessage(userMessage: String) {

ai/app/src/main/java/com/google/firebase/quickstart/ai/ui/navigation/FirebaseAISamples.kt

+8-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ val FIREBASE_AI_SAMPLES = listOf(
99
" travel tips for traveling.",
1010
navRoute = "chat",
1111
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+
},
1620
chatHistory = listOf(
1721
content {
1822
role = "user"

ai/app/src/main/java/com/google/firebase/quickstart/ai/ui/navigation/Sample.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ data class Sample(
2323
val categories: List<Category>,
2424
// Optional parameters
2525
val initialPrompt: Content? = null,
26-
val systemInstructions: String = "",
26+
val systemInstructions: Content? = null,
2727
val chatHistory: List<Content> = emptyList(),
2828
)

0 commit comments

Comments
 (0)