-
Notifications
You must be signed in to change notification settings - Fork 4.6k
[Gemini] Add Java GeminiModelHandler class #39245
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
Open
jrmccluskey
wants to merge
6
commits into
apache:master
Choose a base branch
from
jrmccluskey:javaGemini
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+684
−12
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c2542c5
[Gemini] Add Java GeminiModelHandler class
jrmccluskey f1d81e5
Remove model scratch files
jrmccluskey 799de4d
address review comments
jrmccluskey 703c158
checkstyle issue
jrmccluskey 8aee688
unit test fix
jrmccluskey 9309eca
Fix weird coder UX
jrmccluskey 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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. | ||
| */ | ||
| plugins { | ||
| id 'org.apache.beam.module' | ||
| } | ||
|
|
||
| applyJavaNature( | ||
| automaticModuleName: 'org.apache.beam.sdk.ml.inference.gemini', | ||
| requireJavaVersion: JavaVersion.VERSION_11 | ||
| ) | ||
| provideIntegrationTestingDependencies() | ||
| enableJavaPerformanceTesting() | ||
|
|
||
| description = "Apache Beam :: SDKs :: Java :: ML :: Inference :: Gemini" | ||
| ext.summary = "Gemini model handler for remote inference" | ||
|
|
||
| dependencies { | ||
| implementation project(":sdks:java:ml:inference:remote") | ||
| implementation "com.google.genai:google-genai:1.59.0" | ||
| implementation library.java.jackson_databind | ||
| implementation library.java.jackson_annotations | ||
| implementation library.java.jackson_core | ||
|
|
||
| testRuntimeOnly project(path: ":runners:direct-java", configuration: "shadow") | ||
| testImplementation project(path: ":sdks:java:core", configuration: "shadow") | ||
| testImplementation library.java.slf4j_api | ||
| testRuntimeOnly library.java.slf4j_simple | ||
| testImplementation library.java.junit | ||
| testImplementation library.java.mockito_core | ||
| } |
64 changes: 64 additions & 0 deletions
64
...emini/src/main/java/org/apache/beam/sdk/ml/inference/gemini/GeminiInferenceFunctions.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,64 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.beam.sdk.ml.inference.gemini; | ||
|
|
||
| import com.google.genai.types.GenerateContentConfig; | ||
| import com.google.genai.types.GenerateContentResponse; | ||
| import com.google.genai.types.GenerateImagesConfig; | ||
| import com.google.genai.types.GenerateImagesResponse; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** Common inference functions for Gemini. */ | ||
| public class GeminiInferenceFunctions { | ||
|
|
||
| /** Generates content from string prompts using the standard generateContent API. */ | ||
| public static GeminiRequestFunction<String, String> generateFromString() { | ||
| return (modelName, batch, client) -> { | ||
| List<String> results = new ArrayList<>(); | ||
| for (String input : batch) { | ||
| GenerateContentResponse response = | ||
| client.models.generateContent( | ||
| modelName, input, GenerateContentConfig.builder().build()); | ||
| String text = response.text(); | ||
| results.add(text != null ? text : ""); | ||
| } | ||
| return results; | ||
| }; | ||
| } | ||
|
|
||
| /** Generates images from string prompts using the generateImages API. */ | ||
| public static GeminiRequestFunction<String, byte[]> generateImageFromString() { | ||
| return (modelName, batch, client) -> { | ||
| List<byte[]> results = new ArrayList<>(); | ||
| for (String input : batch) { | ||
| GenerateImagesResponse response = | ||
| client.models.generateImages(modelName, input, GenerateImagesConfig.builder().build()); | ||
| // Retrieve the base64 string or bytes from the first generated image | ||
| List<com.google.genai.types.Image> images = response.images(); | ||
| if (images != null && !images.isEmpty()) { | ||
| byte[] imageBytes = images.get(0).imageBytes().orElse(new byte[0]); | ||
| results.add(imageBytes); | ||
| } else { | ||
| results.add(new byte[0]); | ||
| } | ||
| } | ||
| return results; | ||
| }; | ||
| } | ||
| } |
84 changes: 84 additions & 0 deletions
84
...ence/gemini/src/main/java/org/apache/beam/sdk/ml/inference/gemini/GeminiModelHandler.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,84 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.beam.sdk.ml.inference.gemini; | ||
|
|
||
| import com.google.genai.Client; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.apache.beam.sdk.ml.inference.remote.BaseModelHandler; | ||
| import org.apache.beam.sdk.ml.inference.remote.PredictionResult; | ||
|
|
||
| /** | ||
| * Model handler for Google Gemini API inference requests. | ||
| * | ||
| * <p>This handler manages communication with Google's Gemini API, including client initialization, | ||
| * request formatting, and response parsing. It allows executing a custom {@link | ||
| * GeminiRequestFunction} against a batch of inputs. | ||
| */ | ||
| @SuppressWarnings("nullness") | ||
| public class GeminiModelHandler<InputT, OutputT> | ||
| implements BaseModelHandler<GeminiModelParameters<InputT, OutputT>, InputT, OutputT> { | ||
|
|
||
| private transient Client client; | ||
| private GeminiModelParameters<InputT, OutputT> modelParameters; | ||
|
|
||
| @Override | ||
| public void createClient(GeminiModelParameters<InputT, OutputT> parameters) { | ||
| if (parameters == null) { | ||
| throw new NullPointerException("GeminiModelParameters must not be null"); | ||
| } | ||
| this.modelParameters = parameters; | ||
|
|
||
| // Configure client based on vertex or API key | ||
| if (parameters.getApiKey() != null) { | ||
| if (parameters.getProject() != null || parameters.getLocation() != null) { | ||
| throw new IllegalArgumentException("Project and location must be null if API key is set"); | ||
| } | ||
| this.client = Client.builder().apiKey(parameters.getApiKey()).build(); | ||
| } else { | ||
| Client.Builder builder = Client.builder(); | ||
| if (parameters.getProject() != null && parameters.getLocation() != null) { | ||
| builder.vertexAI(true).project(parameters.getProject()).location(parameters.getLocation()); | ||
| } else if (parameters.getProject() != null || parameters.getLocation() != null) { | ||
| throw new IllegalArgumentException( | ||
| "Project and location must both be provided if one is provided"); | ||
| } | ||
| this.client = builder.build(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Iterable<PredictionResult<InputT, OutputT>> request(List<InputT> input) { | ||
| try { | ||
| GeminiRequestFunction<InputT, OutputT> requestFn = modelParameters.getRequestFn(); | ||
| List<OutputT> responses = requestFn.apply(modelParameters.getModelName(), input, client); | ||
|
|
||
| if (responses.size() != input.size()) { | ||
| throw new IllegalStateException("Number of responses must match number of inputs"); | ||
| } | ||
|
|
||
| List<PredictionResult<InputT, OutputT>> results = new ArrayList<>(); | ||
| for (int i = 0; i < input.size(); i++) { | ||
| results.add(PredictionResult.create(input.get(i), responses.get(i))); | ||
| } | ||
| return results; | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Error during Gemini inference request", e); | ||
| } | ||
| } | ||
|
jrmccluskey marked this conversation as resolved.
|
||
| } | ||
56 changes: 56 additions & 0 deletions
56
...e/gemini/src/main/java/org/apache/beam/sdk/ml/inference/gemini/GeminiModelParameters.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,56 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.beam.sdk.ml.inference.gemini; | ||
|
|
||
| import com.google.auto.value.AutoValue; | ||
| import org.apache.beam.sdk.ml.inference.remote.BaseModelParameters; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
|
||
| @AutoValue | ||
| public abstract class GeminiModelParameters<InputT, OutputT> implements BaseModelParameters { | ||
|
|
||
| public abstract @Nullable String getApiKey(); | ||
|
|
||
| public abstract @Nullable String getProject(); | ||
|
|
||
| public abstract @Nullable String getLocation(); | ||
|
|
||
| public abstract String getModelName(); | ||
|
|
||
| public abstract GeminiRequestFunction<InputT, OutputT> getRequestFn(); | ||
|
|
||
| public static <InputT, OutputT> Builder<InputT, OutputT> builder() { | ||
| return new AutoValue_GeminiModelParameters.Builder<InputT, OutputT>(); | ||
| } | ||
|
|
||
| @AutoValue.Builder | ||
| public abstract static class Builder<InputT, OutputT> { | ||
| public abstract Builder<InputT, OutputT> setApiKey(String apiKey); | ||
|
|
||
| public abstract Builder<InputT, OutputT> setProject(String project); | ||
|
|
||
| public abstract Builder<InputT, OutputT> setLocation(String location); | ||
|
|
||
| public abstract Builder<InputT, OutputT> setModelName(String modelName); | ||
|
|
||
| public abstract Builder<InputT, OutputT> setRequestFn( | ||
| GeminiRequestFunction<InputT, OutputT> requestFn); | ||
|
|
||
| public abstract GeminiModelParameters<InputT, OutputT> build(); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
...e/gemini/src/main/java/org/apache/beam/sdk/ml/inference/gemini/GeminiRequestFunction.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,28 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.beam.sdk.ml.inference.gemini; | ||
|
|
||
| import com.google.genai.Client; | ||
| import java.io.Serializable; | ||
| import java.util.List; | ||
|
|
||
| /** Functional interface for custom request functions to the Gemini API. */ | ||
| @FunctionalInterface | ||
| public interface GeminiRequestFunction<InputT, OutputT> extends Serializable { | ||
| List<OutputT> apply(String modelName, List<InputT> batch, Client client) throws Exception; | ||
| } |
20 changes: 20 additions & 0 deletions
20
.../inference/gemini/src/main/java/org/apache/beam/sdk/ml/inference/gemini/package-info.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,20 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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. | ||
| */ | ||
|
|
||
| /** Gemini model handler for remote inference. */ | ||
| package org.apache.beam.sdk.ml.inference.gemini; |
Oops, something went wrong.
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.