Skip to content

Commit b52db71

Browse files
Adds ability to configure Project & Base URL via env (#144)
* Simplify support for Project-based auth * feat: add env config for base url * fix: correct crash if .env|.env.example missing * chore: typo on docblock --------- Co-authored-by: Evosite <[email protected]>
1 parent 589cf3a commit b52db71

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,23 @@ OPENAI_API_KEY=
7171
OPENAI_ORGANIZATION=
7272
```
7373

74+
### OpenAI Project
75+
76+
For implementations that require a project ID, you can specify
77+
the OpenAI project ID in your environment variables.
78+
79+
```env
80+
OPENAI_PROJECT=proj_...
81+
```
82+
83+
### OpenAI API Base URL
84+
85+
The base URL for the OpenAI API. By default, this is set to `api.openai.com/v1`.
86+
87+
```env
88+
OPENAI_BASE_URL=
89+
```
90+
7491
### Request Timeout
7592

7693
The timeout may be used to specify the maximum number of seconds to wait

config/openai.php

+21
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,27 @@
1515
'api_key' => env('OPENAI_API_KEY'),
1616
'organization' => env('OPENAI_ORGANIZATION'),
1717

18+
/*
19+
|--------------------------------------------------------------------------
20+
| OpenAI API Project
21+
|--------------------------------------------------------------------------
22+
|
23+
| Here you may specify your OpenAI API project. This is used optionally in
24+
| situations where you are using a legacy user API key and need association
25+
| with a project. This is not required for the newer API keys.
26+
*/
27+
'project' => env('OPENAI_PROJECT'),
28+
29+
/*
30+
|--------------------------------------------------------------------------
31+
| OpenAI Base URL
32+
|--------------------------------------------------------------------------
33+
|
34+
| Here you may specify your OpenAI API base URL used to make requests. This
35+
| is needed if using a custom API endpoint. Defaults to: api.openai.com/v1
36+
*/
37+
'base_uri' => env('OPENAI_BASE_URL'),
38+
1839
/*
1940
|--------------------------------------------------------------------------
2041
| Request Timeout

src/Commands/InstallCommand.php

+9
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ private function copyConfig(): void
7878

7979
private function addEnvKeys(string $envFile): void
8080
{
81+
if (! is_writable(base_path($envFile))) {
82+
View::render('components.two-column-detail', [
83+
'left' => $envFile,
84+
'right' => 'File is not writable.',
85+
]);
86+
87+
return;
88+
}
89+
8190
$fileContent = file_get_contents(base_path($envFile));
8291

8392
if ($fileContent === false) {

src/ServiceProvider.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,28 @@ public function register(): void
2525
$this->app->singleton(ClientContract::class, static function (): Client {
2626
$apiKey = config('openai.api_key');
2727
$organization = config('openai.organization');
28+
$project = config('openai.project');
29+
$baseUri = config('openai.base_uri');
2830

2931
if (! is_string($apiKey) || ($organization !== null && ! is_string($organization))) {
3032
throw ApiKeyIsMissing::create();
3133
}
3234

33-
return OpenAI::factory()
35+
$client = OpenAI::factory()
3436
->withApiKey($apiKey)
3537
->withOrganization($organization)
3638
->withHttpHeader('OpenAI-Beta', 'assistants=v2')
37-
->withHttpClient(new \GuzzleHttp\Client(['timeout' => config('openai.request_timeout', 30)]))
38-
->make();
39+
->withHttpClient(new \GuzzleHttp\Client(['timeout' => config('openai.request_timeout', 30)]));
40+
41+
if (is_string($project)) {
42+
$client->withProject($project);
43+
}
44+
45+
if (is_string($baseUri)) {
46+
$client->withBaseUri($baseUri);
47+
}
48+
49+
return $client->make();
3950
});
4051

4152
$this->app->alias(ClientContract::class, 'openai');

0 commit comments

Comments
 (0)