-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af5e00d
commit 86b80f9
Showing
19 changed files
with
238 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,31 @@ | ||
<?php | ||
|
||
use PhpLlm\LlmChain\Bridge\OpenAI\GPT; | ||
use PhpLlm\LlmChain\Bridge\OpenAI\PlatformFactory; | ||
use PhpLlm\LlmChain\Chain; | ||
use PhpLlm\LlmChain\Model\Message\Content\Audio; | ||
use PhpLlm\LlmChain\Model\Message\Message; | ||
use PhpLlm\LlmChain\Model\Message\MessageBag; | ||
use Symfony\Component\Dotenv\Dotenv; | ||
|
||
require_once dirname(__DIR__).'/vendor/autoload.php'; | ||
(new Dotenv())->loadEnv(dirname(__DIR__).'/.env'); | ||
|
||
if (empty($_ENV['OPENAI_API_KEY'])) { | ||
echo 'Please set the OPENAI_API_KEY environment variable.'.PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']); | ||
$llm = new GPT(GPT::GPT_4O_AUDIO); | ||
|
||
$chain = new Chain($platform, $llm); | ||
$messages = new MessageBag( | ||
Message::ofUser( | ||
'What is this recording about?', | ||
new Audio(dirname(__DIR__).'/tests/Fixture/audio.mp3'), | ||
), | ||
); | ||
$response = $chain->call($messages); | ||
|
||
echo $response->getContent().PHP_EOL; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Model\Message\Content; | ||
|
||
use PhpLlm\LlmChain\Exception\InvalidArgumentException; | ||
|
||
final readonly class Audio implements Content | ||
{ | ||
public function __construct( | ||
public string $path, | ||
) { | ||
if (!is_readable($path) || false === file_get_contents($path)) { | ||
throw new InvalidArgumentException(sprintf('The file "%s" does not exist or is not readable.', $path)); | ||
} | ||
} | ||
|
||
/** | ||
* @return array{type: 'input_audio', input_audio: array{data: string, format: string}} | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
$data = file_get_contents($this->path); | ||
$format = pathinfo($this->path, PATHINFO_EXTENSION); | ||
|
||
return [ | ||
'type' => 'input_audio', | ||
'input_audio' => [ | ||
'data' => base64_encode($data), | ||
'format' => $format, | ||
], | ||
]; | ||
} | ||
} |
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Tests\Model\Message\Content; | ||
|
||
use PhpLlm\LlmChain\Model\Message\Content\Audio; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\Attributes\Small; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(Audio::class)] | ||
#[Small] | ||
final class AudioTest extends TestCase | ||
{ | ||
#[Test] | ||
public function constructWithValidPath(): void | ||
{ | ||
$audio = new Audio(dirname(__DIR__, 3).'/Fixture/audio.mp3'); | ||
|
||
self::assertSame(dirname(__DIR__, 3).'/Fixture/audio.mp3', $audio->path); | ||
} | ||
|
||
#[Test] | ||
#[DataProvider('provideValidPaths')] | ||
public function jsonSerializeWithValid(string $path, array $expected): void | ||
{ | ||
$audio = new Audio($path); | ||
|
||
$expected = [ | ||
'type' => 'input_audio', | ||
'input_audio' => $expected, | ||
]; | ||
|
||
$actual = $audio->jsonSerialize(); | ||
|
||
// shortening the base64 data | ||
$actual['input_audio']['data'] = substr($actual['input_audio']['data'], 0, 30); | ||
|
||
self::assertSame($expected, $actual); | ||
} | ||
|
||
public static function provideValidPaths(): \Generator | ||
{ | ||
yield 'mp3' => [dirname(__DIR__, 3).'/Fixture/audio.mp3', [ | ||
'data' => 'SUQzBAAAAAAAfVREUkMAAAAMAAADMj', // shortened | ||
'format' => 'mp3', | ||
]]; | ||
} | ||
|
||
#[Test] | ||
public function constructWithInvalidPath(): void | ||
{ | ||
$this->expectExceptionMessage('The file "foo.mp3" does not exist or is not readable.'); | ||
|
||
new Audio('foo.mp3'); | ||
} | ||
} |
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