Repository navigation
Add hybrid function calling sample #2855
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
2
commits into
master
Choose a base branch
from
function_calling
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.
+790
−22
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
388 changes: 388 additions & 0 deletions
388
...rc/main/java/com/google/firebase/quickstart/ai/feature/hybrid/FunctionCallingViewModel.kt
Large diffs are not rendered by default.
Oops, something went wrong.
345 changes: 345 additions & 0 deletions
345
firebase-ai/app/src/main/java/com/google/firebase/quickstart/ai/ui/FunctionCallingScreen.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,345 @@ | ||
| package com.google.firebase.quickstart.ai.ui | ||
|
|
||
| import androidx.compose.foundation.horizontalScroll | ||
| import androidx.compose.foundation.layout.Arrangement | ||
| import androidx.compose.foundation.layout.Column | ||
| import androidx.compose.foundation.layout.Row | ||
| import androidx.compose.foundation.layout.Spacer | ||
| import androidx.compose.foundation.layout.fillMaxSize | ||
| import androidx.compose.foundation.layout.fillMaxWidth | ||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.foundation.layout.size | ||
| import androidx.compose.foundation.rememberScrollState | ||
| import androidx.compose.foundation.verticalScroll | ||
| import androidx.compose.material3.Button | ||
| import androidx.compose.material3.Card | ||
| import androidx.compose.material3.CardDefaults | ||
| import androidx.compose.material3.CircularProgressIndicator | ||
| import androidx.compose.material3.FilterChip | ||
| import androidx.compose.material3.MaterialTheme | ||
| import androidx.compose.material3.OutlinedTextField | ||
| import androidx.compose.material3.Surface | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.collectAsState | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.saveable.rememberSaveable | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.text.font.FontFamily | ||
| import androidx.compose.ui.text.font.FontWeight | ||
| import androidx.compose.ui.unit.dp | ||
| import androidx.lifecycle.viewmodel.compose.viewModel | ||
| import com.google.firebase.ai.type.PublicPreviewAPI | ||
| import com.google.firebase.quickstart.ai.feature.hybrid.FunctionCallingViewModel | ||
| import com.google.firebase.quickstart.ai.feature.hybrid.FunctionToolOption | ||
| import com.google.firebase.quickstart.ai.feature.hybrid.GenerationMethod | ||
| import com.google.firebase.quickstart.ai.feature.hybrid.InferenceModeOption | ||
| import com.google.firebase.quickstart.ai.feature.hybrid.OnDeviceModelOptionSelection | ||
|
|
||
| @OptIn(PublicPreviewAPI::class) | ||
| @Composable | ||
| fun FunctionCallingScreen( | ||
| viewModel: FunctionCallingViewModel = viewModel() | ||
| ) { | ||
| val uiState by viewModel.uiState.collectAsState() | ||
| var prompt by rememberSaveable { | ||
| mutableStateOf("What was the weather in Boston, MA on October 17, 2024?") | ||
| } | ||
| var selectedToolOption by rememberSaveable { mutableStateOf(FunctionToolOption.MANUAL) } | ||
| var selectedModelOption by rememberSaveable { mutableStateOf(OnDeviceModelOptionSelection.DEFAULT) } | ||
| var selectedMode by rememberSaveable { mutableStateOf(InferenceModeOption.PREFER_ON_DEVICE) } | ||
| var selectedMethod by rememberSaveable { mutableStateOf(GenerationMethod.UNARY) } | ||
|
|
||
| Column( | ||
| modifier = Modifier | ||
| .fillMaxSize() | ||
| .verticalScroll(rememberScrollState()) | ||
| .padding(16.dp), | ||
| verticalArrangement = Arrangement.spacedBy(12.dp) | ||
| ) { | ||
| Card( | ||
| modifier = Modifier.fillMaxWidth(), | ||
| colors = CardDefaults.cardColors( | ||
| containerColor = MaterialTheme.colorScheme.secondaryContainer | ||
| ) | ||
| ) { | ||
| Column(modifier = Modifier.padding(12.dp)) { | ||
| Text( | ||
| text = "On-Device Model Status", | ||
| style = MaterialTheme.typography.labelMedium, | ||
| color = MaterialTheme.colorScheme.onSecondaryContainer | ||
| ) | ||
| Text( | ||
| text = uiState.modelStatus, | ||
| style = MaterialTheme.typography.bodyMedium, | ||
| fontWeight = FontWeight.Bold, | ||
| color = MaterialTheme.colorScheme.onSecondaryContainer | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| Text( | ||
| text = "1. Function Tool Configuration:", | ||
| style = MaterialTheme.typography.titleMedium, | ||
| fontWeight = FontWeight.SemiBold | ||
| ) | ||
| Row( | ||
| modifier = Modifier.horizontalScroll(rememberScrollState()), | ||
| horizontalArrangement = Arrangement.spacedBy(8.dp) | ||
| ) { | ||
| FunctionToolOption.entries.forEach { option -> | ||
| FilterChip( | ||
| selected = selectedToolOption == option, | ||
| onClick = { selectedToolOption = option }, | ||
| label = { Text(option.label) } | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| Text( | ||
| text = "2. On-Device Model Option:", | ||
| style = MaterialTheme.typography.titleMedium, | ||
| fontWeight = FontWeight.SemiBold | ||
| ) | ||
| Row( | ||
| modifier = Modifier.horizontalScroll(rememberScrollState()), | ||
| horizontalArrangement = Arrangement.spacedBy(8.dp) | ||
| ) { | ||
| OnDeviceModelOptionSelection.entries.forEach { modelOpt -> | ||
| FilterChip( | ||
| selected = selectedModelOption == modelOpt, | ||
| onClick = { | ||
| selectedModelOption = modelOpt | ||
| viewModel.checkAndDownloadModel(modelOpt.option) | ||
| }, | ||
| label = { Text(modelOpt.label) } | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| OutlinedTextField( | ||
| value = prompt, | ||
| onValueChange = { prompt = it }, | ||
| label = { Text("User Prompt") }, | ||
| modifier = Modifier.fillMaxWidth(), | ||
| maxLines = 4 | ||
| ) | ||
|
|
||
| Text( | ||
| text = "3. Inference Mode:", | ||
| style = MaterialTheme.typography.titleMedium, | ||
| fontWeight = FontWeight.SemiBold | ||
| ) | ||
| Row( | ||
| modifier = Modifier.horizontalScroll(rememberScrollState()), | ||
| horizontalArrangement = Arrangement.spacedBy(8.dp) | ||
| ) { | ||
| InferenceModeOption.entries.forEach { modeOption -> | ||
| FilterChip( | ||
| selected = selectedMode == modeOption, | ||
| onClick = { selectedMode = modeOption }, | ||
| label = { Text(modeOption.label) } | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| Text( | ||
| text = "4. Generation Method:", | ||
| style = MaterialTheme.typography.titleMedium, | ||
| fontWeight = FontWeight.SemiBold | ||
| ) | ||
| Row( | ||
| modifier = Modifier.horizontalScroll(rememberScrollState()), | ||
| horizontalArrangement = Arrangement.spacedBy(8.dp) | ||
| ) { | ||
| GenerationMethod.entries.forEach { method -> | ||
| FilterChip( | ||
| selected = selectedMethod == method, | ||
| onClick = { selectedMethod = method }, | ||
| label = { Text(method.label) } | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| Button( | ||
| onClick = { | ||
| viewModel.runTest( | ||
| promptText = prompt, | ||
| toolOption = selectedToolOption, | ||
| modelOptionSelection = selectedModelOption, | ||
| mode = selectedMode.mode, | ||
| method = selectedMethod | ||
| ) | ||
| }, | ||
| enabled = !uiState.isLoading, | ||
| modifier = Modifier.fillMaxWidth() | ||
| ) { | ||
| if (uiState.isLoading) { | ||
| CircularProgressIndicator( | ||
| modifier = Modifier.size(20.dp), | ||
| strokeWidth = 2.dp, | ||
| color = MaterialTheme.colorScheme.onPrimary | ||
| ) | ||
| Spacer(modifier = Modifier.size(8.dp)) | ||
| } | ||
| Text("Run Function Calling Test") | ||
| } | ||
|
|
||
| val errorMessage = uiState.errorMessage | ||
| if (errorMessage != null) { | ||
| Card( | ||
| colors = CardDefaults.cardColors( | ||
| containerColor = MaterialTheme.colorScheme.errorContainer | ||
| ), | ||
| modifier = Modifier.fillMaxWidth() | ||
| ) { | ||
| Column(modifier = Modifier.padding(12.dp)) { | ||
| Text( | ||
| text = "Error / Exception", | ||
| style = MaterialTheme.typography.labelLarge, | ||
| fontWeight = FontWeight.Bold, | ||
| color = MaterialTheme.colorScheme.onErrorContainer | ||
| ) | ||
| Text( | ||
| text = errorMessage, | ||
| style = MaterialTheme.typography.bodySmall, | ||
| color = MaterialTheme.colorScheme.onErrorContainer | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (uiState.functionCalls.isNotEmpty()) { | ||
| Card( | ||
| colors = CardDefaults.cardColors( | ||
| containerColor = MaterialTheme.colorScheme.tertiaryContainer | ||
| ), | ||
| modifier = Modifier.fillMaxWidth() | ||
| ) { | ||
| Column( | ||
| modifier = Modifier.padding(16.dp), | ||
| verticalArrangement = Arrangement.spacedBy(8.dp) | ||
| ) { | ||
| Text( | ||
| text = "Function Call(s) Detected (response.functionCalls)", | ||
| style = MaterialTheme.typography.titleMedium, | ||
| fontWeight = FontWeight.Bold, | ||
| color = MaterialTheme.colorScheme.onTertiaryContainer | ||
| ) | ||
| uiState.functionCalls.forEach { call -> | ||
| Column(modifier = Modifier.padding(vertical = 4.dp)) { | ||
| Text( | ||
| text = "Function: ${call.name}", | ||
| style = MaterialTheme.typography.bodyMedium, | ||
| fontWeight = FontWeight.SemiBold, | ||
| color = MaterialTheme.colorScheme.onTertiaryContainer | ||
| ) | ||
| Text( | ||
| text = "Arguments: ${call.args}", | ||
| style = MaterialTheme.typography.bodySmall, | ||
| fontFamily = FontFamily.Monospace, | ||
| color = MaterialTheme.colorScheme.onTertiaryContainer | ||
| ) | ||
| if (call.id != null) { | ||
| Text( | ||
| text = "ID: ${call.id}", | ||
| style = MaterialTheme.typography.bodySmall, | ||
| fontFamily = FontFamily.Monospace, | ||
| color = MaterialTheme.colorScheme.onTertiaryContainer | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val execResult = uiState.functionExecutionResult | ||
| if (execResult != null) { | ||
| Spacer(modifier = Modifier.size(4.dp)) | ||
| Text( | ||
| text = "Execution Result:", | ||
| style = MaterialTheme.typography.labelMedium, | ||
| fontWeight = FontWeight.Bold, | ||
| color = MaterialTheme.colorScheme.onTertiaryContainer | ||
| ) | ||
| Text( | ||
| text = execResult, | ||
| style = MaterialTheme.typography.bodySmall, | ||
| fontFamily = FontFamily.Monospace, | ||
| color = MaterialTheme.colorScheme.onTertiaryContainer | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val responseText = uiState.responseText | ||
| if (responseText != null) { | ||
| Card( | ||
| colors = CardDefaults.cardColors( | ||
| containerColor = MaterialTheme.colorScheme.primaryContainer | ||
| ), | ||
| modifier = Modifier.fillMaxWidth() | ||
| ) { | ||
| Column( | ||
| modifier = Modifier.padding(16.dp), | ||
| verticalArrangement = Arrangement.spacedBy(8.dp) | ||
| ) { | ||
| Row( | ||
| modifier = Modifier.fillMaxWidth(), | ||
| horizontalArrangement = Arrangement.SpaceBetween, | ||
| verticalAlignment = Alignment.CenterVertically | ||
| ) { | ||
| Text( | ||
| text = "Model Response (response.text)", | ||
| style = MaterialTheme.typography.titleMedium, | ||
| fontWeight = FontWeight.Bold, | ||
| color = MaterialTheme.colorScheme.onPrimaryContainer | ||
| ) | ||
| Surface( | ||
| color = MaterialTheme.colorScheme.primary, | ||
| shape = MaterialTheme.shapes.small | ||
| ) { | ||
| Text( | ||
| text = uiState.inferenceSource ?: "UNKNOWN", | ||
| modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp), | ||
| color = MaterialTheme.colorScheme.onPrimary, | ||
| style = MaterialTheme.typography.labelSmall, | ||
| fontWeight = FontWeight.Bold | ||
| ) | ||
| } | ||
| } | ||
| Text( | ||
| text = responseText, | ||
| style = MaterialTheme.typography.bodyLarge, | ||
| color = MaterialTheme.colorScheme.onPrimaryContainer | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Card( | ||
| colors = CardDefaults.cardColors( | ||
| containerColor = MaterialTheme.colorScheme.surfaceVariant | ||
| ), | ||
| modifier = Modifier.fillMaxWidth() | ||
| ) { | ||
| Column( | ||
| modifier = Modifier.padding(12.dp), | ||
| verticalArrangement = Arrangement.spacedBy(4.dp) | ||
| ) { | ||
| Text( | ||
| text = "Diagnostics & Logcat Summary", | ||
| style = MaterialTheme.typography.titleSmall, | ||
| fontWeight = FontWeight.Bold | ||
| ) | ||
| Text( | ||
| text = uiState.logOutput, | ||
| style = MaterialTheme.typography.bodySmall, | ||
| fontFamily = FontFamily.Monospace | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid redundant network requests and job cancellations when the user taps an already selected model option, add a check to ensure
checkAndDownloadModelis only called when the selection actually changes.