Skip to content

Commit 2668312

Browse files
authored
feat(ai-logic): add sample demonstrating auto function calling (#2796)
1 parent 5ffae86 commit 2668312

4 files changed

Lines changed: 95 additions & 3 deletions

File tree

firebase-ai/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ You can find the implementation for each feature by clicking on the links below:
4848

4949
### Tools and function calling
5050
- [Grounding with Google Search](app/src/main/java/com/google/firebase/quickstart/ai/feature/text/GoogleSearchGroundingViewModel.kt): Use Grounding with Google Search to get responses based on up-to-date information from the web.
51-
- [Weather Chat](app/src/main/java/com/google/firebase/quickstart/ai/feature/text/WeatherChatViewModel.kt): Use function calling to get the weather conditions for a specific US city on a specific date.
51+
- [Manual function calling](app/src/main/java/com/google/firebase/quickstart/ai/feature/text/WeatherChatViewModel.kt): Use function calling to get the weather conditions for a specific US city on a specific date.
52+
- [Automatic function calling](app/src/main/java/com/google/firebase/quickstart/ai/feature/text/AutoFunctionCallViewModel.kt): Use automatic function calling to get the weather conditions for a specific US city on a specific date.
5253
- [Gemini Live (audio input)](app/src/main/java/com/google/firebase/quickstart/ai/feature/live/StreamAudioViewModel.kt): Use bidirectional streaming to get information about weather conditions for a specific US city on a specific date
5354
- [Gemini Live (video input)](app/src/main/java/com/google/firebase/quickstart/ai/feature/live/StreamVideoViewModel.kt): Use bidirectional streaming to chat with Gemini using your phone's camera
5455

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.google.firebase.quickstart.ai.feature.text
2+
3+
import com.google.firebase.Firebase
4+
import com.google.firebase.ai.Chat
5+
import com.google.firebase.ai.ai
6+
import com.google.firebase.ai.type.AutoFunctionDeclaration
7+
import com.google.firebase.ai.type.Content
8+
import com.google.firebase.ai.type.FirebaseAutoFunctionException
9+
import com.google.firebase.ai.type.FunctionResponsePart
10+
import com.google.firebase.ai.type.GenerativeBackend
11+
import com.google.firebase.ai.type.JsonSchema
12+
import com.google.firebase.ai.type.Tool
13+
import com.google.firebase.quickstart.ai.feature.text.functioncalling.WeatherRepository
14+
import com.google.firebase.quickstart.ai.ui.UiChatMessage
15+
import kotlinx.serialization.Serializable
16+
import kotlinx.serialization.json.JsonObject
17+
import kotlinx.serialization.json.jsonPrimitive
18+
19+
@Serializable
20+
object AutoFunctionCallRoute
21+
22+
class AutoFunctionCallViewModel : ChatViewModel() {
23+
24+
override val initialPrompt: String = "What was the weather in Boston, MA on October 17, 2024?"
25+
26+
private val chat: Chat
27+
28+
init {
29+
val generativeModel = Firebase.ai(
30+
backend = GenerativeBackend.googleAI()
31+
).generativeModel(
32+
modelName = "gemini-3.1-flash-lite",
33+
tools = listOf(
34+
Tool.functionDeclarations(
35+
autoFunctionDeclarations = listOf(
36+
AutoFunctionDeclaration.create(
37+
functionName = "fetchWeather",
38+
description = "Get the weather conditions for a specific US city on a specific date.",
39+
inputSchema = JsonSchema.obj(
40+
properties = mapOf(
41+
"city" to JsonSchema.string("The US city of the location."),
42+
"state" to JsonSchema.string("The US state of the location."),
43+
"date" to JsonSchema.string(
44+
"The date for which to get the weather." +
45+
" Date must be in the format: YYYY-MM-DD."
46+
),
47+
)
48+
),
49+
functionReference = ::fetchWeather
50+
)
51+
)
52+
)
53+
)
54+
)
55+
chat = generativeModel.startChat()
56+
}
57+
58+
override suspend fun performSendMessage(prompt: Content, currentMessages: List<UiChatMessage>) {
59+
val response = chat.sendMessage(prompt)
60+
validateAndDisplayResponse(response, currentMessages)
61+
}
62+
63+
private suspend fun fetchWeather(input: JsonObject): FunctionResponsePart {
64+
val city = input["city"]?.jsonPrimitive?.content
65+
val state = input["state"]?.jsonPrimitive?.content
66+
val date = input["date"]?.jsonPrimitive?.content
67+
68+
return if (city == null || state == null || date == null) {
69+
// Tell the model there was an error
70+
throw FirebaseAutoFunctionException("Unable to fetch weather - one of the parameters was null")
71+
} else {
72+
// Execute the function call and return the response
73+
val functionResponse = WeatherRepository
74+
.fetchWeather(city, state, date)
75+
76+
FunctionResponsePart("fetchWeather", functionResponse)
77+
}
78+
}
79+
}
80+

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class WeatherChatViewModel : ChatViewModel() {
3636
modelName = "gemini-3.1-flash-lite",
3737
tools = listOf(
3838
Tool.functionDeclarations(
39-
listOf(
39+
functionDeclarations = listOf(
4040
FunctionDeclaration(
4141
"fetchWeather",
4242
"Get the weather conditions for a specific US city on a specific date.",

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import com.google.firebase.quickstart.ai.feature.text.AudioSummarizationRoute
1010
import com.google.firebase.quickstart.ai.feature.text.AudioSummarizationViewModel
1111
import com.google.firebase.quickstart.ai.feature.text.AudioTranslationRoute
1212
import com.google.firebase.quickstart.ai.feature.text.AudioTranslationViewModel
13+
import com.google.firebase.quickstart.ai.feature.text.AutoFunctionCallRoute
14+
import com.google.firebase.quickstart.ai.feature.text.AutoFunctionCallViewModel
1315
import com.google.firebase.quickstart.ai.feature.text.DocumentComparisonRoute
1416
import com.google.firebase.quickstart.ai.feature.text.DocumentComparisonViewModel
1517
import com.google.firebase.quickstart.ai.feature.text.GoogleSearchGroundingRoute
@@ -134,14 +136,23 @@ val FIREBASE_AI_SAMPLES = listOf(
134136
categories = listOf(Category.TOOLS_FC)
135137
),
136138
Sample(
137-
title = "Weather Chat",
139+
title = "Manual function calling",
138140
description = "Use function calling to get the weather conditions" +
139141
" for a specific US city on a specific date.",
140142
route = WeatherChatRoute,
141143
screenType = ScreenType.CHAT,
142144
viewModelClass = WeatherChatViewModel::class,
143145
categories = listOf(Category.TOOLS_FC)
144146
),
147+
Sample(
148+
title = "Automatic function calling",
149+
description = "Use automatic function calling to get the weather conditions" +
150+
" for a specific US city on a specific date.",
151+
route = AutoFunctionCallRoute,
152+
screenType = ScreenType.CHAT,
153+
viewModelClass = AutoFunctionCallViewModel::class,
154+
categories = listOf(Category.TOOLS_FC)
155+
),
145156
Sample(
146157
title = "Gemini Live (audio input)",
147158
description = "Use bidirectional streaming to get information about" +

0 commit comments

Comments
 (0)