-
-
Notifications
You must be signed in to change notification settings - Fork 550
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add stream support for Text To Speech
- Loading branch information
1 parent
44ac258
commit 69a19ba
Showing
15 changed files
with
207 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenAI\Contracts; | ||
|
||
use IteratorAggregate; | ||
|
||
/** | ||
* @template T | ||
* | ||
* @extends IteratorAggregate<int, T> | ||
* | ||
* @internal | ||
*/ | ||
interface ResponseStreamContract extends IteratorAggregate | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace OpenAI\Responses\Audio; | ||
|
||
use Generator; | ||
use Http\Discovery\Psr17Factory; | ||
use OpenAI\Contracts\ResponseHasMetaInformationContract; | ||
use OpenAI\Contracts\ResponseStreamContract; | ||
use OpenAI\Responses\Meta\MetaInformation; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* @implements ResponseStreamContract<string> | ||
*/ | ||
final class SpeechStreamResponse implements ResponseHasMetaInformationContract, ResponseStreamContract | ||
{ | ||
public function __construct( | ||
private readonly ResponseInterface $response, | ||
) { | ||
// | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getIterator(): Generator | ||
{ | ||
while (! $this->response->getBody()->eof()) { | ||
yield $this->response->getBody()->read(1024); | ||
} | ||
} | ||
|
||
public function meta(): MetaInformation | ||
{ | ||
// @phpstan-ignore-next-line | ||
return MetaInformation::from($this->response->getHeaders()); | ||
} | ||
|
||
public static function fake(string $content = null, MetaInformation $meta = null): static | ||
{ | ||
$psr17Factory = new Psr17Factory(); | ||
$response = $psr17Factory->createResponse() | ||
->withBody($psr17Factory->createStream($content ?? (string) file_get_contents(__DIR__.'/../../Testing/Responses/Fixtures/Audio/speech-streamed.mp3'))); | ||
|
||
if ($meta instanceof \OpenAI\Responses\Meta\MetaInformation) { | ||
foreach ($meta->toArray() as $key => $value) { | ||
$response = $response->withHeader($key, (string) $value); | ||
} | ||
} | ||
|
||
return new self($response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
use GuzzleHttp\Psr7\Response; | ||
use GuzzleHttp\Psr7\Stream; | ||
use OpenAI\Responses\Audio\SpeechStreamResponse; | ||
use OpenAI\Responses\Meta\MetaInformation; | ||
|
||
test('from response', function () { | ||
$response = new Response( | ||
body: new Stream(speechStream()), | ||
headers: metaHeaders(), | ||
); | ||
|
||
$speech = new SpeechStreamResponse($response); | ||
|
||
expect($speech) | ||
->toBeInstanceOf(SpeechStreamResponse::class) | ||
->getIterator()->toBeInstanceOf(Iterator::class) | ||
->meta()->toBeInstanceOf(MetaInformation::class); | ||
}); | ||
|
||
test('fake', function () { | ||
$response = SpeechStreamResponse::fake(); | ||
|
||
foreach ($response as $chunk) { | ||
expect($chunk)->toBeString(); | ||
} | ||
}); | ||
|
||
test('fake with override', function () { | ||
$response = SpeechStreamResponse::fake('fake audio'); | ||
|
||
foreach ($response as $chunk) { | ||
expect($chunk)->toBe('fake audio'); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters