-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(genai): Add samples for video, batch prediction, vertexAI and image generation #5380
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
cfloress
wants to merge
10
commits into
GoogleCloudPlatform:main
Choose a base branch
from
cfloress:genai-video-vais-image-generation
base: main
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.
+992
−0
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5bcd48d
genai: added vais, video gen samples
cfloress c392f43
Merge branch 'main' of github.com:GoogleCloudPlatform/golang-samples …
cfloress 4f899b6
genai: added batch prediction samples
cfloress a8ad104
Merge branch 'main' of github.com:GoogleCloudPlatform/golang-samples …
cfloress c1cebee
genai: added image generation samples
cfloress f503bb0
Merge branch 'main' into genai-video-vais-image-generation
cfloress 49c065b
genai: PR comments
cfloress b0656c8
Merge branch 'main' into genai-video-vais-image-generation
cfloress 1fd1244
Merge branch 'main' into genai-video-vais-image-generation
msampathkumar a5fd116
Merge branch 'main' into genai-video-vais-image-generation
cfloress 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
99 changes: 99 additions & 0 deletions
99
genai/batch_prediction/batchpredict_embeddings_with_gcs.go
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,99 @@ | ||
// Copyright 2025 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 | ||
// | ||
// 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 batch_prediction shows how to use the GenAI SDK to batch prediction. | ||
|
||
package batch_prediction | ||
|
||
// [START googlegenaisdk_batchpredict_embeddings_with_gcs] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"time" | ||
|
||
"google.golang.org/genai" | ||
) | ||
|
||
// generateBatchEmbeddings shows how to run a batch embeddings prediction job. | ||
func generateBatchEmbeddings(w io.Writer, outputURI string) error { | ||
// outputURI = "gs://your-bucket/your-prefix" | ||
ctx := context.Background() | ||
|
||
client, err := genai.NewClient(ctx, &genai.ClientConfig{ | ||
HTTPOptions: genai.HTTPOptions{APIVersion: "v1"}, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to create genai client: %w", err) | ||
} | ||
modelName := "text-embedding-005" | ||
// See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.batches.Batches.create | ||
job, err := client.Batches.Create(ctx, | ||
modelName, | ||
&genai.BatchJobSource{ | ||
Format: "jsonl", | ||
// Source link: https://storage.cloud.google.com/cloud-samples-data/generative-ai/embeddings/embeddings_input.jsonl | ||
GCSURI: []string{"gs://cloud-samples-data/generative-ai/embeddings/embeddings_input.jsonl"}, | ||
}, | ||
&genai.CreateBatchJobConfig{ | ||
Dest: &genai.BatchJobDestination{ | ||
Format: "jsonl", | ||
GCSURI: outputURI, | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("failed to create batch job: %w", err) | ||
} | ||
|
||
fmt.Fprintf(w, "Job name: %s\n", job.Name) | ||
fmt.Fprintf(w, "Job state: %s\n", job.State) | ||
// Example response: | ||
// Job name: projects/{PROJECT_ID}/locations/us-central1/batchPredictionJobs/9876453210000000000 | ||
// Job state: JOB_STATE_PENDING | ||
|
||
// See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.types.BatchJob | ||
completedStates := map[genai.JobState]bool{ | ||
genai.JobStateSucceeded: true, | ||
genai.JobStateFailed: true, | ||
genai.JobStateCancelled: true, | ||
genai.JobStatePaused: true, | ||
} | ||
|
||
// Poll until job finishes | ||
for !completedStates[job.State] { | ||
time.Sleep(30 * time.Second) | ||
job, err = client.Batches.Get(ctx, job.Name, nil) | ||
if err != nil { | ||
return fmt.Errorf("failed to get batch job: %w", err) | ||
} | ||
fmt.Fprintf(w, "Job state: %s\n", job.State) | ||
|
||
if job.State == genai.JobStateFailed { | ||
fmt.Fprintf(w, "Error: %+v\n", job.Error) | ||
break | ||
} | ||
} | ||
|
||
// Example response: | ||
// Job state: JOB_STATE_PENDING | ||
// Job state: JOB_STATE_RUNNING | ||
// Job state: JOB_STATE_RUNNING | ||
// ... | ||
// Job state: JOB_STATE_SUCCEEDED | ||
|
||
return nil | ||
} | ||
|
||
// [END googlegenaisdk_batchpredict_embeddings_with_gcs] |
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,96 @@ | ||
// Copyright 2025 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 | ||
// | ||
// 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 batch_prediction shows how to use the GenAI SDK to batch prediction. | ||
|
||
package batch_prediction | ||
|
||
// [START googlegenaisdk_batchpredict_with_bq] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"time" | ||
|
||
"google.golang.org/genai" | ||
) | ||
|
||
// generateBatchPredictWithBQ shows how to run a batch prediction job with BigQuery input/output. | ||
func generateBatchPredictWithBQ(w io.Writer, outputURI string) error { | ||
// outputURI = "bq://your-project.your_dataset.your_table" | ||
ctx := context.Background() | ||
|
||
client, err := genai.NewClient(ctx, &genai.ClientConfig{ | ||
HTTPOptions: genai.HTTPOptions{APIVersion: "v1"}, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to create genai client: %w", err) | ||
} | ||
|
||
// BigQuery input | ||
src := &genai.BatchJobSource{ | ||
Format: "bigquery", | ||
BigqueryURI: "bq://storage-samples.generative_ai.batch_requests_for_multimodal_input", | ||
} | ||
|
||
// BigQuery output | ||
config := &genai.CreateBatchJobConfig{ | ||
Dest: &genai.BatchJobDestination{ | ||
Format: "bigquery", | ||
BigqueryURI: outputURI, | ||
}, | ||
} | ||
|
||
// To use a tuned model, set the model param to your tuned model using the following format: | ||
// modelName:= "projects/{PROJECT_ID}/locations/{LOCATION}/models/{MODEL_ID} | ||
modelName := "gemini-2.5-flash" | ||
job, err := client.Batches.Create(ctx, modelName, src, config) | ||
if err != nil { | ||
return fmt.Errorf("failed to create batch job: %w", err) | ||
} | ||
|
||
fmt.Fprintf(w, "Job name: %s\n", job.Name) | ||
fmt.Fprintf(w, "Job state: %s\n", job.State) | ||
// Example response: | ||
// Job name: projects/{PROJECT_ID}/locations/us-central1/batchPredictionJobs/9876453210000000000 | ||
// Job state: JOB_STATE_PENDING | ||
|
||
// See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.types.BatchJob | ||
completedStates := map[genai.JobState]bool{ | ||
genai.JobStateSucceeded: true, | ||
genai.JobStateFailed: true, | ||
genai.JobStateCancelled: true, | ||
genai.JobStatePaused: true, | ||
} | ||
|
||
for !completedStates[job.State] { | ||
time.Sleep(30 * time.Second) | ||
job, err = client.Batches.Get(ctx, job.Name, nil) | ||
if err != nil { | ||
return fmt.Errorf("failed to get batch job: %w", err) | ||
} | ||
fmt.Fprintf(w, "Job state: %s\n", job.State) | ||
} | ||
|
||
// Example response: | ||
// Job state: JOB_STATE_PENDING | ||
// Job state: JOB_STATE_RUNNING | ||
// Job state: JOB_STATE_RUNNING | ||
// ... | ||
// Job state: JOB_STATE_SUCCEEDED | ||
|
||
return nil | ||
} | ||
|
||
// [END googlegenaisdk_batchpredict_with_bq] |
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,99 @@ | ||
// Copyright 2025 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 | ||
// | ||
// 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 batch_prediction shows how to use the GenAI SDK to batch prediction. | ||
|
||
package batch_prediction | ||
|
||
// [START googlegenaisdk_batchpredict_with_gcs] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"time" | ||
|
||
"google.golang.org/genai" | ||
) | ||
|
||
// generateBatchPredict runs a batch prediction job using GCS input/output. | ||
func generateBatchPredict(w io.Writer, outputURI string) error { | ||
// outputURI = "gs://your-bucket/your-prefix" | ||
ctx := context.Background() | ||
|
||
client, err := genai.NewClient(ctx, &genai.ClientConfig{ | ||
HTTPOptions: genai.HTTPOptions{APIVersion: "v1"}, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to create genai client: %w", err) | ||
} | ||
|
||
// Source file with prompts for prediction | ||
src := &genai.BatchJobSource{ | ||
Format: "jsonl", | ||
// Source link: https://storage.cloud.google.com/cloud-samples-data/batch/prompt_for_batch_gemini_predict.jsonl | ||
GCSURI: []string{"gs://cloud-samples-data/batch/prompt_for_batch_gemini_predict.jsonl"}, | ||
} | ||
|
||
// Batch job config with output GCS location | ||
config := &genai.CreateBatchJobConfig{ | ||
Dest: &genai.BatchJobDestination{ | ||
Format: "jsonl", | ||
GCSURI: outputURI, | ||
}, | ||
} | ||
// To use a tuned model, set the model param to your tuned model using the following format: | ||
// modelName:= "projects/{PROJECT_ID}/locations/{LOCATION}/models/{MODEL_ID} | ||
modelName := "gemini-2.5-flash" | ||
// See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.batches.Batches.create | ||
job, err := client.Batches.Create(ctx, modelName, src, config) | ||
if err != nil { | ||
return fmt.Errorf("failed to create batch job: %w", err) | ||
} | ||
|
||
fmt.Fprintf(w, "Job name: %s\n", job.Name) | ||
fmt.Fprintf(w, "Job state: %s\n", job.State) | ||
// Example response: | ||
// Job name: projects/{PROJECT_ID}/locations/us-central1/batchPredictionJobs/9876453210000000000 | ||
// Job state: JOB_STATE_PENDING | ||
|
||
// See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.types.BatchJob | ||
completedStates := map[genai.JobState]bool{ | ||
genai.JobStateSucceeded: true, | ||
genai.JobStateFailed: true, | ||
genai.JobStateCancelled: true, | ||
genai.JobStatePaused: true, | ||
} | ||
|
||
for !completedStates[job.State] { | ||
time.Sleep(30 * time.Second) | ||
|
||
job, err = client.Batches.Get(ctx, job.Name, nil) | ||
if err != nil { | ||
return fmt.Errorf("failed to get batch job: %w", err) | ||
} | ||
|
||
fmt.Fprintf(w, "Job state: %s\n", job.State) | ||
} | ||
cfloress marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Example response: | ||
// Job state: JOB_STATE_PENDING | ||
// Job state: JOB_STATE_RUNNING | ||
// Job state: JOB_STATE_RUNNING | ||
// ... | ||
// Job state: JOB_STATE_SUCCEEDED | ||
|
||
return nil | ||
} | ||
|
||
// [END googlegenaisdk_batchpredict_with_gcs] |
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,80 @@ | ||
// Copyright 2025 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 batch_prediction | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil" | ||
) | ||
|
||
const gcsOutputBucket = "golang-docs-samples-tests" | ||
|
||
func TestBatchPrediction(t *testing.T) { | ||
tc := testutil.SystemTest(t) | ||
|
||
t.Setenv("GOOGLE_GENAI_USE_VERTEXAI", "1") | ||
t.Setenv("GOOGLE_CLOUD_LOCATION", "us-central1") | ||
t.Setenv("GOOGLE_CLOUD_PROJECT", tc.ProjectID) | ||
|
||
prefix := fmt.Sprintf("embeddings_output/%d", time.Now().UnixNano()) | ||
outputURI := fmt.Sprintf("gs://%s/%s", gcsOutputBucket, prefix) | ||
buf := new(bytes.Buffer) | ||
|
||
t.Run("generate batch embeddings with GCS", func(t *testing.T) { | ||
buf.Reset() | ||
err := generateBatchEmbeddings(buf, outputURI) | ||
if err != nil { | ||
t.Fatalf("generateBatchEmbeddings failed: %v", err) | ||
} | ||
|
||
output := buf.String() | ||
if output == "" { | ||
t.Error("expected non-empty output, got empty") | ||
} | ||
}) | ||
|
||
t.Run("generate batch predict with gcs input/output", func(t *testing.T) { | ||
buf.Reset() | ||
err := generateBatchPredict(buf, outputURI) | ||
if err != nil { | ||
t.Fatalf("generateBatchPredict failed: %v", err) | ||
} | ||
|
||
output := buf.String() | ||
if output == "" { | ||
t.Error("expected non-empty output, got empty") | ||
} | ||
|
||
}) | ||
|
||
t.Run("generate batch predict with BigQuery", func(t *testing.T) { | ||
buf.Reset() | ||
outputURIBQ := "bq://your-project.your_dataset.your_table" | ||
cfloress marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
err := generateBatchPredictWithBQ(buf, outputURIBQ) | ||
if err != nil { | ||
t.Fatalf("generateBatchPredictWithBQ failed: %v", err) | ||
} | ||
|
||
output := buf.String() | ||
if output == "" { | ||
t.Error("expected non-empty output, got empty") | ||
} | ||
}) | ||
} |
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.