-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(genai): Add samples for live generation #5412
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
Open
cfloress
wants to merge
2
commits into
GoogleCloudPlatform:main
Choose a base branch
from
cfloress:genai-live-code-tr-generation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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,113 @@ | ||
| // 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 live shows how to use the GenAI SDK to generate text with live resources. | ||
| package live | ||
|
|
||
| // [START googlegenaisdk_live_audio_with_txt] | ||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
|
|
||
| "google.golang.org/genai" | ||
| ) | ||
|
|
||
| // generateLiveAudioWithText demonstrates using a live Gemini model | ||
| // that performs live audio with text and handles responses. | ||
| func generateLiveAudioWithText(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.0-flash-live-preview-04-09" | ||
|
|
||
| voiceName := "Aoede" | ||
|
|
||
| config := &genai.LiveConnectConfig{ | ||
| ResponseModalities: []genai.Modality{genai.ModalityAudio}, | ||
| SpeechConfig: &genai.SpeechConfig{ | ||
| VoiceConfig: &genai.VoiceConfig{ | ||
| PrebuiltVoiceConfig: &genai.PrebuiltVoiceConfig{ | ||
| VoiceName: voiceName, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // Open a live session | ||
| session, err := client.Live.Connect(ctx, modelName, config) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to connect live: %w", err) | ||
| } | ||
| defer session.Close() | ||
|
|
||
| // Send the text input | ||
| textInput := "Hello? Gemini are you there?" | ||
| fmt.Fprintf(w, "> %s\n\n", textInput) | ||
|
|
||
| err = session.SendClientContent(genai.LiveClientContentInput{ | ||
| Turns: []*genai.Content{ | ||
| { | ||
| Role: genai.RoleUser, | ||
| Parts: []*genai.Part{ | ||
| {Text: textInput}, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to send content: %w", err) | ||
| } | ||
|
|
||
| // Receive streaming audio chunks | ||
| var audioData []byte | ||
| for { | ||
| chunk, err := session.Receive() | ||
| if err != nil { | ||
| if err == io.EOF { | ||
| break | ||
| } | ||
| return fmt.Errorf("error receiving stream: %w", err) | ||
| } | ||
|
|
||
| if chunk.ServerContent != nil && chunk.ServerContent.ModelTurn != nil { | ||
| for _, part := range chunk.ServerContent.ModelTurn.Parts { | ||
| if part.InlineData != nil { | ||
| audioData = append(audioData, part.InlineData.Data...) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Save audio if data received | ||
| if len(audioData) > 0 { | ||
| audioFile := "output.wav" | ||
| if err := os.WriteFile(audioFile, audioData, 0644); err != nil { | ||
| return fmt.Errorf("failed to write WAV file: %w", err) | ||
| } | ||
|
|
||
| fmt.Fprintf(w, "Received audio answer saved to %s\n", audioFile) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // [END googlegenaisdk_live_audio_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,110 @@ | ||
| // 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 live shows how to use the GenAI SDK to generate text with live resources. | ||
| package live | ||
|
|
||
| // [START googlegenaisdk_live_code_exec_with_txt] | ||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
|
|
||
| "google.golang.org/genai" | ||
| ) | ||
|
|
||
| // generateLiveCodeExecWithTxt demonstrates using a live Gemini model | ||
| // that performs code exec with text calls and handles responses. | ||
| func generateLiveCodeExecWithTxt(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.0-flash-live-preview-04-09" | ||
|
|
||
| config := &genai.LiveConnectConfig{ | ||
| ResponseModalities: []genai.Modality{genai.ModalityText}, | ||
| Tools: []*genai.Tool{ | ||
| { | ||
| CodeExecution: &genai.ToolCodeExecution{}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| session, err := client.Live.Connect(ctx, modelName, config) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to connect live session: %w", err) | ||
| } | ||
| defer session.Close() | ||
|
|
||
| textInput := "Compute the largest prime palindrome under 10" | ||
| fmt.Fprintf(w, "> %s\n\n", textInput) | ||
|
|
||
| err = session.SendClientContent(genai.LiveClientContentInput{ | ||
| Turns: []*genai.Content{ | ||
| { | ||
| Role: genai.RoleUser, | ||
| Parts: []*genai.Part{ | ||
| {Text: textInput}, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to send client content: %w", err) | ||
| } | ||
|
|
||
| var response string | ||
|
|
||
| // Receive streaming responses | ||
| for { | ||
| chunk, err := session.Receive() | ||
| if err == io.EOF { | ||
| break | ||
| } | ||
| if err != nil { | ||
| return fmt.Errorf("error receiving stream: %w", err) | ||
| } | ||
|
|
||
| // Handle the main model output | ||
| if chunk.ServerContent != nil { | ||
| if chunk.ServerContent.ModelTurn != nil { | ||
| for _, part := range chunk.ServerContent.ModelTurn.Parts { | ||
| if part == nil { | ||
| continue | ||
| } | ||
| if part.ExecutableCode != nil { | ||
| fmt.Fprint(w, part.ExecutableCode.Code) | ||
| } | ||
| if part.CodeExecutionResult != nil { | ||
| response += part.CodeExecutionResult.Output | ||
| } | ||
| } | ||
cfloress marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| } | ||
| } | ||
| } | ||
| // Example output: | ||
| // > Compute the largest prime palindrome under 10 | ||
| // Final Answer: The final answer is $\boxed{7}$ | ||
| fmt.Fprintln(w, response) | ||
| return nil | ||
| } | ||
|
|
||
| // [END googlegenaisdk_live_code_exec_with_txt] | ||
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.