client icon indicating copy to clipboard operation
client copied to clipboard

Make base URI configurable

Open LeoVie opened this issue 3 years ago • 5 comments

Hi,

what do you think about making the base URI configurable? At the moment, the base URI is hardcoded to https://api.openai.com/v1.

Making it configurable would make end-to-end testing of applications using the OpenAI client easier, as one could use an url to a mocking server in the test environment.

I would implement this as a non-breaking change via the following steps:

  1. Extract interface from OpenAI\ValueObjects\Transporter\BaseUri
  2. Change parameter type of $baseUri in OpenAI\ValueObjects\Transporter\Payload::toRequest to extracted interface
  3. Add optional parameter BaseUriInterface $baseUri = null to OpenAI::client
  4. Modify OpenAI::client, so that it handles the default value for $baseUri like that:
public static function client(string $apiToken, string $organization = null, BaseUriInterface $baseUri = null): Client
{
    ...
    $baseUri = $baseUri ?? BaseUri::from('api.openai.com/v1');
    ...
}

What do you think about that? If you don't object, I would implement that.

LeoVie avatar Dec 29 '22 13:12 LeoVie

We think we would open for a pull request that adds this:

OpenAI::client('sk-...')
    ->withBaseUri(''api.foo-bar.com/v1'); // returns a new instance...

nunomaduro avatar Dec 29 '22 14:12 nunomaduro

Thank you. One issue I see with your proposal is, that this would force the mock server to support https, as the base URI gets prefixed by https in the BaseUri::toString method. That's the reason, I suggested the extracted interface.

Another option would be to remove the protocol prefixing in BaseURI::toString and use the URL with protocol directly in BaseUri::from.

So the existing code in BaseUri would change to

public function toString(): string
{
    return "{$this->baseUri}/";
}

and in OpenAI::client to

public static function client(string $apiToken, string $organization = null): Client
{
    ...
    $baseUri = BaseUri::from('https://api.openai.com/v1');
    ...
}

Then the method withBaseUri can be easily added and be used like

OpenAI::client('sk-...')
    ->withBaseUri('http://api.foo-base.com/v1');

What do you think?

LeoVie avatar Dec 29 '22 15:12 LeoVie

Just add protocol, via the BaseUri, if it's missing from the given string.

nunomaduro avatar Dec 29 '22 15:12 nunomaduro

Okay, I will prepare a PR later that week :)

LeoVie avatar Dec 29 '22 15:12 LeoVie

Hmm, I guess, doing it via

OpenAI::client('sk-...')
    ->withBaseUri('http://api.foo-base.com/v1');

is not that trivial, as the Client created via OpenAI::client does not know the base URI directly, but via HttpTransporter.

I've created a PR, that allows usage in the form

OpenAI::clientWithBaseUri('sk-...', 'http://api.foo-base.com/v1');

I'm open to opinions on this :)

LeoVie avatar Dec 29 '22 17:12 LeoVie

This can now be achieved by using the newly introduced factory:

$client = OpenAI::factory()
    ->withApiKey('<your-api-key>')
    ->withBaseUri('openai.example.com/v1')
    ->make();

gehrisandro avatar Mar 17 '23 19:03 gehrisandro