add mistra voxtral
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package transcriber
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/sashabaranov/go-openai"
|
||||
)
|
||||
|
||||
// MistralAdapter implements TranscriptionAdapter for Mistral Voxtral API
|
||||
type MistralAdapter struct {
|
||||
client *openai.Client
|
||||
config Config
|
||||
}
|
||||
|
||||
func NewMistralAdapter(config Config) *MistralAdapter {
|
||||
clientConfig := openai.DefaultConfig(config.APIKey)
|
||||
clientConfig.BaseURL = "https://api.mistral.ai/v1"
|
||||
client := openai.NewClientWithConfig(clientConfig)
|
||||
|
||||
return &MistralAdapter{
|
||||
client: client,
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *MistralAdapter) 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 transcription request
|
||||
req := openai.AudioRequest{
|
||||
Model: a.config.Model,
|
||||
Reader: bytes.NewReader(wavData),
|
||||
FilePath: "audio.wav",
|
||||
Language: a.config.Language,
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
resp, err := a.client.CreateTranscription(ctx, req)
|
||||
duration := time.Since(start)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("mistral-adapter: API call failed after %v: %v", duration, err)
|
||||
return "", fmt.Errorf("mistral transcription: %w", err)
|
||||
}
|
||||
|
||||
log.Printf("mistral-adapter: transcribed %d bytes in %v: %q", len(audioData), duration, resp.Text)
|
||||
return resp.Text, nil
|
||||
}
|
||||
@@ -51,6 +51,12 @@ func NewTranscriber(config Config) (Transcriber, error) {
|
||||
}
|
||||
adapter = NewGroqTranslationAdapter(config)
|
||||
|
||||
case "mistral-transcription":
|
||||
if config.APIKey == "" {
|
||||
return nil, fmt.Errorf("Mistral API key required")
|
||||
}
|
||||
adapter = NewMistralAdapter(config)
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported provider: %s", config.Provider)
|
||||
}
|
||||
|
||||
@@ -75,6 +75,26 @@ func TestNewTranscriber(t *testing.T) {
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "valid mistral-transcription config",
|
||||
config: Config{
|
||||
Provider: "mistral-transcription",
|
||||
APIKey: "test-key",
|
||||
Language: "de",
|
||||
Model: "voxtral-mini-latest",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "mistral-transcription config without api key",
|
||||
config: Config{
|
||||
Provider: "mistral-transcription",
|
||||
APIKey: "",
|
||||
Language: "de",
|
||||
Model: "voxtral-mini-latest",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "unsupported provider",
|
||||
config: Config{
|
||||
|
||||
Reference in New Issue
Block a user