-
-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathServiceProvider.php
More file actions
68 lines (59 loc) · 1.73 KB
/
ServiceProvider.php
File metadata and controls
68 lines (59 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
declare(strict_types=1);
namespace OpenAI\Laravel;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use OpenAI;
use OpenAI\Client;
use OpenAI\Contracts\ClientContract;
use OpenAI\Laravel\Exceptions\ApiKeyIsMissing;
/**
* @internal
*/
final class ServiceProvider extends BaseServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*/
public function register(): void
{
$this->app->singleton(ClientContract::class, static function (): Client {
$apiKey = config('openai.api_key');
$organization = config('openai.organization');
if (! is_string($apiKey) || ($organization !== null && ! is_string($organization))) {
throw ApiKeyIsMissing::create();
}
return OpenAI::factory()
->withApiKey($apiKey)
->withOrganization($organization)
->withHttpClient(GuzzleTransporter::getClient())
->make();
});
$this->app->alias(ClientContract::class, 'openai');
$this->app->alias(ClientContract::class, Client::class);
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/openai.php' => config_path('openai.php'),
]);
}
}
/**
* Get the services provided by the provider.
*
* @return array<int, string>
*/
public function provides(): array
{
return [
Client::class,
ClientContract::class,
'openai',
];
}
}