Skip to content

Commit 0e37b0b

Browse files
authored
feat: create Firebase AI quickstart (#2667)
1 parent e702dd9 commit 0e37b0b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2216
-2
lines changed

copy_mock_google_services_json.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ cp mock-google-services.json crash/app/google-services.json
1414
cp mock-google-services.json database/app/google-services.json
1515
cp mock-google-services.json dataconnect/app/google-services.json
1616
cp mock-google-services.json dynamiclinks/app/google-services.json
17+
cp mock-google-services.json firebase-ai/app/google-services.json
1718
cp mock-google-services.json firestore/app/google-services.json
1819
cp mock-google-services.json functions/app/google-services.json
1920
cp mock-google-services.json inappmessaging/app/google-services.json

firebase-ai/.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties

firebase-ai/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Firebase AI Logic quickstart sample app
2+
3+
This Android sample app demonstrates how to use state-of-the-art
4+
generative AI models (like Gemini) to build AI-powered features and applications.
5+
For more information about Firebase AI Logic, visit the [documentation](http://firebase.google.com/docs/ai-logic).
6+
7+
## Getting Started
8+
9+
To try out this sample app, you need to use latest stable version of Android Studio.
10+
However, if you want to latest lint checks and AI productivity features in Android
11+
Studio use the latest preview version of [Android Studio](https://developer.android.com/studio/preview).
12+
13+
## Features
14+
15+
There are 2 main files that demonstrate the use of Firebase AI Logic:
16+
17+
- [ChatViewModel.kt](app/src/main/java/com/google/firebase/quickstart/ai/feature/text/ChatViewModel.kt)
18+
which can do things such as:
19+
- [Generate Text](https://firebase.google.com/docs/ai-logic/generate-text)
20+
- [Generate structured output (JSON)](https://firebase.google.com/docs/ai-logic/generate-structured-output)
21+
- [Analyze images](https://firebase.google.com/docs/ai-logic/analyze-images)
22+
- [Analyze video](https://firebase.google.com/docs/ai-logic/analyze-video)
23+
- [Analyze audio](https://firebase.google.com/docs/ai-logic/analyze-audio)
24+
- [Analyze documents (PDFs)](https://firebase.google.com/docs/ai-logic/analyze-documents)
25+
- [Generate images using Gemini 2.0](https://firebase.google.com/docs/ai-logic/generate-images-imagen)
26+
- [ImagenViewModel](app/src/main/java/com/google/firebase/quickstart/ai/feature/media/imagen/ImagenViewModel.kt)
27+
which shows how to [Generate images using Imagen models](https://firebase.google.com/docs/ai-logic/generate-images-imagen)
28+
29+
## All samples
30+
31+
The full list of available samples can be found in the
32+
[FirebaseAISamples.kt file](app/src/main/java/com/google/firebase/quickstart/ai/FirebaseAISamples.kt).

firebase-ai/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

firebase-ai/app/build.gradle.kts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
plugins {
2+
id("com.android.application")
3+
id("org.jetbrains.kotlin.android")
4+
id("org.jetbrains.kotlin.plugin.compose")
5+
kotlin("plugin.serialization") version "2.1.20"
6+
id("com.google.gms.google-services")
7+
}
8+
9+
android {
10+
namespace = "com.google.firebase.quickstart.ai"
11+
compileSdk = 35
12+
13+
defaultConfig {
14+
applicationId = "com.google.firebase.quickstart.ai"
15+
minSdk = 23
16+
targetSdk = 35
17+
versionCode = 1
18+
versionName = "1.0"
19+
20+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
21+
}
22+
23+
buildTypes {
24+
release {
25+
isMinifyEnabled = false
26+
proguardFiles(
27+
getDefaultProguardFile("proguard-android-optimize.txt"),
28+
"proguard-rules.pro"
29+
)
30+
}
31+
}
32+
compileOptions {
33+
sourceCompatibility = JavaVersion.VERSION_11
34+
targetCompatibility = JavaVersion.VERSION_11
35+
}
36+
kotlinOptions {
37+
jvmTarget = "11"
38+
}
39+
buildFeatures {
40+
compose = true
41+
}
42+
}
43+
44+
dependencies {
45+
46+
implementation(libs.androidx.core.ktx)
47+
implementation(libs.androidx.lifecycle.runtime.ktx)
48+
implementation(libs.androidx.activity.compose)
49+
50+
implementation(platform(libs.androidx.compose.bom))
51+
implementation(libs.androidx.ui)
52+
implementation(libs.androidx.ui.graphics)
53+
implementation(libs.androidx.ui.tooling.preview)
54+
implementation(libs.androidx.material3)
55+
implementation(libs.androidx.material.icons.extended)
56+
implementation(libs.androidx.material3.adaptive.navigation.suite)
57+
implementation(libs.compose.navigation)
58+
implementation(libs.androidx.lifecycle.viewmodel.ktx)
59+
// ViewModel utilities for Compose
60+
implementation(libs.androidx.lifecycle.viewmodel.compose)
61+
implementation(libs.androidx.lifecycle.viewmodel.savedstate)
62+
implementation(libs.kotlinx.serialization.json)
63+
64+
// Firebase
65+
implementation(platform(libs.firebase.bom))
66+
implementation(libs.firebase.ai)
67+
68+
testImplementation(libs.junit)
69+
androidTestImplementation(libs.androidx.junit)
70+
androidTestImplementation(libs.androidx.espresso.core)
71+
androidTestImplementation(platform(libs.androidx.compose.bom))
72+
androidTestImplementation(libs.androidx.ui.test.junit4)
73+
debugImplementation(libs.androidx.ui.tooling)
74+
debugImplementation(libs.androidx.ui.test.manifest)
75+
}

firebase-ai/app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:dataExtractionRules="@xml/data_extraction_rules"
8+
android:fullBackupContent="@xml/backup_rules"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/Theme.FirebaseAIServices"
14+
tools:targetApi="31">
15+
<activity
16+
android:name=".MainActivity"
17+
android:exported="true"
18+
android:label="@string/app_name"
19+
android:theme="@style/Theme.FirebaseAIServices">
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN" />
22+
23+
<category android:name="android.intent.category.LAUNCHER" />
24+
</intent-filter>
25+
</activity>
26+
</application>
27+
28+
</manifest>
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
package com.google.firebase.quickstart.ai
2+
3+
import com.google.firebase.ai.type.ResponseModality
4+
import com.google.firebase.ai.type.content
5+
import com.google.firebase.ai.type.generationConfig
6+
import com.google.firebase.quickstart.ai.ui.navigation.Category
7+
import com.google.firebase.quickstart.ai.ui.navigation.Sample
8+
9+
val FIREBASE_AI_SAMPLES = listOf(
10+
Sample(
11+
title = "Travel tips",
12+
description = "The user wants the model to help a new traveler" +
13+
" with travel tips",
14+
navRoute = "chat",
15+
categories = listOf(Category.TEXT),
16+
systemInstructions = content {
17+
text(
18+
"You are a Travel assistant. You will answer" +
19+
" questions the user asks based on the information listed" +
20+
" in Relevant Information. Do not hallucinate. Do not use" +
21+
" the internet."
22+
)
23+
},
24+
chatHistory = listOf(
25+
content {
26+
role = "user"
27+
text("I have never traveled before. When should I book a flight?")
28+
},
29+
content {
30+
role = "model"
31+
text(
32+
"You should book flights a couple of months ahead of time." +
33+
" It will be cheaper and more flexible for you."
34+
)
35+
},
36+
content {
37+
role = "user"
38+
text("Do I need a passport?")
39+
},
40+
content {
41+
role = "model"
42+
text(
43+
"If you are traveling outside your own country, make sure" +
44+
" your passport is up-to-date and valid for more" +
45+
" than 6 months during your travel."
46+
)
47+
}
48+
),
49+
initialPrompt = content { text("What else is important when traveling?") }
50+
),
51+
Sample(
52+
title = "Chatbot recommendations for courses",
53+
description = "A chatbot suggests courses for a performing arts program.",
54+
navRoute = "chat",
55+
categories = listOf(Category.TEXT),
56+
systemInstructions = content {
57+
text(
58+
"You are a chatbot for the county's performing and fine arts" +
59+
" program. You help students decide what course they will" +
60+
" take during the summer."
61+
)
62+
},
63+
initialPrompt = content {
64+
text("I am interested in Performing Arts. I have taken Theater 1A.")
65+
}
66+
),
67+
Sample(
68+
title = "Audio Summarization",
69+
description = "Summarize an audio file",
70+
navRoute = "chat",
71+
categories = listOf(Category.AUDIO),
72+
chatHistory = listOf(
73+
content { text("Can you help me summarize an audio file?") },
74+
content("model") {
75+
text(
76+
"Of course! Click on the attach button" +
77+
" below and choose an audio file for me to summarize."
78+
)
79+
}
80+
),
81+
initialPrompt = content {
82+
text(
83+
"I have attached the audio file. Please analyze it and summarize the contents" +
84+
" of the audio as bullet points."
85+
)
86+
}
87+
),
88+
Sample(
89+
title = "Translation from audio",
90+
description = "Translate an audio file",
91+
navRoute = "chat",
92+
categories = listOf(Category.AUDIO),
93+
initialPrompt = content {
94+
fileData(
95+
"https://storage.googleapis.com/cloud-samples-data/generative-ai/audio/" +
96+
"How_to_create_a_My_Map_in_Google_Maps.mp3",
97+
"audio/mpeg"
98+
)
99+
text("Please translate the audio to Mandarin.")
100+
}
101+
),
102+
Sample(
103+
title = "Blog post creator",
104+
description = "Create a blog post from an image file.",
105+
navRoute = "chat",
106+
categories = listOf(Category.IMAGE),
107+
initialPrompt = content {
108+
fileData(
109+
"https://storage.googleapis.com/cloud-samples-data/generative-ai/image/meal-prep.jpeg",
110+
"image/jpeg"
111+
)
112+
text(
113+
"Write a short, engaging blog post based on this picture." +
114+
" It should include a description of the meal in the" +
115+
" photo and talk about my journey meal prepping."
116+
)
117+
}
118+
),
119+
Sample(
120+
title = "Imagen 3 - image generation",
121+
description = "Generate images using Imagen 3",
122+
navRoute = "imagen",
123+
categories = listOf(Category.IMAGE),
124+
initialPrompt = content {
125+
text(
126+
"A photo of a modern building with water in the background"
127+
)
128+
}
129+
),
130+
Sample(
131+
title = "Gemini 2.0 Flash - image generation",
132+
description = "Generate and/or edit images using Gemini 2.0 Flash",
133+
navRoute = "chat",
134+
categories = listOf(Category.IMAGE),
135+
modelName = "gemini-2.0-flash-preview-image-generation",
136+
initialPrompt = content {
137+
text(
138+
"Hi, can you create a 3d rendered image of a pig " +
139+
"with wings and a top hat flying over a happy " +
140+
"futuristic scifi city with lots of greenery?"
141+
)
142+
},
143+
generationConfig = generationConfig {
144+
responseModalities = listOf(ResponseModality.TEXT, ResponseModality.IMAGE)
145+
}
146+
),
147+
Sample(
148+
title = "Document comparison",
149+
description = "Compare the contents of 2 documents",
150+
navRoute = "chat",
151+
categories = listOf(Category.DOCUMENT),
152+
initialPrompt = content {
153+
fileData(
154+
"https://storage.googleapis.com/cloud-samples-data/generative-ai/pdf/form_1040_2013.pdf",
155+
"application/pdf"
156+
)
157+
fileData(
158+
"https://storage.googleapis.com/cloud-samples-data/generative-ai/pdf/form_1040_2023.pdf",
159+
"application/pdf"
160+
)
161+
text(
162+
"The first document is from 2013, and the second document is" +
163+
" from 2023. How did the standard deduction evolve?"
164+
)
165+
}
166+
),
167+
Sample(
168+
title = "Hashtags for a video",
169+
description = "Generate hashtags for a video ad",
170+
navRoute = "chat",
171+
categories = listOf(Category.VIDEO),
172+
initialPrompt = content {
173+
fileData(
174+
"https://storage.googleapis.com/cloud-samples-data/generative-ai/video/google_home_celebrity_ad.mp4",
175+
"video/mpeg"
176+
)
177+
text(
178+
"Generate 5-10 hashtags that relate to the video content." +
179+
" Try to use more popular and engaging terms," +
180+
" e.g. #Viral. Do not add content not related to" +
181+
" the video.\n Start the output with 'Tags:'"
182+
)
183+
}
184+
),
185+
Sample(
186+
title = "Summarize video",
187+
description = "Summarize a video and extract important dialogue.",
188+
navRoute = "chat",
189+
categories = listOf(Category.VIDEO),
190+
chatHistory = listOf(
191+
content { text("Can you help me with the description of a video file?") },
192+
content("model") {
193+
text(
194+
"Sure! Click on the attach button below and choose a" +
195+
" video file for me to describe."
196+
)
197+
}
198+
),
199+
initialPrompt = content {
200+
text(
201+
"I have attached the video file. Provide a description of" +
202+
" the video. The description should also contain" +
203+
" anything important which people say in the video."
204+
)
205+
}
206+
)
207+
)

0 commit comments

Comments
 (0)