Skip to content

Commit 0c85315

Browse files
authored
Add Gemini overview and Vertex AI Gemini API snippets (#686)
Code snippets are from these existing pages: https://developer.android.com/ai/gemini https://developer.android.com/ai/vertex-ai-firebase Co-authored-by: Kat Kuan <kkuan2011@users.noreply.github.com>
1 parent f43eaed commit 0c85315

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.snippets.ai
18+
19+
import com.google.firebase.Firebase
20+
import com.google.firebase.ai.ai
21+
import com.google.firebase.ai.type.GenerativeBackend
22+
23+
class GeminiOverview {
24+
25+
suspend fun generateContent() {
26+
// [START android_gemini_ai_models_overview]
27+
// For Vertex AI, use `backend = GenerativeBackend.vertexAI()`
28+
val model = Firebase.ai(backend = GenerativeBackend.googleAI())
29+
.generativeModel("gemini-2.5-flash")
30+
31+
val response = model.generateContent("Write a story about a magic backpack")
32+
val output = response.text
33+
// [END android_gemini_ai_models_overview]
34+
}
35+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.snippets.ai;
18+
19+
import com.google.common.util.concurrent.FutureCallback;
20+
import com.google.common.util.concurrent.Futures;
21+
import com.google.common.util.concurrent.ListenableFuture;
22+
import com.google.firebase.ai.FirebaseAI;
23+
import com.google.firebase.ai.GenerativeModel;
24+
import com.google.firebase.ai.java.GenerativeModelFutures;
25+
import com.google.firebase.ai.type.Content;
26+
import com.google.firebase.ai.type.GenerateContentResponse;
27+
import com.google.firebase.ai.type.GenerativeBackend;
28+
import java.util.concurrent.Executor;
29+
30+
class GeminiOverviewJava {
31+
32+
void generateContent(Executor executor) {
33+
// [START android_gemini_ai_models_overview_java]
34+
// For Vertex AI, use `backend = GenerativeBackend.vertexAI()`
35+
GenerativeModel firebaseAI = FirebaseAI.getInstance(GenerativeBackend.googleAI())
36+
.generativeModel("gemini-2.5-flash");
37+
38+
// Use the GenerativeModelFutures Java compatibility layer which offers
39+
// support for ListenableFuture and Publisher APIs
40+
GenerativeModelFutures model = GenerativeModelFutures.from(firebaseAI);
41+
42+
Content prompt = new Content.Builder()
43+
.addText("Write a story about a magic backpack.")
44+
.build();
45+
46+
ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
47+
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
48+
@Override
49+
public void onSuccess(GenerateContentResponse result) {
50+
String resultText = result.getText();
51+
// ...
52+
}
53+
54+
@Override
55+
public void onFailure(Throwable t) {
56+
t.printStackTrace();
57+
}
58+
}, executor);
59+
// [END android_gemini_ai_models_overview_java]
60+
}
61+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.snippets.ai
18+
19+
import com.google.firebase.Firebase
20+
import com.google.firebase.ai.ai
21+
import com.google.firebase.ai.type.GenerativeBackend
22+
import kotlinx.coroutines.CoroutineScope
23+
import kotlinx.coroutines.launch
24+
25+
class VertexAiGeminiApi {
26+
27+
// [START android_snippets_vertex_ai_gemini_api_model]
28+
val model = Firebase.ai(backend = GenerativeBackend.vertexAI())
29+
.generativeModel("gemini-2.5-flash")
30+
// [END android_snippets_vertex_ai_gemini_api_model]
31+
32+
fun generateText(scope: CoroutineScope) {
33+
// [START android_snippets_vertex_ai_generate_content]
34+
// Note: generateContent() is a suspend function, which integrates well
35+
// with existing Kotlin code.
36+
scope.launch {
37+
val response = model.generateContent("Write a story about a magic backpack.")
38+
}
39+
// [END android_snippets_vertex_ai_generate_content]
40+
}
41+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.snippets.ai;
18+
19+
import com.google.common.util.concurrent.FutureCallback;
20+
import com.google.common.util.concurrent.Futures;
21+
import com.google.common.util.concurrent.ListenableFuture;
22+
import com.google.firebase.ai.FirebaseAI;
23+
import com.google.firebase.ai.GenerativeModel;
24+
import com.google.firebase.ai.java.GenerativeModelFutures;
25+
import com.google.firebase.ai.type.Content;
26+
import com.google.firebase.ai.type.GenerateContentResponse;
27+
import com.google.firebase.ai.type.GenerativeBackend;
28+
29+
import java.util.concurrent.Executor;
30+
31+
public class VertexAiGeminiApiJava {
32+
33+
// [START android_snippets_vertex_ai_gemini_api_model_java]
34+
GenerativeModel firebaseAI = FirebaseAI.getInstance(GenerativeBackend.vertexAI())
35+
.generativeModel("gemini-2.5-flash");
36+
37+
GenerativeModelFutures model = GenerativeModelFutures.from(firebaseAI);
38+
// [END android_snippets_vertex_ai_gemini_api_model_java]
39+
40+
void generateText(Executor executor) {
41+
// [START android_snippets_vertex_ai_generate_content_java]
42+
Content prompt = new Content.Builder()
43+
.addText("Write a story about a magic backpack.")
44+
.build();
45+
46+
ListenableFuture<GenerateContentResponse> response = model.generateContent(prompt);
47+
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
48+
@Override
49+
public void onSuccess(GenerateContentResponse result) {
50+
String resultText = result.getText();
51+
// ...
52+
}
53+
54+
@Override
55+
public void onFailure(Throwable t) {
56+
t.printStackTrace();
57+
}
58+
}, executor);
59+
// [END android_snippets_vertex_ai_generate_content_java]
60+
}
61+
}

0 commit comments

Comments
 (0)