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

Allow configuration of base uri #72

Open
wants to merge 3 commits 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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ for a response. By default, the client will time out after 30 seconds.
OPENAI_REQUEST_TIMEOUT=
```

### Custom API Endpoint
If you want to use a local model compatible with the openai-api you can set the base url:

```env
OPENAI_BASE_URL=localhost:1234/v1/
```

## Usage

For usage examples, take a look at the [openai-php/client](https://github.com/openai-php/client) repository.
Expand Down
13 changes: 13 additions & 0 deletions config/openai.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,17 @@
*/

'request_timeout' => env('OPENAI_REQUEST_TIMEOUT', 30),

/*
|--------------------------------------------------------------------------
| Base URL
|--------------------------------------------------------------------------
|
| This is the base URL used to make requests to the OpenAI API. Unless you
| are using a custom API endpoint, you should not need to change this.
| Make sure to omit the http(s):// prefix.
| Default is 'api.openai.com/v1'
*/

'base_uri' => env('OPENAI_BASE_URL', 'api.openai.com/v1'),
];
15 changes: 10 additions & 5 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use OpenAI\Contracts\ClientContract;
use OpenAI\Laravel\Exceptions\ApiKeyIsMissing;

use function is_string;

/**
* @internal
*/
Expand All @@ -24,16 +26,19 @@ public function register(): void
$this->app->singleton(ClientContract::class, static function (): Client {
$apiKey = config('openai.api_key');
$organization = config('openai.organization');

$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)
->withHttpClient(new \GuzzleHttp\Client(['timeout' => config('openai.request_timeout', 30)]))
->make();
->withHttpClient(new \GuzzleHttp\Client(['timeout' => config('openai.request_timeout', 30)]));
if (is_string($baseUri)) {
$client->withBaseUri($baseUri);
}

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

$this->app->alias(ClientContract::class, 'openai');
Expand Down
1 change: 1 addition & 0 deletions tests/Facades/OpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
$app->bind('config', fn () => new Repository([
'openai' => [
'api_key' => 'test',
'base_uri' => 'api.openai.com/v1/',
],
]));

Expand Down
2 changes: 2 additions & 0 deletions tests/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
$app->bind('config', fn () => new Repository([
'openai' => [
'api_key' => 'test',
'base_uri' => 'api.openai.com/v1/',
],
]));

Expand All @@ -26,6 +27,7 @@
$app->bind('config', fn () => new Repository([
'openai' => [
'api_key' => 'test',
'base_uri' => 'api.openai.com/v1/',
],
]));

Expand Down