Skip to content

Adds ability to configure Project & Base URL via env #144

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

Merged
merged 4 commits into from
Apr 8, 2025
Merged
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ OPENAI_API_KEY=
OPENAI_ORGANIZATION=
```

### OpenAI Project

For implementations that require a project ID, you can specify
the OpenAI project ID in your environment variables.

```env
OPENAI_PROJECT=proj_...
```

### OpenAI API Base URL

The base URL for the OpenAI API. By default, this is set to `api.openai.com/v1`.

```env
OPENAI_BASE_URL=
```

### Request Timeout

The timeout may be used to specify the maximum number of seconds to wait
Expand Down
21 changes: 21 additions & 0 deletions config/openai.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@
'api_key' => env('OPENAI_API_KEY'),
'organization' => env('OPENAI_ORGANIZATION'),

/*
|--------------------------------------------------------------------------
| OpenAI API Project
|--------------------------------------------------------------------------
|
| Here you may specify your OpenAI API project. This is used optionally in
| situations where you are using a legacy user API key and need association
| with a project. This is not required for the newer API keys.
*/
'project' => env('OPENAI_PROJECT'),

/*
|--------------------------------------------------------------------------
| OpenAI Base URL
|--------------------------------------------------------------------------
|
| Here you may specify your OpenAI API base URL used to make requests. This
| is needed if using a custom API endpoint. Defaults to: api.openai.com/v1
*/
'base_uri' => env('OPENAI_BASE_URL'),

/*
|--------------------------------------------------------------------------
| Request Timeout
Expand Down
9 changes: 9 additions & 0 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ private function copyConfig(): void

private function addEnvKeys(string $envFile): void
{
if (! is_writable(base_path($envFile))) {
View::render('components.two-column-detail', [
'left' => $envFile,
'right' => 'File is not writable.',
]);

return;
}

$fileContent = file_get_contents(base_path($envFile));

if ($fileContent === false) {
Expand Down
17 changes: 14 additions & 3 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,28 @@ public function register(): void
$this->app->singleton(ClientContract::class, static function (): Client {
$apiKey = config('openai.api_key');
$organization = config('openai.organization');
$project = config('openai.project');
$baseUri = config('openai.base_uri');

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

return OpenAI::factory()
$client = OpenAI::factory()
->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)]));

if (is_string($project)) {
$client->withProject($project);
}

if (is_string($baseUri)) {
$client->withBaseUri($baseUri);
}

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

$this->app->alias(ClientContract::class, 'openai');
Expand Down
Loading