Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OpenAI factory service #106

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace OpenAI\Laravel;

use Illuminate\Container\Container;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use OpenAI;
Expand All @@ -12,6 +13,10 @@
use OpenAI\Laravel\Commands\InstallCommand;
use OpenAI\Laravel\Exceptions\ApiKeyIsMissing;

use function assert;
use function config;
use function is_string;

/**
* @internal
*/
Expand All @@ -22,7 +27,7 @@ final class ServiceProvider extends BaseServiceProvider implements DeferrablePro
*/
public function register(): void
{
$this->app->singleton(ClientContract::class, static function (): Client {
$this->app->bind(OpenAI\Factory::class, static function (): OpenAI\Factory {
$apiKey = config('openai.api_key');
$organization = config('openai.organization');

Expand All @@ -34,10 +39,18 @@ public function register(): void
->withApiKey($apiKey)
->withOrganization($organization)
->withHttpHeader('OpenAI-Beta', 'assistants=v2')
->withHttpClient(new \GuzzleHttp\Client(['timeout' => config('openai.request_timeout', 30)]))
->make();
->withHttpClient(new \GuzzleHttp\Client(['timeout' => config('openai.request_timeout', 30)]));
});

$this->app->singleton(ClientContract::class, static function (Container $app): Client {
$factory = $app->make(OpenAI\Factory::class);
assert($factory instanceof OpenAI\Factory);

return $factory->make();
}
);

$this->app->alias(OpenAI\Factory::class, 'openai.factory');
$this->app->alias(ClientContract::class, 'openai');
$this->app->alias(ClientContract::class, Client::class);
}
Expand Down Expand Up @@ -68,7 +81,9 @@ public function provides(): array
return [
Client::class,
ClientContract::class,
OpenAI\Factory::class,
'openai',
'openai.factory',
];
}
}
1 change: 1 addition & 0 deletions tests/Arch.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
->toOnlyUse([
'GuzzleHttp\Client',
'Illuminate\Support\ServiceProvider',
'Illuminate\Container\Container',
'OpenAI\Laravel',
'OpenAI',
'Illuminate\Contracts\Support\DeferrableProvider',
Expand Down
17 changes: 17 additions & 0 deletions tests/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,24 @@
use Illuminate\Config\Repository;
use OpenAI\Client;
use OpenAI\Contracts\ClientContract;
use OpenAI\Factory;
use OpenAI\Laravel\Exceptions\ApiKeyIsMissing;
use OpenAI\Laravel\ServiceProvider;

it('binds the factory on the container', function () {
$app = app();

$app->bind('config', fn () => new Repository([
'openai' => [
'api_key' => 'test',
],
]));

(new ServiceProvider($app))->register();

expect($app->get(Factory::class))->toBeInstanceOf(Factory::class);
});

it('binds the client on the container', function () {
$app = app();

Expand Down Expand Up @@ -55,6 +70,8 @@
expect($provides)->toBe([
Client::class,
ClientContract::class,
Factory::class,
'openai',
'openai.factory',
]);
});