Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
3768857
genai: Add samples for Tools and Text-generation local images and video
cfloress Sep 2, 2025
90d0887
genai: PR comments
cfloress Sep 2, 2025
6f27726
genai: fix conflict
cfloress Sep 3, 2025
bb71db6
Merge branch 'main' into genai-local-media-content-generation
cfloress Sep 4, 2025
d4ca468
Merge branch 'main' into genai-local-media-content-generation
cfloress Sep 5, 2025
40f3d80
Merge branch 'main' into genai-local-media-content-generation
cfloress Sep 8, 2025
9bf4d51
Merge branch 'main' of github.com:GoogleCloudPlatform/golang-samples …
cfloress Sep 8, 2025
3035ce2
Merge branch 'genai-local-media-content-generation' of github.com:cfl…
cfloress Sep 8, 2025
b8771d4
Merge branch 'main' into genai-local-media-content-generation
msampathkumar Sep 8, 2025
1f617e4
Merge branch 'main' into genai-local-media-content-generation
cfloress Sep 9, 2025
84c1652
Merge branch 'main' into genai-local-media-content-generation
msampathkumar Sep 11, 2025
62549cd
Merge branch 'main' into genai-local-media-content-generation
cfloress Sep 12, 2025
429f280
Merge branch 'genai-local-media-content-generation' of github.com:cfl…
cfloress Sep 15, 2025
a3e92f1
genai: PR comments
cfloress Sep 15, 2025
445507d
Merge branch 'main' into genai-local-media-content-generation
msampathkumar Sep 16, 2025
ad878cd
Merge branch 'main' into genai-local-media-content-generation
msampathkumar Sep 17, 2025
506d424
Merge branch 'main' into genai-local-media-content-generation
msampathkumar Sep 22, 2025
3fa3440
Merge branch 'main' into genai-local-media-content-generation
msampathkumar Sep 25, 2025
cd35a7b
Merge branch 'main' into genai-local-media-content-generation
cfloress Sep 30, 2025
f7c2928
Merge branch 'main' into genai-local-media-content-generation
msampathkumar Oct 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file added genai/text_generation/testdata/latte.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added genai/text_generation/testdata/scones.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions genai/text_generation/text_generation_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,30 @@ func TestTextGeneration(t *testing.T) {
}
})

t.Run("generate with local video file input", func(t *testing.T) {
buf.Reset()
err := generateWithLocalVideo(buf)
if err != nil {
t.Fatalf("generateWithLocalVideo failed: %v", err)
}

output := buf.String()
if output == "" {
t.Error("expected non-empty output, got empty")
}
})

t.Run("generate with local multi local images input", func(t *testing.T) {
buf.Reset()
err := generateWithMultiLocalImages(buf)
if err != nil {
t.Fatalf("generateWithMultiLocalImages failed: %v", err)
}

output := buf.String()
if output == "" {
t.Error("expected non-empty output, got empty")
}
})

}
80 changes: 80 additions & 0 deletions genai/text_generation/textgen_with_local_video.go
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
//
// 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 text_generation shows examples of generating text using the GenAI SDK.
package text_generation

// [START googlegenaisdk_textgen_with_local_video]
import (
"context"
"fmt"
"io"
"os"

genai "google.golang.org/genai"
)

// generateWithLocalVideo shows how to generate text using a local video input.
func generateWithLocalVideo(w io.Writer) error {
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)
}

// Read local video file content
data, err := os.ReadFile("testdata/describe_video_content.mp4")
if err != nil {
return fmt.Errorf("failed to read local video: %w", err)
}

modelName := "gemini-2.5-flash"
contents := []*genai.Content{
{
Role: "user",
Parts: []*genai.Part{
{Text: `Write a short and engaging blog post based on this video.`},
{InlineData: &genai.Blob{
MIMEType: "video/mp4",
Data: data,
}},
},
},
}

resp, err := client.Models.GenerateContent(ctx, modelName, contents, nil)
if err != nil {
return fmt.Errorf("failed to generate content: %w", err)
}

respText := resp.Text()
fmt.Fprintln(w, respText)

// Example response:
// Finding Your Flow: The Focused Ascent
//
// Ever watched someone scale an indoor climbing wall and been captivated by their precision and power? This video perfectly captures that intense focus and calculated movement.
//
// Our climber isn't just pulling himself up; he's engaging in a dynamic dance with gravity. Every reach, every foot placement, every clip of the rope is a deliberate part of solving the route's puzzle. You can almost feel the concentration as his eyes scan for the next optimal hold, his muscles working in unison to propel him upwards.
//
// Indoor climbing....
// ...

return nil
}

// [END googlegenaisdk_textgen_with_local_video]
95 changes: 95 additions & 0 deletions genai/text_generation/textgen_with_multi_local_img.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// 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 text_generation shows examples of generating text using the GenAI SDK.
package text_generation

// [START googlegenaisdk_textgen_with_multi_local_img]
import (
"context"
"fmt"
"io"
"os"

genai "google.golang.org/genai"
)

// generateWithMultiLocalImages shows how to generate text using multiple local image inputs.
func generateWithMultiLocalImages(w io.Writer) error {
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)
}

// Read local image files
image1, err := os.ReadFile("testdata/latte.jpg")
if err != nil {
return fmt.Errorf("failed to read image1: %w", err)
}
image2, err := os.ReadFile("testdata/scones.jpg")
if err != nil {
return fmt.Errorf("failed to read image2: %w", err)
}

modelName := "gemini-2.5-flash"
contents := []*genai.Content{
{
Role: "user",
Parts: []*genai.Part{
{Text: "Generate a list of all the objects contained in both images."},
{InlineData: &genai.Blob{
MIMEType: "image/jpeg",
Data: image1,
}},
{InlineData: &genai.Blob{
MIMEType: "image/jpeg",
Data: image2,
}},
},
},
}

// Call the model
resp, err := client.Models.GenerateContent(ctx, modelName, contents, nil)
if err != nil {
return fmt.Errorf("failed to generate content: %w", err)
}

fmt.Fprintln(w, resp.Text())

// Example response:
// Here is a list of all the distinct objects found in both images:
// 1. **Coffee** (in mugs/cups; one is clearly a latte with heart art, others are also coffee/latte)
// 2. **Mug(s)/Cup(s)** (yellow in the top image, white in the bottom image)
// 3. **Cake** (sliced, in the top image)
// 4. **Plate** (white, under the cake slice in the top image)
// 5. **Fork** (partially visible on the plate in the top image)
// 6. **Scones/Biscuits** (blueberry, in the bottom image)
// 7. **Blueberries** (scattered and in a bowl in the bottom image)
// 8. **Bowl** (small, dark, holding blueberries in the bottom image)
// 9. **Spoon** (silver, with "LET'S JAM" inscription, in the bottom image)
// 10. **Flowers** (peonies, in the bottom image)
// 11. **Leaves** (green, possibly mint, in the bottom image)
// 12. **Paper** (parchment or wax paper, in the bottom image)
// 13. **Table/Surface** (wooden in the top image, textured/painted in the bottom image)
// ...

return nil
}

// [END googlegenaisdk_textgen_with_multi_local_img]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
105 changes: 105 additions & 0 deletions genai/tools/tools_code_exec_with_txt_local_img.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// 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 tools shows examples of various tools that Gemini model can use to generate text.
package tools

// [START googlegenaisdk_tools_code_exec_with_txt_local_img]
import (
"context"
"fmt"
"io"
"os"

genai "google.golang.org/genai"
)

// generateWithLocalImgAndCodeExec shows how to combine a local image, a text prompt,
// and enable the code execution tool in a request.
func generateWithLocalImgAndCodeExec(w io.Writer) error {
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)
}

// Read local image
imgBytes, err := os.ReadFile("testdata/640px-Monty_open_door.svg.png")
if err != nil {
return fmt.Errorf("failed to read image: %w", err)
}

// Define the prompt
prompt := "Run a simulation of the Monty Hall Problem with 1,000 trials.\n" +
"Here's how this works as a reminder. In the Monty Hall Problem, you're on a game\n" +
"show with three doors. Behind one is a car, and behind the others are goats. You\n" +
"pick a door. The host, who knows what's behind the doors, opens a different door\n" +
"to reveal a goat. Should you switch to the remaining unopened door?\n" +
"The answer has always been a little difficult for me to understand when people\n" +
"solve it with math - so please run a simulation with Python to show me what the\n" +
"best strategy is.\n" +
"Thank you!"

// Enable the code execution tool
tools := []*genai.Tool{
{CodeExecution: &genai.ToolCodeExecution{}},
}

// Build contents with image + text
contents := []*genai.Content{
{
Role: "user",
Parts: []*genai.Part{
{InlineData: &genai.Blob{
MIMEType: "image/png",
Data: imgBytes,
}},
{Text: prompt},
},
},
}

// Call the model
resp, err := client.Models.GenerateContent(ctx, "gemini-2.5-flash", contents, &genai.GenerateContentConfig{
Tools: tools,
Temperature: genai.Ptr(float32(0.0)),
})
if err != nil {
return fmt.Errorf("failed to generate content: %w", err)
}

// Print result
fmt.Fprintln(w, "# Code:")
fmt.Fprintln(w, resp.ExecutableCode())
fmt.Fprintln(w, "# Outcome:")
fmt.Fprintln(w, resp.CodeExecutionResult())

// Example output:
// # Code:
// import random
//
// def run_monty_hall_trial(strategy):
// """
// Runs a single trial of the Monty Hall problem.
//
// Args:
// strategy (str): 'stick' or 'switch'

return nil
}

// [END googlegenaisdk_tools_code_exec_with_txt_local_img]
13 changes: 13 additions & 0 deletions genai/tools/tools_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,17 @@ func TestTextGeneration(t *testing.T) {
t.Error("expected non-empty output, got empty")
}
})

t.Run("generate with Local img and code exec", func(t *testing.T) {
buf.Reset()
err := generateWithLocalImgAndCodeExec(buf)
if err != nil {
t.Fatalf("generateWithLocalImgAndCodeExec failed: %v", err)
}

output := buf.String()
if output == "" {
t.Error("expected non-empty output, got empty")
}
})
}
Loading