Filter
uncanny-automator
automator_openai_request_timeout
Filters the timeout duration for outgoing requests made to the OpenAI API integration.
add_filter( 'automator_openai_request_timeout', $callback, 10, 1 );
Description
Allows modification of the Open AI API request timeout. Developers can adjust how long the plugin waits for a response from the OpenAI API, useful for handling slower responses or ensuring faster timeouts. The default timeout is applied if not overridden.
Usage
add_filter( 'automator_openai_request_timeout', 'your_function_name', 10, 1 );
Parameters
-
$args(mixed) - This parameter is an array that contains various arguments for the OpenAI API request, including the HTTP method, whether to reduce credits, and the request timeout.
Return Value
The filtered value.
Examples
/**
* Increase the default OpenAI API request timeout for longer-running operations.
*
* By default, WordPress might have a lower timeout. This filter allows
* us to extend it to prevent requests from failing due to timeouts,
* especially for complex OpenAI completions.
*
* @param int $timeout The current timeout value in seconds.
* @return int The modified timeout value in seconds.
*/
add_filter( 'automator_openai_request_timeout', function( $timeout ) {
// If the original timeout is already quite high, we don't want to set it
// excessively high, but for typical OpenAI requests, 60 seconds is a good
// baseline to avoid premature timeouts.
if ( $timeout < 60 ) {
return 60; // Set a minimum timeout of 60 seconds
}
return $timeout; // Otherwise, keep the original timeout
}, 10, 1 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/integrations/open-ai/helpers/open-ai-api-caller.php:124
public function openai_request( $endpoint, $body = array(), $args = array() ) {
$args = wp_parse_args(
$args,
array(
'method' => 'POST',
'reduce_credits' => true,
'timeout' => 120,
)
);
if ( true === AUTOMATOR_DISABLE_APP_INTEGRATION_REQUESTS ) {
throw new Exception( esc_html_x( 'App integrations have been disabled in wp-config.php.', 'OpenAI', 'uncanny-automator' ), 500 );
}
$api_key = (string) $this->helpers->get_credentials();
if ( empty( $api_key ) ) {
throw new Exception( esc_html_x( 'OpenAI API key is not configured.', 'OpenAI', 'uncanny-automator' ), 400 );
}
// Reduce Automator credits after validating the key but before making the request.
if ( $args['reduce_credits'] ) {
Api_Server::get_instance()->charge_usage();
}
$request_args = array(
'method' => $args['method'],
'timeout' => apply_filters( 'automator_openai_request_timeout', absint( $args['timeout'] ) ),
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $api_key,
),
);
// JSON-encode the body for POST requests.
if ( 'POST' === $args['method'] && ! empty( $body ) ) {
$request_args['body'] = wp_json_encode( $body );
}
$url = self::OPENAI_API_URL . ltrim( $endpoint, '/' );
$response = wp_remote_request( $url, $request_args );
if ( is_wp_error( $response ) ) {
throw new Exception( esc_html( $response->get_error_message() ), 500 );
}
$status_code = wp_remote_retrieve_response_code( $response );
$response_body = json_decode( wp_remote_retrieve_body( $response ), true );
if ( 200 !== $status_code ) {
$error_type = $response_body['error']['type'] ?? 'unknown_error';
$error_message = $response_body['error']['message'] ?? 'OpenAI returned an error with no message.';
throw new Exception(
// translators: %1$s is the error type, %2$s is the error message.
esc_html( sprintf( 'OpenAI error: [%1$s] %2$s', $error_type, $error_message ) ),
absint( $status_code )
);
}
return $response_body;
}