59 lines
2.8 KiB
Python
59 lines
2.8 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from dataclasses import replace
|
||
|
|
|
||
|
|
from yuxi.channel.streaming.models import (
|
||
|
|
BlockReplyChunking,
|
||
|
|
BlockReplyCoalescing,
|
||
|
|
StreamingConfig,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
class LineStreaming:
|
||
|
|
streaming_mode = "block"
|
||
|
|
preview_stream_throttle_ms = StreamingConfig.defaults()["preview_stream_throttle_ms"]
|
||
|
|
preview_min_initial_chars = StreamingConfig.defaults()["preview_min_initial_chars"]
|
||
|
|
|
||
|
|
block_streaming_enabled = True
|
||
|
|
block_streaming_break = StreamingConfig.defaults()["block_streaming_break"]
|
||
|
|
block_streaming_chunk_min_chars = StreamingConfig.defaults()["block_streaming_chunk_min_chars"]
|
||
|
|
block_streaming_chunk_max_chars = StreamingConfig.defaults()["block_streaming_chunk_max_chars"]
|
||
|
|
block_streaming_chunk_break_preference = StreamingConfig.defaults()["block_streaming_chunk_break_preference"]
|
||
|
|
|
||
|
|
block_streaming_flush_on_paragraph = False
|
||
|
|
block_streaming_coalesce_min_chars = 400
|
||
|
|
block_streaming_coalesce_max_chars = 800
|
||
|
|
block_streaming_coalesce_idle_ms = StreamingConfig.defaults()["block_streaming_coalesce_idle_ms"]
|
||
|
|
|
||
|
|
def build_streaming_config(self) -> StreamingConfig:
|
||
|
|
return replace(
|
||
|
|
StreamingConfig.empty(),
|
||
|
|
preview_stream_throttle_ms=self.preview_stream_throttle_ms,
|
||
|
|
preview_min_initial_chars=self.preview_min_initial_chars,
|
||
|
|
block_streaming_enabled=self.block_streaming_enabled,
|
||
|
|
block_streaming_break=self.block_streaming_break,
|
||
|
|
block_streaming_chunk_min_chars=self.block_streaming_chunk_min_chars,
|
||
|
|
block_streaming_chunk_max_chars=self.block_streaming_chunk_max_chars,
|
||
|
|
block_streaming_chunk_break_preference=self.block_streaming_chunk_break_preference,
|
||
|
|
block_streaming_flush_on_paragraph=self.block_streaming_flush_on_paragraph,
|
||
|
|
block_streaming_coalesce_min_chars=self.block_streaming_coalesce_min_chars,
|
||
|
|
block_streaming_coalesce_max_chars=self.block_streaming_coalesce_max_chars,
|
||
|
|
block_streaming_coalesce_idle_ms=self.block_streaming_coalesce_idle_ms,
|
||
|
|
)
|
||
|
|
|
||
|
|
def build_chunking_config(self) -> BlockReplyChunking:
|
||
|
|
return BlockReplyChunking(
|
||
|
|
min_chars=self.block_streaming_chunk_min_chars,
|
||
|
|
max_chars=self.block_streaming_chunk_max_chars,
|
||
|
|
break_preference=self.block_streaming_chunk_break_preference,
|
||
|
|
flush_on_paragraph=self.block_streaming_flush_on_paragraph,
|
||
|
|
)
|
||
|
|
|
||
|
|
def build_coalesce_config(self) -> BlockReplyCoalescing:
|
||
|
|
return BlockReplyCoalescing(
|
||
|
|
min_chars=self.block_streaming_coalesce_min_chars,
|
||
|
|
max_chars=self.block_streaming_coalesce_max_chars,
|
||
|
|
idle_ms=self.block_streaming_coalesce_idle_ms,
|
||
|
|
joiner="",
|
||
|
|
flush_on_enqueue=False,
|
||
|
|
)
|