fix: improve perf

This commit is contained in:
Javier Martinez
2026-07-30 15:09:03 +02:00
parent 94710b9272
commit f558dd431c
2 changed files with 45 additions and 6 deletions

View File

@@ -17,16 +17,20 @@ class DeduplicateEventInterceptor(ChatResponseLoopInterceptor):
``input_json_delta`` with an unchanged ``partial_json_obj``). Those
duplicates add noise for clients without changing state, so they are
suppressed. Ping keepalives are always forwarded.
Equality is checked via pydantic's structural ``__eq__`` (field-by-field
comparison) rather than ``model_dump_json``, since serializing every
streamed event just to compare it adds significant latency.
"""
def __init__(self) -> None:
self._last_fingerprint: str | None = None
self._last_event: Event | None = None
async def on_iteration_start(self, context: ChatInterceptorContext) -> None:
self._last_fingerprint = None
self._last_event = None
async def on_iteration_end(self, context: ChatInterceptorContext) -> None:
self._last_fingerprint = None
self._last_event = None
async def intercept_event(
self,
@@ -36,11 +40,10 @@ class DeduplicateEventInterceptor(ChatResponseLoopInterceptor):
if isinstance(event, PingEvent):
return event
fingerprint = event.model_dump_json()
if fingerprint == self._last_fingerprint:
if event == self._last_event:
return None
self._last_fingerprint = fingerprint
self._last_event = event
return event
def model_copy(

View File

@@ -681,6 +681,42 @@ async def test_unit_duplicate_content_block_deltas_are_dropped() -> None:
assert deltas[1].delta.partial_json_obj == {"object": "deals", "query": "x"}
def test_unit_content_block_delta_equality_ignores_object_identity() -> None:
"""Two distinct RawContentBlockDeltaEvent objects compare equal when their
field values match, and unequal when they differ -- this is what
DeduplicateEventInterceptor relies on instead of model_dump_json()."""
from private_gpt.events.models import InputJSONDelta
bid = _block_id()
equal_a = RawContentBlockDeltaEvent(
index=1,
block_id=bid,
delta=InputJSONDelta(
partial_json="", partial_json_obj={"object": "deals", "query": None}
),
)
equal_b = RawContentBlockDeltaEvent(
index=1,
block_id=bid,
delta=InputJSONDelta(
partial_json="", partial_json_obj={"object": "deals", "query": None}
),
)
assert equal_a is not equal_b
assert equal_a == equal_b
different = RawContentBlockDeltaEvent(
index=1,
block_id=bid,
delta=InputJSONDelta(
partial_json='{"object":"deals","query":"x"}',
partial_json_obj={"object": "deals", "query": "x"},
),
)
assert equal_a != different
@pytest.mark.asyncio
async def test_unit_ping_events_are_never_deduplicated() -> None:
"""Ping keepalives always pass through even when consecutive."""