流式步骤
流式步骤可让工作流逐步生成输出。每个生成的块都会立即写入数据库,因此如果步骤在流式处理中途失败,已持久化的块仍会保留,并可在重试时使用。
这适用于 LLM 流式响应、大型文件处理,以及任何不应丢失部分进度的场景。
StreamingStep 接口
流式步骤需要实现 Queuety\Contracts\StreamingStep:
namespace Queuety\Contracts;
interface StreamingStep {
public function stream( array $state, array $existing_chunks = [] ): \Generator;
public function on_complete( array $chunks, array $state ): array;
public function config(): array;
}
| 方法 | 说明 |
|---|---|
stream( $state, $existing_chunks ) |
生成器,逐块生成字符串。每次生成都会立即写入 DB。 |
on_complete( $chunks, $state ) |
流处理结束后调用。接收所有数据块(之前的和新增的),并返回要合并到工作流状态中的数据。 |
config() |
可选配置。支持 max_attempts 和 backoff;可使用策略名称或逐次尝试的延迟列表。 |
块的持久化方式
每当生成器生成一个值,Queuety 就会调用 ChunkStore::append_chunk() 将其写入 queuety_chunks 表。不会进行批处理或缓冲。每次生成都会执行一次 INSERT。
数据块行存储 job_id、workflow_id、step_index、claim_token、chunk_index(从零开始)以及字符串 content。每次写入都会检查实时所有权;已被取代的尝试无法覆盖其他尝试。
可恢复性
流式步骤失败后重试时,工作进程会将最新的可恢复尝试复制到新的认领中,加载该尝试的数据块,并将它们作为 $existing_chunks 参数传入。如何恢复由你的实现决定:
- 跳过已处理的项目:检查现有数据块的数量
- 使用数据块数量作为页偏移量,定位到分页 API 中的相应位置
- 重新开始不可恢复的推理,将其作为新的逻辑操作。忽略已保存的数据块会把部分输出拼接到新响应前面,因此不能将其视为已完成的结果。
public function stream( array $state, array $existing_chunks = [] ): \Generator {
$offset = count( $existing_chunks );
$pages = $this->client->paginate( $state['query'], offset: $offset );
foreach ( $pages as $page ) {
yield json_encode( $page );
}
}
流式处理期间的心跳
长时间运行的流应发送心跳,以防止过期任务检测器回收任务。在生成器循环中调用 Heartbeat::beat():
use Queuety\Heartbeat;
public function stream( array $state, array $existing_chunks = [] ): \Generator {
foreach ( $this->client->stream( $state['prompt'] ) as $i => $chunk ) {
Heartbeat::beat( [ 'chunks' => $i + 1 ] );
yield $chunk;
}
}
详情请参阅 心跳,了解 Heartbeat API。
清理块
只有在带围栏的工作流完成事务提交后,工作进程才会清除该成功尝试的数据块。如果后续步骤分派或检查点失败,这些数据块会保留。成功清理时也会清除较早尝试的数据。其行边界和令牌范围可保护较新的替代尝试。已提交的 on_complete() 输出就是最终结果。
如果步骤失败并重试,块不会被删除。下一次尝试时,它们会通过 $existing_chunks 传回。
LLM 流不会自动恢复
已保存的令牌前缀并不能证明模型已经完成。不要仅仅因为 $existing_chunks 非空就返回成功。大多数提供商的流无法从令牌偏移处恢复推理。请将部分输出标记为部分结果;通过具有独立操作身份的新逻辑请求重试,或使用明确支持恢复和完成状态的提供商协议。
Neuron流式示例说明了这一界限。Queuety保证尝试所有权和事务性发布,但无法生成缺失的提供商完成回执。
完整示例:文件处理
use Queuety\Contracts\StreamingStep;
use Queuety\Heartbeat;
class ProcessCSVRows implements StreamingStep {
public function stream( array $state, array $existing_chunks = [] ): \Generator {
$file = fopen( $state['csv_path'], 'r' );
$offset = count( $existing_chunks );
$row = 0;
// Skip the header.
fgetcsv( $file );
while ( $line = fgetcsv( $file ) ) {
$row++;
// Skip rows already processed in a previous attempt.
if ( $row <= $offset ) {
continue;
}
$result = $this->process_row( $line );
Heartbeat::beat( [ 'row' => $row ] );
yield json_encode( $result );
}
fclose( $file );
}
public function on_complete( array $chunks, array $state ): array {
$results = array_map( 'json_decode', $chunks );
return [
'processed_count' => count( $results ),
'results' => $results,
];
}
public function config(): array {
return [];
}
private function process_row( array $line ): array {
// Your row processing logic here.
return [ 'id' => $line[0], 'status' => 'ok' ];
}
}
分块存储 API
ChunkStore 类负责管理块的持久化。通过 Queuety::chunk_store() 访问它。
$store = Queuety::chunk_store();
| 方法 | 返回值 | 说明 |
|---|---|---|
get_chunks( int $job_id, ?string $claim_token = null ) |
string[] |
按数据块索引排序,获取某个作业的所有数据块 |
append_chunk( int $job_id, int $chunk_index, string $content, ?int $workflow_id, ?int $step_index ) |
void |
追加单个数据块 |
clear_chunks( int $job_id, ?string $claim_token = null ) |
void |
删除选定的尝试;未指定上下文的管理性省略会选中所有尝试 |
chunk_count( int $job_id ) |
int |
统计某个作业的数据块数量 |
get_accumulated( int $job_id ) |
string |
将所有数据块拼接成一个字符串 |
通常无需直接调用 ChunkStore。处理 StreamingStep 时,工作进程会自动处理持久化和清理。API 可用于检查处理中块或手动清理等高级场景。