Skip to content

chore(go): add text-to-speech samples #3253

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
wants to merge 2 commits into
base: main
Choose a base branch
from
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
Binary file added go/samples/text-to-speech/genkit.wav
Binary file not shown.
95 changes: 95 additions & 0 deletions go/samples/text-to-speech/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// 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 main

import (
"context"
"encoding/base64"
"io"
"log"
"os"

"github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/genkit"
"github.com/firebase/genkit/go/plugins/googlegenai"
"google.golang.org/genai"
)

func main() {
ctx := context.Background()

// Initialize Genkit with the Google AI plugin. When you pass nil for the
// Config parameter, the Google AI plugin will get the API key from the
// GEMINI_API_KEY or GOOGLE_API_KEY environment variable, which is the recommended
// practice.
g, err := genkit.Init(ctx,
genkit.WithPlugins(&googlegenai.GoogleAI{}),
genkit.WithDefaultModel("googleai/gemini-2.5-flash-preview-tts"),
)
if err != nil {
log.Fatal(err)
}

// Define a simple flow that generates an audio from a given text
genkit.DefineFlow(g, "text-to-speech-flow", func(ctx context.Context, input any) (string, error) {
resp, err := genkit.Generate(ctx, g,
ai.WithConfig(&genai.GenerateContentConfig{
Temperature: genai.Ptr[float32](1.0),
ResponseModalities: []string{"AUDIO"},
SpeechConfig: &genai.SpeechConfig{
VoiceConfig: &genai.VoiceConfig{
PrebuiltVoiceConfig: &genai.PrebuiltVoiceConfig{
VoiceName: "Algenib",
},
},
},
}),
ai.WithPrompt("Say: Genkit is the best Gen AI library!"))
if err != nil {
return "", err
}

// base64 encoded audio
text := resp.Text()
return text, nil
})

// Define a simple flow that generates audio transcripts from a given audio
genkit.DefineFlow(g, "speech-to-text-flow", func(ctx context.Context, input any) (string, error) {
audio, err := os.Open("./genkit.wav")
if err != nil {
return "", err
}
defer audio.Close()

audioBytes, err := io.ReadAll(audio)
if err != nil {
return "", err
}
resp, err := genkit.Generate(ctx, g,
ai.WithModelName("googleai/gemini-2.5-flash"),
ai.WithMessages(ai.NewUserMessage(
ai.NewTextPart("Can you transcribe the next audio?"),
ai.NewMediaPart("audio/wav", "data:audio/wav;base64,"+base64.StdEncoding.EncodeToString(audioBytes)))),
)
if err != nil {
return "", err
}

return resp.Text(), nil
})

<-ctx.Done()
}
Loading