Skip to content

Commit 6146a29

Browse files
Migrate to Firebase AI Logic
Migrate chatbot implementation to Firebase AI Logic
1 parent a49ac40 commit 6146a29

6 files changed

Lines changed: 55 additions & 22 deletions

File tree

README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,9 @@ Here are the screens that make up SociaLite:
4646
2. Open the whole project in Android Studio.
4747
3. Sync & Run `app` configuration
4848

49-
> Note: The Google AI client SDK for Android used in this code sample is
50-
recommended for prototyping only. For non-prototyping use cases, we strongly
51-
recommend that you use [Vertex AI for Firebase](https://firebase.google.com/docs/vertex-ai/migrate-to-vertex-ai?platform=android).
52-
53-
### (optional) Add Gemini API:
54-
4. Create a Gemini API key in [Google AI studio](https://aistudio.google.com/app/apikey),
55-
5. Add your Gemini API key as `API_KEY` to your [`local.properties`] file,
56-
6. Sync & Run `app` configuration,
49+
### (optional) Add Gemini API via Firebase AI Logic:
50+
4. Set up a Firebase Project, connect your app to the _Gemini Developer API_ by following [these steps](https://firebase.google.com/docs/ai-logic/get-started?platform=android&api=dev),
51+
5. Replace google-services.json with your own & Run `app` configuration,
5752
7. In the app, go to Settings and tap the "AI Chatbot" button.
5853

5954
## Reporting Issues

app/build.gradle.kts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ plugins {
2525
alias(libs.plugins.ksp)
2626
alias(libs.plugins.secrets)
2727
alias(libs.plugins.download)
28+
alias(libs.plugins.google.gms.google.services)
2829
}
2930

3031
kotlin {
@@ -176,6 +177,7 @@ dependencies {
176177
implementation(libs.coil)
177178
implementation(libs.coil.compose)
178179

179-
implementation(libs.generativeai)
180+
implementation(platform(libs.firebase.bom))
181+
implementation(libs.firebase.ai)
180182
implementation(libs.datastore)
181183
}

app/google-services.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"comment": "NOTE: Replace this file with your own google-services.json. Learn more at: https://firebase.google.com/docs/ai-logic/get-started?api=dev#no-existing-firebase",
3+
"project_info": {
4+
"project_number": "123456",
5+
"project_id": "mock_project",
6+
"storage_bucket": "mock_project.firebasestorage.app"
7+
},
8+
"client": [
9+
{
10+
"client_info": {
11+
"mobilesdk_app_id": "1:123456:android:123456",
12+
"android_client_info": {
13+
"package_name": "com.google.android.samples.socialite"
14+
}
15+
},
16+
"oauth_client": [],
17+
"api_key": [
18+
{
19+
"current_key": "123456-abcde"
20+
}
21+
],
22+
"services": {
23+
"appinvite_service": {
24+
"other_platform_oauth_client": []
25+
}
26+
}
27+
}
28+
],
29+
"configuration_version": "1"
30+
}

app/src/main/java/com/google/android/samples/socialite/repository/ChatRepository.kt

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@ package com.google.android.samples.socialite.repository
1919
import android.content.Context
2020
import android.graphics.BitmapFactory
2121
import android.net.Uri
22+
import android.util.Log
2223
import android.widget.Toast
2324
import androidx.datastore.core.DataStore
2425
import androidx.datastore.preferences.core.Preferences
2526
import androidx.datastore.preferences.core.booleanPreferencesKey
2627
import androidx.datastore.preferences.core.edit
2728
import androidx.datastore.preferences.preferencesDataStore
28-
import com.google.ai.client.generativeai.GenerativeModel
29-
import com.google.ai.client.generativeai.type.Content
30-
import com.google.ai.client.generativeai.type.content
31-
import com.google.android.samples.socialite.BuildConfig
3229
import com.google.android.samples.socialite.R
3330
import com.google.android.samples.socialite.data.ChatDao
3431
import com.google.android.samples.socialite.data.ContactDao
@@ -38,6 +35,12 @@ import com.google.android.samples.socialite.di.AppCoroutineScope
3835
import com.google.android.samples.socialite.model.ChatDetail
3936
import com.google.android.samples.socialite.model.Message
4037
import com.google.android.samples.socialite.widget.model.WidgetModelRepository
38+
import com.google.firebase.Firebase
39+
import com.google.firebase.FirebaseApp
40+
import com.google.firebase.ai.ai
41+
import com.google.firebase.ai.type.Content
42+
import com.google.firebase.ai.type.GenerativeBackend
43+
import com.google.firebase.ai.type.content
4144
import dagger.hilt.android.qualifiers.ApplicationContext
4245
import javax.inject.Inject
4346
import javax.inject.Singleton
@@ -97,11 +100,8 @@ class ChatRepository @Inject internal constructor(
97100
saveMessageAndNotify(chatId, text, 0L, mediaUri, mediaMimeType, detail, PushReason.OutgoingMessage)
98101

99102
// Create a generative AI Model to interact with the Gemini API.
100-
val generativeModel = GenerativeModel(
101-
modelName = "gemini-1.5-pro-latest",
102-
// Set your Gemini API in as an `API_KEY` variable in your local.properties file
103-
apiKey = BuildConfig.API_KEY,
104-
// Set a system instruction to set the behavior of the model.
103+
val generativeModel = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
104+
modelName = "gemini-2.0-flash-lite-001",
105105
systemInstruction = content {
106106
text("Please respond to this chat conversation like a friendly ${detail.firstContact.replyModel}.")
107107
},
@@ -297,7 +297,10 @@ class ChatRepository @Inject internal constructor(
297297
}
298298

299299
fun toggleChatbotSetting() {
300-
if (BuildConfig.API_KEY == "DUMMY_API_KEY") {
300+
val firebaseApp = FirebaseApp.getInstance()
301+
302+
Log.d("ChatRepository", "Firebase App ID: ${firebaseApp.options.projectId}")
303+
if (firebaseApp.options.projectId == "mock_project") {
301304
Toast.makeText(
302305
appContext,
303306
appContext.getString(R.string.set_api_key_toast),

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<string name="ai_chatbot_setting">AI Chatbot</string>
7373
<string name="ai_chatbot_setting_enabled">enabled</string>
7474
<string name="ai_chatbot_setting_disabled">disabled</string>
75-
<string name="set_api_key_toast">Please set your API key in local.properties</string>
75+
<string name="set_api_key_toast">Please connect your app to Firebase to enable AI Chatbot</string>
7676
<string name="gemini_error">Gemini error (%1$s)</string>
7777
<string name="image_error">Can\'t open image</string>
7878
<string name="unknown_error">Unknown error</string>

gradle/libs.versions.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ core = "1.15.0"
2929
core-performance = "1.0.0"
3030
core-splashscreen = "1.0.1"
3131
espresso = "3.6.1"
32+
firebaseBoM = "33.14.0"
33+
googleGmsGoogleServices = "4.4.2"
3234
download = "5.6.0"
3335
graphics = "1.0.1"
3436
hilt = "2.49"
@@ -53,7 +55,6 @@ window = "1.3.0"
5355
material3-adaptive-navigation-suite = "1.3.1"
5456
glance = "1.1.1"
5557
secrets = "2.0.1"
56-
generativeai = "0.9.0"
5758
datastore = "1.1.1"
5859
visionCommon = "17.3.0"
5960
segmentationSelfie = "16.0.0-beta6"
@@ -123,7 +124,8 @@ turbine = { group = "app.cash.turbine", name = "turbine", version.ref = "turbine
123124
uiautomator = { group = "androidx.test.uiautomator", name = "uiautomator", version.ref = "uiautomator" }
124125
window = { group = "androidx.window", name = "window", version.ref = "window" }
125126
ktlint = "com.pinterest.ktlint:ktlint-cli:1.1.1" # Used in build.gradle.kts
126-
generativeai = { group = "com.google.ai.client.generativeai", name = "generativeai", version.ref = "generativeai"}
127+
firebase-bom = { group = "com.google.firebase", name = "firebase-bom", version.ref = "firebaseBoM" }
128+
firebase-ai = { group = "com.google.firebase", name = "firebase-ai" }
127129
datastore = { group = "androidx.datastore", name = "datastore-preferences", version.ref = "datastore"}
128130
vision-common = { group = "com.google.mlkit", name = "vision-common", version.ref = "visionCommon" }
129131
segmentation-selfie = { group = "com.google.mlkit", name = "segmentation-selfie", version.ref = "segmentationSelfie" }
@@ -143,3 +145,4 @@ ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
143145
spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }
144146
secrets = { id = "com.google.android.libraries.mapsplatform.secrets-gradle-plugin", version.ref = "secrets" }
145147
download = { id = "de.undercouch.download", version.ref = "download" }
148+
google-gms-google-services = { id = "com.google.gms.google-services", version.ref = "googleGmsGoogleServices" }

0 commit comments

Comments
 (0)