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

Allows to change the HTTP client in the factory #65

Closed
wants to merge 4 commits into from
Closed
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
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
],
"require": {
"php": "^8.1.0",
"guzzlehttp/guzzle": "^7.5.0"
"guzzlehttp/psr7": "^2.4.4",
"php-http/discovery": "^1.14",
"psr/http-client-implementation": "^1.0"
},
"require-dev": {
"guzzlehttp/guzzle": "^7.5.0",
"laravel/pint": "^1.6.0",
"nunomaduro/collision": "^7.0.5",
"pestphp/pest": "^2.0.0",
Expand Down Expand Up @@ -42,7 +45,8 @@
"sort-packages": true,
"preferred-install": "dist",
"allow-plugins": {
"pestphp/pest-plugin": true
"pestphp/pest-plugin": true,
"php-http/discovery": false
}
},
"scripts": {
Expand Down
7 changes: 4 additions & 3 deletions src/OpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

declare(strict_types=1);

use GuzzleHttp\Client as GuzzleClient;
use Http\Discovery\Psr18ClientDiscovery;
use OpenAI\Client;
use OpenAI\Transporters\HttpTransporter;
use OpenAI\ValueObjects\ApiKey;
use OpenAI\ValueObjects\Transporter\BaseUri;
use OpenAI\ValueObjects\Transporter\Headers;
use Psr\Http\Client\ClientInterface;

final class OpenAI
{
/**
* Creates a new Open AI Client with the given API token.
*/
public static function client(string $apiKey, string $organization = null): Client
public static function client(string $apiKey, string $organization = null, ClientInterface $client = null): Client
{
$apiKey = ApiKey::from($apiKey);

Expand All @@ -26,7 +27,7 @@ public static function client(string $apiKey, string $organization = null): Clie
$headers = $headers->withOrganization($organization);
}

$client = new GuzzleClient();
GromNaN marked this conversation as resolved.
Show resolved Hide resolved
$client ??= Psr18ClientDiscovery::find();

$transporter = new HttpTransporter($client, $baseUri, $headers);

Expand Down
1 change: 1 addition & 0 deletions tests/Arch.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
]);

test('openai')->expect('OpenAI')->toOnlyUse([
'Http\Discovery\Psr18ClientDiscovery',
'Psr\Http\Client',
'GuzzleHttp\Client',
'GuzzleHttp\Psr7',
Expand Down
19 changes: 19 additions & 0 deletions tests/OpenAI.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use OpenAI\Client;

it('may create a client', function () {
Expand All @@ -13,3 +17,18 @@

expect($openAI)->toBeInstanceOf(Client::class);
});

it('accepts a custom http client', function () {
$mock = new MockHandler([
new Response(200, [], '{"id":"test-id","object":"text_completion","created":1589478378,"model":"text-davinci-003","choices":[{"text":"\n\nThis is indeed a test","index":0,"logprobs":null,"finish_reason":"length"}],"usage":{"prompt_tokens":5,"completion_tokens":7,"total_tokens":12}}'),
]);
$handlerStack = HandlerStack::create($mock);
$client = new GuzzleClient(['handler' => $handlerStack]);
$openAI = OpenAI::client('foo', client: $client);
GromNaN marked this conversation as resolved.
Show resolved Hide resolved
$response = $openAI->completions()->create([
'model' => 'text-davinci-003',
'prompt' => 'Say this is a test',
]);

expect($response->id)->toBe('test-id');
});