diff --git a/go/samples/text-to-speech/genkit.wav b/go/samples/text-to-speech/genkit.wav new file mode 100644 index 0000000000..7d48e5e061 Binary files /dev/null and b/go/samples/text-to-speech/genkit.wav differ diff --git a/go/samples/text-to-speech/main.go b/go/samples/text-to-speech/main.go new file mode 100644 index 0000000000..efef7286c6 --- /dev/null +++ b/go/samples/text-to-speech/main.go @@ -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() +}