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

retry after

ahmadmayahi opened this issue · comments

At times, ChatGPT may display the following error message: "That model is currently overloaded with other requests." This issue arises due to the high volume of users attempting to access the API.

To address this problem, referring to the documentation, it is recommended to retry the request after a certain period of time. Adding a 'retryAfter' method, akin to Laravel's 'Http::retry()', could potentially resolve this issue.

It was something I was thinking add a retry mechanism either. HTTP::retry() won't work because of package uses guzzle client not HTTP facade. Is there a retry-after param on response header in this sitution?

I will open a pr for this feature.

Until we get this feature (great feature request), do you have any suggestions to deal correctly with HTTP error codes or error messages like the model overload?
Do I understand correctly that openai-php doesn't provide direct way to easily deal with errors?

@giancarloerra

I solved it by switching to Laravel's Http client which is built on top of Guzzle.

Actually, you don't need a package for OpenAI, Guzzle is more than enough. Because OpenAI API is very easy to use.

 return Http::withToken($token)
            ->baseUrl('https://api.openai.com/v1/')
            ->retry(5, 500)
            ->post('https://api.openai.com/v1/chat/completions',
                [
                    'model' => 'gpt-4',
                    'messages' => [
                        [
                            'role' => 'system',
                            'content' => 'You are a helpful assistant.',
                        ],
                        [
                            'role' => 'user',
                            'content' => $prompt,
                        ],
                    ],
                    'functions' => [
                        [
                            'name' => $function, 'parameters' => config('schema.'.$function),
                        ],
                    ],
                    'function_call' => [
                        'name' => $function,
                    ],
                    'temperature' => 0.6,
                    'top_p' => 1,
                ]
            )
            ->throw()
            ->json();

That's it :-)