From 7c223b016a3f1d837c9ea7d624c3de01c2cf0438 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Sat, 18 Mar 2023 09:54:45 +0800 Subject: [PATCH] Changed underlying `openai/client` package version from 0.3.4 to 0.4.0 (#3) --- composer.json | 5 +++-- src/ClientFactory.php | 26 +++++++++----------------- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index 769c50d..c3f23bb 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.4" + "openai-php/client": "^0.4.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.0", @@ -44,7 +44,8 @@ "config": { "sort-packages": true, "allow-plugins": { - "pestphp/pest-plugin": true + "pestphp/pest-plugin": true, + "php-http/discovery": true } }, "minimum-stability": "dev", diff --git a/src/ClientFactory.php b/src/ClientFactory.php index 68e7f02..ac8694e 100644 --- a/src/ClientFactory.php +++ b/src/ClientFactory.php @@ -12,11 +12,7 @@ use FriendsOfHyperf\OpenAi\Exception\ApiKeyIsMissing; 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 OpenAI; use Psr\Container\ContainerInterface; final class ClientFactory @@ -30,17 +26,13 @@ public function __invoke(ContainerInterface $container) throw ApiKeyIsMissing::create(); } - $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); + return OpenAI::factory() + ->withApiKey($apiKey) + ->withOrganization($organization) + ->withBaseUri('api.openai.com/v1') + ->withHttpClient( + $container->get(GuzzleClientFactory::class)->create() + ) + ->make(); } }