diff --git a/misc/src/main/java/com/example/snippets/ai/GeminiOverview.kt b/misc/src/main/java/com/example/snippets/ai/GeminiOverview.kt new file mode 100644 index 000000000..2dc02e70a --- /dev/null +++ b/misc/src/main/java/com/example/snippets/ai/GeminiOverview.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2025 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.snippets.ai + +import com.google.firebase.Firebase +import com.google.firebase.ai.ai +import com.google.firebase.ai.type.GenerativeBackend + +class GeminiOverview { + + suspend fun generateContent() { + // [START android_gemini_ai_models_overview] + // For Vertex AI, use `backend = GenerativeBackend.vertexAI()` + val model = Firebase.ai(backend = GenerativeBackend.googleAI()) + .generativeModel("gemini-2.5-flash") + + val response = model.generateContent("Write a story about a magic backpack") + val output = response.text + // [END android_gemini_ai_models_overview] + } +} diff --git a/misc/src/main/java/com/example/snippets/ai/GeminiOverviewJava.java b/misc/src/main/java/com/example/snippets/ai/GeminiOverviewJava.java new file mode 100644 index 000000000..e9ab81aee --- /dev/null +++ b/misc/src/main/java/com/example/snippets/ai/GeminiOverviewJava.java @@ -0,0 +1,61 @@ +/* + * Copyright 2025 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.snippets.ai; + +import com.google.common.util.concurrent.FutureCallback; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.firebase.ai.FirebaseAI; +import com.google.firebase.ai.GenerativeModel; +import com.google.firebase.ai.java.GenerativeModelFutures; +import com.google.firebase.ai.type.Content; +import com.google.firebase.ai.type.GenerateContentResponse; +import com.google.firebase.ai.type.GenerativeBackend; +import java.util.concurrent.Executor; + +class GeminiOverviewJava { + + void generateContent(Executor executor) { + // [START android_gemini_ai_models_overview_java] + // For Vertex AI, use `backend = GenerativeBackend.vertexAI()` + GenerativeModel firebaseAI = FirebaseAI.getInstance(GenerativeBackend.googleAI()) + .generativeModel("gemini-2.5-flash"); + + // Use the GenerativeModelFutures Java compatibility layer which offers + // support for ListenableFuture and Publisher APIs + GenerativeModelFutures model = GenerativeModelFutures.from(firebaseAI); + + Content prompt = new Content.Builder() + .addText("Write a story about a magic backpack.") + .build(); + + ListenableFuture response = model.generateContent(prompt); + Futures.addCallback(response, new FutureCallback() { + @Override + public void onSuccess(GenerateContentResponse result) { + String resultText = result.getText(); + // ... + } + + @Override + public void onFailure(Throwable t) { + t.printStackTrace(); + } + }, executor); + // [END android_gemini_ai_models_overview_java] + } +} diff --git a/misc/src/main/java/com/example/snippets/ai/VertexAiGeminiApi.kt b/misc/src/main/java/com/example/snippets/ai/VertexAiGeminiApi.kt new file mode 100644 index 000000000..7f2ae2172 --- /dev/null +++ b/misc/src/main/java/com/example/snippets/ai/VertexAiGeminiApi.kt @@ -0,0 +1,41 @@ +/* + * Copyright 2025 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.snippets.ai + +import com.google.firebase.Firebase +import com.google.firebase.ai.ai +import com.google.firebase.ai.type.GenerativeBackend +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.launch + +class VertexAiGeminiApi { + + // [START android_snippets_vertex_ai_gemini_api_model] + val model = Firebase.ai(backend = GenerativeBackend.vertexAI()) + .generativeModel("gemini-2.5-flash") + // [END android_snippets_vertex_ai_gemini_api_model] + + fun generateText(scope: CoroutineScope) { + // [START android_snippets_vertex_ai_generate_content] + // Note: generateContent() is a suspend function, which integrates well + // with existing Kotlin code. + scope.launch { + val response = model.generateContent("Write a story about a magic backpack.") + } + // [END android_snippets_vertex_ai_generate_content] + } +} diff --git a/misc/src/main/java/com/example/snippets/ai/VertexAiGeminiApiJava.java b/misc/src/main/java/com/example/snippets/ai/VertexAiGeminiApiJava.java new file mode 100644 index 000000000..1877b7852 --- /dev/null +++ b/misc/src/main/java/com/example/snippets/ai/VertexAiGeminiApiJava.java @@ -0,0 +1,61 @@ +/* + * Copyright 2025 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.snippets.ai; + +import com.google.common.util.concurrent.FutureCallback; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.firebase.ai.FirebaseAI; +import com.google.firebase.ai.GenerativeModel; +import com.google.firebase.ai.java.GenerativeModelFutures; +import com.google.firebase.ai.type.Content; +import com.google.firebase.ai.type.GenerateContentResponse; +import com.google.firebase.ai.type.GenerativeBackend; + +import java.util.concurrent.Executor; + +public class VertexAiGeminiApiJava { + + // [START android_snippets_vertex_ai_gemini_api_model_java] + GenerativeModel firebaseAI = FirebaseAI.getInstance(GenerativeBackend.vertexAI()) + .generativeModel("gemini-2.5-flash"); + + GenerativeModelFutures model = GenerativeModelFutures.from(firebaseAI); + // [END android_snippets_vertex_ai_gemini_api_model_java] + + void generateText(Executor executor) { + // [START android_snippets_vertex_ai_generate_content_java] + Content prompt = new Content.Builder() + .addText("Write a story about a magic backpack.") + .build(); + + ListenableFuture response = model.generateContent(prompt); + Futures.addCallback(response, new FutureCallback() { + @Override + public void onSuccess(GenerateContentResponse result) { + String resultText = result.getText(); + // ... + } + + @Override + public void onFailure(Throwable t) { + t.printStackTrace(); + } + }, executor); + // [END android_snippets_vertex_ai_generate_content_java] + } +}