use crate::providers::TranscriptionProvider;
use anyhow::{Context, Result};
use async_trait::async_trait;
use groq_api_rust::{AsyncGroqClient, SpeechToTextRequest};
use std::env;
use std::time::Duration;
use tracing::{debug, info, instrument};
pub struct GroqProvider {
client: AsyncGroqClient,
}
impl GroqProvider {
pub async fn new() -> Self {
let api_key = env::var("GROQ_API_KEY").expect("GROQ_API_KEY environment variable not set");
Self {
client: AsyncGroqClient::new(api_key, None).await,
}
}
}
#[async_trait]
impl TranscriptionProvider for GroqProvider {
fn name(&self) -> &'static str {
"Groq"
}
fn min_chunk_duration(&self) -> Duration {
Duration::from_secs(10) }
#[instrument(skip(self, audio_data))]
async fn transcribe(&self, audio_data: &[u8]) -> Result<String> {
info!("Starting Groq transcription request");
debug!("Audio data size: {} bytes", audio_data.len());
let request = SpeechToTextRequest::new(audio_data.to_vec())
.temperature(0.7)
.language("en")
.model("whisper-large-v3");
let response = self
.client
.speech_to_text(request)
.await
.context("Failed to get response from Groq")?;
info!("Groq transcription successful");
info!("Received transcription: {}", response.text);
Ok(response.text)
}
}