45 lines
1.6 KiB
PHP
45 lines
1.6 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Support\AiProviders;
|
||
|
|
|
||
|
|
use App\Models\AiProvider;
|
||
|
|
use Illuminate\Http\Client\ConnectionException;
|
||
|
|
use Illuminate\Support\Facades\Http;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Text-to-speech via ElevenLabs. Used for narration/voiceover. Unlike the
|
||
|
|
* other drivers, a successful response body IS the generated content
|
||
|
|
* (raw audio bytes), not JSON.
|
||
|
|
*/
|
||
|
|
class ElevenLabsDriver implements AiProviderDriver
|
||
|
|
{
|
||
|
|
public function generate(AiProvider $provider, string $prompt, array $options = []): AiGenerationResult
|
||
|
|
{
|
||
|
|
$voiceId = $options['voice_id'] ?? $provider->config['voice_id'] ?? null;
|
||
|
|
|
||
|
|
if (! $voiceId) {
|
||
|
|
throw new AiGenerationException("AI provider '{$provider->name}' has no voice_id configured for text-to-speech.");
|
||
|
|
}
|
||
|
|
|
||
|
|
$modelId = $options['model_id'] ?? $provider->config['model_id'] ?? 'eleven_multilingual_v2';
|
||
|
|
|
||
|
|
try {
|
||
|
|
$response = Http::withHeaders(['xi-api-key' => $provider->api_key])
|
||
|
|
->post("https://api.elevenlabs.io/v1/text-to-speech/{$voiceId}", [
|
||
|
|
'text' => $prompt,
|
||
|
|
'model_id' => $modelId,
|
||
|
|
]);
|
||
|
|
} catch (ConnectionException $e) {
|
||
|
|
throw new AiGenerationException("could not connect to ElevenLabs: {$e->getMessage()}");
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($response->failed()) {
|
||
|
|
$error = $response->json('detail.message') ?? $response->json('detail') ?? 'unexpected response from ElevenLabs';
|
||
|
|
|
||
|
|
throw new AiGenerationException(is_string($error) ? $error : 'unexpected response from ElevenLabs');
|
||
|
|
}
|
||
|
|
|
||
|
|
return new AiGenerationResult($response->body(), 'audio/mpeg');
|
||
|
|
}
|
||
|
|
}
|