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

Add retry mechanism #53

Open
wants to merge 2 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
22 changes: 22 additions & 0 deletions config/openai.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,26 @@
*/

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

/*
|--------------------------------------------------------------------------
| Maximum Retry Attempt
|--------------------------------------------------------------------------
|
| The retry attempt may be used to specify how many times retry when OpenAI server return error .
| By default, the library will try once.
*/

'max_retry_attempt' => env('OPENAI_RETRY_ATTEMPT', 5),

/*
|--------------------------------------------------------------------------
| Retry Delay
|--------------------------------------------------------------------------
|
| Decides how long after a request should be repeated when a request fails.
| By default, resend the request immediately.
*/

'retry_delay' => env('OPENAI_RETRY_DELAY'),
];
73 changes: 73 additions & 0 deletions src/GuzzleTransporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace OpenAI\Laravel;

use Closure;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;

class GuzzleTransporter
{
/**
* Get Guzzle Client
*/
public static function getClient(): Client
{
$stack = HandlerStack::create();
$stack->push((new GuzzleTransporter)->getRetryMiddleware());

return new Client([
'timeout' => config('openai.request_timeout', 30),
'handler' => $stack,
]);
}

/**
* Get retry middleware callable
*/
public function getRetryMiddleware(): callable
{
return Middleware::retry($this->getDecider(), $this->getDelayDuration());
}

/**
* Get decider logic
*/
public function getDecider(): Closure
{
$maxRetries = config('openai.max_retry_attempt');

return function (
int $retries,
?RequestInterface $request = null,
?ResponseInterface $response = null,
?RuntimeException $e = null
) use ($maxRetries): bool {
if ($retries > $maxRetries) {
return false;
}

if ($e instanceof RequestException || $e instanceof ConnectException) {
return true;
}

return false;
};
}

/**
* Get delay duration
*/
public function getDelayDuration(): callable
{
return function () {
return 1000 * config('openai.retry_delay');
};
}
}
2 changes: 1 addition & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function register(): void
return OpenAI::factory()
->withApiKey($apiKey)
->withOrganization($organization)
->withHttpClient(new \GuzzleHttp\Client(['timeout' => config('openai.request_timeout', 30)]))
->withHttpClient(GuzzleTransporter::getClient())
->make();
});

Expand Down
17 changes: 17 additions & 0 deletions tests/Arch.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,20 @@
'config',
'config_path',
]);

test('guzzle transporter')
->expect('OpenAI\Laravel\GuzzleTransporter')
->toOnlyUse([
'Closure',
'GuzzleHttp\Client',
'GuzzleHttp\Exception\ConnectException',
'GuzzleHttp\Exception\RequestException',
'GuzzleHttp\HandlerStack',
'GuzzleHttp\Middleware',
'Psr\Http\Message\RequestInterface',
'Psr\Http\Message\ResponseInterface',
'RuntimeException',

// helpers...
'config',
]);
35 changes: 35 additions & 0 deletions tests/GuzzleTransporterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use GuzzleHttp\Client;
use OpenAI\Laravel\GuzzleTransporter;

test('getRetryMiddleware returns a callable', function () {
$instance = new GuzzleTransporter();

$retryMiddleware = $instance->getRetryMiddleware();

expect($retryMiddleware)->toBeCallable();
});

test('getClient returns a Client', function () {

$instance = GuzzleTransporter::getClient();

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

test('getDelayDuration returns a callable', function () {
$instance = new GuzzleTransporter();

$delayCallable = $instance->getDelayDuration();

expect($delayCallable)->toBeCallable();
});

test('getDecider returns a callable', function () {
$instance = new GuzzleTransporter();

$deciderCallable = $instance->getDecider();

expect($deciderCallable)->toBeCallable();
});