diff --git a/genai/text_generation/testdata/describe_video_content.mp4 b/genai/text_generation/testdata/describe_video_content.mp4 new file mode 100644 index 0000000000..93176ae76f Binary files /dev/null and b/genai/text_generation/testdata/describe_video_content.mp4 differ diff --git a/genai/text_generation/testdata/latte.jpg b/genai/text_generation/testdata/latte.jpg new file mode 100644 index 0000000000..e942ca6230 Binary files /dev/null and b/genai/text_generation/testdata/latte.jpg differ diff --git a/genai/text_generation/testdata/scones.jpg b/genai/text_generation/testdata/scones.jpg new file mode 100644 index 0000000000..b5ee1b0707 Binary files /dev/null and b/genai/text_generation/testdata/scones.jpg differ diff --git a/genai/text_generation/text_generation_examples_test.go b/genai/text_generation/text_generation_examples_test.go index b92fb758f5..2b34dd4416 100644 --- a/genai/text_generation/text_generation_examples_test.go +++ b/genai/text_generation/text_generation_examples_test.go @@ -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") + } + }) + } diff --git a/genai/text_generation/textgen_with_local_video.go b/genai/text_generation/textgen_with_local_video.go new file mode 100644 index 0000000000..2035b45fad --- /dev/null +++ b/genai/text_generation/textgen_with_local_video.go @@ -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] diff --git a/genai/text_generation/textgen_with_multi_local_img.go b/genai/text_generation/textgen_with_multi_local_img.go new file mode 100644 index 0000000000..867e6ae334 --- /dev/null +++ b/genai/text_generation/textgen_with_multi_local_img.go @@ -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] diff --git a/genai/tools/testdata/640px-Monty_open_door.svg.png b/genai/tools/testdata/640px-Monty_open_door.svg.png new file mode 100644 index 0000000000..90f83375e3 Binary files /dev/null and b/genai/tools/testdata/640px-Monty_open_door.svg.png differ diff --git a/genai/tools/tools_code_exec_with_txt_local_img.go b/genai/tools/tools_code_exec_with_txt_local_img.go new file mode 100644 index 0000000000..404b420725 --- /dev/null +++ b/genai/tools/tools_code_exec_with_txt_local_img.go @@ -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] diff --git a/genai/tools/tools_examples_test.go b/genai/tools/tools_examples_test.go index 867e868ae1..a40ca03bc0 100644 --- a/genai/tools/tools_examples_test.go +++ b/genai/tools/tools_examples_test.go @@ -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") + } + }) }