Repository navigation
add quick start app for system isntructions #2846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VinayGuthal
wants to merge
5
commits into
master
Choose a base branch
from
system_instruction
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
270108c
add quick start app for system isntructions
VinayGuthal e5b866f
Update firebase-ai/app/src/main/java/com/google/firebase/quickstart/a…
VinayGuthal f29dac7
Update firebase-ai/app/src/main/java/com/google/firebase/quickstart/a…
VinayGuthal 7ff40af
Update firebase-ai/app/src/main/java/com/google/firebase/quickstart/a…
VinayGuthal ab9d9b9
address comments
VinayGuthal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
267 changes: 267 additions & 0 deletions
267
.../main/java/com/google/firebase/quickstart/ai/feature/hybrid/SystemInstructionViewModel.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,267 @@ | ||
| package com.google.firebase.quickstart.ai.feature.hybrid | ||
|
|
||
| import android.graphics.Bitmap | ||
| import android.util.Log | ||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.viewModelScope | ||
| import com.google.firebase.Firebase | ||
| import com.google.firebase.ai.DownloadStatus | ||
| import com.google.firebase.ai.InferenceMode | ||
| import com.google.firebase.ai.InferenceSource | ||
| import com.google.firebase.ai.OnDeviceConfig | ||
| import com.google.firebase.ai.OnDeviceModelStatus | ||
| import com.google.firebase.ai.ai | ||
| import com.google.firebase.ai.type.Content | ||
| import com.google.firebase.ai.type.GenerativeBackend | ||
| import com.google.firebase.ai.type.PublicPreviewAPI | ||
| import com.google.firebase.ai.type.content | ||
| import com.google.firebase.quickstart.ai.ui.SystemInstructionUiState | ||
| import kotlinx.coroutines.CancellationException | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.update | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| object SystemInstructionRoute | ||
|
|
||
| enum class SystemInstructionCase(val label: String) { | ||
| SINGLE_TEXT("Single TextPart"), | ||
| MULTIPLE_TEXT("Multiple TextParts"), | ||
| NON_TEXT_PART("Non-Text Part (Image)"), | ||
| NONE("No System Instruction") | ||
| } | ||
|
|
||
| enum class GenerationMethod(val label: String) { | ||
| UNARY("generateContent"), | ||
| STREAMING("generateContentStream") | ||
| } | ||
|
|
||
| @OptIn(PublicPreviewAPI::class) | ||
| enum class InferenceModeOption(val label: String, val mode: InferenceMode) { | ||
| ONLY_ON_DEVICE("ONLY_ON_DEVICE", InferenceMode.ONLY_ON_DEVICE), | ||
| PREFER_ON_DEVICE("PREFER_ON_DEVICE", InferenceMode.PREFER_ON_DEVICE), | ||
| PREFER_IN_CLOUD("PREFER_IN_CLOUD", InferenceMode.PREFER_IN_CLOUD), | ||
| ONLY_IN_CLOUD("ONLY_IN_CLOUD", InferenceMode.ONLY_IN_CLOUD) | ||
| } | ||
|
|
||
| @OptIn(PublicPreviewAPI::class) | ||
| class SystemInstructionViewModel : ViewModel() { | ||
| private val _uiState = MutableStateFlow(SystemInstructionUiState()) | ||
| val uiState: StateFlow<SystemInstructionUiState> = _uiState | ||
|
|
||
| private var testJob: Job? = null | ||
|
|
||
| private val statusCheckModel = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel( | ||
| modelName = "gemini-3.5-flash-lite", | ||
| onDeviceConfig = OnDeviceConfig(mode = InferenceMode.PREFER_ON_DEVICE) | ||
| ) | ||
|
|
||
| init { | ||
| checkAndDownloadModel() | ||
| } | ||
|
|
||
| private fun checkAndDownloadModel() { | ||
| viewModelScope.launch { | ||
| try { | ||
| val status = statusCheckModel.onDeviceExtension?.checkStatus() | ||
| val statusText = when (status) { | ||
| OnDeviceModelStatus.AVAILABLE -> "Model available" | ||
| OnDeviceModelStatus.DOWNLOADABLE -> "Model downloadable" | ||
| OnDeviceModelStatus.DOWNLOADING -> "Model downloading..." | ||
| else -> "On-device model unavailable" | ||
| } | ||
| _uiState.update { it.copy(modelStatus = statusText) } | ||
|
|
||
| if (status == OnDeviceModelStatus.DOWNLOADABLE) { | ||
| statusCheckModel.onDeviceExtension?.download()?.collect { downloadStatus -> | ||
| when (downloadStatus) { | ||
| is DownloadStatus.DownloadStarted -> { | ||
| _uiState.update { it.copy(modelStatus = "Downloading model...") } | ||
| } | ||
| is DownloadStatus.DownloadInProgress -> { | ||
| val progress = downloadStatus.totalBytesDownloaded | ||
| _uiState.update { it.copy(modelStatus = "Downloading: $progress bytes") } | ||
| } | ||
| is DownloadStatus.DownloadCompleted -> { | ||
| _uiState.update { it.copy(modelStatus = "Model available") } | ||
| } | ||
| is DownloadStatus.DownloadFailed -> { | ||
| _uiState.update { | ||
| it.copy( | ||
| modelStatus = "Download failed", | ||
| errorMessage = "Model download failed" | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } catch (e: Exception) { | ||
| _uiState.update { | ||
| it.copy( | ||
| modelStatus = "Error checking status", | ||
| errorMessage = e.localizedMessage ?: e.toString() | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun buildSystemInstruction( | ||
| case: SystemInstructionCase, | ||
| primaryInstruction: String, | ||
| secondaryInstruction: String, | ||
| testImage: Bitmap? | ||
| ): Content? { | ||
| return when (case) { | ||
| SystemInstructionCase.SINGLE_TEXT -> content { | ||
| text(primaryInstruction) | ||
| } | ||
| SystemInstructionCase.MULTIPLE_TEXT -> content { | ||
| text(primaryInstruction) | ||
| text(secondaryInstruction) | ||
| } | ||
| SystemInstructionCase.NON_TEXT_PART -> content { | ||
| text(primaryInstruction) | ||
| if (testImage != null) { | ||
| image(testImage) | ||
| } | ||
| } | ||
|
VinayGuthal marked this conversation as resolved.
|
||
| SystemInstructionCase.NONE -> null | ||
| } | ||
| } | ||
|
|
||
| fun runTest( | ||
| promptText: String, | ||
| primaryInstruction: String, | ||
| secondaryInstruction: String, | ||
| case: SystemInstructionCase, | ||
| mode: InferenceMode, | ||
| method: GenerationMethod, | ||
| testImage: Bitmap? = null | ||
| ) { | ||
| testJob?.cancel() | ||
| testJob = viewModelScope.launch { | ||
| _uiState.update { | ||
| it.copy( | ||
| isLoading = true, | ||
| errorMessage = null, | ||
| responseText = null, | ||
| inferenceSource = null, | ||
| logOutput = "Running inference..." | ||
| ) | ||
| } | ||
|
|
||
| val systemInstruction = | ||
| buildSystemInstruction(case, primaryInstruction, secondaryInstruction, testImage) | ||
| val partsSummary = systemInstruction?.parts?.joinToString(", ") { part -> | ||
| part::class.java.simpleName | ||
| } ?: "null" | ||
|
|
||
| Log.i(TAG, "==========================================================") | ||
| Log.i(TAG, ">>> [SYSTEM INSTRUCTION TEST - INPUT] >>>") | ||
| Log.i(TAG, "Case: ${case.name} ($partsSummary)") | ||
| Log.i(TAG, "Inference Mode: $mode") | ||
| Log.i(TAG, "Generation Method: ${method.label}") | ||
| Log.i(TAG, "Prompt: \"$promptText\"") | ||
| Log.i(TAG, "==========================================================") | ||
|
|
||
| try { | ||
| val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel( | ||
| modelName = "gemini-3.5-flash-lite", | ||
| onDeviceConfig = OnDeviceConfig(mode = mode), | ||
| systemInstruction = systemInstruction | ||
| ) | ||
|
|
||
| var fullText = "" | ||
| var source: InferenceSource? = null | ||
|
|
||
| when (method) { | ||
| GenerationMethod.UNARY -> { | ||
| val response = model.generateContent(promptText) | ||
| fullText = response.text.orEmpty() | ||
| source = response.inferenceSource | ||
| } | ||
| GenerationMethod.STREAMING -> { | ||
| model.generateContentStream(promptText).collect { chunk -> | ||
| fullText += chunk.text.orEmpty() | ||
| source = chunk.inferenceSource | ||
| _uiState.update { | ||
| it.copy( | ||
| responseText = fullText, | ||
| inferenceSource = formatInferenceSource(source) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val sourceName = formatInferenceSource(source) | ||
| Log.i(TAG, "<<< [SYSTEM INSTRUCTION TEST - SUCCESS] <<<") | ||
| Log.i(TAG, "Inference Source: $sourceName") | ||
| Log.i(TAG, "Response Text: \"$fullText\"") | ||
|
|
||
| val summaryLog = """ | ||
| === INPUT === | ||
| Case: ${case.label} (parts: $partsSummary) | ||
| Mode: $mode | ||
| Method: ${method.label} | ||
| Prompt: "$promptText" | ||
|
|
||
| === OUTPUT (SUCCESS) === | ||
| Inference Source: $sourceName | ||
| Response: | ||
| $fullText | ||
| """.trimIndent() | ||
|
|
||
| _uiState.update { | ||
| it.copy( | ||
| isLoading = false, | ||
| responseText = fullText, | ||
| inferenceSource = sourceName, | ||
| logOutput = summaryLog | ||
| ) | ||
| } | ||
| } catch (e: CancellationException) { | ||
| throw e | ||
| } catch (e: Exception) { | ||
| val causeName = e.cause?.let { " (cause: ${it::class.java.simpleName})" }.orEmpty() | ||
| Log.e(TAG, "<<< [SYSTEM INSTRUCTION TEST - FAILURE] <<<", e) | ||
|
|
||
| val errorLog = """ | ||
| === INPUT === | ||
| Case: ${case.label} (parts: $partsSummary) | ||
| Mode: $mode | ||
| Method: ${method.label} | ||
|
|
||
| === OUTPUT (FAILURE) === | ||
| Exception: ${e::class.java.simpleName}$causeName | ||
| Message: ${e.localizedMessage ?: e.toString()} | ||
| """.trimIndent() | ||
|
|
||
| _uiState.update { | ||
| it.copy( | ||
| isLoading = false, | ||
| errorMessage = "${e::class.java.simpleName}$causeName: ${e.localizedMessage ?: e.toString()}", | ||
| logOutput = errorLog | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun formatInferenceSource(source: InferenceSource?): String = | ||
| when (source) { | ||
| InferenceSource.ON_DEVICE -> "ON_DEVICE" | ||
| InferenceSource.IN_CLOUD -> "IN_CLOUD" | ||
| null -> "UNKNOWN" | ||
| else -> source.toString() | ||
| } | ||
|
|
||
| companion object { | ||
| private const val TAG = "SystemInstructionDemo" | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.