These Go examples are real runnable files. Edit the source file first; this page is rebuilt from the checked-in example and its metadata header.
Go Text To Speech
Generates speech audio through OpenAI.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
beginner - Run:
npm run example -- go src/examples/go/audio/speech_audio.go - Source: src/examples/go/audio/speech_audio.go
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
ax "github.com/ax-llm/ax/packages/go"
)
func openAIClient() *ax.OpenAICompatibleClient {
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" { apiKey = os.Getenv("OPENAI_APIKEY") }
if apiKey == "" { panic("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.") }
model := os.Getenv("AX_OPENAI_MODEL")
if model == "" { model = "gpt-5.4-mini" }
return ax.NewOpenAICompatibleClient(map[string]ax.Value{"api_key": apiKey, "model": model, "model_config": ax.Object("temperature", 0)})
}
func printJSON(value ax.Value) {
data, err := json.MarshalIndent(value, "", " ")
if err != nil { panic(err) }
fmt.Println(string(data))
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
client := openAIClient()
speech, err := client.Speak(ctx, map[string]ax.Value{"text": "Ax turns LLM prompts into typed programs.", "voice": "alloy", "format": "mp3"}, nil)
if err != nil { panic(err) }
sp := speech.(map[string]ax.Value)
audio, _ := sp["audio"].(string)
printJSON(ax.Object("format", sp["format"], "audioBytesBase64", len(audio)))
}Go Speech To Text
Transcribes a checked-in WAV file through OpenAI.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
intermediate - Run:
npm run example -- go src/examples/go/audio/transcribe_audio.go - Source: src/examples/go/audio/transcribe_audio.go
package main
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"time"
"io/ioutil"
ax "github.com/ax-llm/ax/packages/go"
)
func openAIClient() *ax.OpenAICompatibleClient {
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" { apiKey = os.Getenv("OPENAI_APIKEY") }
if apiKey == "" { panic("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.") }
model := os.Getenv("AX_OPENAI_MODEL")
if model == "" { model = "gpt-5.4-mini" }
return ax.NewOpenAICompatibleClient(map[string]ax.Value{"api_key": apiKey, "model": model, "model_config": ax.Object("temperature", 0)})
}
func printJSON(value ax.Value) {
data, err := json.MarshalIndent(value, "", " ")
if err != nil { panic(err) }
fmt.Println(string(data))
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
client := openAIClient()
wav, err := ioutil.ReadFile("src/examples/assets/presentation.wav")
if err != nil { panic(err) }
transcript, err := client.Transcribe(ctx, map[string]ax.Value{"audio": base64.StdEncoding.EncodeToString(wav), "language": "en", "model": "gpt-4o-mini-transcribe", "format": "json"}, nil)
if err != nil { panic(err) }
printJSON(transcript)
}Go Audio Summary Pipeline
Transcribes audio and summarizes the transcript with an OpenAI-backed generator.
- Provider:
openai - Env:
OPENAI_API_KEY,OPENAI_APIKEY - Level:
advanced - Run:
npm run example -- go src/examples/go/audio/pipeline_audio.go - Source: src/examples/go/audio/pipeline_audio.go
package main
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"time"
"io/ioutil"
ax "github.com/ax-llm/ax/packages/go"
)
func openAIClient() *ax.OpenAICompatibleClient {
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" { apiKey = os.Getenv("OPENAI_APIKEY") }
if apiKey == "" { panic("Set OPENAI_API_KEY or OPENAI_APIKEY to run this example.") }
model := os.Getenv("AX_OPENAI_MODEL")
if model == "" { model = "gpt-5.4-mini" }
return ax.NewOpenAICompatibleClient(map[string]ax.Value{"api_key": apiKey, "model": model, "model_config": ax.Object("temperature", 0)})
}
func printJSON(value ax.Value) {
data, err := json.MarshalIndent(value, "", " ")
if err != nil { panic(err) }
fmt.Println(string(data))
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
client := openAIClient()
wav, err := ioutil.ReadFile("src/examples/assets/presentation.wav")
if err != nil { panic(err) }
transcript, err := client.Transcribe(ctx, map[string]ax.Value{"audio": base64.StdEncoding.EncodeToString(wav), "language": "en", "model": "gpt-4o-mini-transcribe", "format": "json"}, nil)
if err != nil { panic(err) }
summarize := ax.NewAx("transcript:string -> summary:string, followUps:string[]", nil)
result, err := summarize.Forward(ctx, client, map[string]ax.Value{"transcript": transcript.(map[string]ax.Value)["text"]}, nil)
if err != nil { panic(err) }
printJSON(ax.Object("transcript", transcript.(map[string]ax.Value)["text"], "result", result))
}