Skip to content

Commit 9f0976a

Browse files
committed
ElevenLabs provider
1 parent ea3e45f commit 9f0976a

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/Providers/ElevenLabs/ElevenLabsSpeechToText.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,28 @@
55
namespace NeuronAI\Providers\ElevenLabs;
66

77
use Generator;
8+
use NeuronAI\Chat\Messages\AssistantMessage;
89
use NeuronAI\Chat\Messages\Message;
910
use NeuronAI\Exceptions\HttpException;
1011
use NeuronAI\Exceptions\ProviderException;
1112
use NeuronAI\HttpClient\GuzzleHttpClient;
1213
use NeuronAI\HttpClient\HasHttpClient;
1314
use NeuronAI\HttpClient\HttpClientInterface;
15+
use NeuronAI\HttpClient\HttpRequest;
1416
use NeuronAI\Providers\AIProviderInterface;
1517
use NeuronAI\Providers\MessageMapperInterface;
1618
use NeuronAI\Providers\ToolMapperInterface;
1719

1820
use function trim;
21+
use function end;
22+
use function fopen;
23+
use function is_array;
1924

2025
class ElevenLabsSpeechToText implements AIProviderInterface
2126
{
2227
use HasHttpClient;
2328

24-
protected string $baseUri = 'https://api.elevenlabs.io/v1';
29+
protected string $baseUri = 'https://api.elevenlabs.io/v1/speech-to-text';
2530

2631
/**
2732
* System instructions.
@@ -54,16 +59,29 @@ public function systemPrompt(?string $prompt): AIProviderInterface
5459
*/
5560
public function chat(Message|array $messages): Message
5661
{
62+
$message = is_array($messages) ? end($messages) : $messages;
5763

64+
$body = [
65+
'file' => fopen($message->getAudio(), 'r'),
66+
'model' => $this->model,
67+
];
68+
69+
$response = $this->httpClient->request(
70+
HttpRequest::post(
71+
uri: 'audio/transcriptions',
72+
body: $body
73+
)
74+
)->json();
75+
76+
return new AssistantMessage($response['text']);
5877
}
5978

6079
/**
61-
* @throws HttpException
6280
* @throws ProviderException
6381
*/
6482
public function stream(Message|array $messages): Generator
6583
{
66-
84+
throw new ProviderException('Streaming is not supported by OpenAI Text to Speech.');
6785
}
6886

6987
public function structured(array|Message $messages, string $class, array $response_schema): Message

0 commit comments

Comments
 (0)