Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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/bounding_box/boundingbox_with_txt_img.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func generateBoundingBoxesWithText(w io.Writer) error {

contents := []*genai.Content{
{
Role: "user",
Role: genai.RoleUser,
Parts: []*genai.Part{
{
FileData: &genai.FileData{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func createContentCache(w io.Writer) (string, error) {
MIMEType: "application/pdf",
}},
},
Role: "user",
Role: genai.RoleUser,
},
}
config := &genai.CreateCachedContentConfig{
Expand Down
2 changes: 1 addition & 1 deletion genai/controlled_generation/ctrlgen_with_class_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func generateWithClassSchema(w io.Writer) error {
contents := []*genai.Content{
{Parts: []*genai.Part{
{Text: "List a few popular cookie recipes."},
}, Role: "user"},
}, Role: genai.RoleUser},
}
// JSON Schema for []Recipe
schema := &genai.Schema{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func generateWithEnumClassSchema(w io.Writer) error {
contents := []*genai.Content{
{Parts: []*genai.Part{
{Text: "What type of instrument is a guitar?"},
}, Role: "user"},
}, Role: genai.RoleUser},
}

config := &genai.GenerateContentConfig{
Expand Down
2 changes: 1 addition & 1 deletion genai/controlled_generation/ctrlgen_with_enum_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func generateWithEnumSchema(w io.Writer) error {
contents := []*genai.Content{
{Parts: []*genai.Part{
{Text: "What type of instrument is an oboe?"},
}, Role: "user"},
}, Role: genai.RoleUser},
}
config := &genai.GenerateContentConfig{
ResponseMIMEType: "text/x.enum",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func generateWithNestedClassSchema(w io.Writer) error {
contents := []*genai.Content{
{Parts: []*genai.Part{
{Text: "List about 10 home-baked cookies and give them grades based on tastiness."},
}, Role: "user"},
}, Role: genai.RoleUser},
}

config := &genai.GenerateContentConfig{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Finally, Saturday rounds off the week with sunny skies, a temperature of 80°F,
{Parts: []*genai.Part{
{Text: prompt},
},
Role: "user"},
Role: genai.RoleUser},
}
config := &genai.GenerateContentConfig{
ResponseMIMEType: "application/json",
Expand Down
2 changes: 1 addition & 1 deletion genai/controlled_generation/ctrlgen_with_resp_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func generateWithRespSchema(w io.Writer) error {
{Parts: []*genai.Part{
{Text: "List a few popular cookie recipes."},
},
Role: "user"},
Role: genai.RoleUser},
}
modelName := "gemini-2.5-flash"

Expand Down
2 changes: 1 addition & 1 deletion genai/count_tokens/counttoken_compute_with_txt.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func computeWithTxt(w io.Writer) error {
{Parts: []*genai.Part{
{Text: "What's the longest word in the English language?"},
},
Role: "user"},
Role: genai.RoleUser},
}

resp, err := client.Models.ComputeTokens(ctx, modelName, contents, nil)
Expand Down
2 changes: 1 addition & 1 deletion genai/count_tokens/counttoken_resp_with_txt.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func generateTextAndCount(w io.Writer) error {
{Parts: []*genai.Part{
{Text: "Why is the sky blue?"},
},
Role: "user"},
Role: genai.RoleUser},
}

resp, err := client.Models.GenerateContent(ctx, modelName, contents, nil)
Expand Down
2 changes: 1 addition & 1 deletion genai/count_tokens/counttoken_with_txt_vid.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func countWithTxtAndVid(w io.Writer) error {
MIMEType: "video/mp4",
}},
},
Role: "user"},
Role: genai.RoleUser},
}

resp, err := client.Models.CountTokens(ctx, modelName, contents, nil)
Expand Down
2 changes: 1 addition & 1 deletion genai/embeddings/embeddings_docretrieval_with_txt.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func generateEmbedContentWithText(w io.Writer) error {
Text: "Driver's knowledge test study guide",
},
},
Role: "user",
Role: genai.RoleUser,
},
}

Expand Down
2 changes: 1 addition & 1 deletion genai/express_mode/expressmode_with_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func generateContentWithApiKey(w io.Writer) error {
[]*genai.Content{
{Parts: []*genai.Part{
{Text: "Explain bubble sort to me."},
}, Role: "user"},
}, Role: genai.RoleUser},
},
nil,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func generateMMFlashTxtImgWithText(w io.Writer) error {
{Text: "Generate an illustrated recipe for a paella. " +
"Create images to go alongside the text as you generate the recipe."},
},
Role: "user",
Role: genai.RoleUser,
},
}

Expand Down
2 changes: 1 addition & 1 deletion genai/image_generation/imggen_mmflash_with_txt.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func generateMMFlashWithText(w io.Writer) error {
Parts: []*genai.Part{
{Text: "Generate an image of the Eiffel tower with fireworks in the background."},
},
Role: "user",
Role: genai.RoleUser,
},
}

Expand Down
113 changes: 113 additions & 0 deletions genai/live/live_audio_with_txt.go
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]
110 changes: 110 additions & 0 deletions genai/live/live_code_exec_with_txt.go
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
}
}

}
}
}
// 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]
Loading