From 47b4d107d93ac60074ff36d2fc43dedb7aeed4bb Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Sun, 5 Mar 2023 06:48:23 +0800 Subject: [PATCH] Bump `openai-php/client` to `0.3.4` (#2) * Revert ClientFactory * Format * Bump openai-client to 0.3.4 --- composer.json | 2 +- src/ClientFactory.php | 20 ++++++++++++++++++-- tests/Pest.php | 19 ++++++++----------- tests/TestCase.php | 9 ++++++++- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index 193c1e8..742d2b6 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "hyperf/config": "~3.0.0", "hyperf/di": "~3.0.0", "hyperf/guzzle": "~3.0.0", - "openai-php/client": "^0.3.2" + "openai-php/client": "^0.3.4" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.0", diff --git a/src/ClientFactory.php b/src/ClientFactory.php index cab2bed..4a91cbd 100644 --- a/src/ClientFactory.php +++ b/src/ClientFactory.php @@ -11,7 +11,12 @@ namespace FriendsOfHyperf\OpenAi; use FriendsOfHyperf\OpenAi\Exception\ApiKeyIsMissing; -use OpenAI; +use Hyperf\Guzzle\ClientFactory as GuzzleClientFactory; +use OpenAI\Client; +use OpenAI\Transporters\HttpTransporter; +use OpenAI\ValueObjects\ApiKey; +use OpenAI\ValueObjects\Transporter\BaseUri; +use OpenAI\ValueObjects\Transporter\Headers; use Psr\Container\ContainerInterface; class ClientFactory @@ -25,6 +30,17 @@ public function __invoke(ContainerInterface $container) throw ApiKeyIsMissing::create(); } - return OpenAI::client($apiKey, $organization); + $apiKey = ApiKey::from($apiKey); + $baseUri = BaseUri::from('api.openai.com/v1'); + $headers = Headers::withAuthorization($apiKey); + + if ($organization !== null) { + $headers = $headers->withOrganization($organization); + } + + $client = $container->get(GuzzleClientFactory::class)->create(); + $transporter = new HttpTransporter($client, $baseUri, $headers); + + return new Client($transporter); } } diff --git a/tests/Pest.php b/tests/Pest.php index 5949c61..ac00dd0 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1,16 +1,13 @@ in('Feature'); /* diff --git a/tests/TestCase.php b/tests/TestCase.php index cfb05b6..88da3c2 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,10 +1,17 @@