-
Notifications
You must be signed in to change notification settings - Fork 693
Add java snippets to compilation tests #8290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+204
−12
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
ai-logic/firebase-ai/src/testUtil/java/com/google/firebase/ai/JavaSnippetCompileTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * 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 | ||
| * | ||
| * http://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 java.com.google.firebase.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.TemplateChat; | ||
| import com.google.firebase.ai.TemplateGenerativeModel; | ||
| import com.google.firebase.ai.java.TemplateChatFutures; | ||
| import com.google.firebase.ai.type.Content; | ||
| import com.google.firebase.ai.type.FunctionCallPart; | ||
| import com.google.firebase.ai.type.GenerateContentResponse; | ||
| import com.google.firebase.ai.type.GenerativeBackend; | ||
| import com.google.firebase.ai.type.JsonSchema; | ||
| import com.google.firebase.ai.type.RequestOptions; | ||
| import com.google.firebase.ai.type.TemplateFunctionDeclaration; | ||
| import com.google.firebase.ai.type.TemplateTool; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.Executor; | ||
| import java.util.concurrent.Executors; | ||
| import kotlinx.serialization.json.JsonObject; | ||
| import kotlinx.serialization.json.JsonPrimitive; | ||
|
|
||
| /** | ||
| * Contains various Java snippets used in documentation for Gemini/Vertex AI. | ||
| * Compilation is a good litmus test that the snippets are sane. | ||
| */ | ||
| public class JavaSnippetCompileTests { | ||
| // Various stubs for compilation | ||
| private final Executor executor = Executors.newSingleThreadExecutor(); | ||
|
|
||
| private Object fetchWeather(String city, String state, String date) { | ||
| return null; // Stub | ||
| } | ||
|
|
||
| // Snippets | ||
|
|
||
| // https://firebase.google.com/docs/ai-logic/server-prompt-templates/multi-turn-interactions?api=dev#chat-use-template-in-code | ||
| public void template_chat() { | ||
| // ... | ||
|
emilypgoogle marked this conversation as resolved.
|
||
|
|
||
| // Initialize the Vertex AI Gemini API backend service. | ||
| TemplateGenerativeModel templateModel = | ||
| FirebaseAI.getInstance(GenerativeBackend.vertexAI("global")).templateGenerativeModel(); | ||
| // Initialize TemplateChat with history and inputs | ||
| TemplateChat templateChat = | ||
| templateModel.startChat("my-chat-template-v1-0-0", Map.of(), Collections.emptyList()); | ||
| TemplateChatFutures chatFutures = TemplateChatFutures.from(templateChat); | ||
|
|
||
| // Send a message asynchronously | ||
| Content prompt = new Content.Builder().addText("I need a copy of my invoice.").build(); | ||
| ListenableFuture<GenerateContentResponse> responseFuture = chatFutures.sendMessage(prompt); | ||
|
|
||
| Futures.addCallback( | ||
| responseFuture, | ||
| new FutureCallback<GenerateContentResponse>() { | ||
| @Override | ||
| public void onSuccess(GenerateContentResponse result) { | ||
| System.out.println("Response: " + result.getText()); | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(Throwable t) { | ||
| t.printStackTrace(); | ||
| } | ||
| }, | ||
| executor); | ||
| } | ||
|
|
||
| // https://firebase.google.com/docs/ai-logic/server-prompt-templates/multi-turn-interactions?api=dev#function-calling-use-template-in-code | ||
| public void template_functionCallingSchemaInTemplate(String userMessage) { | ||
| // ... | ||
|
|
||
| // Initialize the Vertex AI Gemini API backend service. | ||
| // Create a `TemplateGenerativeModel` instance. | ||
| TemplateGenerativeModel model = | ||
| FirebaseAI.getInstance(GenerativeBackend.vertexAI("global")).templateGenerativeModel(); | ||
|
|
||
| // Start a chat session with a template that has functions listed as tools. | ||
| TemplateChat chatSession = | ||
| model.startChat( | ||
| "my-function-calling-template-v1-0-0", // Template ID | ||
| Map.of(), // Inputs | ||
| List.of() // History | ||
| ); | ||
| TemplateChatFutures chatFutures = TemplateChatFutures.from(chatSession); | ||
|
|
||
| // Send a message that might trigger a function call. | ||
| Content prompt = new Content.Builder().addText(userMessage).build(); | ||
| ListenableFuture<GenerateContentResponse> responseFuture = chatFutures.sendMessage(prompt); | ||
|
|
||
| Futures.addCallback( | ||
| responseFuture, | ||
| new FutureCallback<GenerateContentResponse>() { | ||
| @Override | ||
| public void onSuccess(GenerateContentResponse result) { | ||
| // When the model responds with one or more function calls, invoke the function(s). | ||
| // Note that this is the same as when *not* using server prompt templates. | ||
| for (FunctionCallPart callPart : result.getFunctionCalls()) { | ||
| if ("fetchWeather".equals(callPart.getName())) { | ||
| // Forward the structured input data from the model to the hypothetical external | ||
| // API. | ||
| JsonObject location = (JsonObject) callPart.getArgs().get("location"); | ||
| String city = ((JsonPrimitive) location.get("city")).getContent(); | ||
| String state = ((JsonPrimitive) location.get("state")).getContent(); | ||
| String date = ((JsonPrimitive) callPart.getArgs().get("date")).getContent(); | ||
| Object response = fetchWeather(city, state, date); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void onFailure(Throwable t) { | ||
| t.printStackTrace(); | ||
| } | ||
| }, | ||
| executor); | ||
| } | ||
|
|
||
| // https://firebase.google.com/docs/ai-logic/server-prompt-templates/multi-turn-interactions?api=dev#function-calling-schema-defined-in-code | ||
| public void template_functionCallingSchemaInCode() { | ||
| TemplateFunctionDeclaration tool = | ||
| new TemplateFunctionDeclaration( | ||
| "fetchWeather", // Name | ||
| JsonSchema.obj( // Parameters | ||
| Map.of( | ||
| "location", | ||
| JsonSchema.obj( | ||
| Map.of( | ||
| "city", JsonSchema.string("The city of the location."), | ||
| "state", JsonSchema.string("The state of the location."), | ||
| "zipCode", | ||
| JsonSchema.string( | ||
| "Optional zip code of the location", true // Nullable | ||
| )), | ||
| List.of("zipCode"), | ||
| ""), | ||
| "date", | ||
| JsonSchema.string( | ||
| "The date for which to get the weather. Date must be in the format: YY-MM-DD."), | ||
| "unit", | ||
| JsonSchema.enumeration( | ||
| List.of("CELSIUS", "FAHRENHEIT"), | ||
| "The temperature unit.", | ||
| true // Nullable | ||
| ))), | ||
| JsonSchema.string("A description of the current weather.")); | ||
| TemplateGenerativeModel templateModel = | ||
| FirebaseAI.getInstance(GenerativeBackend.vertexAI("global")) | ||
| .templateGenerativeModel( | ||
| new RequestOptions(), | ||
| List.of(TemplateTool.functionDeclarations(List.of(tool), List.of()))); | ||
| TemplateChat chat = | ||
| templateModel.startChat( | ||
| "my-function-calling-template-with-no-function-schema-v1-0-0", Map.of(), List.of()); | ||
| TemplateChatFutures futures = TemplateChatFutures.from(chat); | ||
|
|
||
| // Send a message that might trigger a function call. | ||
| // ... | ||
|
|
||
| // When the model responds with one or more function calls, invoke the function(s). | ||
| // Note that this is the same as when *not* using server prompt templates. | ||
| // ... | ||
|
|
||
| // Forward the structured input data from the model to the hypothetical external API. | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.