openai-php / laravel

⚡️ OpenAI PHP for Laravel is a supercharged PHP API client that allows you to interact with OpenAI API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I use factory for Azure

DC-Sebastian opened this issue · comments

Can i use

$client = OpenAI::factory() ->withBaseUri('{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}') ->withHttpHeader('api-key', '{your-api-key}') ->withQueryParam('api-version', '{version}') ->make();

for Azure as described here?

I don't know exactly how to implement this with OpenAI PHP for Laravel.

Hi @DC-Sebastian

One way is to override the singleton in your service provider:

$this->app->singleton(ClientContract::class, static function (): Client {
    $apiKey = config('openai.api_key');

    if (! is_string($apiKey)) {
        throw ApiKeyIsMissing::create();
    }

    return OpenAI::factory()
        ->withBaseUri('{your-resource-name}.openai.azure.com/openai/deployments/{deployment-id}')
        ->withHttpHeader('api-key', '{your-api-key}')
        ->withQueryParam('api-version', '{version}')
        ->withHttpClient(new \GuzzleHttp\Client(['timeout' => config('openai.request_timeout', 30)]))
        ->make();
});

Here is the original implementation from the package provided.
https://github.com/openai-php/laravel/blob/main/src/ServiceProvider.php#L24

Hope this helps.