Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
2 changes: 1 addition & 1 deletion genai/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.23.0

require (
github.com/GoogleCloudPlatform/golang-samples v0.0.0-20250201051611-5fb145d1e974
google.golang.org/genai v0.4.0
google.golang.org/genai v1.6.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions genai/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ golang.org/x/time v0.9.0 h1:EsRrnYcQiGH+5FfbgvV4AP7qEZstoyrHB0DzarOQ4ZY=
golang.org/x/time v0.9.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
google.golang.org/api v0.217.0 h1:GYrUtD289o4zl1AhiTZL0jvQGa2RDLyC+kX1N/lfGOU=
google.golang.org/api v0.217.0/go.mod h1:qMc2E8cBAbQlRypBTBWHklNJlaZZJBwDv81B1Iu8oSI=
google.golang.org/genai v0.4.0 h1:nHLpFvp1i2nUGQ8CjIQ8j/6d3H79Echt1jiNLb9myDk=
google.golang.org/genai v0.4.0/go.mod h1:yPyKKBezIg2rqZziLhHQ5CD62HWr7sLDLc2PDzdrNVs=
google.golang.org/genai v1.6.0 h1:aG0J3QF/Ad2GsjHvY8LjRp9hiDl4hvLJN98YwkLDqFE=
google.golang.org/genai v1.6.0/go.mod h1:TyfOKRz/QyCaj6f/ZDt505x+YreXnY40l2I6k8TvgqY=
google.golang.org/genproto v0.0.0-20250115164207-1a7da9e5054f h1:387Y+JbxF52bmesc8kq1NyYIp33dnxCw6eiA7JMsTmw=
google.golang.org/genproto v0.0.0-20250115164207-1a7da9e5054f/go.mod h1:0joYwWwLQh18AOj8zMYeZLjzuqcYTU3/nC5JdCvC3JI=
google.golang.org/genproto/googleapis/api v0.0.0-20250115164207-1a7da9e5054f h1:gap6+3Gk41EItBuyi4XX/bp4oqJ3UwuIMl25yGinuAA=
Expand Down
14 changes: 14 additions & 0 deletions genai/text_generation/text_generation_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,18 @@ func TestTextGeneration(t *testing.T) {
t.Error("expected non-empty output, got empty")
}
})

t.Run("generate with routing", func(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: create a separate function for this test. Don't use t.Run() unless you need to.

Copy link
Contributor Author

@nardosalemu nardosalemu Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following the pattern with how the functions in text_generation folder were being tested. It seems like all the functions defined in the files under text_generation are all tested using t.Run() in TestTextGeneration function.
Do you still suggest I create a separate function for it? Thanks!

buf.Reset()
err := generateWithRouting(buf)
if err != nil {
t.Fatalf("generateWithRouting failed: %v", err)
}

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

}
6 changes: 2 additions & 4 deletions genai/text_generation/textgen_code_with_pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ func generateWithPDF(w io.Writer) error {
return fmt.Errorf("failed to generate content: %w", err)
}

respText, err := resp.Text()
if err != nil {
return fmt.Errorf("failed to convert model response to text: %w", err)
}
respText := resp.Text()

fmt.Fprintln(w, respText)

// Example response:
Expand Down
10 changes: 4 additions & 6 deletions genai/text_generation/textgen_config_with_txt.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func generateWithConfig(w io.Writer) error {
contents := genai.Text("Why is the sky blue?")
// See the documentation: https://googleapis.github.io/python-genai/genai.html#genai.types.GenerateContentConfig
config := &genai.GenerateContentConfig{
Temperature: genai.Ptr(0.0),
CandidateCount: genai.Ptr(int64(1)),
Temperature: genai.Ptr(float32(0.0)),
CandidateCount: int32(1),
ResponseMIMEType: "application/json",
}

Expand All @@ -49,10 +49,8 @@ func generateWithConfig(w io.Writer) error {
return fmt.Errorf("failed to generate content: %w", err)
}

respText, err := resp.Text()
if err != nil {
return fmt.Errorf("failed to convert model response to text: %w", err)
}
respText := resp.Text()

fmt.Fprintln(w, respText)
// Example response:
// {
Expand Down
6 changes: 2 additions & 4 deletions genai/text_generation/textgen_sys_instr_with_txt.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ func generateWithSystem(w io.Writer) error {
return fmt.Errorf("failed to generate content: %w", err)
}

respText, err := resp.Text()
if err != nil {
return fmt.Errorf("failed to convert model response to text: %w", err)
}
respText := resp.Text()

fmt.Fprintln(w, respText)

// Example response:
Expand Down
6 changes: 2 additions & 4 deletions genai/text_generation/textgen_transcript_with_gcs_audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ Use speaker A, speaker B, etc. to identify speakers.`},
return fmt.Errorf("failed to generate content: %w", err)
}

respText, err := resp.Text()
if err != nil {
return fmt.Errorf("failed to convert model response to text: %w", err)
}
respText := resp.Text()

fmt.Fprintln(w, respText)

// Example response:
Expand Down
6 changes: 2 additions & 4 deletions genai/text_generation/textgen_with_gcs_audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ Create a chapter breakdown with timestamps for key sections or topics discussed.
return fmt.Errorf("failed to generate content: %w", err)
}

respText, err := resp.Text()
if err != nil {
return fmt.Errorf("failed to convert model response to text: %w", err)
}
respText := resp.Text()

fmt.Fprintln(w, respText)

// Example response:
Expand Down
6 changes: 2 additions & 4 deletions genai/text_generation/textgen_with_multi_img.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ func generateWithMultiImg(w io.Writer) error {
return fmt.Errorf("failed to generate content: %w", err)
}

respText, err := resp.Text()
if err != nil {
return fmt.Errorf("failed to convert model response to text: %w", err)
}
respText := resp.Text()

fmt.Fprintln(w, respText)

// Example response:
Expand Down
6 changes: 2 additions & 4 deletions genai/text_generation/textgen_with_mute_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ func generateWithMuteVideo(w io.Writer) error {
return fmt.Errorf("failed to generate content: %w", err)
}

respText, err := resp.Text()
if err != nil {
return fmt.Errorf("failed to convert model response to text: %w", err)
}
respText := resp.Text()

fmt.Fprintln(w, respText)

// Example response:
Expand Down
63 changes: 63 additions & 0 deletions genai/text_generation/textgen_with_routing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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_routing]
import (
"context"
"fmt"
"io"

"google.golang.org/genai"
)

// generateWithRouting shows how to generate text using a text prompt and routing configuration.
func generateWithRouting(w io.Writer) error {
ctx := context.Background()

client, err := genai.NewClient(ctx, &genai.ClientConfig{
HTTPOptions: genai.HTTPOptions{APIVersion: "v1beta1"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The APIVersion is set to "v1beta1" here. Other examples in this package (e.g., textgen_config_with_txt.go) use "v1" after the library update. Is "v1beta1" specifically required for the routing feature, or could this be aligned to "v1" for consistency if the feature supports it? Using a beta version might imply certain stability or support considerations.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good point -- should this be "v1"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the features are only supported by "v1beta1"

})
if err != nil {
return fmt.Errorf("failed to create genai client: %w", err)
}

generateContentConfig := &genai.GenerateContentConfig{ModelSelectionConfig: &genai.ModelSelectionConfig{FeatureSelectionPreference: genai.FeatureSelectionPreferencePrioritizeQuality}}

resp, err := client.Models.GenerateContent(ctx,
"model-optimizer-exp-04-09",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The model name "model-optimizer-exp-04-09" seems quite specific and potentially experimental. For a general SDK example, would it be more appropriate to use a commonly available and stable model name? If this specific model is required for the routing feature, perhaps a comment explaining its purpose or availability status would be helpful for users trying to run this example.

genai.Text("How does AI work?"),
generateContentConfig,
)
if err != nil {
return fmt.Errorf("failed to generate content: %w", err)
}

respText := resp.Text()
if err != nil {
return fmt.Errorf("failed to convert model response to text: %w", err)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This error check appears to be redundant and potentially misleading. The err variable here refers to the error returned by client.Models.GenerateContent on line 40, which is already checked on lines 45-47. If GenerateContent succeeded, err would be nil at this point, making this check ineffective. If GenerateContent failed, the function would have already returned.

The resp.Text() method in google.golang.org/genai v1.6.0 (to which this PR updates) no longer returns an error, so there's no new error from resp.Text() to check here.

Could you clarify the intent of this check or remove it if it's indeed redundant?

fmt.Fprintln(w, respText)
// Example response:
// That's a great question! Understanding how AI works can feel like ...
// ...
// **1. The Foundation: Data and Algorithms**
// ...

return nil
}

// [END googlegenaisdk_textgen_with_routing]
6 changes: 2 additions & 4 deletions genai/text_generation/textgen_with_txt.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ func generateWithText(w io.Writer) error {
return fmt.Errorf("failed to generate content: %w", err)
}

respText, err := resp.Text()
if err != nil {
return fmt.Errorf("failed to convert model response to text: %w", err)
}
respText := resp.Text()

fmt.Fprintln(w, respText)
// Example response:
// That's a great question! Understanding how AI works can feel like ...
Expand Down
6 changes: 2 additions & 4 deletions genai/text_generation/textgen_with_txt_img.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ func generateWithTextImage(w io.Writer) error {
return fmt.Errorf("failed to generate content: %w", err)
}

respText, err := resp.Text()
if err != nil {
return fmt.Errorf("failed to convert model response to text: %w", err)
}
respText := resp.Text()

fmt.Fprintln(w, respText)

// Example response:
Expand Down
6 changes: 2 additions & 4 deletions genai/text_generation/textgen_with_txt_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ func generateWithTextStream(w io.Writer) error {
return fmt.Errorf("failed to generate content: %w", err)
}

chunk, err := resp.Text()
if err != nil {
return fmt.Errorf("failed to convert model response to text: %w", err)
}
chunk := resp.Text()

fmt.Fprintln(w, chunk)
}

Expand Down
6 changes: 2 additions & 4 deletions genai/text_generation/textgen_with_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ Create a chapter breakdown with timestamps for key sections or topics discussed.
return fmt.Errorf("failed to generate content: %w", err)
}

respText, err := resp.Text()
if err != nil {
return fmt.Errorf("failed to convert model response to text: %w", err)
}
respText := resp.Text()

fmt.Fprintln(w, respText)

// Example response:
Expand Down
6 changes: 2 additions & 4 deletions genai/text_generation/textgen_with_youtube_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ func generateWithYTVideo(w io.Writer) error {
return fmt.Errorf("failed to generate content: %w", err)
}

respText, err := resp.Text()
if err != nil {
return fmt.Errorf("failed to convert model response to text: %w", err)
}
respText := resp.Text()

fmt.Fprintln(w, respText)

// Example response:
Expand Down
2 changes: 1 addition & 1 deletion genai/tools/tools_code_exec_with_txt.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func generateWithCodeExec(w io.Writer) error {
Tools: []*genai.Tool{
{CodeExecution: &genai.ToolCodeExecution{}},
},
Temperature: genai.Ptr(0.0),
Temperature: genai.Ptr(float32(0.0)),
}
modelName := "gemini-2.0-flash-001"

Expand Down
8 changes: 3 additions & 5 deletions genai/tools/tools_func_desc_with_txt.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func generateWithFuncCall(w io.Writer) error {
Tools: []*genai.Tool{
{FunctionDeclarations: []*genai.FunctionDeclaration{weatherFunc}},
},
Temperature: genai.Ptr(0.0),
Temperature: genai.Ptr(float32(0.0)),
}

modelName := "gemini-2.0-flash-001"
Expand Down Expand Up @@ -113,10 +113,8 @@ func generateWithFuncCall(w io.Writer) error {
return fmt.Errorf("failed to generate content: %w", err)
}

respText, err := resp.Text()
if err != nil {
return fmt.Errorf("failed to convert model response to text: %w", err)
}
respText := resp.Text()

fmt.Fprintln(w, respText)

// Example response:
Expand Down
6 changes: 2 additions & 4 deletions genai/tools/tools_google_search_with_txt.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ func generateWithGoogleSearch(w io.Writer) error {
return fmt.Errorf("failed to generate content: %w", err)
}

respText, err := resp.Text()
if err != nil {
return fmt.Errorf("failed to convert model response to text: %w", err)
}
respText := resp.Text()

fmt.Fprintln(w, respText)

// Example response:
Expand Down
Loading