Make base URI configurable
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:
- Extract interface from
OpenAI\ValueObjects\Transporter\BaseUri - Change parameter type of
$baseUriinOpenAI\ValueObjects\Transporter\Payload::toRequestto extracted interface - Add optional parameter
BaseUriInterface $baseUri = nulltoOpenAI::client - Modify
OpenAI::client, so that it handles the default value for$baseUrilike 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.
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...
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?
Just add protocol, via the BaseUri, if it's missing from the given string.
Okay, I will prepare a PR later that week :)
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 :)
This can now be achieved by using the newly introduced factory:
$client = OpenAI::factory()
->withApiKey('<your-api-key>')
->withBaseUri('openai.example.com/v1')
->make();