mirror of
https://github.com/WenPai-org/wpmind.git
synced 2025-08-04 11:56:05 +08:00
added buffer to PHP to prevent sending small chunks
This commit is contained in:
parent
e8a4b8511f
commit
5e8575cd57
1 changed files with 59 additions and 7 deletions
|
@ -13,6 +13,34 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* Class Mind_Rest
|
||||
*/
|
||||
class Mind_Rest extends WP_REST_Controller {
|
||||
/**
|
||||
* Buffer for streaming response.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $buffer = '';
|
||||
|
||||
/**
|
||||
* Last time the buffer was sent.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $last_send_time = 0;
|
||||
|
||||
/**
|
||||
* Buffer threshold.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private const BUFFER_THRESHOLD = 150;
|
||||
|
||||
/**
|
||||
* Minimum send interval.
|
||||
*
|
||||
* @var float
|
||||
*/
|
||||
private const MIN_SEND_INTERVAL = 0.05;
|
||||
|
||||
/**
|
||||
* Namespace.
|
||||
*
|
||||
|
@ -303,6 +331,9 @@ class Mind_Rest extends WP_REST_Controller {
|
|||
$json_data = trim( substr( $line, 6 ) );
|
||||
|
||||
if ( '[DONE]' === $json_data ) {
|
||||
if ( ! empty( $this->buffer ) ) {
|
||||
$this->send_buffered_chunk();
|
||||
}
|
||||
$this->send_stream_chunk( [ 'done' => true ] );
|
||||
return;
|
||||
}
|
||||
|
@ -311,13 +342,16 @@ class Mind_Rest extends WP_REST_Controller {
|
|||
$data = json_decode( $json_data, true );
|
||||
|
||||
if ( isset( $data['choices'][0]['delta']['content'] ) ) {
|
||||
// Send smaller chunks immediately.
|
||||
$this->send_stream_chunk(
|
||||
[
|
||||
'content' => $data['choices'][0]['delta']['content'],
|
||||
]
|
||||
);
|
||||
flush();
|
||||
$content = $data['choices'][0]['delta']['content'];
|
||||
$this->buffer .= $content;
|
||||
|
||||
$current_time = microtime( true );
|
||||
$time_since_last_send = $current_time - $this->last_send_time;
|
||||
|
||||
if ( strlen( $this->buffer ) >= self::BUFFER_THRESHOLD ||
|
||||
$time_since_last_send >= self::MIN_SEND_INTERVAL ) {
|
||||
$this->send_buffered_chunk();
|
||||
}
|
||||
}
|
||||
} catch ( Exception $e ) {
|
||||
$this->send_stream_error( 'json_error', $e->getMessage() );
|
||||
|
@ -326,6 +360,24 @@ class Mind_Rest extends WP_REST_Controller {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send buffered chunk
|
||||
*/
|
||||
private function send_buffered_chunk() {
|
||||
if ( empty( $this->buffer ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->send_stream_chunk(
|
||||
[
|
||||
'content' => $this->buffer,
|
||||
]
|
||||
);
|
||||
|
||||
$this->buffer = '';
|
||||
$this->last_send_time = microtime( true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Send stream chunk
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue