-
Notifications
You must be signed in to change notification settings - Fork 373
feat(go/plugins/googlegenai): add image generation native support #2630
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a270c3a
feat(go): add image generation native support
hugoaguirre e620474
make the image-gemini sample more complexx
hugoaguirre bd2344c
fix: prevent an error when TEXT modality is not present
hugoaguirre 7a8b673
fix: uncomment text modality
hugoaguirre 372214f
fix: rename vars
hugoaguirre 9b0ab03
Merge branch 'main' into haguirre/gemini-2.0-flash-exp
hugoaguirre a3e62f3
fix: review comments
hugoaguirre a13f504
remove audio check
hugoaguirre 71e02ba
remove audio constant
hugoaguirre 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
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
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
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
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,107 @@ | ||
// 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 main | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/firebase/genkit/go/ai" | ||
"github.com/firebase/genkit/go/genkit" | ||
"github.com/firebase/genkit/go/plugins/googlegenai" | ||
) | ||
|
||
func main() { | ||
ctx := context.Background() | ||
|
||
// Initialize Genkit with the Google AI plugin. When you pass nil for the | ||
// Config parameter, the Google AI plugin will get the API key from the | ||
// GEMINI_API_KEY or GOOGLE_API_KEY environment variable, which is the recommended | ||
// practice. | ||
g, err := genkit.Init(ctx, genkit.WithPlugins(&googlegenai.GoogleAI{})) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Define a simple flow that generates an image of a given topic | ||
genkit.DefineFlow(g, "imageFlow", func(ctx context.Context, input string) (string, error) { | ||
m := googlegenai.GoogleAIModel(g, "gemini-2.0-flash-exp") | ||
if m == nil { | ||
return "", errors.New("imageFlow: failed to find model") | ||
} | ||
|
||
if input == "" { | ||
input = `A little blue gopher with big eyes trying to learn Python, | ||
use a cartoon style, the story should be tragic because he | ||
chose the wrong programming language, the proper programing | ||
language for a gopher should be Go` | ||
} | ||
resp, err := genkit.Generate(ctx, g, | ||
ai.WithModel(m), | ||
ai.WithConfig(&googlegenai.GeminiConfig{ | ||
Temperature: 0.5, | ||
ResponseModalities: []googlegenai.Modality{ | ||
hugoaguirre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
googlegenai.ImageMode, | ||
googlegenai.TextMode, | ||
}, | ||
}), | ||
ai.WithPrompt(fmt.Sprintf(`generate a story about %s and for each scene, generate an image for it`, input))) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
story := "" | ||
scene := 0 | ||
for _, p := range resp.Message.Content { | ||
if p.IsMedia() { | ||
scene += 1 | ||
err = base64toFile(p.Text, fmt.Sprintf("scene_%d.png", scene)) | ||
} | ||
if p.IsText() { | ||
story += p.Text | ||
} | ||
} | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return story, nil | ||
}) | ||
|
||
<-ctx.Done() | ||
} | ||
|
||
func base64toFile(data, path string) error { | ||
dec, err := base64.StdEncoding.DecodeString(data) | ||
if err != nil { | ||
return err | ||
} | ||
f, err := os.Create(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
_, err = f.Write(dec) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return f.Sync() | ||
} |
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.