Add support for Groq Whisper API as an alternative to OpenAI: - groq-transcription: Fast transcription using whisper-large-v3 or whisper-large-v3-turbo - groq-translation: Translation to English (whisper-large-v3 only) Features: - New transcription adapters for both Groq services - Config validation with provider-specific model restrictions - API key support via config file or GROQ_API_KEY environment variable - Updated interactive configuration wizard with provider selection - Comprehensive test coverage for all providers - Shared WAV conversion utility for audio preprocessing Breaking changes: None Backward compatibility: Existing OpenAI configurations continue to work unchanged
64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
package transcriber
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/sashabaranov/go-openai"
|
|
)
|
|
|
|
// GroqTranslationAdapter implements TranscriptionAdapter for Groq Translation API
|
|
// Translates audio to English text. The Language field in config hints at the source language.
|
|
type GroqTranslationAdapter struct {
|
|
client *openai.Client
|
|
config Config
|
|
}
|
|
|
|
func NewGroqTranslationAdapter(config Config) *GroqTranslationAdapter {
|
|
clientConfig := openai.DefaultConfig(config.APIKey)
|
|
clientConfig.BaseURL = "https://api.groq.com/openai/v1"
|
|
client := openai.NewClientWithConfig(clientConfig)
|
|
|
|
return &GroqTranslationAdapter{
|
|
client: client,
|
|
config: config,
|
|
}
|
|
}
|
|
|
|
func (a *GroqTranslationAdapter) Transcribe(ctx context.Context, audioData []byte) (string, error) {
|
|
if len(audioData) == 0 {
|
|
return "", nil
|
|
}
|
|
|
|
// Convert raw PCM to WAV format
|
|
wavData, err := convertToWAV(audioData)
|
|
if err != nil {
|
|
return "", fmt.Errorf("convert to WAV: %w", err)
|
|
}
|
|
|
|
// Create translation request
|
|
// Note: Translation always outputs English, regardless of target language
|
|
// The Language field in the request hints at the source audio language for better accuracy
|
|
req := openai.AudioRequest{
|
|
Model: a.config.Model,
|
|
Reader: bytes.NewReader(wavData),
|
|
FilePath: "audio.wav",
|
|
Language: a.config.Language, // Source language hint
|
|
}
|
|
|
|
start := time.Now()
|
|
resp, err := a.client.CreateTranslation(ctx, req)
|
|
duration := time.Since(start)
|
|
|
|
if err != nil {
|
|
log.Printf("groq-translation-adapter: API call failed after %v: %v", duration, err)
|
|
return "", fmt.Errorf("groq translation: %w", err)
|
|
}
|
|
|
|
log.Printf("groq-translation-adapter: translated %d bytes in %v: %q", len(audioData), duration, resp.Text)
|
|
return resp.Text, nil
|
|
}
|