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
f200e49
genai: added img, thinking and tuning samples
cfloress Sep 16, 2025
0e604ff
Merge branch 'main' of github.com:GoogleCloudPlatform/golang-samples …
cfloress Sep 16, 2025
93e49f5
genai: PR comments
cfloress Sep 16, 2025
30d8b6d
Merge branch 'main' into genai-img-tunn-think-generation
cfloress Sep 17, 2025
04234df
Merge branch 'main' into genai-img-tunn-think-generation
cfloress Sep 25, 2025
503846b
Merge branch 'main' of github.com:GoogleCloudPlatform/golang-samples …
cfloress Sep 25, 2025
5eff6a4
genai: PR comments
cfloress Sep 25, 2025
cb00ca1
Merge branch 'genai-img-tunn-think-generation' of github.com:cfloress…
cfloress Sep 25, 2025
b7665aa
Merge branch 'main' into genai-img-tunn-think-generation
cfloress Sep 30, 2025
e49202c
Merge branch 'main' into genai-img-tunn-think-generation
msampathkumar Oct 3, 2025
5bdb227
genai: model update
cfloress Oct 7, 2025
6928788
Merge branch 'main' into genai-img-tunn-think-generation
cfloress Oct 20, 2025
ffb683b
Merge branch 'main' into genai-img-tunn-think-generation
msampathkumar Oct 22, 2025
74d4985
Merge branch 'main' into genai-img-tunn-think-generation
msampathkumar Oct 24, 2025
69eee46
Merge branch 'main' into genai-img-tunn-think-generation
cfloress Oct 27, 2025
8873d8b
genai: resolved conflicts
cfloress Nov 7, 2025
d97bd3f
Merge branch 'main' of github.com:GoogleCloudPlatform/golang-samples …
cfloress Nov 13, 2025
f30ff57
genai: fix testdata CICD bug
cfloress Nov 13, 2025
2131e8a
Merge branch 'main' into genai-img-tunn-think-generation
cfloress Nov 14, 2025
dc6e458
Merge branch 'main' of github.com:GoogleCloudPlatform/golang-samples …
cfloress Nov 14, 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
6 changes: 6 additions & 0 deletions badfiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ var allowList = []string{
// DLP data
"dlp/snippets/**/testdata/*",

// genai data
"genai/**/*.mp4",
"genai/**/*.jpg",
"genai/**/*.png",
"genai/**/*.md",

// Endpoints samples.
"endpoints/**/*.proto",

Expand Down
Binary file added genai/image_generation/bw-example-image.png
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/image_generation/dog_newspaper.png
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/image_generation/example-image-2.png
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/image_generation/example-image-4.png
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/image_generation/example-image-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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/image_generation/image_generation_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,30 @@ func TestImageGeneration(t *testing.T) {
t.Error("expected non-empty output, got empty")
}
})

t.Run("generate image content with text", func(t *testing.T) {
buf.Reset()
err := generateImageWithText(buf)
if err != nil {
t.Fatalf("generateImageWithText failed: %v", err)
}

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

t.Run("generate mmflash image content with text and image", func(t *testing.T) {
buf.Reset()
err := generateImageMMFlashEditWithTextImg(buf)
if err != nil {
t.Fatalf("generateImageMMFlashEditWithTextImg failed: %v", err)
}

output := buf.String()
if output == "" {
t.Error("expected non-empty output, got empty")
}
})
}
101 changes: 101 additions & 0 deletions genai/image_generation/imggen_mmflash_edit_img_with_txt_img.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// 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 image_generation shows how to use the GenAI SDK to generate images and text.
package image_generation

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

"google.golang.org/genai"
)

// generateImageMMFlashEditWithTextImg demonstrates editing an image with text and image inputs.
func generateImageMMFlashEditWithTextImg(w io.Writer) error {
// TODO(developer): Update below lines
outputFile := "bw-example-image.png"
inputFile := "example-image-eiffel-tower.png"
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)
}

image, err := os.ReadFile(inputFile)
if err != nil {
return fmt.Errorf("failed to read image: %w", err)
}

modelName := "gemini-2.5-flash-image"
prompt := "Edit this image to make it look like a cartoon."
contents := []*genai.Content{
{
Role: "user",
Parts: []*genai.Part{
{Text: prompt},
{InlineData: &genai.Blob{
MIMEType: "image/png",
Data: image,
}},
},
},
}
resp, err := client.Models.GenerateContent(ctx,
modelName,
contents,
&genai.GenerateContentConfig{
ResponseModalities: []string{
string(genai.ModalityText),
string(genai.ModalityImage),
},
},
)
if err != nil {
return fmt.Errorf("failed to generate content: %w", err)
}

if len(resp.Candidates) == 0 || resp.Candidates[0].Content == nil {
return fmt.Errorf("no content was generated")
}

for _, part := range resp.Candidates[0].Content.Parts {
if part.Text != "" {
fmt.Fprintln(w, part.Text)
} else if part.InlineData != nil {
if len(part.InlineData.Data) > 0 {
if err := os.WriteFile(outputFile, part.InlineData.Data, 0644); err != nil {
return fmt.Errorf("failed to save image: %w", err)
}
fmt.Fprintln(w, outputFile)
}
}
}

// Example response:
// Here's the image of the Eiffel Tower and fireworks, cartoonized for you!
// Cartoon-style edit:
// - Simplified the Eiffel Tower with bolder lines and slightly exaggerated proportions.
// - Brightened and saturated the colors of the sky, fireworks, and foliage for a more vibrant, cartoonish look.
// ....
return nil
}

// [END googlegenaisdk_imggen_mmflash_edit_img_with_txt_img]
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func generateMMFlashTxtImgWithText(w io.Writer) error {
return fmt.Errorf("no candidates returned")
}

outputFolder := "testdata"
outputFolder := ""

// Create the markdown file
mdFile := filepath.Join(outputFolder, "paella-recipe.md")
Expand Down
2 changes: 1 addition & 1 deletion genai/image_generation/imggen_mmflash_with_txt.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func generateMMFlashWithText(w io.Writer) error {
if part.Text != "" {
fmt.Fprintln(w, part.Text)
} else if part.InlineData != nil {
fileName = "testdata/example-image-eiffel-tower.png"
fileName = "example-image-eiffel-tower.png"
if err := os.WriteFile(fileName, part.InlineData.Data, 0o644); err != nil {
return fmt.Errorf("failed to save image: %w", err)
}
Expand Down
70 changes: 70 additions & 0 deletions genai/image_generation/imggen_with_txt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// 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 image_generation shows how to use the GenAI SDK to generate images and text.
package image_generation

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

"google.golang.org/genai"
)

// generateImageWithText demonstrates how to generate an image from a text prompt.
func generateImageWithText(w io.Writer) error {
// TODO(developer): Update below line
outputFile := "dog_newspaper.png"
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 := "imagen-4.0-generate-001"
prompt := "A dog reading a newspaper"
resp, err := client.Models.GenerateImages(ctx,
modelName,
prompt,
&genai.GenerateImagesConfig{
ImageSize: "2K",
},
)
if err != nil {
return fmt.Errorf("failed to generate content: %w", err)
}

if len(resp.GeneratedImages) == 0 || resp.GeneratedImages[0].Image == nil {
return fmt.Errorf("no image generated")
}

img := resp.GeneratedImages[0].Image
if err := os.WriteFile(outputFile, img.ImageBytes, 0644); err != nil {
return fmt.Errorf("failed to save image: %w", err)
}

fmt.Fprintln(w, len(img.ImageBytes))

// Example response:
// Created output image using 6098201 bytes
return nil
}

// [END googlegenaisdk_imggen_with_txt]
52 changes: 52 additions & 0 deletions genai/image_generation/paella-recipe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
Let's get cooking! Here's an illustrated recipe for a delicious paella:

## Authentic Spanish Paella

Paella is a classic Spanish dish, famous for its vibrant flavors and communal spirit. While there are many variations, this recipe focuses on a traditional seafood and chicken paella.

### Ingredients:

* **For the Sofrito:**
* 1 large onion, finely chopped
* 2 cloves garlic, minced
* 1 red bell pepper, finely chopped
* 1 green bell pepper, finely chopped
* 2 ripe tomatoes, grated or finely diced
* 1/2 cup olive oil
* Salt and freshly ground black pepper to taste

* **For the Paella:**
* 1 lb boneless, skinless chicken thighs, cut into 1-inch pieces
* 1/2 lb Spanish chorizo, sliced (optional)
* 1 lb large shrimp, peeled and deveined
* 1 lb mussels, scrubbed and de-bearded
* 1 lb calamari, cut into rings (optional)
* 4 cups short-grain Spanish rice (Bomba or Calasparra preferred)
* 8 cups hot chicken or seafood broth
* 1 teaspoon saffron threads, steeped in 1/4 cup hot water
* 1 teaspoon smoked paprika
* 1/2 cup fresh or frozen peas
* 1 lemon, cut into wedges, for serving
* Fresh parsley, chopped, for garnish

### Equipment:

* A 16-inch paella pan (or a wide, shallow pan)

### Instructions:

---

**Step 1: Prepare Your Ingredients**

Chop all your vegetables and chicken. Have your seafood ready. This preparation is key for smooth cooking. ![image](example-image-2.png)

---

**Step 2: Start the Sofrito**

Heat the olive oil in your paella pan over medium heat. Add the chopped onion and bell peppers. Cook until softened, about 8-10 minutes. ![image](example-image-4.png)---

**Step 3: Add Garlic and Tomatoes**

Stir in the minced garlic and cook for another minute until fragrant. Then, add the grated or diced tomatoes, smoked paprika, salt, and pepper. Cook for about 10-15 minutes, stirring occasionally, until the sauce thickens and deepens in color. This is your sofrito base. ![image](example-image-6.png)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
44 changes: 0 additions & 44 deletions genai/image_generation/testdata/paella-recipe.md

This file was deleted.

Loading