-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(genai): Add samples for Tools, Embeddings, Provisioned Throughput and Text-generation #5372
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
msampathkumar
merged 6 commits into
GoogleCloudPlatform:main
from
cfloress:genai-text-tools-embed-provithrou-gen-samples
Sep 4, 2025
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8fae0a7
feat(genai): Add samples for Tools, Embeddings, Provisioned Throughpu…
cfloress f08e9e0
Merge branch 'main' into genai-text-tools-embed-provithrou-gen-samples
cfloress f88174d
genai: PR comments
cfloress 00b7e8a
genai: PR comments
cfloress 2110a11
genai: PR comments
cfloress 547812d
genai: fix conflict
cfloress 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// 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 embeddings shows examples of how Gemini-embedding model can use embedding. | ||
package embeddings | ||
|
||
// [START googlegenaisdk_embeddings_docretrieval_with_txt] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"google.golang.org/genai" | ||
) | ||
|
||
// generateEmbedContentWithText shows how to embed content with text. | ||
func generateEmbedContentWithText(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) | ||
} | ||
|
||
outputDimensionality := int32(3072) | ||
config := &genai.EmbedContentConfig{ | ||
TaskType: "RETRIEVAL_DOCUMENT", //optional | ||
Title: "Driver's License", //optional | ||
OutputDimensionality: &outputDimensionality, //optional | ||
} | ||
|
||
contents := []*genai.Content{ | ||
{ | ||
Parts: []*genai.Part{ | ||
{ | ||
Text: "How do I get a driver's license/learner's permit?", | ||
}, | ||
{ | ||
Text: "How long is my driver's license valid for?", | ||
}, | ||
{ | ||
Text: "Driver's knowledge test study guide", | ||
}, | ||
}, | ||
Role: "user", | ||
}, | ||
cfloress marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
modelName := "gemini-embedding-001" | ||
resp, err := client.Models.EmbedContent(ctx, modelName, contents, config) | ||
if err != nil { | ||
return fmt.Errorf("failed to generate content: %w", err) | ||
} | ||
|
||
fmt.Fprintln(w, resp) | ||
|
||
// Example response: | ||
// embeddings=[ContentEmbedding(values=[-0.06302902102470398, 0.00928034819662571, 0.014716853387653828, -0.028747491538524628, ... ], | ||
// statistics=ContentEmbeddingStatistics(truncated=False, token_count=13.0))] | ||
// metadata=EmbedContentMetadata(billable_character_count=112) | ||
|
||
return nil | ||
} | ||
|
||
// [END googlegenaisdk_embeddings_docretrieval_with_txt] |
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,46 @@ | ||
// 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 embeddings | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil" | ||
) | ||
|
||
func TestEmbedGeneration(t *testing.T) { | ||
tc := testutil.SystemTest(t) | ||
|
||
t.Setenv("GOOGLE_GENAI_USE_VERTEXAI", "1") | ||
t.Setenv("GOOGLE_CLOUD_LOCATION", "us-central1") | ||
t.Setenv("GOOGLE_CLOUD_PROJECT", tc.ProjectID) | ||
|
||
buf := new(bytes.Buffer) | ||
|
||
t.Run("generate embed content with text", func(t *testing.T) { | ||
buf.Reset() | ||
err := generateEmbedContentWithText(buf) | ||
if err != nil { | ||
t.Fatalf("generateEmbedContentWithText failed: %v", err) | ||
} | ||
|
||
output := buf.String() | ||
if output == "" { | ||
t.Error("expected non-empty output, got empty") | ||
} | ||
}) | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
genai/provisioned_throughput/provisionedthroughput_examples_test.go
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,46 @@ | ||
// 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 provisionedthroughput | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil" | ||
) | ||
|
||
func TestProvisionedThroughputGeneration(t *testing.T) { | ||
tc := testutil.SystemTest(t) | ||
|
||
t.Setenv("GOOGLE_GENAI_USE_VERTEXAI", "1") | ||
t.Setenv("GOOGLE_CLOUD_LOCATION", "us-central1") | ||
t.Setenv("GOOGLE_CLOUD_PROJECT", tc.ProjectID) | ||
|
||
buf := new(bytes.Buffer) | ||
|
||
t.Run("generate Provisioned Throughput with text", func(t *testing.T) { | ||
buf.Reset() | ||
err := generateProvisionedThroughputWithText(buf) | ||
if err != nil { | ||
t.Fatalf("generateProvisionedThroughputWithText failed: %v", err) | ||
} | ||
|
||
output := buf.String() | ||
if output == "" { | ||
t.Error("expected non-empty output, got empty") | ||
} | ||
}) | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
genai/provisioned_throughput/provisionedthroughput_with_txt.go
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,69 @@ | ||
// 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 provisionedthroughput shows examples of Gemini model can use to generate with text. | ||
package provisionedthroughput | ||
|
||
// [START googlegenaisdk_provisionedthroughput_with_txt] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"google.golang.org/genai" | ||
) | ||
|
||
// generateProvisionedThroughputWithText shows how to generate text Provisioned Throughput. | ||
func generateProvisionedThroughputWithText(w io.Writer) error { | ||
cfloress marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ctx := context.Background() | ||
|
||
client, err := genai.NewClient(ctx, &genai.ClientConfig{ | ||
HTTPOptions: genai.HTTPOptions{ | ||
APIVersion: "v1", | ||
Headers: http.Header{ | ||
// Options: | ||
// - "dedicated": Use Provisioned Throughput | ||
// - "shared": Use pay-as-you-go | ||
// https://cloud.google.com/vertex-ai/generative-ai/docs/use-provisioned-throughput | ||
"X-Vertex-AI-LLM-Request-Type": []string{"shared"}, | ||
cfloress marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to create genai client: %w", err) | ||
} | ||
|
||
modelName := "gemini-2.5-flash" | ||
cfloress marked this conversation as resolved.
Show resolved
Hide resolved
|
||
contents := genai.Text("How does AI work?") | ||
|
||
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: | ||
// Artificial Intelligence (AI) isn't magic, nor is it a single "thing." Instead, it's a broad field of computer science focused on creating machines that can perform tasks that typically require human intelligence. | ||
// ..... | ||
// In Summary: | ||
// ... | ||
|
||
return nil | ||
} | ||
|
||
// [END googlegenaisdk_provisionedthroughput_with_txt] |
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,74 @@ | ||
// 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_chat_with_txt] | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
|
||
"google.golang.org/genai" | ||
) | ||
|
||
// generateChatWithText shows how to generate chat using a text prompt. | ||
func generateChatWithText(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) | ||
} | ||
modelName := "gemini-2.5-flash" | ||
cfloress marked this conversation as resolved.
Show resolved
Hide resolved
|
||
history := []*genai.Content{ | ||
{ | ||
Role: "user", | ||
Parts: []*genai.Part{ | ||
{Text: "Hello there"}, | ||
}, | ||
}, | ||
{ | ||
Role: "model", | ||
Parts: []*genai.Part{ | ||
{Text: "Great to meet you. What would you like to know?"}, | ||
}, | ||
}, | ||
} | ||
chatSession, err := client.Chats.Create(ctx, modelName, nil, history) | ||
if err != nil { | ||
return fmt.Errorf("failed to create genai chat session: %w", err) | ||
} | ||
contents := genai.Part{Text: "Tell me a story."} | ||
resp, err := chatSession.SendMessage(ctx, contents) | ||
cfloress marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return fmt.Errorf("failed to send message: %w", err) | ||
} | ||
|
||
respText := resp.Text() | ||
|
||
fmt.Fprintln(w, respText) | ||
// Example response: | ||
// Okay, settle in. Let me tell you a story about a quiet cartographer, but not of lands and seas. | ||
// ... | ||
// In the sleepy town of Oakhaven, nestled between the Whispering Hills and the Murmuring River, lived a woman named Elara. | ||
// ... | ||
|
||
return nil | ||
} | ||
|
||
// [END googlegenaisdk_textgen_chat_with_txt] |
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
Oops, something went wrong.
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.