in generativeai-android-sample/app/src/main/kotlin/com/google/ai/sample/feature/multimodal/PhotoReasoningViewModel.kt [39:66]
fun reason(
userInput: String,
selectedImages: List<Bitmap>
) {
_uiState.value = PhotoReasoningUiState.Loading
val prompt = "Look at the image(s), and then answer the following question: $userInput"
viewModelScope.launch(Dispatchers.IO) {
try {
val inputContent = content {
for (bitmap in selectedImages) {
image(bitmap)
}
text(prompt)
}
var outputContent = ""
generativeModel.generateContentStream(inputContent)
.collect { response ->
outputContent += response.text
_uiState.value = PhotoReasoningUiState.Success(outputContent)
}
} catch (e: Exception) {
_uiState.value = PhotoReasoningUiState.Error(e.localizedMessage ?: "")
}
}
}