Skip to content

Commit

Permalink
Optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
huangdijia committed Jan 6, 2023
1 parent d28d62c commit b88b6b1
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"php": ">=8.1",
"hyperf/config": "~3.0.0",
"hyperf/di": "~3.0.0",
"openai-php/client": "^0.2.1",
"hyperf/guzzle": "~3.0.0"
"hyperf/guzzle": "~3.0.0",
"openai-php/client": "^0.2.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
Expand All @@ -39,6 +39,9 @@
"config": "FriendsOfHyperf\\OpenAi\\ConfigProvider"
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"scripts": {
"analyse": "phpstan analyse --memory-limit 300M -l 0 -c phpstan.neon ./src",
Expand Down
2 changes: 1 addition & 1 deletion publish/openai.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
* @contact [email protected]
*/
return [
'api_key' => env('OPENAI_API_KEY'),
'api_key' => env('OPENAI_API_KEY', ''),
'organization' => env('OPENAI_ORGANIZATION'),
];
9 changes: 3 additions & 6 deletions src/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,18 @@ class ClientFactory
public function __invoke(ContainerInterface $container)
{
$config = $container->get(ConfigInterface::class);
$clientFactory = $container->get(GuzzleClientFactory::class);

$apiToken = $config->get('openai.api_key');
$apiKey = $config->get('openai.api_key');
$organization = $config->get('openai.organization');

$apiToken = ApiToken::from($apiToken);
$apiToken = ApiToken::from($apiKey);
$baseUri = BaseUri::from('api.openai.com/v1');
$headers = Headers::withAuthorization($apiToken);

if ($organization !== null) {
$headers = $headers->withOrganization($organization);
}

$client = $clientFactory->create();

$client = $container->get(GuzzleClientFactory::class)->create();
$transporter = new HttpTransporter($client, $baseUri, $headers);

return new Client($transporter);
Expand Down
2 changes: 1 addition & 1 deletion src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __invoke()
'publish' => [
[
'id' => 'config',
'description' => 'The config for openai.',
'description' => 'The config file for OpenAI.',
'source' => __DIR__ . '/../publish/openai.php',
'destination' => BASE_PATH . '/config/autoload/openai.php',
],
Expand Down

0 comments on commit b88b6b1

Please sign in to comment.