-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(genai): Add generate content with routing option #5327
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
Changes from 1 commit
82ce846
e6893e6
d0321ce
b6845ed
f1487b5
db319d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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{ | ||
nardosalemu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
HTTPOptions: genai.HTTPOptions{APIVersion: "v1beta1"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good point -- should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}} | ||
nardosalemu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
resp, err := client.Models.GenerateContent(ctx, | ||
"model-optimizer-exp-04-09", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The model name
nardosalemu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This error check appears to be redundant and potentially misleading. The The 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] |
There was a problem hiding this comment.
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.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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()
inTestTextGeneration
function.Do you still suggest I create a separate function for it? Thanks!