Reduce bulk generation to 1 article per AJAX request
Free OpenRouter models are too slow for batch processing on shared hosting. Reduce batch size from 5 to 1, API timeout from 30s to 15s, and max retries from 3 to 1 to stay within PHP max_execution_time. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -13,7 +13,7 @@ class Peptide_News_LLM {
|
|||||||
const API_URL = 'https://openrouter.ai/api/v1/chat/completions';
|
const API_URL = 'https://openrouter.ai/api/v1/chat/completions';
|
||||||
|
|
||||||
/** @var int Maximum retries for rate-limited requests. */
|
/** @var int Maximum retries for rate-limited requests. */
|
||||||
const MAX_RETRIES = 3;
|
const MAX_RETRIES = 1;
|
||||||
|
|
||||||
/** @var int Maximum elapsed seconds before stopping a batch. */
|
/** @var int Maximum elapsed seconds before stopping a batch. */
|
||||||
const BATCH_TIMEOUT = 120;
|
const BATCH_TIMEOUT = 120;
|
||||||
@@ -255,7 +255,7 @@ class Peptide_News_LLM {
|
|||||||
|
|
||||||
while ( $retries <= self::MAX_RETRIES ) {
|
while ( $retries <= self::MAX_RETRIES ) {
|
||||||
$response = wp_remote_post( self::API_URL, array(
|
$response = wp_remote_post( self::API_URL, array(
|
||||||
'timeout' => 30,
|
'timeout' => 15,
|
||||||
'headers' => array(
|
'headers' => array(
|
||||||
'Authorization' => 'Bearer ' . $api_key,
|
'Authorization' => 'Bearer ' . $api_key,
|
||||||
'Content-Type' => 'application/json',
|
'Content-Type' => 'application/json',
|
||||||
@@ -411,11 +411,11 @@ class Peptide_News_LLM {
|
|||||||
) );
|
) );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process a small batch per request to stay within PHP's
|
// Process ONE article per request to stay within PHP's
|
||||||
// max_execution_time (often 30s on shared hosting). The JS UI
|
// max_execution_time (often 30s on shared hosting). Each article
|
||||||
// loops automatically until all articles are done.
|
// requires 2 API calls (keywords + summary) which can take 10-20s
|
||||||
$batch_size = min( 5, $remaining );
|
// with free models. The JS UI loops automatically until done.
|
||||||
$processed = self::process_unanalyzed( $batch_size, true );
|
$processed = self::process_unanalyzed( 1, true );
|
||||||
|
|
||||||
// Recount remaining after this batch.
|
// Recount remaining after this batch.
|
||||||
$still_remaining = (int) $wpdb->get_var(
|
$still_remaining = (int) $wpdb->get_var(
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class Peptide_News_LLM {
|
|||||||
const API_URL = 'https://openrouter.ai/api/v1/chat/completions';
|
const API_URL = 'https://openrouter.ai/api/v1/chat/completions';
|
||||||
|
|
||||||
/** @var int Maximum retries for rate-limited requests. */
|
/** @var int Maximum retries for rate-limited requests. */
|
||||||
const MAX_RETRIES = 3;
|
const MAX_RETRIES = 1;
|
||||||
|
|
||||||
/** @var int Maximum elapsed seconds before stopping a batch. */
|
/** @var int Maximum elapsed seconds before stopping a batch. */
|
||||||
const BATCH_TIMEOUT = 120;
|
const BATCH_TIMEOUT = 120;
|
||||||
@@ -255,7 +255,7 @@ class Peptide_News_LLM {
|
|||||||
|
|
||||||
while ( $retries <= self::MAX_RETRIES ) {
|
while ( $retries <= self::MAX_RETRIES ) {
|
||||||
$response = wp_remote_post( self::API_URL, array(
|
$response = wp_remote_post( self::API_URL, array(
|
||||||
'timeout' => 30,
|
'timeout' => 15,
|
||||||
'headers' => array(
|
'headers' => array(
|
||||||
'Authorization' => 'Bearer ' . $api_key,
|
'Authorization' => 'Bearer ' . $api_key,
|
||||||
'Content-Type' => 'application/json',
|
'Content-Type' => 'application/json',
|
||||||
@@ -411,11 +411,11 @@ class Peptide_News_LLM {
|
|||||||
) );
|
) );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process a small batch per request to stay within PHP's
|
// Process ONE article per request to stay within PHP's
|
||||||
// max_execution_time (often 30s on shared hosting). The JS UI
|
// max_execution_time (often 30s on shared hosting). Each article
|
||||||
// loops automatically until all articles are done.
|
// requires 2 API calls (keywords + summary) which can take 10-20s
|
||||||
$batch_size = min( 5, $remaining );
|
// with free models. The JS UI loops automatically until done.
|
||||||
$processed = self::process_unanalyzed( $batch_size, true );
|
$processed = self::process_unanalyzed( 1, true );
|
||||||
|
|
||||||
// Recount remaining after this batch.
|
// Recount remaining after this batch.
|
||||||
$still_remaining = (int) $wpdb->get_var(
|
$still_remaining = (int) $wpdb->get_var(
|
||||||
|
|||||||
Reference in New Issue
Block a user