Skip to content

Commit

Permalink
Bump openai-php/client to 0.3.4 (#2)
Browse files Browse the repository at this point in the history
* Revert ClientFactory

* Format

* Bump openai-client to 0.3.4
  • Loading branch information
huangdijia committed Mar 4, 2023
1 parent 6d2ea54 commit 47b4d10
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 18 additions & 2 deletions src/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
19 changes: 8 additions & 11 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

declare(strict_types=1);
/**
* This file is part of openai-client.
*
* @link https://github.com/friendsofhyperf/openai-client
* @document https://github.com/friendsofhyperf/openai-client/blob/main/README.md
* @contact [email protected]
*/
// uses(Tests\TestCase::class)->in('Feature');

/*
Expand Down
9 changes: 8 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
<?php

declare(strict_types=1);
/**
* This file is part of openai-client.
*
* @link https://github.com/friendsofhyperf/openai-client
* @document https://github.com/friendsofhyperf/openai-client/blob/main/README.md
* @contact [email protected]
*/
namespace Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
//
}

0 comments on commit 47b4d10

Please sign in to comment.